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 =
'"([^\\"]*)"'

Instance Attribute Summary collapse

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:



91
92
93
94
95
96
97
98
# File 'lib/cucumber/step_definition.rb', line 91

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

Instance Attribute Details

#procObject (readonly)

Returns the value of attribute proc.



89
90
91
# File 'lib/cucumber/step_definition.rb', line 89

def proc
  @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
77
78
79
# 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_comment = ""
  if(multiline_arg_class == Ast::Table)
    multiline_class_comment = "# #{multiline_arg_class.default_arg_name} is a #{multiline_arg_class.to_s}\n  "
  end

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

Instance Method Details

#fileObject



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

def file
  @file ||= file_colon_line.split(':')[0]
end

#file_colon_lineObject



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

def file_colon_line
  @proc.file_colon_line
end

#invoke(world, args) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/cucumber/step_definition.rb', line 104

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



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

def regexp
  @regexp
end