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.



58
59
60
# File 'lib/cucumber/step_mother.rb', line 58

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.



58
59
60
# File 'lib/cucumber/step_mother.rb', line 58

def snippet_generator=(value)
  @snippet_generator = value
end

Class Method Details

.alias_adverb(adverb) ⇒ Object



53
54
55
# File 'lib/cucumber/step_mother.rb', line 53

def alias_adverb(adverb)
  alias_method adverb, :register_step_definition
end

Instance Method Details

#After(&proc) ⇒ Object



102
103
104
# File 'lib/cucumber/step_mother.rb', line 102

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).



98
99
100
# File 'lib/cucumber/step_mother.rb', line 98

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

#before_and_after(scenario, skip = false) ⇒ Object



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

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



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

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



112
113
114
# File 'lib/cucumber/step_mother.rb', line 112

def current_world
  @current_world
end


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

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.



87
88
89
90
91
92
93
94
# File 'lib/cucumber/step_mother.rb', line 87

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



73
74
75
# File 'lib/cucumber/step_mother.rb', line 73

def scenarios
  @scenarios ||= []
end

#snippet_text(step_keyword, step_name) ⇒ Object



140
141
142
# File 'lib/cucumber/step_mother.rb', line 140

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

#step_definitionsObject



136
137
138
# File 'lib/cucumber/step_mother.rb', line 136

def step_definitions
  @step_definitions ||= []
end

#step_match(step_name, formatted_step_name = nil) ⇒ Object

Raises:



116
117
118
119
120
121
122
# File 'lib/cucumber/step_mother.rb', line 116

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) if matches.size > 1
  matches[0]
end

#step_visited(step) ⇒ Object



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

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

#steps(status = nil) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/cucumber/step_mother.rb', line 64

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).



108
109
110
# File 'lib/cucumber/step_mother.rb', line 108

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