Module: Cucumber::Glue::ProtoWorld
- Defined in:
- lib/cucumber/glue/proto_world.rb
Overview
Defines the basic API methods available in all Cucumber step definitions.
You can, and probably should, extend this API with your own methods that make sense in your domain. For more on that, see Dsl#World
Constant Summary collapse
- AnsiEscapes =
Cucumber::Gherkin::Formatter::AnsiEscapes
Class Method Summary collapse
-
.for(runtime, language) ⇒ Object
Dynamically generate the API module, closuring the dependencies.
Instance Method Summary collapse
-
#ask(question, timeout_seconds = 60) ⇒ Object
Pause the tests and ask the operator for input.
-
#attach(file, media_type = nil, filename = nil) ⇒ Object
Attach a file to the output.
-
#inspect ⇒ Object
Prints the list of modules that are included in the World.
- #log(*messages) ⇒ Object
-
#pending(message = 'TODO') ⇒ Object
Mark the matched step as pending.
-
#skip_this_scenario(message = 'Scenario skipped') ⇒ Object
Skips this step and the remaining steps in the scenario.
-
#step(name, raw_multiline_arg = nil) ⇒ Object
Run a single Gherkin step.
-
#steps(steps_text) ⇒ Object
Run a snippet of Gherkin.
-
#table(text_or_table) ⇒ Object
Parse Gherkin into a Ast::Table object.
-
#to_s ⇒ Object
see #inspect.
Class Method Details
.for(runtime, language) ⇒ Object
Dynamically generate the API module, closuring the dependencies
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/cucumber/glue/proto_world.rb', line 127 def self.for(runtime, language) Module.new do def self.extended(object) # wrap the dynamically generated module so that we can document the methods # for yardoc, which doesn't like define_method. object.extend(ProtoWorld) end # TODO: pass these in when building the module, instead of mutating them later # Extend the World with user-defined modules def add_modules!(world_modules, namespaced_world_modules) add_world_modules!(world_modules) if world_modules.any? add_namespaced_modules!(namespaced_world_modules) if namespaced_world_modules.any? end define_method(:step) do |name, raw_multiline_arg = nil| location = Core::Test::Location.of_caller runtime.invoke_dynamic_step(name, MultilineArgument.from(raw_multiline_arg, location)) end define_method(:steps) do |steps_text| location = Core::Test::Location.of_caller runtime.invoke_dynamic_steps(steps_text, language, location) end define_method(:ask) do |question, timeout_seconds = 60| runtime.ask(question, timeout_seconds) end define_method(:attach) do |file, media_type, filename| runtime.attach(file, media_type, filename) end # Prints the list of modules that are included in the World def inspect modules = [self.class] (class << self; self; end).instance_eval do modules += included_modules end modules << stringify_namespaced_modules format('#<%<modules>s:0x%<object_id>x>', modules: modules.join('+'), object_id: object_id) end private def add_world_modules!(modules) modules.each do |world_module| extend(world_module) end end def add_namespaced_modules!(modules) @__namespaced_modules = modules modules.each do |namespace, world_modules| world_modules.each do |world_module| variable_name = "@__#{namespace}_world" inner_world = instance_variable_get(variable_name) || Object.new instance_variable_set( variable_name, inner_world.extend(world_module) ) self.class.send(:define_method, namespace) do instance_variable_get(variable_name) end end end end def stringify_namespaced_modules return '' if @__namespaced_modules.nil? @__namespaced_modules.map { |k, v| "#{v.join(',')} (as #{k})" }.join('+') end end end |
Instance Method Details
#ask(question, timeout_seconds = 60) ⇒ Object
Pause the tests and ask the operator for input
76 77 78 |
# File 'lib/cucumber/glue/proto_world.rb', line 76 def ask(question, timeout_seconds = 60) super end |
#attach(file, media_type = nil, filename = nil) ⇒ Object
Attach a file to the output
92 93 94 95 96 97 98 99 100 |
# File 'lib/cucumber/glue/proto_world.rb', line 92 def attach(file, media_type = nil, filename = nil) if File.file?(file) media_type = MiniMime.lookup_by_filename(file)&.content_type if media_type.nil? file = File.read(file, mode: 'rb') end super rescue StandardError super end |
#inspect ⇒ Object
Prints the list of modules that are included in the World
117 118 119 |
# File 'lib/cucumber/glue/proto_world.rb', line 117 def inspect super end |
#log(*messages) ⇒ Object
80 81 82 |
# File 'lib/cucumber/glue/proto_world.rb', line 80 def log(*) .each { || attach(.to_s.dup, 'text/x.cucumber.log+plain') } end |
#pending(message = 'TODO') ⇒ Object
Mark the matched step as pending.
103 104 105 106 107 108 109 |
# File 'lib/cucumber/glue/proto_world.rb', line 103 def pending( = 'TODO') raise Pending, unless block_given? yield rescue Exception raise Pending, end |
#skip_this_scenario(message = 'Scenario skipped') ⇒ Object
Skips this step and the remaining steps in the scenario
112 113 114 |
# File 'lib/cucumber/glue/proto_world.rb', line 112 def skip_this_scenario( = 'Scenario skipped') raise Core::Test::Result::Skipped, end |
#step(name, raw_multiline_arg = nil) ⇒ Object
Run a single Gherkin step
29 30 31 |
# File 'lib/cucumber/glue/proto_world.rb', line 29 def step(name, raw_multiline_arg = nil) super end |
#steps(steps_text) ⇒ Object
Run a snippet of Gherkin
40 41 42 |
# File 'lib/cucumber/glue/proto_world.rb', line 40 def steps(steps_text) super end |
#table(text_or_table) ⇒ Object
Parse Gherkin into a Ast::Table object.
Useful in conjunction with the #step method. Returns a Cucumber::MultilineArgument::DataTable for text_or_table
, which can either be a String:
table(%{
| account | description | amount |
| INT-100 | Taxi | 114 |
| CUC-101 | Peeler | 22 |
})
or a 2D Array:
table([
%w{ account description amount },
%w{ INT-100 Taxi 114 },
%w{ CUC-101 Peeler 22 }
])
71 72 73 |
# File 'lib/cucumber/glue/proto_world.rb', line 71 def table(text_or_table) MultilineArgument::DataTable.from(text_or_table) end |
#to_s ⇒ Object
see #inspect
122 123 124 |
# File 'lib/cucumber/glue/proto_world.rb', line 122 def to_s inspect end |