Skip to content
Newer
Older
100644 104 lines (97 sloc) 3.57 KB
8c27e78 got rid of the test/unit output in TextMate
dchelimsky authored
1 module Spec
2 module Matchers
3 class SimpleMatcher
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
4 attr_writer :failure_message, :negative_failure_message, :description
7e1a719 @dchelimsky simple_matcher refactoring - one more time - much simpler
authored
5
8c27e78 got rid of the test/unit output in TextMate
dchelimsky authored
6 def initialize(description, &match_block)
7e1a719 @dchelimsky simple_matcher refactoring - one more time - much simpler
authored
7 @description = description
8c27e78 got rid of the test/unit output in TextMate
dchelimsky authored
8 @match_block = match_block
9 end
10
11 def matches?(actual)
12 @actual = actual
63d2edc @dchelimsky should and should_not now return true on success
authored
13 case @match_block.arity
14 when 2
15 @match_block.call(@actual, self)
16 else
8ec1fa1 @dchelimsky simple_matcher refactoring
authored
17 @match_block.call(@actual)
63d2edc @dchelimsky should and should_not now return true on success
authored
18 end
8c27e78 got rid of the test/unit output in TextMate
dchelimsky authored
19 end
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
20
21 def description
22 @description || explanation
23 end
8c27e78 got rid of the test/unit output in TextMate
dchelimsky authored
24
b18a896 @dchelimsky more simple matching refactoring
authored
25 def failure_message
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
26 @failure_message || (@description.nil? ? explanation : %[expected #{@description.inspect} but got #{@actual.inspect}])
8c27e78 got rid of the test/unit output in TextMate
dchelimsky authored
27 end
7e1a719 @dchelimsky simple_matcher refactoring - one more time - much simpler
authored
28
b18a896 @dchelimsky more simple matching refactoring
authored
29 def negative_failure_message
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
30 @negative_failure_message || (@description.nil? ? explanation : %[expected not to get #{@description.inspect}, but got #{@actual.inspect}])
fa9952c @dchelimsky added messenger to simple_matcher for custom failure messages
authored
31 end
b18a896 @dchelimsky more simple matching refactoring
authored
32
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
33 def explanation
34 "No description provided. See RDoc for simple_matcher()"
35 end
36 end
37
38 # +simple_matcher()+ makes it easy for you to create your own custom
39 # matchers in just a few lines of code when you don't need all the power
40 # of a completely custom matcher object.
41 #
42 # The description argument will appear as part of any failure message.
43 #
44 # The match block can have arity of 1 or 2. The first argument will be the
45 # given value. The second, if the block accepts it will be the matcher
46 # itself, giving you access to set custom failure messages in favor of the
47 # defaults.
48 #
44d52ab @dchelimsky words (simple matcher - cont'd)
authored
49 # If you set custom messages, you don't have to pass the description
50 # argument to the simple_matcher method, but you must then provide any
51 # messages that might get invoked directly to the matcher (see Example
52 # with custom messages, below)
53 #
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
54 # The match_block should return a boolean: true indicates a match, which
55 # will pass if you use +should+ and fail if you use +should_not+. false
63d2edc @dchelimsky should and should_not now return true on success
authored
56 # (or nil) indicates no match, which will do the reverse: fail if you use
57 # +should+ and pass if you use +should_not+.
58 #
59 # An error in the +match_block+ will bubble up, resulting in a failure.
60 # This includes ExpectationNotMet errors (raised by +should+ and
61 # +should_not+), so you can wrap other expectations in a +simple_matcher+
62 # and they'll "do the right thing."
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
63 #
64 # == Example with default messages
65 #
66 # def be_even
67 # simple_matcher("an even number") { |given| given % 2 == 0 }
68 # end
63d2edc @dchelimsky should and should_not now return true on success
authored
69 #
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
70 # describe 2 do
71 # it "should be even" do
72 # 2.should be_even
73 # end
74 # end
75 #
76 # Given an odd number, this example would produce an error message stating
77 # 'expected "an even number"", got 3'
78 #
79 # == Example with custom messages
80 #
81 # def rhyme_with(expected)
44d52ab @dchelimsky words (simple matcher - cont'd)
authored
82 # simple_matcher do |given, matcher|
83 # matcher.description = "string rhymer"
84 # matcher.failure_message = "expected #{given.inspect} to rhyme with #{expected.inspect}"
85 # matcher.negative_failure_message = "expected #{given.inspect} not to rhyme with #{expected.inspect}"
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
86 # actual.rhymes_with? expected
87 # end
88 # end
89 #
90 # describe "pecan" do
91 # it "should rhyme with 'be gone'" do
92 # nut = "pecan"
63d2edc @dchelimsky should and should_not now return true on success
authored
93 # nut.extend Rhymer
94 # nut.should rhyme_with("be gone")
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
95 # end
96 # end
97 #
44d52ab @dchelimsky words (simple matcher - cont'd)
authored
98 # The resulting failure message would be 'expected "pecan" to rhyme with
99 # "be gone"'. (Grandma Rita would be proud!)
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
100 def simple_matcher(description=nil, &match_block)
101 SimpleMatcher.new(description, &match_block)
b18a896 @dchelimsky more simple matching refactoring
authored
102 end
8c27e78 got rid of the test/unit output in TextMate
dchelimsky authored
103 end
104 end
Something went wrong with that request. Please try again.