Class: Cucumber::StepMatch

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


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

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 = step_definition
  @name_to_match = step_name
  @step_arguments = step_arguments
end

Instance Attribute Details

#step_argumentsObject (readonly)

Returns the value of attribute step_arguments


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

def step_arguments
  @step_arguments
end

#step_definitionObject (readonly)

Returns the value of attribute step_definition


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

def step_definition
  @step_definition
end

Instance Method Details

#activate(test_step) ⇒ Object


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

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

#argsObject


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

def args
  current_world = @step_definition.registry.current_world
  @step_arguments.map do |arg|
    arg.value(current_world)
  end
end

#backtrace_lineObject


63
64
65
# File 'lib/cucumber/step_match.rb', line 63

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

#file_colon_lineObject


59
60
61
# File 'lib/cucumber/step_match.rb', line 59

def file_colon_line
  location.to_s
end

#format_args(format = ->(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}]" }

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

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

#inspectObject

:nodoc:


93
94
95
# File 'lib/cucumber/step_match.rb', line 93

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

#invoke(multiline_arg) ⇒ Object


30
31
32
33
34
# File 'lib/cucumber/step_match.rb', line 30

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

#locationObject


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

def location
  @step_definition.location
end

#replace_arguments(string, step_arguments, format) ⇒ Object


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cucumber/step_match.rb', line 71

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

    replacement = if block_given?
                    yield(group.value)
                  elsif Proc == format.class
                    format.call(group.value)
                  else
                    format % group.value
                  end

    s[group.start + offset, group.value.length] = replacement
    offset += replacement.unpack('U*').length - group.value.unpack('U*').length
    past_offset = group.start + group.value.length
  end
  s
end

#text_lengthObject


67
68
69
# File 'lib/cucumber/step_match.rb', line 67

def text_length
  @step_definition.expression.source.to_s.unpack('U*').length
end