Class: Cucumber::Glue::StepDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/glue/step_definition.rb

Overview

A Step Definition holds a Regexp pattern and a Proc, and is typically created by calling Given, When or Then in the step_definitions Ruby files.

Example:

Given /I have (\d+) cucumbers in my belly/ do
  # some code here
end

Defined Under Namespace

Classes: MissingProc

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry, expression, proc) ⇒ StepDefinition

Returns a new instance of StepDefinition


68
69
70
71
72
# File 'lib/cucumber/glue/step_definition.rb', line 68

def initialize(registry, expression, proc)
  raise 'No regexp' if expression.is_a?(Regexp)
  @registry, @expression, @proc = registry, expression, proc
  # @registry.available_step_definition(regexp_source, location)
end

Instance Attribute Details

#expressionObject (readonly)

Returns the value of attribute expression


66
67
68
# File 'lib/cucumber/glue/step_definition.rb', line 66

def expression
  @expression
end

#registryObject (readonly)

Returns the value of attribute registry


66
67
68
# File 'lib/cucumber/glue/step_definition.rb', line 66

def registry
  @registry
end

Class Method Details

.new(registry, string_or_regexp, proc_or_sym, options) ⇒ Object

Raises:


28
29
30
31
# File 'lib/cucumber/glue/step_definition.rb', line 28

def new(registry, string_or_regexp, proc_or_sym, options)
  raise MissingProc if proc_or_sym.nil?
  super registry, registry.create_expression(string_or_regexp), create_proc(proc_or_sym, options)
end

Instance Method Details

#==(step_definition) ⇒ 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.


95
96
97
# File 'lib/cucumber/glue/step_definition.rb', line 95

def ==(step_definition)
  expression.source == step_definition.expression.source
end

#arguments_from(step_name) ⇒ 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.


100
101
102
103
104
# File 'lib/cucumber/glue/step_definition.rb', line 100

def arguments_from(step_name)
  args = @expression.match(step_name)
  # @registry.invoked_step_definition(regexp_source, location) if args
  args
end

#backtrace_lineObject

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.


118
119
120
# File 'lib/cucumber/glue/step_definition.rb', line 118

def backtrace_line
  "#{location}:in `#{@expression}'"
end

#fileObject

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.


138
139
140
# File 'lib/cucumber/glue/step_definition.rb', line 138

def file
  @file ||= location.file
end

#file_colon_lineObject

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.


123
124
125
126
127
128
129
130
# File 'lib/cucumber/glue/step_definition.rb', line 123

def file_colon_line
  case @proc
  when Proc
    location.to_s
  when Symbol
    ":#{@proc}"
  end
end

#invoke(args) ⇒ 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.

TODO: inline this and step definition just be a value object


108
109
110
111
112
113
114
115
# File 'lib/cucumber/glue/step_definition.rb', line 108

def invoke(args)
  begin
    InvokeInWorld.cucumber_instance_exec_in(@registry.current_world, true, @expression.to_s, *args, &@proc)
  rescue ArityMismatchError => e
    e.backtrace.unshift(self.backtrace_line)
    raise e
  end
end

#locationObject

The source location where the step definition can be found


133
134
135
# File 'lib/cucumber/glue/step_definition.rb', line 133

def location
  @location ||= Cucumber::Core::Ast::Location.from_source_location(*@proc.source_location)
end

#to_hashObject

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.


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cucumber/glue/step_definition.rb', line 75

def to_hash
  type = expression.is_a?(CucumberExpressions::RegularExpression) ? 'regular expression' : 'cucumber expression'
  regexp = expression.regexp
  flags = ''
  flags += 'm' if (regexp.options & Regexp::MULTILINE) != 0
  flags += 'i' if (regexp.options & Regexp::IGNORECASE) != 0
  flags += 'x' if (regexp.options & Regexp::EXTENDED) != 0
  {
    source: {
      type: type,
      expression: expression.source
    },
    regexp: {
      source: regexp.source,
      flags: flags
    }
  }
end