betatest 0.0.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,22 @@
1
+ require 'betatest/autorun'
2
+
3
+ class TestBetatestFork < Betatest::Test
4
+ def test_explicit_exit_status_in_forked_process
5
+ @pid = Process.fork do
6
+ exit! 0
7
+ end
8
+
9
+ _, result = Process.wait2(@pid)
10
+
11
+ assert_equal 0, result.exitstatus
12
+ end
13
+
14
+ def test_exit_status_in_forked_process
15
+ @pid = Process.fork do
16
+ end
17
+
18
+ _, result = Process.wait2(@pid)
19
+
20
+ assert_equal 0, result.exitstatus
21
+ end
22
+ end
@@ -0,0 +1,500 @@
1
+ require 'betatest/autorun'
2
+
3
+ class TestBetatestMock < Betatest::Test
4
+ parallelize_me!
5
+
6
+ def setup
7
+ @mock = Betatest::Mock.new.expect(:foo, nil)
8
+ @mock.expect(:meaning_of_life, 42)
9
+ end
10
+
11
+ def test_create_stub_method
12
+ assert_nil @mock.foo
13
+ end
14
+
15
+ def test_allow_return_value_specification
16
+ assert_equal 42, @mock.meaning_of_life
17
+ end
18
+
19
+ def test_blow_up_if_not_called
20
+ @mock.foo
21
+
22
+ util_verify_bad "expected meaning_of_life() => 42, got []"
23
+ end
24
+
25
+ def test_not_blow_up_if_everything_called
26
+ @mock.foo
27
+ @mock.meaning_of_life
28
+
29
+ assert @mock.verify
30
+ end
31
+
32
+ def test_allow_expectations_to_be_added_after_creation
33
+ @mock.expect(:bar, true)
34
+ assert @mock.bar
35
+ end
36
+
37
+ def test_not_verify_if_new_expected_method_is_not_called
38
+ @mock.foo
39
+ @mock.meaning_of_life
40
+ @mock.expect(:bar, true)
41
+
42
+ util_verify_bad "expected bar() => true, got []"
43
+ end
44
+
45
+ def test_blow_up_on_wrong_number_of_arguments
46
+ @mock.foo
47
+ @mock.meaning_of_life
48
+ @mock.expect(:sum, 3, [1, 2])
49
+
50
+ e = assert_raises ArgumentError do
51
+ @mock.sum
52
+ end
53
+
54
+ assert_equal "mocked method :sum expects 2 arguments, got 0", e.message
55
+ end
56
+
57
+ def test_return_mock_does_not_raise
58
+ retval = Betatest::Mock.new
59
+ mock = Betatest::Mock.new
60
+ mock.expect(:foo, retval)
61
+ mock.foo
62
+
63
+ assert mock.verify
64
+ end
65
+
66
+ def test_mock_args_does_not_raise
67
+ skip "non-opaque use of ==" if maglev?
68
+
69
+ arg = Betatest::Mock.new
70
+ mock = Betatest::Mock.new
71
+ mock.expect(:foo, nil, [arg])
72
+ mock.foo(arg)
73
+
74
+ assert mock.verify
75
+ end
76
+
77
+ def test_set_expectation_on_special_methods
78
+ mock = Betatest::Mock.new
79
+
80
+ mock.expect :object_id, "received object_id"
81
+ assert_equal "received object_id", mock.object_id
82
+
83
+ mock.expect :respond_to_missing?, "received respond_to_missing?"
84
+ assert_equal "received respond_to_missing?", mock.respond_to_missing?
85
+
86
+ mock.expect :===, "received ==="
87
+ assert_equal "received ===", mock.===
88
+
89
+ mock.expect :inspect, "received inspect"
90
+ assert_equal "received inspect", mock.inspect
91
+
92
+ mock.expect :to_s, "received to_s"
93
+ assert_equal "received to_s", mock.to_s
94
+
95
+ mock.expect :public_send, "received public_send"
96
+ assert_equal "received public_send", mock.public_send
97
+
98
+ mock.expect :send, "received send"
99
+ assert_equal "received send", mock.send
100
+
101
+ assert mock.verify
102
+ end
103
+
104
+ def test_expectations_can_be_satisfied_via_send
105
+ @mock.send :foo
106
+ @mock.send :meaning_of_life
107
+
108
+ assert @mock.verify
109
+ end
110
+
111
+ def test_expectations_can_be_satisfied_via_public_send
112
+ skip "Doesn't run on 1.8" if RUBY_VERSION < "1.9"
113
+
114
+ @mock.public_send :foo
115
+ @mock.public_send :meaning_of_life
116
+
117
+ assert @mock.verify
118
+ end
119
+
120
+ def test_blow_up_on_wrong_arguments
121
+ @mock.foo
122
+ @mock.meaning_of_life
123
+ @mock.expect(:sum, 3, [1, 2])
124
+
125
+ e = assert_raises MockExpectationError do
126
+ @mock.sum(2, 4)
127
+ end
128
+
129
+ exp = "mocked method :sum called with unexpected arguments [2, 4]"
130
+ assert_equal exp, e.message
131
+ end
132
+
133
+ def test_expect_with_non_array_args
134
+ e = assert_raises ArgumentError do
135
+ @mock.expect :blah, 3, false
136
+ end
137
+
138
+ assert_equal "args must be an array", e.message
139
+ end
140
+
141
+ def test_respond_appropriately
142
+ assert @mock.respond_to?(:foo)
143
+ assert @mock.respond_to?(:foo, true)
144
+ assert @mock.respond_to?('foo')
145
+ assert !@mock.respond_to?(:bar)
146
+ end
147
+
148
+ def test_no_method_error_on_unexpected_methods
149
+ e = assert_raises NoMethodError do
150
+ @mock.bar
151
+ end
152
+
153
+ expected = "unmocked method :bar, expected one of [:foo, :meaning_of_life]"
154
+
155
+ assert_equal expected, e.message
156
+ end
157
+
158
+ def test_assign_per_mock_return_values
159
+ a = Betatest::Mock.new
160
+ b = Betatest::Mock.new
161
+
162
+ a.expect(:foo, :a)
163
+ b.expect(:foo, :b)
164
+
165
+ assert_equal :a, a.foo
166
+ assert_equal :b, b.foo
167
+ end
168
+
169
+ def test_do_not_create_stub_method_on_new_mocks
170
+ a = Betatest::Mock.new
171
+ a.expect(:foo, :a)
172
+
173
+ assert !Betatest::Mock.new.respond_to?(:foo)
174
+ end
175
+
176
+ def test_mock_is_a_blank_slate
177
+ @mock.expect :kind_of?, true, [Fixnum]
178
+ @mock.expect :==, true, [1]
179
+
180
+ assert @mock.kind_of?(Fixnum), "didn't mock :kind_of\?"
181
+ assert @mock == 1, "didn't mock :=="
182
+ end
183
+
184
+ def test_verify_allows_called_args_to_be_loosely_specified
185
+ mock = Betatest::Mock.new
186
+ mock.expect :loose_expectation, true, [Integer]
187
+ mock.loose_expectation 1
188
+
189
+ assert mock.verify
190
+ end
191
+
192
+ def test_verify_raises_with_strict_args
193
+ mock = Betatest::Mock.new
194
+ mock.expect :strict_expectation, true, [2]
195
+
196
+ e = assert_raises MockExpectationError do
197
+ mock.strict_expectation 1
198
+ end
199
+
200
+ exp = "mocked method :strict_expectation called with unexpected arguments [1]"
201
+ assert_equal exp, e.message
202
+ end
203
+
204
+ def test_method_missing_empty
205
+ mock = Betatest::Mock.new
206
+
207
+ mock.expect :a, nil
208
+
209
+ mock.a
210
+
211
+ e = assert_raises MockExpectationError do
212
+ mock.a
213
+ end
214
+
215
+ assert_equal "No more expects available for :a: []", e.message
216
+ end
217
+
218
+ def test_same_method_expects_are_verified_when_all_called
219
+ mock = Betatest::Mock.new
220
+ mock.expect :foo, nil, [:bar]
221
+ mock.expect :foo, nil, [:baz]
222
+
223
+ mock.foo :bar
224
+ mock.foo :baz
225
+
226
+ assert mock.verify
227
+ end
228
+
229
+ def test_same_method_expects_blow_up_when_not_all_called
230
+ mock = Betatest::Mock.new
231
+ mock.expect :foo, nil, [:bar]
232
+ mock.expect :foo, nil, [:baz]
233
+
234
+ mock.foo :bar
235
+
236
+ e = assert_raises(MockExpectationError) { mock.verify }
237
+
238
+ exp = "expected foo(:baz) => nil, got [foo(:bar) => nil]"
239
+
240
+ assert_equal exp, e.message
241
+ end
242
+
243
+ def test_verify_passes_when_mock_block_returns_true
244
+ mock = Betatest::Mock.new
245
+ mock.expect :foo, nil do
246
+ true
247
+ end
248
+
249
+ mock.foo
250
+
251
+ assert mock.verify
252
+ end
253
+
254
+ def test_mock_block_is_passed_function_params
255
+ arg1, arg2, arg3 = :bar, [1,2,3], {:a => 'a'}
256
+ mock = Betatest::Mock.new
257
+ mock.expect :foo, nil do |a1, a2, a3|
258
+ a1 == arg1 &&
259
+ a2 == arg2 &&
260
+ a3 == arg3
261
+ end
262
+
263
+ mock.foo arg1, arg2, arg3
264
+
265
+ assert mock.verify
266
+ end
267
+
268
+ def test_mock_block_is_passed_function_block
269
+ mock = Betatest::Mock.new
270
+ block = proc { 'bar' }
271
+ mock.expect :foo, nil do |arg, &blk|
272
+ arg == 'foo' &&
273
+ blk == block
274
+ end
275
+ mock.foo 'foo', &block
276
+ assert mock.verify
277
+ end
278
+
279
+ def test_verify_fails_when_mock_block_returns_false
280
+ mock = Betatest::Mock.new
281
+ mock.expect :foo, nil do
282
+ false
283
+ end
284
+
285
+ e = assert_raises(MockExpectationError) { mock.foo }
286
+ exp = "mocked method :foo failed block w/ []"
287
+
288
+ assert_equal exp, e.message
289
+ end
290
+
291
+ def test_mock_block_throws_if_args_passed
292
+ mock = Betatest::Mock.new
293
+
294
+ e = assert_raises(ArgumentError) do
295
+ mock.expect :foo, nil, [:a, :b, :c] do
296
+ true
297
+ end
298
+ end
299
+
300
+ exp = "args ignored when block given"
301
+
302
+ assert_equal exp, e.message
303
+ end
304
+
305
+ def test_mock_returns_retval_when_called_with_block
306
+ mock = Betatest::Mock.new
307
+ mock.expect(:foo, 32) do
308
+ true
309
+ end
310
+
311
+ rs = mock.foo
312
+
313
+ assert_equal rs, 32
314
+ end
315
+
316
+ def util_verify_bad exp
317
+ e = assert_raises MockExpectationError do
318
+ @mock.verify
319
+ end
320
+
321
+ assert_equal exp, e.message
322
+ end
323
+
324
+ def test_mock_called_via_send
325
+ mock = Betatest::Mock.new
326
+ mock.expect(:foo, true)
327
+
328
+ mock.send :foo
329
+ mock.verify
330
+ end
331
+
332
+ def test_mock_called_via___send__
333
+ mock = Betatest::Mock.new
334
+ mock.expect(:foo, true)
335
+
336
+ mock.__send__ :foo
337
+ mock.verify
338
+ end
339
+
340
+ def test_mock_called_via_send_with_args
341
+ mock = Betatest::Mock.new
342
+ mock.expect(:foo, true, [1,2,3])
343
+
344
+ mock.send(:foo, 1, 2, 3)
345
+ mock.verify
346
+ end
347
+
348
+ end
349
+
350
+ require_relative 'metametameta'
351
+
352
+ class TestBetatestStub < Betatest::Test
353
+ parallelize_me!
354
+
355
+ def setup
356
+ super
357
+ Betatest::Test.reset
358
+
359
+ @tc = Betatest::Test.new 'fake tc'
360
+ @assertion_count = 1
361
+ end
362
+
363
+ def teardown
364
+ super
365
+ assert_equal @assertion_count, @tc.assertions
366
+ end
367
+
368
+ class Time
369
+ def self.now
370
+ 24
371
+ end
372
+ end
373
+
374
+ def assert_stub val_or_callable
375
+ @assertion_count += 1
376
+
377
+ t = Time.now.to_i
378
+
379
+ Time.stub :now, val_or_callable do
380
+ @tc.assert_equal 42, Time.now
381
+ end
382
+
383
+ @tc.assert_operator Time.now.to_i, :>=, t
384
+ end
385
+
386
+ def test_stub_private_module_method
387
+ @assertion_count += 1
388
+
389
+ t0 = Time.now
390
+
391
+ self.stub :sleep, nil do
392
+ @tc.assert_nil sleep(10)
393
+ end
394
+
395
+ @tc.assert_operator Time.now - t0, :<=, 1
396
+ end
397
+
398
+ def test_stub_private_module_method_indirect
399
+ @assertion_count += 1
400
+
401
+ fail_clapper = Class.new do
402
+ def fail_clap
403
+ fail
404
+ :clap
405
+ end
406
+ end.new
407
+
408
+ fail_clapper.stub :fail, nil do |safe_clapper|
409
+ @tc.assert_equal :clap, safe_clapper.fail_clap # either form works
410
+ @tc.assert_equal :clap, fail_clapper.fail_clap # yay closures
411
+ end
412
+ end
413
+
414
+ def test_stub_public_module_method
415
+ Math.stub :log10, :stubbed do
416
+ @tc.assert_equal :stubbed, Math.log10(1000)
417
+ end
418
+ end
419
+
420
+ def test_stub_value
421
+ assert_stub 42
422
+ end
423
+
424
+ def test_stub_block
425
+ assert_stub lambda { 42 }
426
+ end
427
+
428
+ def test_stub_block_args
429
+ @assertion_count += 1
430
+
431
+ t = Time.now.to_i
432
+
433
+ Time.stub :now, lambda { |n| n * 2 } do
434
+ @tc.assert_equal 42, Time.now(21)
435
+ end
436
+
437
+ @tc.assert_operator Time.now.to_i, :>=, t
438
+ end
439
+
440
+ def test_stub_callable
441
+ obj = Object.new
442
+
443
+ def obj.call
444
+ 42
445
+ end
446
+
447
+ assert_stub obj
448
+ end
449
+
450
+ def test_stub_yield_self
451
+ obj = "foo"
452
+
453
+ val = obj.stub :to_s, "bar" do |s|
454
+ s.to_s
455
+ end
456
+
457
+ @tc.assert_equal "bar", val
458
+ end
459
+
460
+ def test_dynamic_method
461
+ @assertion_count = 2
462
+
463
+ dynamic = Class.new do
464
+ def self.respond_to?(meth)
465
+ meth == :found
466
+ end
467
+
468
+ def self.method_missing(meth, *args, &block)
469
+ if meth == :found
470
+ false
471
+ else
472
+ super
473
+ end
474
+ end
475
+ end
476
+
477
+ val = dynamic.stub(:found, true) do |s|
478
+ s.found
479
+ end
480
+
481
+ @tc.assert_equal true, val
482
+ @tc.assert_equal false, dynamic.found
483
+ end
484
+
485
+ def test_mock_with_yield
486
+ mock = Betatest::Mock.new
487
+ mock.expect(:write, true) do
488
+ true
489
+ end
490
+ rs = nil
491
+
492
+ File.stub(:open, true, mock) do
493
+ File.open("foo.txt", "r") do |f|
494
+ rs = f.write
495
+ end
496
+ end
497
+ @tc.assert_equal true, rs
498
+ end
499
+
500
+ end