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.

snippet_spec.rb 6.5 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. require 'cucumber/cucumber_expressions/parameter_type_registry'
  4. require 'cucumber/cucumber_expressions/parameter_type'
  5. require 'cucumber/cucumber_expressions/cucumber_expression_generator'
  6. require 'cucumber/glue/snippet'
  7. require 'cucumber/formatter/console'
  8. module Cucumber
  9. module Glue
  10. describe Snippet do
  11. include Cucumber::Formatter::Console
  12. let(:code_keyword) { 'Given' }
  13. before do
  14. @step_text = 'we have a missing step'
  15. @multiline_argument = Core::Test::EmptyMultilineArgument.new
  16. @registry = CucumberExpressions::ParameterTypeRegistry.new
  17. @cucumber_expression_generator = CucumberExpressions::CucumberExpressionGenerator.new(@registry)
  18. end
  19. let(:snippet) do
  20. snippet_class.new(@cucumber_expression_generator, code_keyword, @step_text, @multiline_argument)
  21. end
  22. def unindented(snippet)
  23. indent(snippet.split("\n")[1..-2].join("\n"), -10)
  24. end
  25. describe Snippet::Regexp do
  26. let(:snippet_class) { Snippet::Regexp }
  27. let(:snippet_text) { snippet.to_s }
  28. it 'wraps snippet patterns in parentheses' do
  29. @step_text = 'A "string" with 4 spaces'
  30. expect(snippet_text).to eq unindented(%{
  31. Given(/^A "([^"]*)" with (\\d+) spaces$/) do |arg1, arg2|
  32. pending # Write code here that turns the phrase above into concrete actions
  33. end
  34. })
  35. end
  36. it 'recognises numbers in name and make according regexp' do
  37. @step_text = 'Cloud 9 yeah'
  38. expect(snippet_text).to eq unindented(%{
  39. Given(/^Cloud (\\d+) yeah$/) do |arg1|
  40. pending # Write code here that turns the phrase above into concrete actions
  41. end
  42. })
  43. end
  44. it 'recognises a mix of ints, strings and why not a table too' do
  45. @step_text = 'I have 9 "awesome" cukes in 37 "boxes"'
  46. @multiline_argument = Core::Test::DataTable.new([[]])
  47. expect(snippet_text).to eq unindented(%{
  48. Given(/^I have (\\d+) "([^"]*)" cukes in (\\d+) "([^"]*)"$/) do |arg1, arg2, arg3, arg4, table|
  49. # table is a Cucumber::MultilineArgument::DataTable
  50. pending # Write code here that turns the phrase above into concrete actions
  51. end
  52. })
  53. end
  54. it 'recognises quotes in name and make according regexp' do
  55. @step_text = 'A "first" arg'
  56. expect(snippet_text).to eq unindented(%{
  57. Given(/^A "([^"]*)" arg$/) do |arg1|
  58. pending # Write code here that turns the phrase above into concrete actions
  59. end
  60. })
  61. end
  62. it 'recognises several quoted words in name and make according regexp and args' do
  63. @step_text = 'A "first" and "second" arg'
  64. expect(snippet_text).to eq unindented(%{
  65. Given(/^A "([^"]*)" and "([^"]*)" arg$/) do |arg1, arg2|
  66. pending # Write code here that turns the phrase above into concrete actions
  67. end
  68. })
  69. end
  70. it 'does not use quote group when there are no quotes' do
  71. @step_text = 'A first arg'
  72. expect(snippet_text).to eq unindented(%{
  73. Given(/^A first arg$/) do
  74. pending # Write code here that turns the phrase above into concrete actions
  75. end
  76. })
  77. end
  78. it 'is helpful with tables' do
  79. @step_text = 'A "first" arg'
  80. @multiline_argument = Core::Test::DataTable.new([[]])
  81. expect(snippet_text).to eq unindented(%{
  82. Given(/^A "([^"]*)" arg$/) do |arg1, table|
  83. # table is a Cucumber::MultilineArgument::DataTable
  84. pending # Write code here that turns the phrase above into concrete actions
  85. end
  86. })
  87. end
  88. it 'is helpful with doc string' do
  89. @step_text = 'A "first" arg'
  90. @multiline_argument = MultilineArgument.from('', Core::Test::Location.new(''))
  91. expect(snippet_text).to eq unindented(%{
  92. Given(/^A "([^"]*)" arg$/) do |arg1, doc_string|
  93. pending # Write code here that turns the phrase above into concrete actions
  94. end
  95. })
  96. end
  97. end
  98. describe Snippet::Classic do
  99. let(:snippet_class) { Snippet::Classic }
  100. it 'renders snippet as unwrapped regular expression' do
  101. expect(snippet.to_s).to eq unindented(%(
  102. Given /^we have a missing step$/ do
  103. pending # Write code here that turns the phrase above into concrete actions
  104. end
  105. ))
  106. end
  107. end
  108. describe Snippet::Percent do
  109. let(:snippet_class) { Snippet::Percent }
  110. it 'renders snippet as percent-style regular expression' do
  111. expect(snippet.to_s).to eq unindented(%(
  112. Given %r{^we have a missing step$} do
  113. pending # Write code here that turns the phrase above into concrete actions
  114. end
  115. ))
  116. end
  117. end
  118. describe Snippet::CucumberExpression do
  119. let(:snippet_class) { Snippet::CucumberExpression }
  120. it 'renders snippet as cucumber expression' do
  121. @step_text = 'I have 2.3 cukes in my belly'
  122. @registry.define_parameter_type(CucumberExpressions::ParameterType.new(
  123. 'veg',
  124. /(cuke|banana)s?/,
  125. Object,
  126. ->(s) { s },
  127. true,
  128. false
  129. ))
  130. @registry.define_parameter_type(CucumberExpressions::ParameterType.new(
  131. 'cucumis',
  132. /(bella|cuke)s?/,
  133. Object,
  134. ->(s) { s },
  135. true,
  136. false
  137. ))
  138. expect(snippet.to_s).to eq unindented(%{
  139. Given('I have {float} {cucumis} in my belly') do |float, cucumis|
  140. # Given('I have {float} {veg} in my belly') do |float, veg|
  141. pending # Write code here that turns the phrase above into concrete actions
  142. end
  143. })
  144. end
  145. end
  146. end
  147. end
  148. end

No Description

Contributors (1)