Class: Cucumber::Runtime::SupportCode

Inherits:
Object
  • Object
show all
Includes:
Constantize
Defined in:
lib/cucumber/runtime/support_code.rb

Defined Under Namespace

Classes: StepInvoker

Instance Method Summary collapse

Methods included from Constantize

#constantize, #underscore

Constructor Details

#initialize(user_interface, configuration = {}) ⇒ SupportCode

Returns a new instance of SupportCode.



43
44
45
46
47
48
49
# File 'lib/cucumber/runtime/support_code.rb', line 43

def initialize(user_interface, configuration={})
  @configuration = Configuration.parse(configuration)
  @runtime_facade = Runtime::ForProgrammingLanguages.new(self, user_interface)
  @unsupported_programming_languages = []
  @programming_languages = []
  @language_map = {}
end

Instance Method Details

#apply_after_hooks(test_case) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'lib/cucumber/runtime/support_code.rb', line 168

def apply_after_hooks(test_case)
  ruby = load_programming_language('rb')
  scenario = RunningTestCase.new(test_case)

  action_blocks = ruby.hooks_for(:after, scenario).map do |hook|
    ->(result) { hook.invoke('After', scenario.with_result(result)) }
  end
  AfterHooks.new(action_blocks).apply_to(test_case)
end

#apply_before_hooks(test_case) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/cucumber/runtime/support_code.rb', line 158

def apply_before_hooks(test_case)
  ruby = load_programming_language('rb')
  scenario = RunningTestCase.new(test_case)

  action_blocks = ruby.hooks_for(:before, scenario).map do |hook|
    ->(result) { hook.invoke('Before', scenario.with_result(result)) }
  end
  BeforeHooks.new(action_blocks).apply_to(test_case)
end

#configure(new_configuration) ⇒ Object



51
52
53
# File 'lib/cucumber/runtime/support_code.rb', line 51

def configure(new_configuration)
  @configuration = Configuration.parse(new_configuration)
end

#find_after_step_hooks(test_case) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/cucumber/runtime/support_code.rb', line 148

def find_after_step_hooks(test_case)
  ruby = load_programming_language('rb')
  scenario = RunningTestCase.new(test_case)

  action_blocks = ruby.hooks_for(:after_step, scenario).map do |hook|
    ->(*args) { hook.invoke('AfterStep', args) }
  end
  StepHooks.new action_blocks
end

#find_around_hooks(test_case) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/cucumber/runtime/support_code.rb', line 178

def find_around_hooks(test_case)
  ruby = load_programming_language('rb')
  scenario = RunningTestCase.new(test_case)

  ruby.hooks_for(:around, scenario).map do |hook|
    Hooks.around_hook(test_case.source) do |run_scenario|
      hook.invoke('Around', scenario, &run_scenario)
    end
  end
end

#find_match(test_step) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cucumber/runtime/support_code.rb', line 136

def find_match(test_step)
  begin
    match = step_match(test_step.name)
  rescue Cucumber::Undefined
    return NoStepMatch.new(test_step.source.last, test_step.name)
  end
  if @configuration.dry_run?
    return SkippingStepMatch.new
  end
  match
end

#fire_hook(name, *args) ⇒ Object



124
125
126
127
128
# File 'lib/cucumber/runtime/support_code.rb', line 124

def fire_hook(name, *args)
  @programming_languages.each do |programming_language|
    programming_language.send(name, *args)
  end
end

#invoke_dynamic_step(step_name, multiline_argument, location = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This allows users to attempt to find, match and execute steps from code as the features are running, as opposed to regular steps which are compiled into test steps before execution.

These are commonly called nested steps.



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

def invoke_dynamic_step(step_name, multiline_argument, location=nil)
  begin
    step_match(step_name).invoke(multiline_argument)
  rescue Undefined => exception
    raise UndefinedDynamicStep, step_name
  end
end

#invoke_dynamic_steps(steps_text, i18n, file_colon_line) ⇒ Object

Invokes a series of steps steps_text. Example:

invoke(%Q{
  Given I have 8 cukes in my belly
  Then I should not be thirsty
})


61
62
63
64
65
# File 'lib/cucumber/runtime/support_code.rb', line 61

def invoke_dynamic_steps(steps_text, i18n, file_colon_line)
  file, line = file_colon_line.split(':')
  parser = Gherkin::Parser::Parser.new(StepInvoker.new(self), true, 'steps', false, i18n.iso_code)
  parser.parse(steps_text, file, line.to_i)
end

#load_files!(files) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/cucumber/runtime/support_code.rb', line 94

def load_files!(files)
  log.debug("Code:\n")
  files.each do |file|
    load_file(file)
  end
  log.debug("\n")
end

#load_files_from_paths(paths) ⇒ Object



102
103
104
105
# File 'lib/cucumber/runtime/support_code.rb', line 102

def load_files_from_paths(paths)
  files = paths.map { |path| Dir["#{path}/**/*"] }.flatten
  load_files! files
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.



85
86
87
88
89
90
91
92
# File 'lib/cucumber/runtime/support_code.rb', line 85

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(@runtime_facade)
  @programming_languages << programming_language
  @language_map[ext] = programming_language
  programming_language
end

#snippet_text(step_keyword, step_name, multiline_arg) ⇒ Object

:nodoc:



113
114
115
116
117
118
# File 'lib/cucumber/runtime/support_code.rb', line 113

def snippet_text(step_keyword, step_name, multiline_arg) #: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, @configuration.snippet_type)
  end.join("\n")
end

#step_definitionsObject



130
131
132
133
134
# File 'lib/cucumber/runtime/support_code.rb', line 130

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

#step_match(step_name, name_to_report = nil) ⇒ Object

:nodoc:



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

def step_match(step_name, name_to_report=nil) #:nodoc:
  @match_cache ||= {}

  match = @match_cache[[step_name, name_to_report]]
  return match if match

  @match_cache[[step_name, name_to_report]] = step_match_without_cache(step_name, name_to_report)
end

#unknown_programming_language?Boolean

Returns:

  • (Boolean)


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

def unknown_programming_language?
  @programming_languages.empty?
end

#unmatched_step_definitionsObject



107
108
109
110
111
# File 'lib/cucumber/runtime/support_code.rb', line 107

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