From 26b454222ed47b2acc72dcfa94f6ee10eaa824cc Mon Sep 17 00:00:00 2001 From: Michael Leimstaedtner Date: Wed, 12 Jun 2019 17:22:36 +0200 Subject: [PATCH] Refactor the 'I should see the number' step * It can now be composed with the 'within' step * It can now truly match negative numbers * It dropped the hidden dependency on the HTMLEntities gem * It is now tested --- lib/spreewald/web_steps.rb | 21 +++++++++++++----- tests/rails-3_capybara-1/Gemfile.lock | 2 +- tests/rails-3_capybara-2/Gemfile.lock | 2 +- .../app/views/static_pages/numbers.html.haml | 10 +++++++++ tests/shared/db/test.sqlite3 | Bin 12288 -> 3072 bytes .../shared/features/shared/web_steps.feature | 15 +++++++++++++ 6 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 tests/shared/app/views/static_pages/numbers.html.haml diff --git a/lib/spreewald/web_steps.rb b/lib/spreewald/web_steps.rb index 58a86ab..c90ab31 100644 --- a/lib/spreewald/web_steps.rb +++ b/lib/spreewald/web_steps.rb @@ -217,14 +217,23 @@ # # See [here](https://makandracards.com/makandra/1225-test-that-a-number-or-money-amount-is-shown-with-cucumber) for details Then /^I should( not)? see the (?:number|amount) ([\-\d,\.]+)(?: (.*?))?$/ do |negate, amount, unit| - no_minus = amount.starts_with?('-') ? '' : '[^\\-]' - nbsp = " " - regexp = Regexp.new(no_minus + "\\b" + Regexp.quote(amount) + (unit ? "( |#{nbsp}| )(#{unit}|#{Regexp.quote(HTMLEntities.new.encode(unit, :named))})" :"\\b")) - expectation = negate ? :not_to : :to + expect_to_match = negate ? :not_to : :to + amount_included_minus = amount.gsub!(/^-/, '') + expect_minus = amount_included_minus ? '-' : '(?:[^\\-]|^)' + word_boundary = '\b' + expect_unit_or_boundary = if unit + nbsp = " " + "( |#{nbsp})(#{unit})" + else + word_boundary + end + + regexp = Regexp.new(expect_minus + word_boundary + Regexp.quote(amount) + expect_unit_or_boundary) + patiently do - expect(page.body).send(expectation, match(regexp)) + expect(page.text).send(expect_to_match, match(regexp)) end -end.overridable +end.overridable(priority: -5) # priority lower than within # Like `Then I should see`, but with single instead of double quotes. In case # the expected string contains quotes as well. diff --git a/tests/rails-3_capybara-1/Gemfile.lock b/tests/rails-3_capybara-1/Gemfile.lock index 43e91ba..d26bd91 100644 --- a/tests/rails-3_capybara-1/Gemfile.lock +++ b/tests/rails-3_capybara-1/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../.. specs: - spreewald (2.1.3) + spreewald (2.2.0) cucumber cucumber_priority (>= 0.3.0) rspec (>= 2.13.0) diff --git a/tests/rails-3_capybara-2/Gemfile.lock b/tests/rails-3_capybara-2/Gemfile.lock index 74e7c20..137f18b 100644 --- a/tests/rails-3_capybara-2/Gemfile.lock +++ b/tests/rails-3_capybara-2/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../.. specs: - spreewald (2.1.3) + spreewald (2.2.0) cucumber cucumber_priority (>= 0.3.0) rspec (>= 2.13.0) diff --git a/tests/shared/app/views/static_pages/numbers.html.haml b/tests/shared/app/views/static_pages/numbers.html.haml new file mode 100644 index 0000000..2dade13 --- /dev/null +++ b/tests/shared/app/views/static_pages/numbers.html.haml @@ -0,0 +1,10 @@ +%span 1 +%ul + %li 2.3 + %li 4,5 + %li -60,72 € + %li 13 € + %li + .nested-number -10,000.99 EUR + +.unrelated-element diff --git a/tests/shared/db/test.sqlite3 b/tests/shared/db/test.sqlite3 index f668f29f3604c94db995a6695cb6ee7d8e943455..bdc075949d5426afaf0d6689d2b3ad055d017c30 100644 GIT binary patch delta 79 zcmZojXpj&L4)n<^NmWS8FUn0UQ7~p;nJ5^*ulsQaF9QPu6Eg<`^CV`DjfFFqCUfxr bWo7=&!2Ek-;Ya4lzva0QJXRKFeog}bvGy0~ delta 133 zcmZpWXh;wY4)n<^NmWS8FUn0UQ7~o@m?#*)uP3vCmw|zSiJyake-b~(W