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


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

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


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

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

#backtrace_lineObject


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

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

#file_colon_lineObject


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

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}]" }

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

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

#inspectObject

:nodoc:


91
92
93
# File 'lib/cucumber/step_match.rb', line 91

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

#invoke(multiline_arg) ⇒ Object


28
29
30
31
32
# File 'lib/cucumber/step_match.rb', line 28

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

#locationObject


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

def location
  @step_definition.location
end

#replace_arguments(string, step_arguments, format) ⇒ Object


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

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
                    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


65
66
67
# File 'lib/cucumber/step_match.rb', line 65

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