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.

registry_and_more_spec.rb 8.5 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. require 'cucumber/glue/registry_and_more'
  4. require 'support/fake_objects'
  5. module Cucumber
  6. module Glue
  7. describe StepDefinition do
  8. let(:user_interface) { double('user interface') }
  9. let(:registry) { support_code.registry }
  10. let(:support_code) do
  11. Cucumber::Runtime::SupportCode.new(user_interface)
  12. end
  13. let(:dsl) do
  14. registry
  15. Object.new.extend(Glue::Dsl)
  16. end
  17. # rubocop:disable Style/GlobalVars
  18. describe '#load_code_file' do
  19. before(:each) { $foo = nil }
  20. after(:each) do
  21. FileUtils.rm_rf('tmp.rb')
  22. FileUtils.rm_rf('docs.md')
  23. end
  24. def a_file_called(name)
  25. File.open(name, 'w') do |f|
  26. f.puts yield
  27. end
  28. end
  29. context 'by default' do
  30. after(:each) do
  31. FileUtils.rm_rf('tmp1.rb')
  32. end
  33. it 'does not re-load the file when called multiple times' do
  34. a_file_called('tmp1.rb') do
  35. '$foo = 1'
  36. end
  37. registry.load_code_file('tmp1.rb')
  38. expect($foo).to eq 1
  39. a_file_called('tmp1.rb') do
  40. '$foo = 2'
  41. end
  42. registry.load_code_file('tmp1.rb')
  43. expect($foo).to eq 1
  44. end
  45. it 'only loads ruby files' do
  46. a_file_called('docs.md') do
  47. '$foo = 1'
  48. end
  49. registry.load_code_file('docs.md')
  50. expect($foo).to be nil
  51. end
  52. end
  53. context 'With `use_legacy_autoloader` set to true' do
  54. before(:each) do
  55. allow(Cucumber).to receive(:use_legacy_autoloader).and_return(true)
  56. end
  57. after(:each) do
  58. FileUtils.rm_rf('tmp2.rb')
  59. end
  60. it 're-loads the file when called multiple times' do
  61. a_file_called('tmp2.rb') do
  62. '$foo = 1'
  63. end
  64. registry.load_code_file('tmp2.rb')
  65. expect($foo).to eq 1
  66. a_file_called('tmp2.rb') do
  67. '$foo = 2'
  68. end
  69. registry.load_code_file('tmp2.rb')
  70. expect($foo).to eq 2
  71. end
  72. it 'only loads ruby files' do
  73. a_file_called('docs.md') do
  74. '$foo = 1'
  75. end
  76. registry.load_code_file('docs.md')
  77. expect($foo).to be nil
  78. end
  79. end
  80. context 'With `use_legacy_autoloader` set to false' do
  81. before(:each) do
  82. allow(Cucumber).to receive(:use_legacy_autoloader).and_return(false)
  83. end
  84. after(:each) do
  85. FileUtils.rm_rf('tmp3.rb')
  86. end
  87. it 'does not re-load the file when called multiple times' do
  88. a_file_called('tmp3.rb') do
  89. '$foo = 1'
  90. end
  91. registry.load_code_file('tmp3.rb')
  92. expect($foo).to eq 1
  93. a_file_called('tmp3.rb') do
  94. '$foo = 2'
  95. end
  96. registry.load_code_file('tmp3.rb')
  97. expect($foo).to eq 1
  98. end
  99. it 'only loads ruby files' do
  100. a_file_called('docs.md') do
  101. '$foo = 1'
  102. end
  103. registry.load_code_file('docs.md')
  104. expect($foo).to be nil
  105. end
  106. end
  107. # rubocop:enable Style/GlobalVars
  108. end
  109. describe 'Handling the World' do
  110. it 'raises an error if the world is nil' do
  111. dsl.World {}
  112. begin
  113. registry.begin_scenario(nil)
  114. raise 'Should fail'
  115. rescue Glue::NilWorld => e
  116. expect(e.message).to eq 'World procs should never return nil'
  117. expect(e.backtrace.length).to eq 1
  118. expect(e.backtrace[0]).to match(/spec\/cucumber\/glue\/registry_and_more_spec\.rb:\d+:in `World'/)
  119. end
  120. end
  121. it 'implicitlys extend world with modules' do
  122. dsl.World(FakeObjects::ModuleOne, FakeObjects::ModuleTwo)
  123. registry.begin_scenario(double('scenario').as_null_object)
  124. class << registry.current_world
  125. extend RSpec::Matchers
  126. expect(included_modules.inspect).to match(/ModuleOne/) # Workaround for RSpec/Ruby 1.9 issue with namespaces
  127. expect(included_modules.inspect).to match(/ModuleTwo/)
  128. end
  129. expect(registry.current_world.class).to eq Object
  130. end
  131. it 'raises error when we try to register more than one World proc' do
  132. expected_error = %(You can only pass a proc to #World once, but it's happening
  133. in 2 places:
  134. spec/cucumber/glue/registry_and_more_spec.rb:\\d+:in `World'
  135. spec/cucumber/glue/registry_and_more_spec.rb:\\d+:in `World'
  136. Use Ruby modules instead to extend your worlds. See the Cucumber::Glue::Dsl#World RDoc
  137. or http://wiki.github.com/cucumber/cucumber/a-whole-new-world.
  138. )
  139. dsl.World { {} }
  140. expect { dsl.World { [] } }.to raise_error(Glue::MultipleWorld, /#{expected_error}/)
  141. end
  142. end
  143. describe 'Handling namespaced World' do
  144. it 'extends the world with namespaces' do
  145. dsl.World(FakeObjects::ModuleOne, module_two: FakeObjects::ModuleTwo, module_three: FakeObjects::ModuleThree)
  146. registry.begin_scenario(double('scenario').as_null_object)
  147. class << registry.current_world
  148. extend RSpec::Matchers
  149. expect(included_modules.inspect).to match(/ModuleOne/)
  150. end
  151. expect(registry.current_world.class).to eq Object
  152. expect(registry.current_world).to respond_to(:method_one)
  153. expect(registry.current_world.module_two.class).to eq Object
  154. expect(registry.current_world.module_two).to respond_to(:method_two)
  155. expect(registry.current_world.module_three.class).to eq Object
  156. expect(registry.current_world.module_three).to respond_to(:method_three)
  157. end
  158. it 'allows to inspect the included modules' do
  159. dsl.World(FakeObjects::ModuleOne, module_two: FakeObjects::ModuleTwo, module_three: FakeObjects::ModuleThree)
  160. registry.begin_scenario(double('scenario').as_null_object)
  161. class << registry.current_world
  162. extend RSpec::Matchers
  163. end
  164. expect(registry.current_world.inspect).to match(/ModuleOne/)
  165. expect(registry.current_world.inspect).to include('ModuleTwo (as module_two)')
  166. expect(registry.current_world.inspect).to include('ModuleThree (as module_three)')
  167. end
  168. it 'merges methods when assigning different modules to the same namespace' do
  169. dsl.World(namespace: FakeObjects::ModuleOne)
  170. dsl.World(namespace: FakeObjects::ModuleTwo)
  171. registry.begin_scenario(double('scenario').as_null_object)
  172. class << registry.current_world
  173. extend RSpec::Matchers
  174. end
  175. expect(registry.current_world.namespace).to respond_to(:method_one)
  176. expect(registry.current_world.namespace).to respond_to(:method_two)
  177. end
  178. it 'resolves conflicts when assigning different modules to the same namespace' do
  179. dsl.World(namespace: FakeObjects::ModuleOne)
  180. dsl.World(namespace: FakeObjects::ModuleMinusOne)
  181. registry.begin_scenario(double('scenario').as_null_object)
  182. class << registry.current_world
  183. extend RSpec::Matchers
  184. end
  185. expect(registry.current_world.namespace).to respond_to(:method_one)
  186. expect(registry.current_world.namespace.method_one).to eql(-1)
  187. end
  188. end
  189. describe 'hooks' do
  190. it 'finds before hooks' do
  191. fish = dsl.Before('@fish') {}
  192. meat = dsl.Before('@meat') {}
  193. scenario = double('Scenario')
  194. expect(scenario).to receive(:accept_hook?).with(fish) { true }
  195. expect(scenario).to receive(:accept_hook?).with(meat) { false }
  196. expect(registry.hooks_for(:before, scenario)).to eq [fish]
  197. end
  198. it 'finds around hooks' do
  199. a = dsl.Around do |scenario, block|
  200. end
  201. b = dsl.Around('@tag') do |scenario, block|
  202. end
  203. scenario = double('Scenario')
  204. expect(scenario).to receive(:accept_hook?).with(a) { true }
  205. expect(scenario).to receive(:accept_hook?).with(b) { false }
  206. expect(registry.hooks_for(:around, scenario)).to eq [a]
  207. end
  208. end
  209. end
  210. end
  211. end

No Description

Contributors (1)