Class: Cucumber::StepMother

Inherits:
Object show all
Includes:
Constantize
Defined in:
lib/cucumber/step_mother.rb

Overview

This is the meaty part of Cucumber that ties everything together.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Constantize

#constantize, #underscore

Constructor Details

#initializeStepMother

Returns a new instance of StepMother.



46
47
48
49
50
51
# File 'lib/cucumber/step_mother.rb', line 46

def initialize
  @unsupported_programming_languages = []
  @programming_languages = []
  @language_map = {}
  load_natural_language('en')
end

Instance Attribute Details

#log=(value) ⇒ Object

Sets the attribute log

Parameters:

  • value

    the value to set the attribute log to.



44
45
46
# File 'lib/cucumber/step_mother.rb', line 44

def log=(value)
  @log = value
end

#optionsObject

Returns the options passed on the command line.



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

def options
  @options ||= {}
end

#visitor=(value) ⇒ Object (writeonly)

Sets the attribute visitor

Parameters:

  • value

    the value to set the attribute visitor to.



44
45
46
# File 'lib/cucumber/step_mother.rb', line 44

def visitor=(value)
  @visitor = value
end

Instance Method Details

#after(scenario) ⇒ Object

:nodoc:



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

def after(scenario) #:nodoc:
  @current_scenario = nil
  return if options[:dry_run]
  @programming_languages.each do |programming_language|
    programming_language.after(scenario)
  end
end

#after_configuration(configuration) ⇒ Object

:nodoc



221
222
223
224
225
# File 'lib/cucumber/step_mother.rb', line 221

def after_configuration(configuration) #:nodoc
  @programming_languages.each do |programming_language|
    programming_language.after_configuration(configuration)
  end
end

#after_stepObject

:nodoc:



214
215
216
217
218
219
# File 'lib/cucumber/step_mother.rb', line 214

def after_step #:nodoc:
  return if options[:dry_run]
  @programming_languages.each do |programming_language|
    programming_language.execute_after_step(@current_scenario)
  end
end

#announce(msg) ⇒ Object

:nodoc:



126
127
128
# File 'lib/cucumber/step_mother.rb', line 126

def announce(msg) #:nodoc:
  @visitor.announce(msg)
end

#before(scenario) ⇒ Object

:nodoc:



198
199
200
201
202
203
204
# File 'lib/cucumber/step_mother.rb', line 198

def before(scenario) #:nodoc:
  return if options[:dry_run] || @current_scenario
  @current_scenario = scenario
  @programming_languages.each do |programming_language|
    programming_language.before(scenario)
  end
end

#before_and_after(scenario, skip_hooks = false) {|scenario| ... } ⇒ Object

:nodoc:

Yields:

  • (scenario)


182
183
184
185
186
187
# File 'lib/cucumber/step_mother.rb', line 182

def before_and_after(scenario, skip_hooks=false) #:nodoc:
  before(scenario) unless skip_hooks
  yield scenario
  after(scenario) unless skip_hooks
  scenario_visited(scenario)
end

#best_matches(step_name, step_matches) ⇒ Object

:nodoc:



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

def best_matches(step_name, step_matches) #:nodoc:
  no_groups      = step_matches.select {|step_match| step_match.args.length == 0}
  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 no_groups.any?
    longest_regexp_length = no_groups.map {|step_match| step_match.text_length }.max
    no_groups.select {|step_match| step_match.text_length == longest_regexp_length }
  elsif top_groups.any?
    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

#load_code_file(step_def_file) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/cucumber/step_mother.rb', line 77

def load_code_file(step_def_file)
  if programming_language = programming_language_for(step_def_file)
    log.debug("  * #{step_def_file}\n")
    programming_language.load_code_file(step_def_file)
  else
    log.debug("  * #{step_def_file} [NOT SUPPORTED]\n")
  end
end

#load_code_files(step_def_files) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/cucumber/step_mother.rb', line 69

def load_code_files(step_def_files)
  log.debug("Code:\n")
  step_def_files.each do |step_def_file|
    load_code_file(step_def_file)
  end
  log.debug("\n")
end

#load_natural_language(lang) ⇒ Object

Loads a natural language. This has the effect of aliasing Step Definition keywords for all of the registered programming languages (if they support aliasing). See #load_programming_language



104
105
106
# File 'lib/cucumber/step_mother.rb', line 104

def load_natural_language(lang)
  Parser::NaturalLanguage.get(self, lang)
end

#load_plain_text_features(feature_files) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cucumber/step_mother.rb', line 53

def load_plain_text_features(feature_files)
  features = Ast::Features.new

  log.debug("Features:\n")
  feature_files.each do |f|
    feature_file = FeatureFile.new(f)
    feature = feature_file.parse(self, options)
    if feature
      features.add_feature(feature)
      log.debug("  * #{f}\n")
    end
  end
  log.debug("\n")
  features
end

#load_programming_language(ext) ⇒ Object

Loads and registers programming language implementation. Instances are cached, so calling with the same argument twice will return the same instance.



90
91
92
93
94
95
96
97
98
# File 'lib/cucumber/step_mother.rb', line 90

def load_programming_language(ext)
  return @language_map[ext] if @language_map[ext]
  programming_language_class = constantize("Cucumber::#{ext.capitalize}Support::#{ext.capitalize}Language")
  programming_language = programming_language_class.new(self)
  programming_language.alias_adverbs(@adverbs || [])
  @programming_languages << programming_language
  @language_map[ext] = programming_language
  programming_language
end

#register_adverbs(adverbs) ⇒ Object

:nodoc:



189
190
191
192
193
194
195
196
# File 'lib/cucumber/step_mother.rb', line 189

def register_adverbs(adverbs) #:nodoc:
  @adverbs ||= []
  @adverbs += adverbs
  @adverbs.uniq!
  @programming_languages.each do |programming_language|
    programming_language.alias_adverbs(@adverbs)
  end
end

#scenarios(status = nil) ⇒ Object

:nodoc:



130
131
132
133
134
135
136
137
# File 'lib/cucumber/step_mother.rb', line 130

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

#snippet_text(step_keyword, step_name, multiline_arg_class) ⇒ Object

:nodoc:



171
172
173
174
175
176
# File 'lib/cucumber/step_mother.rb', line 171

def snippet_text(step_keyword, step_name, multiline_arg_class) #:nodoc:
  load_programming_language('rb') if unknown_programming_language?
  @programming_languages.map do |programming_language|
    programming_language.snippet_text(step_keyword, step_name, multiline_arg_class)
  end.join("\n")
end

#step_match(step_name, formatted_step_name = nil) ⇒ Object

:nodoc:

Raises:



139
140
141
142
143
144
145
146
147
# File 'lib/cucumber/step_mother.rb', line 139

def step_match(step_name, formatted_step_name=nil) #:nodoc:
  matches = @programming_languages.map do |programming_language| 
    programming_language.step_matches(step_name, formatted_step_name)
  end.flatten
  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

:nodoc:



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

def step_visited(step) #:nodoc:
  steps << step unless steps.index(step)
end

#steps(status = nil) ⇒ Object

:nodoc:



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

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

#unknown_programming_language?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/cucumber/step_mother.rb', line 178

def unknown_programming_language?
  @programming_languages.empty?
end

#unmatched_step_definitionsObject



165
166
167
168
169
# File 'lib/cucumber/step_mother.rb', line 165

def unmatched_step_definitions
  @programming_languages.map do |programming_language| 
    programming_language.unmatched_step_definitions
  end.flatten
end