Skip to content
This repository
Newer
Older
100644 132 lines (125 sloc) 4.841 kb
8c27e784 »
2007-10-31 got rid of the test/unit output in TextMate
1 module Spec
2 module Matchers
3 class SimpleMatcher
d268cdf4 »
2008-06-19 added docs for simple_matcher, plus some feedback when it's not used …
4 attr_writer :failure_message, :negative_failure_message, :description
7e1a7196 »
2008-06-19 simple_matcher refactoring - one more time - much simpler
5
8c27e784 »
2007-10-31 got rid of the test/unit output in TextMate
6 def initialize(description, &match_block)
7e1a7196 »
2008-06-19 simple_matcher refactoring - one more time - much simpler
7 @description = description
8c27e784 »
2007-10-31 got rid of the test/unit output in TextMate
8 @match_block = match_block
9 end
10
8af8ff25 »
2008-09-29 Getting the words right - use 'given' in matchers.
11 def matches?(given)
12 @given = given
63d2edcf »
2008-06-20 should and should_not now return true on success
13 case @match_block.arity
14 when 2
8af8ff25 »
2008-09-29 Getting the words right - use 'given' in matchers.
15 @match_block.call(@given, self)
63d2edcf »
2008-06-20 should and should_not now return true on success
16 else
8af8ff25 »
2008-09-29 Getting the words right - use 'given' in matchers.
17 @match_block.call(@given)
63d2edcf »
2008-06-20 should and should_not now return true on success
18 end
8c27e784 »
2007-10-31 got rid of the test/unit output in TextMate
19 end
d268cdf4 »
2008-06-19 added docs for simple_matcher, plus some feedback when it's not used …
20
21 def description
22 @description || explanation
23 end
8c27e784 »
2007-10-31 got rid of the test/unit output in TextMate
24
b18a8962 »
2008-06-19 more simple matching refactoring
25 def failure_message
8af8ff25 »
2008-09-29 Getting the words right - use 'given' in matchers.
26 @failure_message || (@description.nil? ? explanation : %[expected #{@description.inspect} but got #{@given.inspect}])
8c27e784 »
2007-10-31 got rid of the test/unit output in TextMate
27 end
7e1a7196 »
2008-06-19 simple_matcher refactoring - one more time - much simpler
28
b18a8962 »
2008-06-19 more simple matching refactoring
29 def negative_failure_message
8af8ff25 »
2008-09-29 Getting the words right - use 'given' in matchers.
30 @negative_failure_message || (@description.nil? ? explanation : %[expected not to get #{@description.inspect}, but got #{@given.inspect}])
fa9952c9 »
2008-06-19 added messenger to simple_matcher for custom failure messages
31 end
b18a8962 »
2008-06-19 more simple matching refactoring
32
d268cdf4 »
2008-06-19 added docs for simple_matcher, plus some feedback when it's not used …
33 def explanation
34 "No description provided. See RDoc for simple_matcher()"
35 end
36 end
37
0ab525e6 »
2008-06-21 simple_matcher rdoc
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.
d268cdf4 »
2008-06-19 added docs for simple_matcher, plus some feedback when it's not used …
41 #
0ab525e6 »
2008-06-21 simple_matcher rdoc
42 # The <tt>description</tt> argument will appear as part of any failure
43 # message, and is also the source for auto-generated descriptions.
d268cdf4 »
2008-06-19 added docs for simple_matcher, plus some feedback when it's not used …
44 #
0ab525e6 »
2008-06-21 simple_matcher rdoc
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.
d268cdf4 »
2008-06-19 added docs for simple_matcher, plus some feedback when it's not used …
49 #
0ab525e6 »
2008-06-21 simple_matcher rdoc
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>.
44d52ab7 »
2008-06-19 words (simple matcher - cont'd)
55 #
0ab525e6 »
2008-06-21 simple_matcher rdoc
56 # An error in the <tt>match_block</tt> will bubble up, resulting in a
57 # failure.
d268cdf4 »
2008-06-19 added docs for simple_matcher, plus some feedback when it's not used …
58 #
59 # == Example with default messages
60 #
0ab525e6 »
2008-06-21 simple_matcher rdoc
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
d268cdf4 »
2008-06-19 added docs for simple_matcher, plus some feedback when it's not used …
69 # end
70 #
0ab525e6 »
2008-06-21 simple_matcher rdoc
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:
d268cdf4 »
2008-06-19 added docs for simple_matcher, plus some feedback when it's not used …
77 #
78 # == Example with custom messages
79 #
0ab525e6 »
2008-06-21 simple_matcher rdoc
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}"
8af8ff25 »
2008-09-29 Getting the words right - use 'given' in matchers.
84 # given.rhymes_with? expected
0ab525e6 »
2008-06-21 simple_matcher rdoc
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}"
8af8ff25 »
2008-09-29 Getting the words right - use 'given' in matchers.
95 # given.rhymes_with? expected
0ab525e6 »
2008-06-21 simple_matcher rdoc
96 # end
d268cdf4 »
2008-06-19 added docs for simple_matcher, plus some feedback when it's not used …
97 # end
98 #
0ab525e6 »
2008-06-21 simple_matcher rdoc
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
d268cdf4 »
2008-06-19 added docs for simple_matcher, plus some feedback when it's not used …
105 # end
106 #
0ab525e6 »
2008-06-21 simple_matcher rdoc
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 #
d268cdf4 »
2008-06-19 added docs for simple_matcher, plus some feedback when it's not used …
128 def simple_matcher(description=nil, &match_block)
129 SimpleMatcher.new(description, &match_block)
b18a8962 »
2008-06-19 more simple matching refactoring
130 end
8c27e784 »
2007-10-31 got rid of the test/unit output in TextMate
131 end
132 end
Something went wrong with that request. Please try again.