class Macros4Cuke::MacroCollection

Represents a container of macros. It gathers all the macros encountered by Cucumber while “executing” the feature files. @note This is a singleton class: there is only one macro collection object.

Public Instance Methods

add_macro(aPhrase, aTemplate, useTable) click to toggle source

Add a new macro. Pre-condition: there is no existing macro with the same key. @param aPhrase [String] The text that is enclosed between the square brackets. @param aTemplate [String] A text that consists of a sequence of sub-steps. @param useTable [boolean] A flag indicating whether a table should be used to pass actual values.

# File lib/macros4cuke/macro-collection.rb, line 27
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, aPhrase) if find_macro(aPhrase, useTable)

  macro_steps[new_macro.key] = new_macro
end
clear() click to toggle source

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

# File lib/macros4cuke/macro-collection.rb, line 58
def clear()
  macro_steps.clear
end
macro_steps() click to toggle source

Read accessor for the @macro_steps attribute.

# File lib/macros4cuke/macro-collection.rb, line 63
def macro_steps()
  @macro_steps ||= {}
  return @macro_steps
end
render_steps(aPhrase, rawData = nil) click to toggle source

Render the steps associated to the macro with given phrase and (optionally) given a table of values. Return the rendered steps as a text. @param aPhrase [String] an instance of the macro phrase. @param rawData [Array or nil] An Array with couples of the form: [macro argument name, a value].

Multiple rows with same argument name are acceptable.

@return [String]

# File lib/macros4cuke/macro-collection.rb, line 46
def render_steps(aPhrase, rawData = nil)
  use_table = !rawData.nil?
  macro = find_macro(aPhrase, use_table)
  raise(UnknownMacroError, aPhrase) if macro.nil?

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

Private Instance Methods

find_macro(aMacroPhrase, useTable) click to toggle source

Retrieve the macro, given a phrase.

# File lib/macros4cuke/macro-collection.rb, line 71
def find_macro(aMacroPhrase, useTable)
  key = MacroStep.macro_key(aMacroPhrase, useTable, :invokation)
  return macro_steps[key]
end