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

Classes: Hook

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#options=(value) ⇒ Object

Sets the attribute options

Parameters:

  • value

    the value to set the attribute options to.



93
94
95
# File 'lib/cucumber/step_mother.rb', line 93

def options=(value)
  @options = value
end

#snippet_generator=(value) ⇒ Object (writeonly)

Sets the attribute snippet_generator

Parameters:

  • value

    the value to set the attribute snippet_generator to.



93
94
95
# File 'lib/cucumber/step_mother.rb', line 93

def snippet_generator=(value)
  @snippet_generator = value
end

#visitor=(value) ⇒ Object (writeonly)

Sets the attribute visitor

Parameters:

  • value

    the value to set the attribute visitor to.



93
94
95
# File 'lib/cucumber/step_mother.rb', line 93

def visitor=(value)
  @visitor = value
end

Class Method Details

.alias_adverb(adverb) ⇒ Object



87
88
89
90
# File 'lib/cucumber/step_mother.rb', line 87

def alias_adverb(adverb)
  adverb = adverb.gsub(/\s/, '')
  alias_method adverb, :register_step_definition
end

Instance Method Details

#after(scenario) ⇒ Object



248
249
250
251
# File 'lib/cucumber/step_mother.rb', line 248

def after(scenario)
  execute_after(scenario)
  nil_world!
end

#After(*tag_names, &proc) ⇒ Object



142
143
144
# File 'lib/cucumber/step_mother.rb', line 142

def After(*tag_names, &proc)
  register_hook(:after, tag_names, proc)
end

#before(scenario) ⇒ Object



241
242
243
244
245
246
# File 'lib/cucumber/step_mother.rb', line 241

def before(scenario)
  unless current_world
    new_world!
    execute_before(scenario)
  end
end

#Before(*tag_names, &proc) ⇒ Object

Registers a Before proc. You can call this method as many times as you want (typically from ruby scripts under support).



138
139
140
# File 'lib/cucumber/step_mother.rb', line 138

def Before(*tag_names, &proc)
  register_hook(:before, tag_names, proc)
end

#before_and_after(scenario, skip = false) ⇒ Object



234
235
236
237
238
239
# File 'lib/cucumber/step_mother.rb', line 234

def before_and_after(scenario, skip=false)
  before(scenario) unless skip
  yield
  after(scenario) unless skip
  scenario_visited(scenario)
end

#best_matches(step_name, step_matches) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
# File 'lib/cucumber/step_mother.rb', line 214

def best_matches(step_name, step_matches)
  max_arg_length = step_matches.map {|step_match| step_match.args.length }.max
  top_groups     = step_matches.select {|step_match| step_match.args.length == max_arg_length }

  if top_groups.length > 1
    shortest_capture_length = top_groups.map {|step_match| step_match.args.inject(0) {|sum, c| sum + c.length } }.min
    top_groups.select {|step_match| step_match.args.inject(0) {|sum, c| sum + c.length } == shortest_capture_length }
  else
    top_groups
  end
end

#current_worldObject



202
203
204
# File 'lib/cucumber/step_mother.rb', line 202

def current_world
  @current_world
end

#hooksObject



152
153
154
# File 'lib/cucumber/step_mother.rb', line 152

def hooks
  @hooks ||= Hash.new {|hash, phase| hash[phase] = []}
end

#hooks_for(phase, scenario) ⇒ Object



156
157
158
# File 'lib/cucumber/step_mother.rb', line 156

def hooks_for(phase, scenario)
  hooks[phase].select{|hook| scenario.accept_hook?(hook)}
end

#register_hook(phase, tags, proc) ⇒ Object



146
147
148
149
150
# File 'lib/cucumber/step_mother.rb', line 146

def register_hook(phase, tags, proc)
  hook = Hook.new(tags, proc)
  hooks[phase] << hook
  hook
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.



127
128
129
130
131
132
133
134
# File 'lib/cucumber/step_mother.rb', line 127

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

#scenarios(status = nil) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/cucumber/step_mother.rb', line 108

def scenarios(status = nil)
  @scenarios ||= []
  if(status)
    @scenarios.select{|scenario| scenario.status == status}
  else
    @scenarios
  end
end

#snippet_text(step_keyword, step_name, multiline_arg_class) ⇒ Object



230
231
232
# File 'lib/cucumber/step_mother.rb', line 230

def snippet_text(step_keyword, step_name, multiline_arg_class)
  @snippet_generator.snippet_text(step_keyword, step_name, multiline_arg_class)
end

#step_definitionsObject



226
227
228
# File 'lib/cucumber/step_mother.rb', line 226

def step_definitions
  @step_definitions ||= []
end

#step_match(step_name, formatted_step_name = nil) ⇒ Object

Raises:



206
207
208
209
210
211
212
# File 'lib/cucumber/step_mother.rb', line 206

def step_match(step_name, formatted_step_name=nil)
  matches = step_definitions.map { |d| d.step_match(step_name, formatted_step_name) }.compact
  raise Undefined.new(step_name) if matches.empty?
  matches = best_matches(step_name, matches) if matches.size > 1 && options[:guess]
  raise Ambiguous.new(step_name, matches, options[:guess]) if matches.size > 1
  matches[0]
end

#step_visited(step) ⇒ Object



95
96
97
# File 'lib/cucumber/step_mother.rb', line 95

def step_visited(step)
  steps << step unless steps.index(step)
end

#steps(status = nil) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/cucumber/step_mother.rb', line 99

def steps(status = nil)
  @steps ||= []
  if(status)
    @steps.select{|step| step.status == status}
  else
    @steps
  end
end

#World(*world_modules, &proc) ⇒ Object

Registers any number of world_modules (Ruby Modules) and/or a Proc. The proc will be executed once before each scenario to create an Object that the scenario’s steps will run within. Any world_modules will be mixed into this Object (via Object#extend).

This method is typically called from one or more Ruby scripts under features/support. You can call this method as many times as you like (to register more modules), but if you try to register more than one Proc you will get an error.

Cucumber will not yield anything to the proc (like it used to do before v0.3).

In earlier versions of Cucumber (before 0.3) you could not register any world_modules. Instead you would register several Proc objects (by calling the method several times). The result of each proc would be yielded to the next proc. Example:

World do |world| # NOT SUPPORTED FROM 0.3
  MyClass.new
end

World do |world| # NOT SUPPORTED FROM 0.3
  world.extend(MyModule)
end

From Cucumber 0.3 the recommended way to do this is:

World do
  MyClass.new
end

World(MyModule)


193
194
195
196
197
198
199
200
# File 'lib/cucumber/step_mother.rb', line 193

def World(*world_modules, &proc)
  if(proc)
    raise MultipleWorld.new(@world_proc, proc) if @world_proc
    @world_proc = proc
  end
  @world_modules ||= []
  @world_modules += world_modules
end