Module: Cucumber::Glue::ProtoWorld

Defined in:
lib/cucumber/glue/proto_world.rb

Overview

Defines the basic API methods availlable 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

Instance Method Summary collapse

Class Method Details

.for(runtime, language) ⇒ Object

Dynamially generate the API module, closuring the dependencies


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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/cucumber/glue/proto_world.rb', line 146

def self.for(runtime, language) # rubocop:disable Metrics/MethodLength
  Module.new do # rubocop:disable Metrics/BlockLength
    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)
      add_namespaced_modules!(namespaced_world_modules)
    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|
      runtime.attach(file, media_type)
    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

    # @private
    def add_world_modules!(modules)
      modules.each do |world_module|
        extend(world_module)
      end
    end

    # @private
    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 = if self.class.respond_to?(namespace)
                          instance_variable_get(variable_name)
                        else
                          Object.new
                        end
          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

    # @private
    def stringify_namespaced_modules
      @__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


96
97
98
# File 'lib/cucumber/glue/proto_world.rb', line 96

def ask(question, timeout_seconds = 60)
  super
end

#attach(file, media_type) ⇒ Object


114
115
116
# File 'lib/cucumber/glue/proto_world.rb', line 114

def attach(file, media_type)
  super
end

#embed(file, mime_type, _label = 'Screenshot') ⇒ Object

Embed an image in the output


101
102
103
104
105
106
107
108
# File 'lib/cucumber/glue/proto_world.rb', line 101

def embed(file, mime_type, _label = 'Screenshot')
  Cucumber.deprecate(
    'Please use attach(file, media_type) instead',
    'embed(file, mime_type, label)',
    '5.0.0'
  )
  attach(file, mime_type)
end

#inspectObject

Prints the list of modules that are included in the World


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

def inspect
  super
end

#log(*messages) ⇒ Object


110
111
112
# File 'lib/cucumber/glue/proto_world.rb', line 110

def log(*messages)
  messages.each { |message| attach(message.to_s.dup, 'text/x.cucumber.log+plain') }
end

#pending(message = 'TODO') ⇒ Object

Mark the matched step as pending.

Raises:


119
120
121
122
123
124
125
126
127
128
# File 'lib/cucumber/glue/proto_world.rb', line 119

def pending(message = 'TODO')
  raise Pending, message unless block_given?

  begin
    yield
  rescue Exception # rubocop:disable Lint/RescueException
    raise Pending, message
  end
  raise Pending, "Expected pending '#{message}' to fail. No Error was raised. No longer pending?"
end

#puts(*messages) ⇒ Object

Note:

Cucumber might surprise you with the behaviour of this method. Instead of sending the output directly to STDOUT, Cucumber will intercept and cache the message until the current step has finished, and then display it.

If you'd prefer to see the message immediately, call Kernel.puts instead.

Print a message to the output.


82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cucumber/glue/proto_world.rb', line 82

def puts(*messages)
  Cucumber.deprecate(
    'Messages emitted with "puts" will no longer be caught by Cucumber ' \
    'and sent to the formatter. If you want message to be in the formatted output, ' \
    "please use log(message) instead.\n" \
    'If you simply want it in the console, '\
    'keep using "puts" (or Kernel.puts to avoid this message)',
    'puts(message)',
    '5.0.0'
  )
  messages.each { |message| log(message.to_s) }
end

#skip_this_scenario(message = 'Scenario skipped') ⇒ Object

Skips this step and the remaining steps in the scenario

Raises:

  • (Core::Test::Result::Skipped)

131
132
133
# File 'lib/cucumber/glue/proto_world.rb', line 131

def skip_this_scenario(message = 'Scenario skipped')
  raise Core::Test::Result::Skipped, message
end

#step(name, raw_multiline_arg = nil) ⇒ Object

Run a single Gherkin step

Examples:

Call another step

step "I am logged in"

Call a step with quotes in the name

step %{the user "Dave" is logged in}

Passing a table

step "the following users exist:", table(%{
  | name  | email           |
  | Matt  | [email protected]   |
  | Aslak | [email protected] |
})

Passing a multiline string

step "the email should contain:", "Dear sir,\nYou've won a prize!\n"

Parameters:

  • name (String)

    The name of the step

  • multiline_argument (String, Cucumber::Test::DocString, Cucumber::Ast::Table)

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

Examples:

steps %{
  Given the user "Susan" exists
  And I am logged in as "Susan"
}

Parameters:

  • steps_text (String)

    The Gherkin snippet to run


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     }
])

Examples:

Create a table

users = table(%{
  | name  | email           |
  | Matt  | [email protected]   |
  | Aslak | [email protected] |
})

Parameters:

  • text_or_table (String)

    The Gherkin string that represents the table


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_sObject

see #inspect


141
142
143
# File 'lib/cucumber/glue/proto_world.rb', line 141

def to_s
  inspect
end