Skip to content
Browse files

Allow custom matchers to use #match built in

Addresses problem with dispatching the #match matcher from within a
custom matcher. (Wow... what a mouthful) See Issue #188.
  • Loading branch information...
1 parent b36e3ca commit 6442118aebf9ad6024d803eb0ecb86b08c5ce286 @stevenharman stevenharman committed Dec 6, 2012
Showing with 28 additions and 0 deletions.
  1. +24 −0 features/custom_matchers/define_matcher.feature
  2. +4 −0 lib/rspec/matchers.rb
View
24 features/custom_matchers/define_matcher.feature
@@ -338,3 +338,27 @@ Feature: define matcher
| 4 examples, 2 failures |
| expected 9 to be a multiple of 2 |
| 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"
View
4 lib/rspec/matchers.rb
@@ -491,10 +491,14 @@ def include(*expected)
#
# email.should match(/^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
# email.should match("@example.com")
+ # zipcode.should match_regex(/\A\d{5}(-\d{4})?\z/)
+ # zipcode.should match_regex("90210")
def match(expected)
BuiltIn::Match.new(expected)
end
+ alias_method :match_regex, :match
+
# 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 and messsage specified as a String, matches only if both match.

0 comments on commit 6442118

Please sign in to comment.
Something went wrong with that request. Please try again.