Class: Cucumber::RbSupport::RbStepDefinition

Inherits:
Object
  • Object
show all
Includes:
LanguageSupport::StepDefinitionMethods
Defined in:
lib/cucumber/rb_support/rb_step_definition.rb

Overview

A Ruby Step Definition holds a Regexp and a Proc, and is created by calling Given, When or Then in the step_definitions ruby files. See also RbDsl.

Example:

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

Defined Under Namespace

Classes: MissingProc

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LanguageSupport::StepDefinitionMethods

#backtrace_line, #format_args, #same_regexp?, #step_match, #text_length

Constructor Details

#initialize(rb_language, pattern, proc) ⇒ RbStepDefinition

Returns a new instance of RbStepDefinition.

Raises:



28
29
30
31
32
33
34
35
# File 'lib/cucumber/rb_support/rb_step_definition.rb', line 28

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

Instance Attribute Details

#procObject (readonly)

Returns the value of attribute proc.



26
27
28
# File 'lib/cucumber/rb_support/rb_step_definition.rb', line 26

def proc
  @proc
end

Instance Method Details

#fileObject



55
56
57
# File 'lib/cucumber/rb_support/rb_step_definition.rb', line 55

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

#file_colon_lineObject



51
52
53
# File 'lib/cucumber/rb_support/rb_step_definition.rb', line 51

def file_colon_line
  @proc.file_colon_line
end

#invoke(args) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/cucumber/rb_support/rb_step_definition.rb', line 41

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

#regexpObject



37
38
39
# File 'lib/cucumber/rb_support/rb_step_definition.rb', line 37

def regexp
  @regexp
end