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
16
# 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)

:nodoc:



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

def step_arguments
  @step_arguments
end

#step_definitionObject (readonly)

:nodoc:



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

def step_definition
  @step_definition
end

Instance Method Details

#activate(test_step) ⇒ Object



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

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

#argsObject



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

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

#backtrace_lineObject



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

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

#file_colon_lineObject



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

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


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

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

#inspectObject

:nodoc:



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

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

#invoke(multiline_arg) ⇒ Object



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

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

#locationObject



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

def location
  @step_definition.location
end

#replace_arguments(string, step_arguments, format) ⇒ Object



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

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 format.instance_of?(Proc)
                    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



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

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