Class: Cucumber::Glue::StepDefinition
- Inherits:
-
Object
- Object
- Cucumber::Glue::StepDefinition
- 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
-
#expression ⇒ Object
readonly
Returns the value of attribute expression.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object private
- #arguments_from(step_name) ⇒ Object private
- #backtrace_line ⇒ Object private
- #expression_type ⇒ Object
- #file ⇒ Object private
- #file_colon_line ⇒ Object private
-
#initialize(id, registry, expression, proc) ⇒ StepDefinition
constructor
A new instance of StepDefinition.
-
#invoke(args) ⇒ Object
private
TODO: inline this and step definition just be a value object.
-
#location ⇒ Object
The source location where the step definition can be found.
- #to_envelope ⇒ Object
- #to_hash ⇒ Object private
Constructor Details
#initialize(id, registry, expression, proc) ⇒ StepDefinition
Returns a new instance of StepDefinition.
69 70 71 72 73 74 75 76 |
# File 'lib/cucumber/glue/step_definition.rb', line 69 def initialize(id, registry, expression, proc) raise 'No regexp' if expression.is_a?(Regexp) @id = id @registry = registry @expression = expression @proc = proc end |
Instance Attribute Details
#expression ⇒ Object (readonly)
Returns the value of attribute expression.
67 68 69 |
# File 'lib/cucumber/glue/step_definition.rb', line 67 def expression @expression end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
67 68 69 |
# File 'lib/cucumber/glue/step_definition.rb', line 67 def id @id end |
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
67 68 69 |
# File 'lib/cucumber/glue/step_definition.rb', line 67 def registry @registry end |
Class Method Details
.new(id, registry, string_or_regexp, proc_or_sym, options) ⇒ Object
26 27 28 29 30 |
# File 'lib/cucumber/glue/step_definition.rb', line 26 def new(id, registry, string_or_regexp, proc_or_sym, ) raise MissingProc if proc_or_sym.nil? super id, registry, registry.create_expression(string_or_regexp), create_proc(proc_or_sym, ) end |
Instance Method Details
#==(other) ⇒ 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.
123 124 125 |
# File 'lib/cucumber/glue/step_definition.rb', line 123 def ==(other) expression.source == other.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.
128 129 130 |
# File 'lib/cucumber/glue/step_definition.rb', line 128 def arguments_from(step_name) @expression.match(step_name) end |
#backtrace_line ⇒ 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.
142 143 144 |
# File 'lib/cucumber/glue/step_definition.rb', line 142 def backtrace_line "#{location}:in `#{@expression}'" end |
#expression_type ⇒ Object
96 97 98 99 100 |
# File 'lib/cucumber/glue/step_definition.rb', line 96 def expression_type return Cucumber::Messages::StepDefinitionPatternType::CUCUMBER_EXPRESSION if expression.is_a?(CucumberExpressions::CucumberExpression) Cucumber::Messages::StepDefinitionPatternType::REGULAR_EXPRESSION end |
#file ⇒ 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.
162 163 164 |
# File 'lib/cucumber/glue/step_definition.rb', line 162 def file @file ||= location.file end |
#file_colon_line ⇒ 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.
147 148 149 150 151 152 153 154 |
# File 'lib/cucumber/glue/step_definition.rb', line 147 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
134 135 136 137 138 139 |
# File 'lib/cucumber/glue/step_definition.rb', line 134 def invoke(args) InvokeInWorld.cucumber_instance_exec_in(@registry.current_world, true, @expression.to_s, *args, &@proc) rescue ArityMismatchError => e e.backtrace.unshift(backtrace_line) raise e end |
#location ⇒ Object
The source location where the step definition can be found
157 158 159 |
# File 'lib/cucumber/glue/step_definition.rb', line 157 def location @location ||= Cucumber::Core::Test::Location.from_source_location(*@proc.source_location) end |
#to_envelope ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cucumber/glue/step_definition.rb', line 78 def to_envelope Cucumber::Messages::Envelope.new( step_definition: Cucumber::Messages::StepDefinition.new( id: id, pattern: Cucumber::Messages::StepDefinitionPattern.new( source: expression.source.to_s, type: expression_type ), source_reference: Cucumber::Messages::SourceReference.new( uri: location.file, location: Cucumber::Messages::Location.new( line: location.lines.first ) ) ) ) end |
#to_hash ⇒ 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.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/cucumber/glue/step_definition.rb', line 103 def to_hash type = expression.is_a?(CucumberExpressions::RegularExpression) ? 'regular expression' : 'cucumber expression' regexp = expression.regexp flags = '' flags += 'm' if (regexp. & Regexp::MULTILINE) != 0 flags += 'i' if (regexp. & Regexp::IGNORECASE) != 0 flags += 'x' if (regexp. & Regexp::EXTENDED) != 0 { source: { type: type, expression: expression.source }, regexp: { source: regexp.source, flags: flags } } end |