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_definition_spec.rb 7.1 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. # frozen_string_literal: true
  2. # rubocop:disable Style/ClassVars
  3. require 'spec_helper'
  4. require 'cucumber/glue/registry_and_more'
  5. require 'support/fake_objects'
  6. module Cucumber
  7. module Glue
  8. describe StepDefinition do
  9. let(:id) { double }
  10. let(:user_interface) { double('user interface') }
  11. let(:support_code) { Cucumber::Runtime::SupportCode.new(user_interface) }
  12. let(:registry) { support_code.registry }
  13. let(:test_case) { double('scenario', language: 'en').as_null_object }
  14. let(:dsl) do
  15. registry
  16. Object.new.extend(Cucumber::Glue::Dsl)
  17. end
  18. before do
  19. registry.begin_scenario(test_case)
  20. @@inside = nil
  21. end
  22. def run_step(text)
  23. step_match(text).invoke(MultilineArgument::None.new)
  24. end
  25. def step_match(text)
  26. StepMatchSearch.new(registry.method(:step_matches), Configuration.default).call(text).first
  27. end
  28. it 'allows calling of other steps' do
  29. dsl.Given(/Outside/) do
  30. step 'Inside'
  31. end
  32. dsl.Given(/Inside/) do
  33. @@inside = true
  34. end
  35. run_step 'Outside'
  36. expect(@@inside).to be true
  37. end
  38. it 'allows calling of other steps with inline arg' do
  39. dsl.Given(/Outside/) do
  40. step 'Inside', table([['inside']])
  41. end
  42. dsl.Given(/Inside/) do |t|
  43. @@inside = t.raw[0][0]
  44. end
  45. run_step 'Outside'
  46. expect(@@inside).to eq 'inside'
  47. end
  48. context 'mapping to world methods' do
  49. it 'calls a method on the world when specified with a symbol' do
  50. expect(registry.current_world).to receive(:with_symbol)
  51. dsl.Given(/With symbol/, :with_symbol)
  52. run_step 'With symbol'
  53. end
  54. it 'calls a method on a specified object' do
  55. target = double('target')
  56. allow(registry.current_world).to receive(:target) { target }
  57. dsl.Given(/With symbol on block/, :with_symbol, on: -> { target })
  58. expect(target).to receive(:with_symbol)
  59. run_step 'With symbol on block'
  60. end
  61. it 'calls a method on a specified world attribute' do
  62. target = double('target')
  63. allow(registry.current_world).to receive(:target) { target }
  64. dsl.Given(/With symbol on symbol/, :with_symbol, on: :target)
  65. expect(target).to receive(:with_symbol)
  66. run_step 'With symbol on symbol'
  67. end
  68. it 'has the correct location' do
  69. dsl.Given(/With symbol/, :with_symbol)
  70. expect(step_match('With symbol').file_colon_line).to eq "spec/cucumber/glue/step_definition_spec.rb:#{__LINE__ - 1}"
  71. end
  72. end
  73. it 'raises UndefinedDynamicStep when inside step is not defined' do
  74. dsl.Given(/Outside/) do
  75. step 'Inside'
  76. end
  77. expect { run_step 'Outside' }.to raise_error(Cucumber::UndefinedDynamicStep)
  78. end
  79. it 'raises UndefinedDynamicStep when an undefined step is parsed dynamically' do
  80. dsl.Given(/Outside/) do
  81. steps %(
  82. Given Inside
  83. )
  84. end
  85. expect { run_step 'Outside' }.to raise_error(Cucumber::UndefinedDynamicStep)
  86. end
  87. it 'raises UndefinedDynamicStep when an undefined step with doc string is parsed dynamically' do
  88. dsl.Given(/Outside/) do
  89. steps %(
  90. Given Inside
  91. """
  92. abc
  93. """
  94. )
  95. end
  96. expect { run_step 'Outside' }.to raise_error(Cucumber::UndefinedDynamicStep)
  97. end
  98. it 'raises UndefinedDynamicStep when an undefined step with data table is parsed dynamically' do
  99. dsl.Given(/Outside/) do
  100. steps %(
  101. Given Inside
  102. | a |
  103. | 1 |
  104. )
  105. end
  106. expect { run_step 'Outside' }.to raise_error(Cucumber::UndefinedDynamicStep)
  107. end
  108. it 'allows forced pending' do
  109. dsl.Given(/Outside/) do
  110. pending('Do me!')
  111. end
  112. expect { run_step 'Outside' }.to raise_error(Cucumber::Pending, 'Do me!')
  113. end
  114. it 'raises ArityMismatchError when the number of capture groups differs from the number of step arguments' do
  115. dsl.Given(/No group: \w+/) do |arg|
  116. end
  117. expect { run_step 'No group: arg' }.to raise_error(Cucumber::Glue::ArityMismatchError)
  118. end
  119. it 'does not modify the step_match arg when arg is modified in a step' do
  120. dsl.Given(/My car is (.*)/) do |colour|
  121. colour << 'xxx'
  122. end
  123. step_name = 'My car is white'
  124. step_args = step_match(step_name).args
  125. expect { run_step step_name }.not_to change { step_args.first } # rubocop:disable Lint/AmbiguousBlockAssociation
  126. end
  127. context 'with ParameterType' do
  128. before(:each) do
  129. @actor = nil
  130. dsl.ParameterType(
  131. name: 'actor',
  132. regexp: /[A-Z]{1}[a-z]+/,
  133. transformer: ->(name) { @actor = FakeObjects::Actor.new(name) }
  134. )
  135. end
  136. it 'uses the instance created by the ParameterType transformer proc' do
  137. dsl.Given 'capture this: {actor}' do |arg|
  138. expect(arg.name).to eq 'Anjie'
  139. expect(arg).to be @actor
  140. end
  141. run_step 'capture this: Anjie'
  142. end
  143. it 'does not modify the step_match arg when arg is modified in a step' do
  144. dsl.Given 'capture this: {actor}' do |actor|
  145. actor.name = 'Dave'
  146. end
  147. run_step 'capture this: Anjie'
  148. step_args = step_match('capture this: Anjie').args
  149. expect(step_args[0].name).to_not eq 'Dave'
  150. expect(step_args[0].name).to eq 'Anjie'
  151. end
  152. end
  153. context 'allows log' do
  154. it 'calls "attach" with the correct media type' do
  155. expect(user_interface).to receive(:attach).with('wasup', 'text/x.cucumber.log+plain')
  156. dsl.Given(/Loud/) do
  157. log 'wasup'
  158. end
  159. run_step 'Loud'
  160. end
  161. it 'calls `to_s` if the message is not a String' do
  162. expect(user_interface).to receive(:attach).with('["Not", 1, "string"]', 'text/x.cucumber.log+plain')
  163. dsl.Given(/Loud/) do
  164. log ['Not', 1, 'string']
  165. end
  166. run_step 'Loud'
  167. end
  168. end
  169. it 'recognizes $arg style captures' do
  170. arg_value = 'up'
  171. dsl.Given 'capture this: {word}' do |arg|
  172. expect(arg).to eq arg_value
  173. end
  174. run_step 'capture this: up'
  175. end
  176. it 'has a JSON representation of the signature' do
  177. expect(StepDefinition.new(
  178. id,
  179. registry,
  180. /I CAN HAZ (\d+) CUKES/i,
  181. -> {},
  182. {}
  183. ).to_hash).to eq(
  184. source: {
  185. type: 'regular expression',
  186. expression: 'I CAN HAZ (\\d+) CUKES'
  187. },
  188. regexp: {
  189. source: 'I CAN HAZ (\\d+) CUKES', flags: 'i'
  190. }
  191. )
  192. end
  193. end
  194. end
  195. end
  196. # rubocop:enable Style/ClassVars

No Description

Contributors (1)