Module: Cucumber::StepMother
- Defined in:
- lib/cucumber/step_mother.rb
Overview
This is the main interface for registering step definitions, which is done from *_steps.rb
files. This module is included right at the top-level so #register_step_definition (and more interestingly - its aliases) are available from the top-level.
Defined Under Namespace
Modules: WorldMethods
Instance Attribute Summary collapse
-
#options ⇒ Object
writeonly
Sets the attribute options.
-
#snippet_generator ⇒ Object
writeonly
Sets the attribute snippet_generator.
Instance Method Summary collapse
- #After(&proc) ⇒ Object
-
#Before(&proc) ⇒ Object
Registers a Before proc.
- #best_matches(step_name, step_definitions) ⇒ Object
-
#new_world ⇒ Object
Creates a new world instance.
- #print_step_definitions(out) ⇒ Object
-
#register_step_definition(regexp, &proc) ⇒ Object
Registers a new StepDefinition.
- #snippet_text(step_keyword, step_name) ⇒ Object
-
#step_definition(step_name) ⇒ Object
Looks up the StepDefinition that matches
step_name
. - #step_definitions ⇒ Object
- #world(scenario, prior_world = nil, &proc) ⇒ Object
-
#World(&proc) ⇒ Object
Registers a World proc.
Instance Attribute Details
#options=(value) ⇒ Object
Sets the attribute options
45 46 47 |
# File 'lib/cucumber/step_mother.rb', line 45 def (value) @options = value end |
#snippet_generator=(value) ⇒ Object (writeonly)
Sets the attribute snippet_generator
44 45 46 |
# File 'lib/cucumber/step_mother.rb', line 44 def snippet_generator=(value) @snippet_generator = value end |
Instance Method Details
#After(&proc) ⇒ Object
86 87 88 |
# File 'lib/cucumber/step_mother.rb', line 86 def After(&proc) (@after_procs ||= []) << proc end |
#Before(&proc) ⇒ Object
Registers a Before proc. You can call this method as many times as you want (typically from ruby scripts under support
).
82 83 84 |
# File 'lib/cucumber/step_mother.rb', line 82 def Before(&proc) (@before_procs ||= []) << proc end |
#best_matches(step_name, step_definitions) ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/cucumber/step_mother.rb', line 121 def best_matches(step_name, step_definitions) top_group_score = step_definitions.map {|s| s.match(step_name).captures.length }.sort.last top_groups = step_definitions.select {|s| s.match(step_name).captures.length == top_group_score } if top_groups.size > 1 shortest_capture_length = top_groups.map {|s| s.match(step_name).captures.inject(0) {|sum, c| sum + c.length } }.sort.first top_groups.select {|s| s.match(step_name).captures.inject(0) {|sum, c| sum + c.length } == shortest_capture_length } else top_groups end end |
#new_world ⇒ Object
Creates a new world instance
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/cucumber/step_mother.rb', line 97 def new_world #:nodoc: world = Object.new (@world_procs ||= []).each do |proc| world = proc.call(world) end world.extend(WorldMethods) world.__cucumber_step_mother = self world.extend(::Spec::Matchers) if defined?(::Spec::Matchers) world end |
#print_step_definitions(out) ⇒ Object
140 141 142 143 144 145 |
# File 'lib/cucumber/step_mother.rb', line 140 def print_step_definitions(out) step_definitions.each do |step_definition| indent = max_step_definition_length - step_definition.text_length out.puts(step_definition.to_s(indent)) end end |
#register_step_definition(regexp, &proc) ⇒ Object
Registers a new StepDefinition. This method is aliased to Given
, When
and Then
.
See Cucumber#alias_steps for details on how to create your own aliases.
The &proc gets executed in the context of a world
object, which is defined by #World. A new world
object is created for each scenario and is shared across step definitions within that scenario.
57 58 59 60 61 62 63 64 |
# File 'lib/cucumber/step_mother.rb', line 57 def register_step_definition(regexp, &proc) step_definition = StepDefinition.new(regexp, &proc) step_definitions.each do |already| raise Redundant.new(already, step_definition) if already.match(regexp) end step_definitions << step_definition step_definition end |
#snippet_text(step_keyword, step_name) ⇒ Object
136 137 138 |
# File 'lib/cucumber/step_mother.rb', line 136 def snippet_text(step_keyword, step_name) @snippet_generator.snippet_text(step_keyword, step_name) end |
#step_definition(step_name) ⇒ Object
Looks up the StepDefinition that matches step_name
111 112 113 114 115 116 117 118 119 |
# File 'lib/cucumber/step_mother.rb', line 111 def step_definition(step_name) #:nodoc: found = step_definitions.select do |step_definition| step_definition.match(step_name) end raise Undefined.new(step_name) if found.empty? found = best_matches(step_name, found) if found.size > 1 && [:guess] raise Ambiguous.new(step_name, found) if found.size > 1 found[0] end |
#step_definitions ⇒ Object
132 133 134 |
# File 'lib/cucumber/step_mother.rb', line 132 def step_definitions @step_definitions ||= [] end |
#world(scenario, prior_world = nil, &proc) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/cucumber/step_mother.rb', line 66 def world(scenario, prior_world = nil, &proc) world = prior_world || new_world begin (@before_procs ||= []).each do |proc| world.cucumber_instance_exec(false, 'Before', scenario, &proc) end yield world ensure (@after_procs ||= []).each do |proc| world.cucumber_instance_exec(false, 'After', scenario, &proc) end end end |
#World(&proc) ⇒ Object
Registers a World proc. You can call this method as many times as you want (typically from ruby scripts under support
).
92 93 94 |
# File 'lib/cucumber/step_mother.rb', line 92 def World(&proc) (@world_procs ||= []) << proc end |