Skip to content

Commit

Permalink
Refactor the 'I should see the number' step
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
makmic committed Jun 14, 2019
1 parent f20885c commit ebce7b9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
22 changes: 16 additions & 6 deletions lib/spreewald/web_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,24 @@
#
# 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
is_negative = amount.start_with?('-')
absolute_amount = amount.clone.tap {|a| a.gsub!(/^-/, '') }
expect_minus = is_negative ? '-' : '(?:[^\\-]|^)'
word_boundary = '\b'
unit_or_boundary = if unit
nbsp = " "
"( |#{nbsp})(#{unit})"
else
word_boundary
end

regexp = Regexp.new(expect_minus + word_boundary + Regexp.quote(absolute_amount) + 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.
Expand Down
2 changes: 1 addition & 1 deletion tests/rails-3_capybara-1/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/rails-3_capybara-2/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
10 changes: 10 additions & 0 deletions tests/shared/app/views/static_pages/numbers.html.haml
Original file line number Diff line number Diff line change
@@ -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
Binary file modified tests/shared/db/test.sqlite3
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/shared/features/shared/web_steps.feature
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,18 @@ Feature: Web steps
And I should see a link labeled "Also matches via the title attribute"
But I should not see a link labeled "Nonexistent Link"
And I should not see a link labeled "First visible link" within ".unrelated-element"


Scenario: /^I should( not)? see the (?:number|amount) ([\-\d,\.]+)(?: (.*?))?$/
When I am on "/static_pages/numbers"
Then I should see the number 1
And I should see the number 2.3
And I should see the number 4,5
And I should see the amount -60,72 €
And I should see the amount 13 €
And I should see the amount -10,000.99 EUR within ".nested-number"
But I should not see the number 2,3
And I should not see the number 4.5
And I should not see the number 60,72
And I should not see the amount -60,7 €
And I should not see the amount -10,000.99 EUR within ".unrelated-element"

0 comments on commit ebce7b9

Please sign in to comment.