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.



32
33
34
35
36
37
38
# File 'lib/cucumber/runtime/support_code.rb', line 32

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

#around(scenario, block) ⇒ Object



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

def around(scenario, block)
  @programming_languages.reverse.inject(block) do |blk, programming_language|
    proc do
      programming_language.around(scenario) do
        blk.call(scenario)
      end
    end
  end.call
end

#configure(new_configuration) ⇒ Object



40
41
42
# File 'lib/cucumber/runtime/support_code.rb', line 40

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

#fire_hook(name, *args) ⇒ Object



110
111
112
113
114
# File 'lib/cucumber/runtime/support_code.rb', line 110

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

#invoke(step_name, multiline_argument = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/cucumber/runtime/support_code.rb', line 56

def invoke(step_name, multiline_argument=nil)
  multiline_argument = Cucumber::Ast::MultilineArgument.from(multiline_argument)
  # It is very important to leave multiline_argument=nil as a vararg. Cuke4Duke needs it that way.
  begin
    step_match(step_name).invoke(multiline_argument)
  rescue Exception => e
    e.nested! if Undefined === e
    raise e
  end
end

#invoke_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
})


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

def invoke_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



80
81
82
83
84
85
86
# File 'lib/cucumber/runtime/support_code.rb', line 80

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



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

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.



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

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_class) ⇒ Object

:nodoc:



99
100
101
102
103
104
# File 'lib/cucumber/runtime/support_code.rb', line 99

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, @configuration.snippet_type)
  end.join("\n")
end

#step_definitionsObject



126
127
128
129
130
# File 'lib/cucumber/runtime/support_code.rb', line 126

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:



132
133
134
135
136
137
138
139
# File 'lib/cucumber/runtime/support_code.rb', line 132

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)


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

def unknown_programming_language?
  @programming_languages.empty?
end

#unmatched_step_definitionsObject



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

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