kookaburra 1.3.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,237 +0,0 @@
1
- require 'kookaburra/mental_model_matcher'
2
-
3
- # Makes the specs themselves a bit less verbose. You should probably read the
4
- # specs first, though.
5
- module MentalModelMatcherMacros
6
- def self.included(receiver)
7
- receiver.extend ClassMethods
8
- end
9
-
10
- module ClassMethods
11
- def pp_array(array)
12
- array = array.sort if array.all? { |e| e.respond_to?(:<=>) }
13
- array.inspect
14
- end
15
-
16
- def it_matches
17
- it "matches" do
18
- matcher.matches?(target).should be_true
19
- end
20
- end
21
-
22
- def it_doesnt_match
23
- it "doesn't match" do
24
- matcher.matches?(target).should be_false
25
- end
26
- end
27
-
28
- def from_line
29
- #puts '#' * 80
30
- #puts "it is: '#{caller(2).first.split(':').inspect}'"
31
- #puts '#' * 81
32
- '(line %d)' % caller(2).first.split(':')[1]
33
- end
34
-
35
- def it_complains_about_missing(missing, options)
36
- expected = options[:expected]
37
- it "complains about a mismatch #{from_line}" do
38
- failure_msg.should include("expected widgets to match the user's mental model, but:")
39
- end
40
- it "says what it expected to be present #{from_line}" do
41
- failure_msg.should include("expected to be present: #{pp_array(expected)}")
42
- end
43
- it "complains about missing items #{from_line}" do
44
- failure_msg.should include("the missing elements were: #{pp_array(missing)}")
45
- end
46
- end
47
-
48
- def it_complains_about_extra(extra, options)
49
- unexpected = options[:unexpected]
50
- it "complains about a mismatch #{from_line}" do
51
- failure_msg.should include("expected widgets to match the user's mental model, but:")
52
- end
53
- it "says what it expected not to find #{from_line}" do
54
- failure_msg.should include("expected to not be present: #{pp_array(unexpected)}")
55
- end
56
- it "complains about extra items #{from_line}" do
57
- failure_msg.should include("the unexpected extra elements: #{pp_array(extra)}")
58
- end
59
- end
60
- end
61
-
62
- def matcher_for(collection_key)
63
- Kookaburra::MentalModel::Matcher.new(mm, collection_key).tap do |m|
64
- m.matches?(target)
65
- end
66
- end
67
-
68
- def pp_array(array)
69
- self.class.pp_array(array)
70
- end
71
- end
72
-
73
- describe Kookaburra::MentalModel::Matcher do
74
- include MentalModelMatcherMacros
75
-
76
- let(:mm) { Kookaburra::MentalModel.new }
77
- let(:matcher) { matcher_for(:widgets) }
78
- let(:failure_msg) { matcher.failure_message_for_should }
79
-
80
- def self.foo; 'FOO' ; end
81
- def self.bar; 'BAR' ; end
82
- def self.yak; 'YAK' ; end
83
- let(:foo) { self.class.foo }
84
- let(:bar) { self.class.bar }
85
- let(:yak) { self.class.yak }
86
-
87
- context "when mental model is [foo];" do
88
- before(:each) do
89
- mm.widgets[:foo] = foo
90
- end
91
-
92
- context "for [] (foo missing)" do
93
- let(:target) { [] }
94
- it_doesnt_match
95
- it_complains_about_missing [foo], :expected => [foo]
96
- end
97
-
98
- context "for [foo] (OK: exact match)" do
99
- let(:target) { [foo] }
100
- it_matches
101
- end
102
-
103
- context "for [foo, bar] (OK: bar ignored)" do
104
- let(:target) { [foo, bar] }
105
- it_matches
106
- end
107
- end
108
-
109
- context "when mental model is [];" do
110
- context "for [] (OK)" do
111
- let(:target) { [] }
112
- it_matches
113
- end
114
-
115
- context "for [foo] (OK: foo ignored)" do
116
- let(:target) { [foo] }
117
- it_matches
118
- end
119
- end
120
-
121
- context "when mental model is [foo, bar];" do
122
- before(:each) do
123
- mm.widgets[:foo] = foo
124
- mm.widgets[:bar] = bar
125
- end
126
-
127
- context "for [] (foo, bar missing)" do
128
- let(:target) { [] }
129
- it_doesnt_match
130
- it_complains_about_missing [foo, bar], :expected => [foo, bar]
131
- end
132
-
133
- context "for [foo] (bar missing)" do
134
- let(:target) { [foo] }
135
- it_doesnt_match
136
- it_complains_about_missing [bar], :expected => [foo, bar]
137
- end
138
-
139
- context "for [foo, bar] (OK: exact match)" do
140
- let(:target) { [foo, bar] }
141
- it_matches
142
- end
143
-
144
- context "for [foo, bar, yak] (OK: foo, bar expected; yak ignored)" do
145
- let(:target) { [foo, bar, yak] }
146
- it_matches
147
- end
148
- end
149
-
150
- context "when mental model is [foo], not expecting [bar];" do
151
- before(:each) do
152
- mm.widgets[:foo] = foo
153
- mm.widgets[:bar] = bar
154
- mm.widgets.delete(:bar)
155
- end
156
-
157
- context "for [] (foo missing)" do
158
- let(:target) { [] }
159
- it_doesnt_match
160
- it_complains_about_missing [foo], :expected => [foo]
161
- end
162
-
163
- context "for [bar] (foo missing, bar deleted)" do
164
- let(:target) { [bar] }
165
- it_doesnt_match
166
- it_complains_about_missing [foo], :expected => [foo]
167
- it_complains_about_extra [bar], :unexpected => [bar]
168
- end
169
-
170
- context "for [foo, bar] (bar deleted)" do
171
- let(:target) { [foo, bar] }
172
- it_doesnt_match
173
- it_complains_about_extra [bar], :unexpected => [bar]
174
- end
175
-
176
- context "for [foo] (OK: foo expected, bar not found)" do
177
- let(:target) { [foo] }
178
- it_matches
179
- end
180
-
181
- context "for [foo, yak] (OK: foo expected; yak ignored)" do
182
- let(:target) { [foo, yak] }
183
- it_matches
184
- end
185
- end
186
-
187
- describe "postfix scoping methods" do
188
- context "when mental model is [foo, bar];" do
189
- before(:each) do
190
- mm.widgets[:foo] = foo
191
- mm.widgets[:bar] = bar
192
- end
193
-
194
- context "but scoped to .only(:foo)" do
195
- let(:matcher) { matcher_for(:widgets).only(:foo) }
196
-
197
- context "for [foo] (OK)" do
198
- let(:target) { [foo] }
199
- it_matches
200
- end
201
-
202
- context "for [foo, bar] (not expecting [bar])" do
203
- let(:target) { [foo, bar] }
204
- it_doesnt_match
205
- it_complains_about_extra [bar], :unexpected => [bar]
206
- end
207
- end
208
- end
209
-
210
- context "when mental model is [foo];" do
211
- before(:each) do
212
- mm.widgets[:foo] = foo
213
- end
214
-
215
- context "but scoped with .expecting_nothing" do
216
- let(:matcher) { matcher_for(:widgets).expecting_nothing }
217
-
218
- context "for [] (OK)" do
219
- let(:target) { [] }
220
- it_matches
221
- end
222
-
223
- context "for [foo] (unexpected foo)" do
224
- let(:target) { [foo] }
225
- it_doesnt_match
226
- it_complains_about_extra [foo], :unexpected => [foo]
227
- end
228
-
229
- context "for [foo, bar] (unexpected [foo]; bar ignored)" do
230
- let(:target) { [foo, bar] }
231
- it_doesnt_match
232
- it_complains_about_extra [foo], :unexpected => [foo]
233
- end
234
- end
235
- end
236
- end
237
- end