Class: Cucumber::StepMatch

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

Overview

Represents the match found between a Test Step and its activation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(step_definition, step_name, step_arguments) ⇒ StepMatch

Returns a new instance of StepMatch.



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

def initialize(step_definition, step_name, step_arguments)
  raise "step_arguments can't be nil (but it can be an empty array)" if step_arguments.nil?
  @step_definition, @name_to_match, @step_arguments = step_definition, step_name, step_arguments
end

Instance Attribute Details

#step_argumentsObject (readonly)

:nodoc:



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

def step_arguments
  @step_arguments
end

#step_definitionObject (readonly)

:nodoc:



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

def step_definition
  @step_definition
end

Instance Method Details

#activate(test_step) ⇒ Object



18
19
20
21
22
# File 'lib/cucumber/step_match.rb', line 18

def activate(test_step)
  test_step.with_action(@step_definition.location) do
    invoke(MultilineArgument.from_core(test_step.source.last.multiline_arg))
  end
end

#argsObject



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

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

#backtrace_lineObject



57
58
59
# File 'lib/cucumber/step_match.rb', line 57

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

#file_colon_lineObject



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

def file_colon_line
  location.to_s
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}]" }


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

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

#inspectObject

:nodoc:



86
87
88
# File 'lib/cucumber/step_match.rb', line 86

def inspect #:nodoc:
  "#<#{self.class}: #{location}>"
end

#invoke(multiline_arg) ⇒ Object



24
25
26
27
28
# File 'lib/cucumber/step_match.rb', line 24

def invoke(multiline_arg)
  all_args = deep_clone_args
  multiline_arg.append_to(all_args)
  @step_definition.invoke(all_args)
end

#locationObject



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

def location
  @step_definition.location
end

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cucumber/step_match.rb', line 65

def replace_arguments(string, step_arguments, format, &proc)
  s = string.dup
  offset = past_offset = 0
  step_arguments.each do |step_argument|
    next if step_argument.offset.nil? || step_argument.offset < past_offset

    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.offset + offset, step_argument.val.length] = replacement
    offset += replacement.unpack('U*').length - step_argument.val.unpack('U*').length
    past_offset = step_argument.offset + step_argument.val.length
  end
  s
end

#text_lengthObject



61
62
63
# File 'lib/cucumber/step_match.rb', line 61

def text_length
  @step_definition.regexp_source.unpack('U*').length
end