Class: Macros4Cuke::MacroStep
- Inherits:
-
Object
- Object
- Macros4Cuke::MacroStep
- Defined in:
- lib/macros4cuke/macro-step.rb
Overview
A macro-step object is a Cucumber step that is itself an aggregation of lower-level sub-steps. When a macro-step is used in a scenario, then its execution is equivalent to the execution of its sub-steps. A macro-step may have zero or more arguments. The actual values bound to these arguments are passed to the sub-steps at execution time.
Constant Summary collapse
- BuiltinParameters =
The set of predefined macro argument constant values.
{ 'quotes' => '"""' }
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
The list of macro argument names (as appearing in the substeps AND in the macro phrase).
-
#key ⇒ Object
readonly
Unique key of the macro as derived from the macro phrase.
-
#phrase ⇒ Object
readonly
The sentence fragment that defines the syntax of the macro-step.
-
#phrase_args ⇒ Object
readonly
The list of macro arguments that appears in the macro phrase.
-
#renderer ⇒ Object
readonly
A template engine that expands the sub-steps upon request.
Class Method Summary collapse
-
.macro_key(aMacroPhrase, useTable, mode) ⇒ String
Compute the identifier of the macro from the given macro phrase.
Instance Method Summary collapse
-
#expand(aPhrase, rawData) ⇒ Object
Render the steps from the template, given the values taken by the parameters [macro argument name, a value].
-
#initialize(aMacroPhrase, theSubsteps, useTable) ⇒ MacroStep
constructor
Constructor.
Constructor Details
#initialize(aMacroPhrase, theSubsteps, useTable) ⇒ MacroStep
Constructor.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/macros4cuke/macro-step.rb', line 46 def initialize(aMacroPhrase, theSubsteps, useTable) @phrase = aMacroPhrase @key = self.class.macro_key(aMacroPhrase, useTable, :definition) # Retrieve the macro arguments embedded in the phrase. @phrase_args = scan_arguments(aMacroPhrase, :definition) @renderer = Templating::Engine.new(theSubsteps) substeps_vars = renderer.variables @args = validate_phrase_args(@phrase_args, substeps_vars) @args.concat(substeps_vars) @args.uniq! end |
Instance Attribute Details
#args ⇒ Object (readonly)
The list of macro argument names (as appearing in the substeps AND in the macro phrase).
37 38 39 |
# File 'lib/macros4cuke/macro-step.rb', line 37 def args @args end |
#key ⇒ Object (readonly)
Unique key of the macro as derived from the macro phrase.
30 31 32 |
# File 'lib/macros4cuke/macro-step.rb', line 30 def key @key end |
#phrase ⇒ Object (readonly)
The sentence fragment that defines the syntax of the macro-step
27 28 29 |
# File 'lib/macros4cuke/macro-step.rb', line 27 def phrase @phrase end |
#phrase_args ⇒ Object (readonly)
The list of macro arguments that appears in the macro phrase.
33 34 35 |
# File 'lib/macros4cuke/macro-step.rb', line 33 def phrase_args @phrase_args end |
#renderer ⇒ Object (readonly)
A template engine that expands the sub-steps upon request.
24 25 26 |
# File 'lib/macros4cuke/macro-step.rb', line 24 def renderer @renderer end |
Class Method Details
.macro_key(aMacroPhrase, useTable, mode) ⇒ String
Compute the identifier of the macro from the given macro phrase.
A macro phrase is a text that may contain zero or more placeholders.
In definition mode, a placeholder is delimited by chevrons <..>.
In invokation mode, a value bound to a placeholder is delimited
by double quotes.
The rule for building the identifying key 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'
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/macros4cuke/macro-step.rb', line 84 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 [macro argument name, a value]. Multiple rows with same argument name are acceptable.
119 120 121 122 123 124 125 126 |
# File 'lib/macros4cuke/macro-step.rb', line 119 def (aPhrase, rawData) params = validate_params(aPhrase, rawData) # Add built-in constants if necessary. params = BuiltinParameters.merge(params) return renderer.render(nil, params) end |