Class: Macros4Cuke::MacroCollection

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

Overview

Note:

This is a singleton class (i.e. there is only one macro collection object).

Represents a container of macros.
It gathers all the macros encountered by Cucumber while "executing" the feature files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#macro_steps.Object (readonly)

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



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

Instance Method Details

#add_macro(aPhrase, aTemplate, useTable) ⇒ Object

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

Parameters:

  • aPhrase (String)

    The text that is enclosed between the square brackets.

  • aTemplate (String)

    A text that consists of a sequence of sub-steps.

  • useTable (boolean)

    A flag indicating whether a table should be used to pass actual values.

Raises:



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/macros4cuke/macro-collection.rb', line 25

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

#clearObject

Clear/remove all macro definitions from the collection. Post-condition: we are back to the same situation as no macro was ever defined.



56
57
58
# File 'lib/macros4cuke/macro-collection.rb', line 56

def clear()
  macro_steps.clear()
end

#macro_stepsObject

Read accessor for the @macro_steps attribute.



62
63
64
65
# File 'lib/macros4cuke/macro-collection.rb', line 62

def macro_steps()
  @macro_steps ||= {}
  return @macro_steps
end

#render_steps(aPhrase, rawData = nil) ⇒ String

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

Parameters:

  • aPhrase (String)

    an instance of the macro phrase.

  • rawData (Array or nil) (defaults to: nil)

    An Array with coupples of the form: [macro argument name, a value]. Multiple rows with same argument name are acceptable.

Returns:

  • (String)

Raises:



44
45
46
47
48
49
50
51
# File 'lib/macros4cuke/macro-collection.rb', line 44

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