Class: Cucumber::StepDefinition

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

Overview

A Step Definition holds a Regexp and a Proc, and is created by calling Given, When or Then in the step_definitions ruby files - for example:

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

Defined Under Namespace

Classes: MissingProc

Constant Summary collapse

PARAM_PATTERN =
/"([^\"]*)"/
ESCAPED_PARAM_PATTERN =
'"([^\\"]*)"'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StepDefinitionMethods

#backtrace_line, #format_args, #match, #step_match, #text_length

Constructor Details

#initialize(pattern, &proc) ⇒ StepDefinition

Returns a new instance of StepDefinition.

Raises:



86
87
88
89
90
91
92
93
# File 'lib/cucumber/step_definition.rb', line 86

def initialize(pattern, &proc)
  raise MissingProc if proc.nil?
  if String === pattern
    p = pattern.gsub(/\$\w+/, '(.*)') # Replace $var with (.*)
    pattern = Regexp.new("^#{p}$") 
  end
  @regexp, @proc = pattern, proc
end

Class Method Details

.snippet_text(step_keyword, step_name, multiline_arg_class = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cucumber/step_definition.rb', line 62

def self.snippet_text(step_keyword, step_name, multiline_arg_class = nil)
  escaped = Regexp.escape(step_name).gsub('\ ', ' ').gsub('/', '\/')
  escaped = escaped.gsub(PARAM_PATTERN, ESCAPED_PARAM_PATTERN)

  n = 0
  block_args = escaped.scan(ESCAPED_PARAM_PATTERN).map do |a|
    n += 1
    "arg#{n}"
  end
  block_args << multiline_arg_class.default_arg_name unless multiline_arg_class.nil?
  block_arg_string = block_args.empty? ? "" : " |#{block_args.join(", ")}|"
  multiline_class_string = multiline_arg_class ? "# #{multiline_arg_class.default_arg_name} is a #{multiline_arg_class.to_s}\n  " : ""

  "#{step_keyword} /^#{escaped}$/ do#{block_arg_string}\n  #{multiline_class_string}pending\nend"
end

Instance Method Details

#file_colon_lineObject



109
110
111
# File 'lib/cucumber/step_definition.rb', line 109

def file_colon_line
  @proc.file_colon_line
end

#invoke(world, args) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/cucumber/step_definition.rb', line 99

def invoke(world, args)
  args = args.map{|arg| Ast::PyString === arg ? arg.to_s : arg}
  begin
    world.cucumber_instance_exec(true, regexp.inspect, *args, &@proc)
  rescue Cucumber::ArityMismatchError => e
    e.backtrace.unshift(self.backtrace_line)
    raise e
  end
end

#regexpObject



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

def regexp
  @regexp
end