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.

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.



66
67
68
# File 'lib/cucumber/step_mother.rb', line 66

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.



66
67
68
# File 'lib/cucumber/step_mother.rb', line 66

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.



66
67
68
# File 'lib/cucumber/step_mother.rb', line 66

def visitor=(value)
  @visitor = value
end

Class Method Details

.alias_adverb(adverb) ⇒ Object



60
61
62
63
# File 'lib/cucumber/step_mother.rb', line 60

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

Instance Method Details

#After(&proc) ⇒ Object



110
111
112
# File 'lib/cucumber/step_mother.rb', line 110

def After(&proc)
  (@after_procs ||= []).unshift(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).



106
107
108
# File 'lib/cucumber/step_mother.rb', line 106

def Before(&proc)
  (@before_procs ||= []) << proc
end

#before_and_after(scenario, skip = false) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cucumber/step_mother.rb', line 152

def before_and_after(scenario, skip=false)
  unless current_world || skip
    new_world!
    execute_before(scenario)
  end
  if block_given?
    yield
    execute_after(scenario) unless skip
    nil_world!
    scenario_visited(scenario)
  end
end

#best_matches(step_name, step_matches) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/cucumber/step_mother.rb', line 132

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



120
121
122
# File 'lib/cucumber/step_mother.rb', line 120

def current_world
  @current_world
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.



95
96
97
98
99
100
101
102
# File 'lib/cucumber/step_mother.rb', line 95

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

#scenariosObject



81
82
83
# File 'lib/cucumber/step_mother.rb', line 81

def scenarios
  @scenarios ||= []
end

#snippet_text(step_keyword, step_name, multiline_arg_class) ⇒ Object



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

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

#step_definitionsObject



144
145
146
# File 'lib/cucumber/step_mother.rb', line 144

def step_definitions
  @step_definitions ||= []
end

#step_match(step_name, formatted_step_name = nil) ⇒ Object

Raises:



124
125
126
127
128
129
130
# File 'lib/cucumber/step_mother.rb', line 124

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



68
69
70
# File 'lib/cucumber/step_mother.rb', line 68

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

#steps(status = nil) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/cucumber/step_mother.rb', line 72

def steps(status = nil)
  @steps ||= []
  if(status)
    @steps.select{|step| step.status == status}
  else
    @steps
  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).



116
117
118
# File 'lib/cucumber/step_mother.rb', line 116

def World(&proc)
  (@world_procs ||= []) << proc
end