Class: Macros4Cuke::MacroStep

Inherits:
Object
  • Object
show all
Defined in:
lib/macros4cuke/macro-step.rb

Overview

In essence, a macro step object represents a Cucumber step that is itself an aggregation of lower-level Cucumber steps.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aMacroPhrase, theSubsteps, useTable) ⇒ MacroStep

Constructor.

aMacroPhrase

The text from the macro step definition that is between the square brackets.

theSubsteps

The source text of the steps to be expanded upon macro invokation.

useTable

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/macros4cuke/macro-step.rb', line 30

def initialize(aMacroPhrase, theSubsteps, useTable)
  @key = self.class.macro_key(aMacroPhrase, useTable, :definition)
  
  # Retrieve the macro arguments embedded in the phrase.
  @phrase_args = scan_arguments(aMacroPhrase, :definition)
  
  # Manipulate the substeps source text
  substeps_processed = preprocess(theSubsteps)

  @renderer = TemplateEngine.new(substeps_processed)
  substeps_vars = renderer.variables
  
  
  @args = validate_phrase_args(@phrase_args, substeps_vars)
  @args.concat(substeps_vars)
  @args.uniq!
end

Instance Attribute Details

#argsObject (readonly)

The list of macro argument names (as appearing in the substeps and in the macro phrase).



23
24
25
# File 'lib/macros4cuke/macro-step.rb', line 23

def args
  @args
end

#keyObject (readonly)

Unique key of the macro as derived from the macro phrase.



17
18
19
# File 'lib/macros4cuke/macro-step.rb', line 17

def key
  @key
end

#phrase_argsObject (readonly)

The list of macro arguments that appears in the macro phrase.



20
21
22
# File 'lib/macros4cuke/macro-step.rb', line 20

def phrase_args
  @phrase_args
end

#rendererObject (readonly)

A template engine that expands the substeps upon request.



14
15
16
# File 'lib/macros4cuke/macro-step.rb', line 14

def renderer
  @renderer
end

Class Method Details

.macro_key(aMacroPhrase, useTable, mode) ⇒ Object

Compute the identifier of the macro from the given macro phrase. A macro phrase is a text that must start with a recognised verb and may contain zero or more placeholders. In definition mode, a placeholder is delimited by chevrons <..> In invokation mode, a placeholder is delimited by double quotes. The rule for building the identifier are:

  • Leading and trailing space(s) are removed.

  • Each underscore character is removed.

  • Every sequence of one or more space(s) is converted into an underscore

  • Each placeholder (i.e. = delimiters + enclosed text) is converted into a letter X.

  • when useTable is true, concatenate: _T

Example: Consider the macro phrase: ‘create the following “contactType” contact]:’ The resulting macro_key is: ‘create_the_following_X_contact_T’

aMacroPhrase

The text from the macro step definition that is between the square brackets.

useTable

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

mode

one of the following: :definition, :invokation



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/macros4cuke/macro-step.rb', line 65

def self.macro_key(aMacroPhrase, useTable, mode)
  stripped_phrase = aMacroPhrase.strip # Remove leading ... trailing space(s)
  
  # Remove every underscore
  stripped_phrase.gsub!(/_/, '')
  
  # Replace all consecutive whitespaces by an underscore    
  stripped_phrase.gsub!(/\s+/, '_')
  
  
  # Determine the pattern to isolate each argument/parameter with its delimiters
  pattern = case mode
    when :definition
      /<(?:[^\\<>]|\\.)*>/
    when :invokation
      /"([^\\"]|\\.)*"/

  end
  
  # Each text between quotes or chevron is replaced by the letter X
  normalized = stripped_phrase.gsub(pattern, 'X')
  
  key = normalized + (useTable ? '_T' : '')
  
  return key
end

Instance Method Details

#expand(aPhrase, rawData) ⇒ Object

Render the steps from the template, given the values taken by the parameters

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.



99
100
101
102
# File 'lib/macros4cuke/macro-step.rb', line 99

def expand(aPhrase, rawData)
  params = validate_params(aPhrase, rawData)
  return renderer.render(nil, params)
end