Class: Macros4Cuke::MacroCollection

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/macros4cuke/macro-collection.rb

Overview

Represents a container of macros. It gather all the macros encountered by Cucumber while “executing” the feature files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#macro_stepsObject (readonly)

A Hash with pairs of the form: phrase => MacroStep object



15
16
17
# File 'lib/macros4cuke/macro-collection.rb', line 15

def macro_steps
  @macro_steps
end

Instance Method Details

#add_macro(aPhrase, aTemplate, useTable) ⇒ Object

Add a new macro. Pre-condition: there is no existing macro with the same key.

aPhrase

The text that is enclosed between the square brackets.

aTemplate

A text that consists of a sequence of Cucumber steps.

useTable

A boolean that indicates whether a table should be used to pass actual values.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/macros4cuke/macro-collection.rb', line 35

def add_macro(aPhrase, aTemplate, useTable)
  new_macro = MacroStep.new(aPhrase, aTemplate, useTable)
  
  # Prevent collision of macros (macros with same phrase).
  # This can occur if a macro was defined in a background section.
  # An exception is raised if the phrase syntax of both macros are the     
  raise DuplicateMacroError.new(aPhrase) if find_macro(aPhrase, useTable)
  
  @macro_steps[new_macro.key] = new_macro    

end

#has_macro?(aPhrase, mode) ⇒ Boolean

Return true iff the host has a macro with the given key.

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/macros4cuke/macro-collection.rb', line 24

def has_macro?(aPhrase, mode)
  key = MacroStep::macro_key(aPhrase, mode)
  return @macro_steps.include? key
end

#initObject

Init the pool if it was not done yet.



18
19
20
# File 'lib/macros4cuke/macro-collection.rb', line 18

def init()
  @macro_steps = {} if @macro_steps.nil?
end

#render_steps(aPhrase, rawData = nil) ⇒ Object

Render the steps associated to the macro with given phrase and (optionally) given a table of values. Return the rendered steps as a text.

aPhrase

an instance of the macro phrase.

rawData

An Array of couples.

Each couple is of the form: argument name, a value. Multiple rows with same argument name are acceptable.

Raises:



54
55
56
57
58
59
60
61
# File 'lib/macros4cuke/macro-collection.rb', line 54

def render_steps(aPhrase, rawData = nil)
  useTable = ! rawData.nil?
  macro = find_macro(aPhrase, useTable) 
  raise UnknownMacroError.new(aPhrase) if macro.nil?

  # Render the steps
  return  macro.expand(aPhrase, rawData)
end