1module Spec
2 module Matchers
3
4 class Match #:nodoc:
5 def initialize(expected)
6 @expected = expected
7 end
8
9 def matches?(actual)
10 @actual = actual
11 return true if actual =~ @expected
12 return false
13 end
14
15 def failure_message
16 return "expected #{@actual.inspect} to match #{@expected.inspect}", @expected, @actual
17 end
18
19 def negative_failure_message
20 return "expected #{@actual.inspect} not to match #{@expected.inspect}", @expected, @actual
21 end
22
23 def description
24 "match #{@expected.inspect}"
25 end
26 end
27
28 # :call-seq:
29 # should match(regexp)
30 # should_not match(regexp)
31 #
32 # Given a Regexp, passes if actual =~ regexp
33 #
34 # == Examples
35 #
36 # email.should match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
37 def match(regexp)
38 Matchers::Match.new(regexp)
39 end
40 end
41end