Class: Cucumber::StepMatch

Inherits:
Object show all
Defined in:
lib/cucumber/step_match.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(step_definition, name_to_match, name_to_report, step_arguments) ⇒ StepMatch

Creates a new StepMatch. The name_to_report argument is what’s reported, unless it’s is, in which case name_to_report is used instead.



8
9
10
11
# File 'lib/cucumber/step_match.rb', line 8

def initialize(step_definition, name_to_match, name_to_report, step_arguments)
  raise "name_to_match can't be nil" if name_to_match.nil?
  @step_definition, @name_to_match, @name_to_report, @step_arguments = step_definition, name_to_match, name_to_report, step_arguments
end

Instance Attribute Details

#step_definitionObject (readonly)

Returns the value of attribute step_definition.



3
4
5
# File 'lib/cucumber/step_match.rb', line 3

def step_definition
  @step_definition
end

Instance Method Details

#argsObject



13
14
15
# File 'lib/cucumber/step_match.rb', line 13

def args
  @step_arguments.map{|g| g.val}
end

#backtrace_lineObject



50
51
52
# File 'lib/cucumber/step_match.rb', line 50

def backtrace_line
  "#{file_colon_line}:in `#{@step_definition.regexp_source}'"
end

#file_colon_lineObject



46
47
48
# File 'lib/cucumber/step_match.rb', line 46

def file_colon_line
  @step_definition.file_colon_line
end

#format_args(format = lambda{|a| a}, &proc) ⇒ Object

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

The format can 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}]" }


42
43
44
# File 'lib/cucumber/step_match.rb', line 42

def format_args(format = lambda{|a| a}, &proc)
  @name_to_report || replace_arguments(@name_to_match, @step_arguments, format, &proc)
end

#inspectObject

:nodoc:



77
78
79
# File 'lib/cucumber/step_match.rb', line 77

def inspect #:nodoc:
  sprintf("#<%s:0x%x>", self.class, self.object_id)
end

#invoke(multiline_arg) ⇒ Object



21
22
23
24
25
# File 'lib/cucumber/step_match.rb', line 21

def invoke(multiline_arg)
  all_args = args
  all_args << multiline_arg.dup if multiline_arg
  @step_definition.invoke(all_args)
end

#nameObject



17
18
19
# File 'lib/cucumber/step_match.rb', line 17

def name
  @name_to_report
end

#replace_arguments(string, step_arguments, format, &proc) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cucumber/step_match.rb', line 58

def replace_arguments(string, step_arguments, format, &proc)
  s = string.dup
  offset = 0
  step_arguments.each do |step_argument|
    next if step_argument.byte_offset.nil?
    replacement = if block_given?
      proc.call(step_argument.val)
    elsif Proc === format
      format.call(step_argument.val)
    else
      format % step_argument.val
    end

    s[step_argument.byte_offset + offset, step_argument.val.length] = replacement
    offset += replacement.jlength - step_argument.val.jlength
  end
  s
end

#text_lengthObject



54
55
56
# File 'lib/cucumber/step_match.rb', line 54

def text_length
  @step_definition.regexp_source.jlength
end