Skip to content

Commit

Permalink
Allow custom matchers to use #match built in
Browse files Browse the repository at this point in the history
Addresses problem with dispatching the #match matcher from within a
custom matcher. (Wow... what a mouthful) See Issue #188.
  • Loading branch information
stevenharman committed Dec 6, 2012
1 parent b36e3ca commit 6442118
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
24 changes: 24 additions & 0 deletions features/custom_matchers/define_matcher.feature
Expand Up @@ -338,3 +338,27 @@ Feature: define matcher
| 4 examples, 2 failures | | 4 examples, 2 failures |
| expected 9 to be a multiple of 2 | | expected 9 to be a multiple of 2 |
| expected 9 not to be a multiple of 3 | | expected 9 not to be a multiple of 3 |

Scenario: that match against a regular expression
Given a file named "regular_expression_matcher_spec.rb" with:
"""ruby
RSpec::Matchers.define :be_valid_us_zipcode do
match do |actual|
expect(actual).to match_regex(/\A\d{5}(-\d{4})?\z/)
end
end
describe "30316" do
it { should be_valid_us_zipcode }
end
describe "30316-0001" do
it { should be_valid_us_zipcode }
end
describe "1000-61303" do
it { should_not be_valid_us_zipcode }
end
"""
When I run `rspec regular_expression_matcher_spec.rb`
Then the stdout should contain "3 examples, 0 failures"
4 changes: 4 additions & 0 deletions lib/rspec/matchers.rb
Expand Up @@ -491,10 +491,14 @@ def include(*expected)
# #
# email.should match(/^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i) # email.should match(/^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
# email.should match("@example.com") # email.should match("@example.com")
# zipcode.should match_regex(/\A\d{5}(-\d{4})?\z/)
# zipcode.should match_regex("90210")
def match(expected) def match(expected)
BuiltIn::Match.new(expected) BuiltIn::Match.new(expected)
end end


alias_method :match_regex, :match

# With no args, matches if any error is raised. # With no args, matches if any error is raised.
# With a named error, matches only if that specific error is raised. # With a named error, matches only if that specific error is raised.
# With a named error and messsage specified as a String, matches only if both match. # With a named error and messsage specified as a String, matches only if both match.
Expand Down

0 comments on commit 6442118

Please sign in to comment.