Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 132 lines (125 sloc) 4.841 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
8af8ff2 @dchelimsky Getting the words right - use 'given' in matchers.
authored
11 def matches?(given)
12 @given = given
63d2edc @dchelimsky should and should_not now return true on success
authored
13 case @match_block.arity
14 when 2
8af8ff2 @dchelimsky Getting the words right - use 'given' in matchers.
authored
15 @match_block.call(@given, self)
63d2edc @dchelimsky should and should_not now return true on success
authored
16 else
8af8ff2 @dchelimsky Getting the words right - use 'given' in matchers.
authored
17 @match_block.call(@given)
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
8af8ff2 @dchelimsky Getting the words right - use 'given' in matchers.
authored
26 @failure_message || (@description.nil? ? explanation : %[expected #{@description.inspect} but got #{@given.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
8af8ff2 @dchelimsky Getting the words right - use 'given' in matchers.
authored
30 @negative_failure_message || (@description.nil? ? explanation : %[expected not to get #{@description.inspect}, but got #{@given.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
0ab525e @dchelimsky simple_matcher rdoc
authored
38 # simple_matcher makes it easy for you to create your own custom matchers
39 # in just a few lines of code when you don't need all the power of a
40 # completely custom matcher object.
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
41 #
0ab525e @dchelimsky simple_matcher rdoc
authored
42 # The <tt>description</tt> argument will appear as part of any failure
43 # message, and is also the source for auto-generated descriptions.
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
44 #
0ab525e @dchelimsky simple_matcher rdoc
authored
45 # The <tt>match_block</tt> can have an arity of 1 or 2. The first block
46 # argument will be the given value. The second, if the block accepts it
47 # will be the matcher itself, giving you access to set custom failure
48 # messages in favor of the defaults.
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
49 #
0ab525e @dchelimsky simple_matcher rdoc
authored
50 # The <tt>match_block</tt> should return a boolean: <tt>true</tt>
51 # indicates a match, which will pass if you use <tt>should</tt> and fail
52 # if you use <tt>should_not</tt>. false (or nil) indicates no match,
53 # which will do the reverse: fail if you use <tt>should</tt> and pass if
54 # you use <tt>should_not</tt>.
44d52ab @dchelimsky words (simple matcher - cont'd)
authored
55 #
0ab525e @dchelimsky simple_matcher rdoc
authored
56 # An error in the <tt>match_block</tt> will bubble up, resulting in a
57 # failure.
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
58 #
59 # == Example with default messages
60 #
0ab525e @dchelimsky simple_matcher rdoc
authored
61 # def be_even
62 # simple_matcher("an even number") { |given| given % 2 == 0 }
63 # end
64 #
65 # describe 2 do
66 # it "should be even" do
67 # 2.should be_even
68 # end
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
69 # end
70 #
0ab525e @dchelimsky simple_matcher rdoc
authored
71 # Given an odd number, this example would produce an error message stating:
72 # expected "an even number", got 3.
73 #
74 # Unfortunately, if you're a fan of auto-generated descriptions, this will
75 # produce "should an even number." Not the most desirable result. You can
76 # control that using custom messages:
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
77 #
78 # == Example with custom messages
79 #
0ab525e @dchelimsky simple_matcher rdoc
authored
80 # def rhyme_with(expected)
81 # simple_matcher("rhyme with #{expected.inspect}") do |given, matcher|
82 # matcher.failure_message = "expected #{given.inspect} to rhyme with #{expected.inspect}"
83 # matcher.negative_failure_message = "expected #{given.inspect} not to rhyme with #{expected.inspect}"
8af8ff2 @dchelimsky Getting the words right - use 'given' in matchers.
authored
84 # given.rhymes_with? expected
0ab525e @dchelimsky simple_matcher rdoc
authored
85 # end
86 # end
87 #
88 # # OR
89 #
90 # def rhyme_with(expected)
91 # simple_matcher do |given, matcher|
92 # matcher.description = "rhyme with #{expected.inspect}"
93 # matcher.failure_message = "expected #{given.inspect} to rhyme with #{expected.inspect}"
94 # matcher.negative_failure_message = "expected #{given.inspect} not to rhyme with #{expected.inspect}"
8af8ff2 @dchelimsky Getting the words right - use 'given' in matchers.
authored
95 # given.rhymes_with? expected
0ab525e @dchelimsky simple_matcher rdoc
authored
96 # end
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
97 # end
98 #
0ab525e @dchelimsky simple_matcher rdoc
authored
99 # describe "pecan" do
100 # it "should rhyme with 'be gone'" do
101 # nut = "pecan"
102 # nut.extend Rhymer
103 # nut.should rhyme_with("be gone")
104 # end
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
105 # end
106 #
0ab525e @dchelimsky simple_matcher rdoc
authored
107 # The resulting messages would be:
108 # description: rhyme with "be gone"
109 # failure_message: expected "pecan" to rhyme with "be gone"
110 # negative failure_message: expected "pecan" not to rhyme with "be gone"
111 #
112 # == Wrapped Expectations
113 #
114 # Because errors will bubble up, it is possible to wrap other expectations
115 # in a SimpleMatcher.
116 #
117 # def be_even
118 # simple_matcher("an even number") { |given| (given % 2).should == 0 }
119 # end
120 #
121 # BE VERY CAREFUL when you do this. Only use wrapped expectations for
122 # matchers that will always be used in only the positive
123 # (<tt>should</tt>) or negative (<tt>should_not</tt>), but not both.
124 # The reason is that is you wrap a <tt>should</tt> and call the wrapper
125 # with <tt>should_not</tt>, the correct result (the <tt>should</tt>
126 # failing), will fail when you want it to pass.
127 #
d268cdf @dchelimsky added docs for simple_matcher, plus some feedback when it's not used …
authored
128 def simple_matcher(description=nil, &match_block)
129 SimpleMatcher.new(description, &match_block)
b18a896 @dchelimsky more simple matching refactoring
authored
130 end
8c27e78 got rid of the test/unit output in TextMate
dchelimsky authored
131 end
132 end
Something went wrong with that request. Please try again.