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_search_spec.rb 4.0 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # frozen_string_literal: true
  2. require 'cucumber/step_match_search'
  3. require 'cucumber/glue/dsl'
  4. require 'cucumber/glue/registry_and_more'
  5. require 'cucumber/configuration'
  6. module Cucumber
  7. describe StepMatchSearch do
  8. let(:search) { StepMatchSearch.new(registry.method(:step_matches), configuration) }
  9. let(:registry) { Glue::RegistryAndMore.new(runtime, configuration) }
  10. let(:runtime) do
  11. # TODO: break out step definitions collection from Glue::RegistryAndMore so we don't need this
  12. :unused
  13. end
  14. let(:configuration) { Configuration.new(options) }
  15. let(:options) { {} }
  16. let(:dsl) do
  17. # TODO: stop relying on implicit global state
  18. registry
  19. Object.new.extend(Glue::Dsl)
  20. end
  21. context 'caching' do
  22. it 'caches step match results' do
  23. dsl.Given(/it (.*) in (.*)/) { |what, month| }
  24. step_match = search.call('it snows in april').first
  25. expect(registry).not_to receive(:step_matches)
  26. second_step_match = search.call('it snows in april').first
  27. expect(step_match).to equal(second_step_match)
  28. end
  29. end
  30. describe 'resolving step definition matches' do
  31. it 'raises Ambiguous error with guess hint when multiple step definitions match' do
  32. expected_error = %{Ambiguous match of "Three blind mice":
  33. spec/cucumber/step_match_search_spec.rb:\\d+:in `/Three (.*) mice/'
  34. spec/cucumber/step_match_search_spec.rb:\\d+:in `/Three blind (.*)/'
  35. You can run again with --guess to make Cucumber be more smart about it
  36. }
  37. dsl.Given(/Three (.*) mice/) { |disability| }
  38. dsl.Given(/Three blind (.*)/) { |animal| }
  39. expect { search.call('Three blind mice').first }.to raise_error(Ambiguous, /#{expected_error}/)
  40. end
  41. describe 'when --guess is used' do
  42. let(:options) { { guess: true } }
  43. it 'does not show --guess hint' do
  44. expected_error = %{Ambiguous match of "Three cute mice":
  45. spec/cucumber/step_match_search_spec.rb:\\d+:in `/Three (.*) mice/'
  46. spec/cucumber/step_match_search_spec.rb:\\d+:in `/Three cute (.*)/'
  47. }
  48. dsl.Given(/Three (.*) mice/) { |disability| }
  49. dsl.Given(/Three cute (.*)/) { |animal| }
  50. expect { search.call('Three cute mice').first }.to raise_error(Ambiguous, /#{expected_error}/)
  51. end
  52. it 'does not raise Ambiguous error when multiple step definitions match' do
  53. dsl.Given(/Three (.*) mice/) { |disability| }
  54. dsl.Given(/Three (.*)/) { |animal| }
  55. expect { search.call('Three blind mice').first }.not_to raise_error
  56. end
  57. it 'picks right step definition when an equal number of capture groups' do
  58. right = dsl.Given(/Three (.*) mice/) { |disability| }
  59. _wrong = dsl.Given(/Three (.*)/) { |animal| }
  60. expect(search.call('Three blind mice').first.step_definition).to eq right
  61. end
  62. it 'picks right step definition when an unequal number of capture groups' do
  63. right = dsl.Given(/Three (.*) mice ran (.*)/) { |disability| }
  64. _wrong = dsl.Given(/Three (.*)/) { |animal| }
  65. expect(search.call('Three blind mice ran far').first.step_definition).to eq right
  66. end
  67. it 'picks most specific step definition when an unequal number of capture groups' do
  68. _general = dsl.Given(/Three (.*) mice ran (.*)/) { |disability| }
  69. _specific = dsl.Given(/Three blind mice ran far/) { ; }
  70. more_specific = dsl.Given(/^Three blind mice ran far$/) { ; }
  71. expect(search.call('Three blind mice ran far').first.step_definition).to eq more_specific
  72. end
  73. end
  74. # TODO: remove this - it's ... redundant
  75. # http://railsforum.com/viewtopic.php?pid=93881
  76. it "does not raise Redundant unless it's really redundant" do
  77. dsl.Given(/^(.*) (.*) user named '(.*)'$/) { |a, b, c| }
  78. dsl.Given(/^there is no (.*) user named '(.*)'$/) { |a, b| }
  79. end
  80. end
  81. end
  82. end

No Description

Contributors (1)