Class: Cucumber::StepDefinition

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, &proc) ⇒ StepDefinition

Returns a new instance of StepDefinition.

Raises:



27
28
29
30
31
32
33
34
# File 'lib/cucumber/step_definition.rb', line 27

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

Instance Attribute Details

#regexpObject (readonly)

Returns the value of attribute regexp.



25
26
27
# File 'lib/cucumber/step_definition.rb', line 25

def regexp
  @regexp
end

Class Method Details

.snippet_text(step_keyword, step_name) ⇒ Object



14
15
16
17
# File 'lib/cucumber/step_definition.rb', line 14

def self.snippet_text(step_keyword, step_name)
  escaped = Regexp.escape(step_name).gsub('\ ', ' ').gsub('/', '\/')
  "#{step_keyword} /^#{escaped}$/ do\n  pending\nend"
end

Instance Method Details

#execute(step_name, world, *args) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/cucumber/step_definition.rb', line 68

def execute(step_name, 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.to_backtrace_line)
    raise e
  end
end

#file_colon_lineObject



82
83
84
# File 'lib/cucumber/step_definition.rb', line 82

def file_colon_line
  @proc.file_colon_line
end

#format_args(step_name, format) ⇒ Object

Formats the matched arguments of the associated Step. This method is usually called from visitors, which render output.

The format either be a String or a Proc.

If it is a String it should be a format string according to Kernel#sprinf, for example:

'<span class="param">%s</span></tt>'

If it is a Proc, it should take one argument and return the formatted argument, for example:

lambda { |param| "[#{param}]" }


60
61
62
# File 'lib/cucumber/step_definition.rb', line 60

def format_args(step_name, format)
  step_name.gzub(@regexp, format)
end

#match(step_name) ⇒ Object

:stopdoc:



38
39
40
41
42
43
# File 'lib/cucumber/step_definition.rb', line 38

def match(step_name)
  case step_name
  when String then @regexp.match(step_name)
  when Regexp then @regexp == step_name
  end
end

#matched_args(step_name) ⇒ Object



64
65
66
# File 'lib/cucumber/step_definition.rb', line 64

def matched_args(step_name)
  step_name.match(@regexp).captures
end

#to_backtrace_lineObject



78
79
80
# File 'lib/cucumber/step_definition.rb', line 78

def to_backtrace_line
  "#{file_colon_line}:in `#{@regexp.inspect}'"
end