flexmock 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'flexmock'
5
+
6
+ class TemperatureSampler
7
+ def initialize(sensor)
8
+ @sensor = sensor
9
+ end
10
+
11
+ def average_temp
12
+ total = (0...3).collect { @sensor.read_temperature }.inject { |i, s| i + s }
13
+ total / 3.0
14
+ end
15
+
16
+ end
17
+
18
+ class TestTemperatureSampler < Test::Unit::TestCase
19
+ def test_tempurature_sampler
20
+ readings = [10, 12, 14]
21
+ mock_sensor = FlexMock.new
22
+ mock_sensor.mock_handle(:read_temperature) { readings.shift }
23
+ sampler = TemperatureSampler.new(mock_sensor)
24
+ assert_equal 12, sampler.average_temp
25
+ end
26
+ end
data/test/test_mock.rb ADDED
@@ -0,0 +1,171 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'flexmock'
5
+
6
+ class TestFlexMock < Test::Unit::TestCase
7
+ def setup
8
+ @mock = FlexMock.new
9
+ end
10
+
11
+ def test_handle
12
+ args = nil
13
+ @mock.mock_handle(:hi) { |a, b| args = [a,b] }
14
+ @mock.hi(1,2)
15
+ assert_equal [1,2], args
16
+ end
17
+
18
+ def test_handle_no_block
19
+ @mock.mock_handle(:blip)
20
+ @mock.blip
21
+ assert true, "just checking for failures"
22
+ end
23
+
24
+ def test_called_with_block
25
+ called = false
26
+ @mock.mock_handle(:blip) { |block| block.call }
27
+ @mock.blip { called = true }
28
+ assert called, "Block to blip should be called"
29
+ end
30
+
31
+ def test_return_value
32
+ @mock.mock_handle(:blip) { 10 }
33
+ assert_equal 10, @mock.blip
34
+ end
35
+
36
+ def test_handle_missing_method
37
+ expected_error = (VERSION >= "1.8.0") ? NoMethodError : NameError
38
+ ex = assert_raises(expected_error) {
39
+ @mock.not_defined
40
+ }
41
+ assert_match /not_defined/, ex.message
42
+ end
43
+
44
+ def test_ignore_missing_method
45
+ @mock.mock_ignore_missing
46
+ @mock.blip
47
+ assert true, "just checking for failures"
48
+ end
49
+
50
+ def test_good_counts
51
+ @mock.mock_handle(:blip, 3)
52
+ @mock.blip
53
+ @mock.blip
54
+ @mock.blip
55
+ @mock.mock_verify
56
+ end
57
+
58
+ def test_bad_counts
59
+ @mock.mock_handle(:blip, 3)
60
+ @mock.blip
61
+ @mock.blip
62
+ begin
63
+ @mock.mock_verify
64
+ rescue Test::Unit::AssertionFailedError => err
65
+ end
66
+ assert_not_nil err
67
+ end
68
+
69
+ def test_undetermined_counts
70
+ FlexMock.use('fs') { |m|
71
+ m.mock_handle(:blip)
72
+ m.blip
73
+ m.blip
74
+ m.blip
75
+ }
76
+ end
77
+
78
+ def test_zero_counts
79
+ assert_raises(Test::Unit::AssertionFailedError) do
80
+ FlexMock.use { |m|
81
+ m.mock_handle(:blip, 0)
82
+ m.blip
83
+ }
84
+ end
85
+ end
86
+
87
+ def test_file_io_with_use
88
+ file = FlexMock.use do |m|
89
+ filedata = ["line 1", "line 2"]
90
+ m.mock_handle(:gets, 3) { filedata.shift }
91
+ assert_equal 2, count_lines(m)
92
+ end
93
+ end
94
+
95
+ def count_lines(stream)
96
+ result = 0
97
+ while line = stream.gets
98
+ result += 1
99
+ end
100
+ result
101
+ end
102
+
103
+ def test_use
104
+ assert_raises(Test::Unit::AssertionFailedError) {
105
+ FlexMock.use do |m|
106
+ m.mock_handle(:blip, 2)
107
+ m.blip
108
+ end
109
+ }
110
+ end
111
+
112
+ def test_failures_during_use
113
+ ex = assert_raises(NameError) {
114
+ FlexMock.use do |m|
115
+ m.mock_handle(:blip, 2)
116
+ xyz
117
+ end
118
+ }
119
+ assert_match /undefined local variable or method/, ex.message
120
+ end
121
+
122
+ def test_sequential_values
123
+ values = [1,4,9,16]
124
+ @mock.mock_handle(:get) { values.shift }
125
+ assert_equal 1, @mock.get
126
+ assert_equal 4, @mock.get
127
+ assert_equal 9, @mock.get
128
+ assert_equal 16, @mock.get
129
+ end
130
+
131
+ def test_respond_to_returns_false_for_non_handled_methods
132
+ assert(!@mock.respond_to?(:blah), "should not respond to blah")
133
+ end
134
+
135
+ def test_respond_to_returns_true_for_explicit_methods
136
+ @mock.mock_handle(:xyz)
137
+ assert(@mock.respond_to?(:xyz), "should respond to test")
138
+ end
139
+
140
+ def test_respond_to_returns_true_for_missing_methods_when_ignoring_missing
141
+ @mock.mock_ignore_missing
142
+ assert(@mock.respond_to?(:yada), "should respond to yada now")
143
+ end
144
+
145
+ def test_respond_to_returns_true_for_missing_methods_when_ignoring_missing_using_should
146
+ @mock.should_ignore_missing
147
+ assert(@mock.respond_to?(:yada), "should respond to yada now")
148
+ end
149
+
150
+ def test_method_proc_raises_error_on_unknown
151
+ assert_raises(NameError) {
152
+ @mock.method(:xyzzy)
153
+ }
154
+ end
155
+
156
+ def test_method_returns_callable_proc
157
+ got_it = false
158
+ @mock.mock_handle(:xyzzy) { got_it = true }
159
+ method_proc = @mock.method(:xyzzy)
160
+ assert_not_nil method_proc
161
+ method_proc.call
162
+ assert(got_it, "method proc should run")
163
+ end
164
+
165
+ def test_method_returns_do_nothing_proc_for_missing_methods
166
+ @mock.mock_ignore_missing
167
+ method_proc = @mock.method(:plugh)
168
+ assert_not_nil method_proc
169
+ method_proc.call
170
+ end
171
+ end
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'flexmock'
5
+
6
+ class TestNaming < Test::Unit::TestCase
7
+ def test_name
8
+ m = FlexMock.new("m")
9
+ assert_equal "m", m.mock_name
10
+ end
11
+
12
+ def test_name_in_no_handler_found_error
13
+ m = FlexMock.new("mmm")
14
+ ex = assert_raises(Test::Unit::AssertionFailedError) {
15
+ m.should_receive(:xx).with(1)
16
+ m.xx(2)
17
+ }
18
+ assert_match /'mmm'/, ex.message
19
+ end
20
+
21
+ def test_name_in_received_count_error
22
+ m = FlexMock.new("mmm")
23
+ ex = assert_raises(Test::Unit::AssertionFailedError) {
24
+ m.should_receive(:xx).once
25
+ m.mock_verify
26
+ }
27
+ assert_match /'mmm'/, ex.message
28
+ end
29
+
30
+ def test_naming_with_use
31
+ FlexMock.use("blah") do |m|
32
+ assert_equal "blah", m.mock_name
33
+ end
34
+ end
35
+
36
+ def test_naming_with_multiple_mocks_in_use
37
+ FlexMock.use("blah", "yuk") do |a, b|
38
+ assert_equal "blah", a.mock_name
39
+ assert_equal "yuk", b.mock_name
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #---
4
+ # Copyright 2003 by Jim Weirich (jweirich@one.net).
5
+ # All rights reserved.
6
+
7
+ # Permission is granted for use, copying, modification, distribution,
8
+ # and distribution of modified versions of this work as long as the
9
+ # above copyright notice is included.
10
+ #+++
11
+
12
+ require 'test/unit'
13
+
14
+ # Sample FlexMock Usage.
15
+
16
+ class TestSamples < Test::Unit::TestCase
17
+
18
+ # This is a basic example where we setup a mock object to mimic an
19
+ # IO object. We know that the +count_lines+ method uses gets, so we
20
+ # tell the mock object to handle +gets+ by returning successive
21
+ # elements of an array (just as the real +gets+ returns successive
22
+ # elements of a file.
23
+ def test_file_io
24
+ file = FlexMock.new
25
+ filedata = ["line 1", "line 2"]
26
+ file.mock_handle(:gets) { filedata.shift }
27
+ assert_equal 2, count_lines(file)
28
+ end
29
+
30
+ # Count the number of lines in a file. Used in the test_file_io
31
+ # test.
32
+ def count_lines(file)
33
+ n = 0
34
+ while file.gets
35
+ n += 1
36
+ end
37
+ n
38
+ end
39
+
40
+ def test_x
41
+ end
42
+ end
43
+
@@ -0,0 +1,443 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'flexmock'
5
+
6
+ class TestFlexMock < Test::Unit::TestCase
7
+
8
+ def test_defaults
9
+ FlexMock.use do |m|
10
+ m.should_receive(:hi)
11
+ assert_nil m.hi
12
+ assert_nil m.hi(1)
13
+ assert_nil m.hi("hello", 2)
14
+ end
15
+ end
16
+
17
+ def test_returns_with_value
18
+ FlexMock.use do |m|
19
+ m.should_receive(:hi).returns(1)
20
+ assert_equal 1, m.hi
21
+ assert_equal 1, m.hi(123)
22
+ end
23
+ end
24
+
25
+ def test_returns_with_block
26
+ FlexMock.use do |m|
27
+ result = nil
28
+ m.should_receive(:hi).with(Object).returns { |obj| result = obj }
29
+ m.hi(3)
30
+ assert_equal 3, result
31
+ end
32
+ end
33
+
34
+ def test_and_returns_alias
35
+ FlexMock.use do |m|
36
+ m.should_receive(:hi).and_return(4)
37
+ assert_equal 4, m.hi
38
+ end
39
+ end
40
+
41
+ def test_multiple_expectations
42
+ FlexMock.use do |m|
43
+ m.should_receive(:hi).with(1).returns(10)
44
+ m.should_receive(:hi).with(2).returns(20)
45
+
46
+ assert_equal 10, m.hi(1)
47
+ assert_equal 20, m.hi(2)
48
+ end
49
+ end
50
+
51
+ def test_with_no_args_with_no_args
52
+ FlexMock.use do |m|
53
+ m.should_receive(:hi).with_no_args
54
+ m.hi
55
+ end
56
+ end
57
+
58
+ def test__with_no_args_but_with_args
59
+ ex = assert_failure do
60
+ FlexMock.use do |m|
61
+ m.should_receive(:hi).with_no_args
62
+ m.hi(1)
63
+ end
64
+ end
65
+ end
66
+
67
+ def test__with_any_args
68
+ FlexMock.use do |m|
69
+ m.should_receive(:hi).with_any_args
70
+ m.hi
71
+ m.hi(1)
72
+ m.hi(1,2,3)
73
+ m.hi("this is a test")
74
+ end
75
+ end
76
+
77
+ def test_args_matching_with_regex
78
+ FlexMock.use do |m|
79
+ m.should_receive(:hi).with(/one/).returns(10)
80
+ m.should_receive(:hi).with(/t/).returns(20)
81
+
82
+ assert_equal 10, m.hi("one")
83
+ assert_equal 10, m.hi("done")
84
+ assert_equal 20, m.hi("two")
85
+ assert_equal 20, m.hi("three")
86
+ end
87
+ end
88
+
89
+ def test_arg_matching_with_regex_matching_non_string
90
+ FlexMock.use do |m|
91
+ m.should_receive(:hi).with(/1/).returns(10)
92
+ assert_equal 10, m.hi(319)
93
+ end
94
+ end
95
+
96
+ def test_arg_matching_with_class
97
+ FlexMock.use do |m|
98
+ m.should_receive(:hi).with(Fixnum).returns(10)
99
+ m.should_receive(:hi).with(Object).returns(20)
100
+
101
+ assert_equal 10, m.hi(319)
102
+ assert_equal 10, m.hi(Fixnum)
103
+ assert_equal 20, m.hi("hi")
104
+ end
105
+ end
106
+
107
+ def test_arg_matching_with_no_match
108
+ FlexMock.use do |m|
109
+ m.should_receive(:hi).with(1).returns(10)
110
+ assert_failure {
111
+ assert_equal 20, m.hi(2)
112
+ }
113
+ end
114
+ end
115
+
116
+ def test_never_and_never_called
117
+ FlexMock.use do |m|
118
+ m.should_receive(:hi).with(1).never
119
+ end
120
+ end
121
+
122
+ def test_never_and_called_once
123
+ ex = assert_failure do
124
+ FlexMock.use do |m|
125
+ m.should_receive(:hi).with(1).never
126
+ m.hi(1)
127
+ end
128
+ end
129
+ end
130
+
131
+ def test_once_called_once
132
+ FlexMock.use do |m|
133
+ m.should_receive(:hi).with(1).returns(10).once
134
+ m.hi(1)
135
+ end
136
+ end
137
+
138
+ def test_once_but_never_called
139
+ ex = assert_failure do
140
+ FlexMock.use do |m|
141
+ m.should_receive(:hi).with(1).returns(10).once
142
+ end
143
+ end
144
+ end
145
+
146
+ def test_once_but_called_twice
147
+ ex = assert_failure do
148
+ FlexMock.use do |m|
149
+ m.should_receive(:hi).with(1).returns(10).once
150
+ m.hi(1)
151
+ m.hi(1)
152
+ end
153
+ end
154
+ end
155
+
156
+ def test_twice_and_called_twice
157
+ FlexMock.use do |m|
158
+ m.should_receive(:hi).with(1).returns(10).twice
159
+ m.hi(1)
160
+ m.hi(1)
161
+ end
162
+ end
163
+
164
+ def test_zero_or_more_called_zero
165
+ FlexMock.use do |m|
166
+ m.should_receive(:hi).zero_or_more_times
167
+ end
168
+ end
169
+
170
+ def test_zero_or_more_called_once
171
+ FlexMock.use do |m|
172
+ m.should_receive(:hi).zero_or_more_times
173
+ m.hi
174
+ end
175
+ end
176
+
177
+ def test_zero_or_more_called_100
178
+ FlexMock.use do |m|
179
+ m.should_receive(:hi).zero_or_more_times
180
+ 100.times { m.hi }
181
+ end
182
+ end
183
+
184
+ def test_times
185
+ FlexMock.use do |m|
186
+ m.should_receive(:hi).with(1).returns(10).times(10)
187
+ 10.times { m.hi(1) }
188
+ end
189
+ end
190
+
191
+ def test_at_least_called_once
192
+ FlexMock.use do |m|
193
+ m.should_receive(:hi).with(1).returns(10).at_least.once
194
+ m.hi(1)
195
+ end
196
+ end
197
+
198
+ def test_at_least_but_never_called
199
+ ex = assert_failure do
200
+ FlexMock.use do |m|
201
+ m.should_receive(:hi).with(1).returns(10).at_least.once
202
+ end
203
+ end
204
+ end
205
+
206
+ def test_at_least_once_but_called_twice
207
+ FlexMock.use do |m|
208
+ m.should_receive(:hi).with(1).returns(10).at_least.once
209
+ m.hi(1)
210
+ m.hi(1)
211
+ end
212
+ end
213
+
214
+ def test_at_least_and_exact
215
+ ex = assert_failure do
216
+ FlexMock.use do |m|
217
+ m.should_receive(:hi).with(1).returns(10).at_least.once.once
218
+ m.hi(1)
219
+ m.hi(1)
220
+ end
221
+ end
222
+ end
223
+
224
+ def test_at_most_but_never_called
225
+ FlexMock.use do |m|
226
+ m.should_receive(:hi).with(1).returns(10).at_most.once
227
+ end
228
+ end
229
+
230
+ def test_at_most_called_once
231
+ FlexMock.use do |m|
232
+ m.should_receive(:hi).with(1).returns(10).at_most.once
233
+ m.hi(1)
234
+ end
235
+ end
236
+
237
+ def test_at_most_called_twice
238
+ ex = assert_failure do
239
+ FlexMock.use do |m|
240
+ m.should_receive(:hi).with(1).returns(10).at_most.once
241
+ m.hi(1)
242
+ m.hi(1)
243
+ end
244
+ end
245
+ end
246
+
247
+ def test_at_most_and_at_least_called_never
248
+ ex = assert_failure do
249
+ FlexMock.use do |m|
250
+ m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
251
+ end
252
+ end
253
+ end
254
+
255
+ def test_at_most_and_at_least_called_once
256
+ FlexMock.use do |m|
257
+ m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
258
+ m.hi(1)
259
+ end
260
+ end
261
+
262
+ def test_at_most_and_at_least_called_twice
263
+ FlexMock.use do |m|
264
+ m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
265
+ m.hi(1)
266
+ m.hi(1)
267
+ end
268
+ end
269
+
270
+ def test_at_most_and_at_least_called_three_times
271
+ ex = assert_failure do
272
+ FlexMock.use do |m|
273
+ m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
274
+ m.hi(1)
275
+ m.hi(1)
276
+ m.hi(1)
277
+ end
278
+ end
279
+ end
280
+
281
+ def test_call_counts_only_apply_to_matching_args
282
+ FlexMock.use do |m|
283
+ m.should_receive(:hi).with(1).once
284
+ m.should_receive(:hi).with(2).twice
285
+ m.should_receive(:hi).with(3)
286
+ m.hi(1)
287
+ m.hi(2)
288
+ m.hi(2)
289
+ 20.times { m.hi(3) }
290
+ end
291
+ end
292
+
293
+ def test_call_counts_only_apply_to_matching_args_with_mismatch
294
+ ex = assert_failure do
295
+ FlexMock.use do |m|
296
+ m.should_receive(:hi).with(1).once
297
+ m.should_receive(:hi).with(2).twice
298
+ m.should_receive(:hi).with(3)
299
+ m.should_receive(:lo)
300
+ m.hi(1)
301
+ m.hi(2)
302
+ m.lo
303
+ 20.times { m.hi(3) }
304
+ end
305
+ end
306
+ end
307
+
308
+ def test_ordered_calls_in_order
309
+ FlexMock.use 'm' do |m|
310
+ m.should_receive(:hi).ordered
311
+ m.should_receive(:lo).ordered
312
+
313
+ m.hi
314
+ m.lo
315
+ end
316
+ end
317
+
318
+ def test_ordered_calls_out_of_order
319
+ ex = assert_failure do
320
+ FlexMock.use 'm' do |m|
321
+ m.should_receive(:hi).ordered
322
+ m.should_receive(:lo).ordered
323
+
324
+ m.lo
325
+ m.hi
326
+ end
327
+ end
328
+ end
329
+
330
+ def test_order_calls_with_different_arg_lists_and_in_order
331
+ FlexMock.use 'm' do |m|
332
+ m.should_receive(:hi).with("one").ordered
333
+ m.should_receive(:hi).with("two").ordered
334
+
335
+ m.hi("one")
336
+ m.hi("two")
337
+ end
338
+ end
339
+
340
+ def test_order_calls_with_different_arg_lists_and_out_of_order
341
+ ex = assert_failure do
342
+ FlexMock.use 'm' do |m|
343
+ m.should_receive(:hi).with("one").ordered
344
+ m.should_receive(:hi).with("two").ordered
345
+
346
+ m.hi("two")
347
+ m.hi("one")
348
+ end
349
+ end
350
+ end
351
+
352
+ def test_unordered_calls_do_not_effect_ordered_testing
353
+ FlexMock.use 'm' do |m|
354
+ m.should_receive(:blah)
355
+ m.should_receive(:hi).ordered
356
+ m.should_receive(:lo).ordered
357
+
358
+ m.blah
359
+ m.hi
360
+ m.blah
361
+ m.lo
362
+ m.blah
363
+ end
364
+ end
365
+
366
+ def test_ordered_with_multiple_calls_is_ok
367
+ FlexMock.use 'm' do |m|
368
+ m.should_receive(:hi).ordered
369
+ m.should_receive(:lo).ordered
370
+
371
+ m.hi
372
+ m.hi
373
+ m.lo
374
+ m.lo
375
+ end
376
+ end
377
+
378
+ def test_explicit_ordering
379
+ FlexMock.use 'm' do |m|
380
+ m.should_receive(:start).ordered(2)
381
+ m.should_receive(:flip).ordered(10)
382
+ m.should_receive(:flop).ordered(10)
383
+ m.should_receive(:final).ordered
384
+
385
+ m.start
386
+ m.flop
387
+ m.flip
388
+ m.flop
389
+ m.final
390
+ end
391
+ end
392
+
393
+ def test_explicit_ordering_mixed_with_implicit_ordering_should_not_overlap
394
+ FlexMock.use 'm' do |m|
395
+ xstart = m.should_receive(:start).ordered
396
+ xmid = m.should_receive(:mid).ordered(2)
397
+ xend = m.should_receive(:end).ordered
398
+ assert xstart.order_number < xmid.order_number
399
+ assert xmid.order_number < xend.order_number
400
+ end
401
+ end
402
+
403
+ def test_explicit_ordering_with_overlap_with_start
404
+ ex = assert_failure do
405
+ FlexMock.use 'm' do |m|
406
+ xstart = m.should_receive(:start).ordered
407
+ xmid = m.should_receive(:mid).ordered(1)
408
+ end
409
+ end
410
+ end
411
+
412
+ def test_explicit_ordering_with_explicit_misorders
413
+ assert_failure do
414
+ FlexMock.use 'm' do |m|
415
+ m.should_receive(:hi).ordered(2)
416
+ m.should_receive(:lo).ordered(10)
417
+
418
+ m.lo
419
+ m.hi
420
+ end
421
+ end
422
+ end
423
+
424
+ def test_explicit_ordering_with_out_of_order_explicitness
425
+ assert_failure do
426
+ FlexMock.use 'm' do |m|
427
+ m.should_receive(:lo).ordered(10)
428
+ m.should_receive(:hi).ordered(2)
429
+ end
430
+ end
431
+ end
432
+
433
+ def test_expectation_formating
434
+ m = FlexMock.new("m")
435
+ exp = m.should_receive(:f).with(1,"two", /^3$/).and_return(0).at_least.once
436
+ assert_equal 'f(1, "two", /^3$/)', exp.to_s
437
+ end
438
+
439
+ def assert_failure
440
+ assert_raises(Test::Unit::AssertionFailedError) do yield end
441
+ end
442
+
443
+ end