You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

step_match.rb 3.7 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # frozen_string_literal: true
  2. require 'cucumber/multiline_argument'
  3. module Cucumber
  4. # Represents the match found between a Test Step and its activation
  5. class StepMatch # :nodoc:
  6. attr_reader :step_definition, :step_arguments
  7. def initialize(step_definition, step_name, step_arguments)
  8. raise "step_arguments can't be nil (but it can be an empty array)" if step_arguments.nil?
  9. @step_definition = step_definition
  10. @name_to_match = step_name
  11. @step_arguments = step_arguments
  12. end
  13. def args
  14. current_world = @step_definition.registry.current_world
  15. @step_arguments.map do |arg|
  16. arg.value(current_world)
  17. end
  18. end
  19. def activate(test_step)
  20. test_step.with_action(@step_definition.location) do
  21. invoke(MultilineArgument.from_core(test_step.multiline_arg))
  22. end
  23. end
  24. def invoke(multiline_arg)
  25. all_args = args
  26. multiline_arg.append_to(all_args)
  27. @step_definition.invoke(all_args)
  28. end
  29. # Formats the matched arguments of the associated Step. This method
  30. # is usually called from visitors, which render output.
  31. #
  32. # The +format+ can either be a String or a Proc.
  33. #
  34. # If it is a String it should be a format string according to
  35. # <tt>Kernel#sprinf</tt>, for example:
  36. #
  37. # '<span class="param">%s</span></tt>'
  38. #
  39. # If it is a Proc, it should take one argument and return the formatted
  40. # argument, for example:
  41. #
  42. # lambda { |param| "[#{param}]" }
  43. #
  44. def format_args(format = ->(a) { a }, &proc)
  45. replace_arguments(@name_to_match, @step_arguments, format, &proc)
  46. end
  47. def location
  48. @step_definition.location
  49. end
  50. def file_colon_line
  51. location.to_s
  52. end
  53. def backtrace_line
  54. "#{file_colon_line}:in `#{@step_definition.expression}'"
  55. end
  56. def text_length
  57. @step_definition.expression.source.to_s.unpack('U*').length
  58. end
  59. def replace_arguments(string, step_arguments, format)
  60. s = string.dup
  61. offset = past_offset = 0
  62. step_arguments.each do |step_argument|
  63. group = step_argument.group
  64. next if group.value.nil? || group.start < past_offset
  65. replacement = if block_given?
  66. yield(group.value)
  67. elsif Proc == format.class
  68. format.call(group.value)
  69. else
  70. format % group.value
  71. end
  72. s[group.start + offset, group.value.length] = replacement
  73. offset += replacement.unpack('U*').length - group.value.unpack('U*').length
  74. past_offset = group.start + group.value.length
  75. end
  76. s
  77. end
  78. def inspect # :nodoc:
  79. "#<#{self.class}: #{location}>"
  80. end
  81. end
  82. class SkippingStepMatch
  83. def activate(test_step)
  84. test_step.with_action { raise Core::Test::Result::Skipped }
  85. end
  86. end
  87. class NoStepMatch # :nodoc:
  88. attr_reader :step_definition, :name
  89. def initialize(step, name)
  90. @step = step
  91. @name = name
  92. end
  93. def format_args(*_args)
  94. @name
  95. end
  96. def location
  97. raise "No location for #{@step}" unless @step.location
  98. @step.location
  99. end
  100. def file_colon_line
  101. location.to_s
  102. end
  103. def backtrace_line
  104. @step.backtrace_line
  105. end
  106. def text_length
  107. @step.text.length
  108. end
  109. def step_arguments
  110. []
  111. end
  112. def activate(test_step)
  113. # noop
  114. test_step
  115. end
  116. end
  117. class AmbiguousStepMatch
  118. def initialize(error)
  119. @error = error
  120. end
  121. def activate(test_step)
  122. test_step.with_action { raise @error }
  123. end
  124. end
  125. end

No Description

Contributors (1)