jnunemaker-matchy 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/../lib/matchy.rb'
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestMatcherBuilder < Test::Unit::TestCase
4
+ include Matchy::MatcherBuilder
5
+
6
+ def setup
7
+ @obj = Object.new
8
+ end
9
+
10
+ def test_matcher_responds_to_matches
11
+ block = lambda {|given, matcher, args| true}
12
+ build_matcher(:m, &block).should respond_to(:matches?)
13
+ end
14
+
15
+ def test_fail_positive
16
+ block = lambda {|given, matcher, args| false}
17
+ lambda {@obj.should build_matcher(:m, &block)}.should raise_error
18
+ end
19
+
20
+ def test_pass_positive
21
+ block = lambda {|given, matcher, args| true}
22
+ @obj.should build_matcher(:m, &block)
23
+ end
24
+
25
+ def test_fail_negative
26
+ block = lambda {|given, matcher, args| true}
27
+ lambda {@obj.should_not build_matcher(:m, &block)}.should raise_error
28
+ end
29
+
30
+ def test_pass_negative
31
+ block = lambda {|given, matcher, args| false}
32
+ @obj.should_not build_matcher(:m, &block)
33
+ end
34
+
35
+ def test_takes_arguments
36
+ block = lambda {|given, matcher, args| $args = args; true}
37
+ @obj.should build_matcher(:m,[1,2,3], &block)
38
+ $args.should eql([1,2,3])
39
+ end
40
+
41
+ def test_received_method
42
+ block = lambda {|given, matcher, args| $chained_messages = matcher.chained_messages; true}
43
+ @obj.should build_matcher(:m, &block).method1
44
+ $chained_messages[0].name.should eql(:method1)
45
+ end
46
+
47
+ def test_received_method_takes_args
48
+ block = lambda {|given, matcher, args| $chained_messages = matcher.chained_messages; true}
49
+ @obj.should build_matcher(:m, &block).method1(1,2,3)
50
+ $chained_messages[0].args.should eql([1,2,3])
51
+ end
52
+
53
+ def test_received_method_takes_block
54
+ block = lambda {|given, matcher, args| $chained_messages = matcher.chained_messages; true}
55
+ @obj.should build_matcher(:m, &block).method1 { "Hello, World!"}
56
+ $chained_messages[0].block.call.should eql("Hello, World!")
57
+ end
58
+
59
+ def test_received_method_chained
60
+ block = lambda {|given, matcher, args| $chained_messages = matcher.chained_messages; true}
61
+ @obj.should build_matcher(:m, &block).method1(1,2,3) { "Hello, World!"}.
62
+ method2(4,5,6) { "Hello chained messages" }
63
+
64
+ $chained_messages[0].name.should eql(:method1)
65
+ $chained_messages[1].name.should eql(:method2)
66
+ $chained_messages[0].args.should eql([1,2,3])
67
+ $chained_messages[1].args.should eql([4,5,6])
68
+ $chained_messages[0].block.call.should eql("Hello, World!")
69
+ $chained_messages[1].block.call.should eql("Hello chained messages")
70
+ end
71
+
72
+ end
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestModals < Test::Unit::TestCase
4
+ def setup
5
+ @expectation = build_matcher() {|r,m,a| true}
6
+ @bad_expectation = build_matcher() {|r,m,a| false}
7
+ end
8
+
9
+ def test_should
10
+ 3.should(@expectation)
11
+ end
12
+
13
+ def test_will
14
+ 3.will(@expectation)
15
+ end
16
+
17
+ def test_should_not
18
+ 3.should_not(@bad_expectation)
19
+ end
20
+
21
+ def test_will_not
22
+ 3.will_not(@bad_expectation)
23
+ end
24
+
25
+ def test_wont
26
+ 3.wont(@bad_expectation)
27
+ end
28
+
29
+ def test_should_operator_expectation_returned
30
+ obj = 3.should
31
+ assert_equal Matchy::Expectations::OperatorExpectation, obj.class
32
+ end
33
+
34
+
35
+ def test_should_not_operator_expectation_returned
36
+ obj = 3.should_not
37
+ assert_equal Matchy::Expectations::OperatorExpectation, obj.class
38
+ end
39
+ end
@@ -0,0 +1,157 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestOperatorExpectations < Test::Unit::TestCase
4
+ # EQUALS (==)
5
+ def test_equals
6
+ 3.should == 3
7
+ end
8
+
9
+ def test_equals_fails
10
+ lambda {
11
+ 3.should == 5
12
+ }.should raise_error(Test::Unit::AssertionFailedError)
13
+ end
14
+
15
+ def test_negative_equals
16
+ 3.should_not == 4
17
+ end
18
+
19
+ def test_negative_equals_fails
20
+ lambda {
21
+ 3.should_not == 3
22
+ }.should raise_error(Test::Unit::AssertionFailedError)
23
+ end
24
+
25
+ # LESS THAN (<)
26
+ def test_less_than
27
+ 3.should < 5
28
+ end
29
+
30
+ def test_less_than_fails
31
+ lambda {
32
+ 4.should < 3
33
+ }.should raise_error(Test::Unit::AssertionFailedError)
34
+ end
35
+
36
+ def test_less_than_equals
37
+ 3.should_not < 2
38
+ end
39
+
40
+ def test_negative_less_than_fails
41
+ lambda {
42
+ 4.should_not < 5
43
+ }.should raise_error(Test::Unit::AssertionFailedError)
44
+ end
45
+
46
+ # GREATER THAN (>)
47
+ def test_greater_than
48
+ 3.should > 2
49
+ end
50
+
51
+ def test_greater_than_fails
52
+ lambda {
53
+ 4.should > 5
54
+ }.should raise_error(Test::Unit::AssertionFailedError)
55
+ end
56
+
57
+ def test_greater_than_equals
58
+ 3.should_not > 5
59
+ end
60
+
61
+ def test_negative_greater_than_fails
62
+ lambda {
63
+ 4.should_not > 3
64
+ }.should raise_error(Test::Unit::AssertionFailedError)
65
+ end
66
+
67
+ # LESS THAN EQUAL (<=)
68
+ def test_less_than_equal
69
+ 3.should <= 5
70
+ end
71
+
72
+ def test_less_than_equal_equal
73
+ 3.should <= 3
74
+ end
75
+
76
+ def test_less_than_equal_fails
77
+ lambda {
78
+ 4.should <= 3
79
+ }.should raise_error(Test::Unit::AssertionFailedError)
80
+ end
81
+
82
+ def test_negative_less_than_equal
83
+ 3.should_not <= 2
84
+ end
85
+
86
+ def test_negative_less_than_equal_fails
87
+ lambda {
88
+ 4.should_not <= 5
89
+ }.should raise_error(Test::Unit::AssertionFailedError)
90
+ end
91
+
92
+ # GREATER THAN EQUALS (>=)
93
+ def test_greater_than_equal
94
+ 3.should >= 2
95
+ end
96
+
97
+ def test_greater_than_equal_equals
98
+ 3.should >= 3
99
+ end
100
+
101
+ def test_greater_than_equal_fails
102
+ lambda {
103
+ 4.should >= 5
104
+ }.should raise_error(Test::Unit::AssertionFailedError)
105
+ end
106
+
107
+ def test_negative_greater_than_equal_equals
108
+ 3.should_not >= 5
109
+ end
110
+
111
+ def test_negative_greater_than_equal_fails
112
+ lambda {
113
+ 4.should_not >= 3
114
+ }.should raise_error(Test::Unit::AssertionFailedError)
115
+ end
116
+
117
+ # MATCHES (=~)
118
+ def test_matches
119
+ "hey world".should =~ /world/
120
+ end
121
+
122
+ def test_matches_fails
123
+ lambda {
124
+ "d00d ur 1337".should =~ /world/
125
+ }.should raise_error(Test::Unit::AssertionFailedError)
126
+ end
127
+
128
+ def test_negative_matches
129
+ "1337@age".should_not =~ /434/
130
+ end
131
+
132
+ def test_negative_matches_fails
133
+ lambda {
134
+ "it's a freak out!".should_not =~ /freak/
135
+ }.should raise_error(Test::Unit::AssertionFailedError)
136
+ end
137
+
138
+ def test_fail_message
139
+ obj = Matchy::Expectations::OperatorExpectation.new(3, true)
140
+
141
+ def obj.flunk(msg)
142
+ msg
143
+ end
144
+
145
+ (obj == 4).should == "Expected 3 to == 4."
146
+ end
147
+
148
+ def test_negative_fail_message
149
+ obj = Matchy::Expectations::OperatorExpectation.new(3, false)
150
+
151
+ def obj.flunk(msg)
152
+ msg
153
+ end
154
+
155
+ (obj == 3).should == "Expected 3 to not == 3."
156
+ end
157
+ end
@@ -0,0 +1,373 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class Exister
4
+ def initialize(what)
5
+ @what = what
6
+ end
7
+
8
+ def exist?
9
+ @what
10
+ end
11
+ end
12
+
13
+ class TestTruthExpectations < Test::Unit::TestCase
14
+
15
+ def setup
16
+ @obj = Object.new
17
+ end
18
+
19
+ def test_equal
20
+ 3.should equal(3)
21
+ end
22
+
23
+ def test_negative_equal
24
+ instance = String.new
25
+
26
+ instance.should_not equal(String.new)
27
+ end
28
+
29
+ def test_equal_fail
30
+ lambda {
31
+ 3.should equal(4)
32
+ }.should raise_error(Test::Unit::AssertionFailedError)
33
+ end
34
+
35
+ def test_negative_equal_fail
36
+ lambda {
37
+ 3.should_not equal(3)
38
+ }.should raise_error(Test::Unit::AssertionFailedError)
39
+ end
40
+
41
+ def test_eql
42
+ 3.should eql(3)
43
+ end
44
+
45
+ def test_eql_array
46
+ [1,2,3].should eql([1,2,3])
47
+ end
48
+
49
+ def test_negative_eql
50
+ 3.should_not eql(9)
51
+ end
52
+
53
+ def test_eql_fail
54
+ lambda {
55
+ 3.should eql(13)
56
+ }.should raise_error(Test::Unit::AssertionFailedError)
57
+ end
58
+
59
+ def test_negative_eql_fail
60
+ lambda {
61
+ 3.should_not eql(3)
62
+ }.should raise_error(Test::Unit::AssertionFailedError)
63
+ end
64
+
65
+ def test_exists
66
+ thing = Exister.new(true)
67
+ thing.should exist
68
+ end
69
+
70
+ def test_negative_exists
71
+ thing = Exister.new(false)
72
+ thing.should_not exist
73
+ end
74
+
75
+ def test_exists_fail
76
+ lambda {
77
+ thing = Exister.new(false)
78
+ thing.should exist
79
+ }.should raise_error(Test::Unit::AssertionFailedError)
80
+ end
81
+
82
+ def test_negative_exists_fail
83
+ lambda {
84
+ thing = Exister.new(true)
85
+ thing.should_not exist
86
+ }.should raise_error(Test::Unit::AssertionFailedError)
87
+ end
88
+
89
+ def test_be
90
+ true.should be(true)
91
+ end
92
+
93
+ def test_be_array
94
+ [1,2,3].should be([1,2,3])
95
+ end
96
+
97
+ def test_negative_be
98
+ true.should_not be(false)
99
+ end
100
+
101
+ def test_be_fail
102
+ lambda {
103
+ true.should be(false)
104
+ }.should raise_error(Test::Unit::AssertionFailedError)
105
+ end
106
+
107
+ def test_be_close
108
+ (5.0 - 2.0).should be_close(3.0)
109
+ end
110
+
111
+ def test_be_close_with_delta
112
+ (5.0 - 2.0).should be_close(3.0, 0.2)
113
+ end
114
+
115
+ def test_be_close_fail
116
+ lambda {
117
+ (19.0 - 13.0).should be_close(33.04)
118
+ }.should raise_error(Test::Unit::AssertionFailedError)
119
+ end
120
+
121
+ def test_be_close_with_delta_fail
122
+ lambda {
123
+ (19.0 - 13.0).should be_close(6.0, 0.0)
124
+ }.should raise_error(Test::Unit::AssertionFailedError)
125
+ end
126
+
127
+ def test_satisfy
128
+ 13.should satisfy(lambda {|i| i < 15})
129
+ end
130
+
131
+ def test_negative_satisfy
132
+ 13.should_not satisfy(lambda {|i| i < 10})
133
+ end
134
+
135
+ def test_satisfy_fail
136
+ lambda {
137
+ 13.should satisfy(lambda {|i| i > 15})
138
+ }.should raise_error(Test::Unit::AssertionFailedError)
139
+ end
140
+
141
+ def test_negative_satisfy_fail
142
+ lambda {
143
+ 13.should_not satisfy(lambda {|i| i < 15})
144
+ }.should raise_error(Test::Unit::AssertionFailedError)
145
+ end
146
+
147
+ def test_equal_fail_message
148
+ obj = equal(4)
149
+ obj.matches?(5)
150
+
151
+ obj.failure_message.should be("Expected 5 to return true for equal?, with '4'.")
152
+ end
153
+
154
+ def test_equal_negative_fail_message
155
+ obj = equal(5)
156
+ obj.matches?(5)
157
+
158
+ obj.negative_failure_message.should be("Expected 5 to not return true for equal?, with '5'.")
159
+ end
160
+
161
+ def test_eql_fail_message
162
+ obj = eql(4)
163
+ obj.matches?(5)
164
+
165
+ obj.failure_message.should be("Expected 5 to return true for eql?, with '4'.")
166
+ end
167
+
168
+ def test_eql_negative_fail_message_for_eql
169
+ obj = eql(5)
170
+ obj.matches?(5)
171
+
172
+ obj.negative_failure_message.should be("Expected 5 to not return true for eql?, with '5'.")
173
+ end
174
+
175
+ def test_exist_fail_message
176
+ obj = exist
177
+ obj.matches?(Exister.new(false))
178
+
179
+ obj.failure_message.should =~ /Expected #<(.*)> to return true for exist?./
180
+ end
181
+
182
+ def test_exist_negative_fail_message
183
+ obj = exist
184
+ obj.matches?(Exister.new(true))
185
+
186
+ obj.negative_failure_message.should =~ /Expected #<(.*)> to not return true for exist?./
187
+ end
188
+
189
+ def test_be_close_fail_message
190
+ obj = be_close(3.0)
191
+ obj.matches?(6.0)
192
+
193
+ obj.failure_message.should be("Expected 6.0 to be close to 3.0 (delta: 0.3).")
194
+ end
195
+
196
+ def test_be_close_negative_fail_message
197
+ obj = be_close(5.0)
198
+ obj.matches?(5.0)
199
+
200
+ obj.negative_failure_message.should be("Expected 5.0 to not be close to 5.0 (delta: 0.3).")
201
+ end
202
+
203
+ def test_be_fail_message
204
+ obj = be(4)
205
+ obj.matches?(5)
206
+
207
+ obj.failure_message.should be("Expected 5 to be 4.")
208
+ end
209
+
210
+ def test_be_negative_fail_message
211
+ obj = be(5)
212
+ obj.matches?(5)
213
+
214
+ obj.negative_failure_message.should be("Expected 5 to not be 5.")
215
+ end
216
+
217
+ def test_satisfy_fail_message
218
+ obj = satisfy(lambda {|x| x == 4})
219
+ obj.matches?(6)
220
+
221
+ obj.failure_message.should be("Expected 6 to satisfy given block.")
222
+ end
223
+
224
+ def test_eql_negative_fail_message_for_matches
225
+ obj = satisfy(lambda {|x| x == 4})
226
+ obj.matches?(4)
227
+
228
+ obj.negative_failure_message.should be("Expected 4 to not satisfy given block.")
229
+ end
230
+
231
+ def test_kind_of
232
+ 3.should be_kind_of(Fixnum)
233
+ end
234
+
235
+ def test_kind_of_fail
236
+ lambda {
237
+ 3.should be_kind_of(Hash)
238
+ }.should raise_error(Test::Unit::AssertionFailedError)
239
+ end
240
+
241
+ def test_negative_kind_of
242
+ 3.should_not be_kind_of(Hash)
243
+ end
244
+
245
+ def test_negative_kind_of_fail
246
+ lambda {
247
+ 3.should_not be_kind_of(Fixnum)
248
+ }.should raise_error(Test::Unit::AssertionFailedError)
249
+ end
250
+
251
+ def test_respond_to
252
+ "foo".should respond_to(:length)
253
+ end
254
+
255
+ def test_respond_to_fail
256
+ lambda {
257
+ "foo".should respond_to(:nonexistant_method)
258
+ }.should raise_error(Test::Unit::AssertionFailedError)
259
+ end
260
+
261
+ def test_negative_respond_to
262
+ "foo".should_not respond_to(:nonexistant_method)
263
+ end
264
+
265
+ def test_negative_respond_to_fail
266
+ lambda {
267
+ "foo".should_not respond_to(:length)
268
+ }.should raise_error(Test::Unit::AssertionFailedError)
269
+ end
270
+
271
+ # be_something
272
+ def test_positive_be_something_method_missing_pass
273
+ def @obj.something?
274
+ true
275
+ end
276
+ @obj.should be_something
277
+ end
278
+
279
+ def test_positive_be_something_method_missing_fails
280
+ def @obj.something?
281
+ false
282
+ end
283
+ lambda {@obj.should be_something}.should raise_error(Test::Unit::AssertionFailedError)
284
+ end
285
+
286
+ def test_negative_be_something_method_missing_pass
287
+ def @obj.something?
288
+ false
289
+ end
290
+ @obj.should_not be_something
291
+ end
292
+
293
+ def test_negative_be_something_method_missing_fails
294
+ def @obj.something?
295
+ true
296
+ end
297
+ lambda {@obj.should_not be_something}.should raise_error(Test::Unit::AssertionFailedError)
298
+ end
299
+
300
+ def test_be_something_method_missing_fail_message
301
+ obj = "foo"
302
+ def obj.something?
303
+ true
304
+ end
305
+ matcher_obj = be_something
306
+ obj.should matcher_obj
307
+
308
+ matcher_obj.failure_message.should be("Expected \"foo\" to return true for something?, with 'no args'.")
309
+ end
310
+
311
+ def test_be_something_method_missing_negative_fail_message
312
+ obj = "foo"
313
+ def obj.something?
314
+ false
315
+ end
316
+ matcher_obj = be_something
317
+ obj.should_not matcher_obj
318
+
319
+ matcher_obj.negative_failure_message.should =~ /Expected \"foo\" to not return true for something?/
320
+ end
321
+
322
+ # be_something(arg)
323
+ def test_positive_be_something_with_arg_method_missing_pass
324
+ def @obj.something?(arg)
325
+ true
326
+ end
327
+ @obj.should be_something(1)
328
+ end
329
+
330
+ def test_positive_be_something_with_arg_method_missing_fails
331
+ def @obj.something?(arg)
332
+ false
333
+ end
334
+ lambda {@obj.should be_something(1)}.should raise_error(Test::Unit::AssertionFailedError)
335
+ end
336
+
337
+ def test_negative_be_something_method_missing_pass
338
+ def @obj.something?(arg)
339
+ false
340
+ end
341
+ @obj.should_not be_something(1)
342
+ end
343
+
344
+ def test_negative_be_something_method_missing_fails
345
+ def @obj.something?(arg)
346
+ true
347
+ end
348
+ lambda {@obj.should_not be_something(1)}.should raise_error(Test::Unit::AssertionFailedError)
349
+ end
350
+
351
+ def test_be_something_method_missing_fail_message
352
+ obj = "foo"
353
+ def obj.something?(arg)
354
+ true
355
+ end
356
+ matcher_obj = be_something(1)
357
+ obj.should matcher_obj
358
+
359
+ matcher_obj.failure_message.should be("Expected \"foo\" to return true for something?, with '1'.")
360
+ end
361
+
362
+ def test_be_something_method_missing_negative_fail_message
363
+ obj = "foo"
364
+ def obj.something?(arg)
365
+ false
366
+ end
367
+ matcher_obj = be_something(1)
368
+ obj.should_not matcher_obj
369
+
370
+ matcher_obj.negative_failure_message.should be("Expected \"foo\" to not return true for something?, with '1'.")
371
+ end
372
+
373
+ end