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