minitest 5.11.3 → 5.14.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/History.rdoc +102 -3
- data/Manifest.txt +1 -0
- data/README.rdoc +61 -7
- data/Rakefile +4 -16
- data/lib/minitest/assertions.rb +140 -26
- data/lib/minitest/benchmark.rb +2 -2
- data/lib/minitest/expectations.rb +54 -35
- data/lib/minitest/mock.rb +5 -1
- data/lib/minitest/spec.rb +19 -8
- data/lib/minitest.rb +90 -21
- data/test/minitest/metametameta.rb +42 -8
- data/test/minitest/test_minitest_assertions.rb +1575 -0
- data/test/minitest/test_minitest_mock.rb +14 -4
- data/test/minitest/test_minitest_spec.rb +215 -141
- data/test/minitest/test_minitest_test.rb +38 -1096
- data.tar.gz.sig +0 -0
- metadata +33 -22
- metadata.gz.sig +0 -0
@@ -0,0 +1,1575 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require "minitest/autorun"
|
4
|
+
|
5
|
+
if defined? Encoding then
|
6
|
+
e = Encoding.default_external
|
7
|
+
if e != Encoding::UTF_8 then
|
8
|
+
warn ""
|
9
|
+
warn ""
|
10
|
+
warn "NOTE: External encoding #{e} is not UTF-8. Tests WILL fail."
|
11
|
+
warn " Run tests with `RUBYOPT=-Eutf-8 rake` to avoid errors."
|
12
|
+
warn ""
|
13
|
+
warn ""
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
SomeError = Class.new Exception
|
18
|
+
|
19
|
+
unless defined? MyModule then
|
20
|
+
module MyModule; end
|
21
|
+
class AnError < StandardError; include MyModule; end
|
22
|
+
end
|
23
|
+
|
24
|
+
class TestMinitestAssertions < Minitest::Test
|
25
|
+
# do not call parallelize_me! - teardown accesses @tc._assertions
|
26
|
+
# which is not threadsafe. Nearly every method in here is an
|
27
|
+
# assertion test so it isn't worth splitting it out further.
|
28
|
+
|
29
|
+
RUBY18 = !defined? Encoding
|
30
|
+
|
31
|
+
class DummyTest
|
32
|
+
include Minitest::Assertions
|
33
|
+
# include Minitest::Reportable # TODO: why do I really need this?
|
34
|
+
|
35
|
+
attr_accessor :assertions, :failure
|
36
|
+
|
37
|
+
def initialize
|
38
|
+
self.assertions = 0
|
39
|
+
self.failure = nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def setup
|
44
|
+
super
|
45
|
+
|
46
|
+
Minitest::Test.reset
|
47
|
+
|
48
|
+
@tc = DummyTest.new
|
49
|
+
@zomg = "zomg ponies!" # TODO: const
|
50
|
+
@assertion_count = 1
|
51
|
+
end
|
52
|
+
|
53
|
+
def teardown
|
54
|
+
assert_equal(@assertion_count, @tc.assertions,
|
55
|
+
"expected #{@assertion_count} assertions to be fired during the test, not #{@tc.assertions}")
|
56
|
+
end
|
57
|
+
|
58
|
+
def assert_deprecated name
|
59
|
+
dep = /DEPRECATED: #{name}. From #{__FILE__}:\d+(?::.*)?/
|
60
|
+
dep = "" if $-w.nil?
|
61
|
+
|
62
|
+
assert_output nil, dep do
|
63
|
+
yield
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def assert_triggered expected, klass = Minitest::Assertion
|
68
|
+
e = assert_raises klass do
|
69
|
+
yield
|
70
|
+
end
|
71
|
+
|
72
|
+
msg = e.message.sub(/(---Backtrace---).*/m, '\1')
|
73
|
+
msg.gsub!(/\(oid=[-0-9]+\)/, "(oid=N)")
|
74
|
+
msg.gsub!(/(\d\.\d{6})\d+/, '\1xxx') # normalize: ruby version, impl, platform
|
75
|
+
|
76
|
+
assert_msg = Regexp === expected ? :assert_match : :assert_equal
|
77
|
+
self.send assert_msg, expected, msg
|
78
|
+
end
|
79
|
+
|
80
|
+
def assert_unexpected expected
|
81
|
+
expected = Regexp.new expected if String === expected
|
82
|
+
|
83
|
+
assert_triggered expected, Minitest::UnexpectedError do
|
84
|
+
yield
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def clean s
|
89
|
+
s.gsub(/^ {6,10}/, "")
|
90
|
+
end
|
91
|
+
|
92
|
+
def non_verbose
|
93
|
+
orig_verbose = $VERBOSE
|
94
|
+
$VERBOSE = false
|
95
|
+
|
96
|
+
yield
|
97
|
+
ensure
|
98
|
+
$VERBOSE = orig_verbose
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_assert
|
102
|
+
@assertion_count = 2
|
103
|
+
|
104
|
+
@tc.assert_equal true, @tc.assert(true), "returns true on success"
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_assert__triggered
|
108
|
+
assert_triggered "Expected false to be truthy." do
|
109
|
+
@tc.assert false
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_assert__triggered_message
|
114
|
+
assert_triggered @zomg do
|
115
|
+
@tc.assert false, @zomg
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_assert__triggered_lambda
|
120
|
+
assert_triggered "whoops" do
|
121
|
+
@tc.assert false, lambda { "whoops" }
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_assert_empty
|
126
|
+
@assertion_count = 2
|
127
|
+
|
128
|
+
@tc.assert_empty []
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_assert_empty_triggered
|
132
|
+
@assertion_count = 2
|
133
|
+
|
134
|
+
assert_triggered "Expected [1] to be empty." do
|
135
|
+
@tc.assert_empty [1]
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_assert_equal
|
140
|
+
@tc.assert_equal 1, 1
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_assert_equal_different_collection_array_hex_invisible
|
144
|
+
object1 = Object.new
|
145
|
+
object2 = Object.new
|
146
|
+
msg = "No visible difference in the Array#inspect output.
|
147
|
+
You should look at the implementation of #== on Array or its members.
|
148
|
+
[#<Object:0xXXXXXX>]".gsub(/^ +/, "")
|
149
|
+
assert_triggered msg do
|
150
|
+
@tc.assert_equal [object1], [object2]
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_assert_equal_different_collection_hash_hex_invisible
|
155
|
+
h1, h2 = {}, {}
|
156
|
+
h1[1] = Object.new
|
157
|
+
h2[1] = Object.new
|
158
|
+
msg = "No visible difference in the Hash#inspect output.
|
159
|
+
You should look at the implementation of #== on Hash or its members.
|
160
|
+
{1=>#<Object:0xXXXXXX>}".gsub(/^ +/, "")
|
161
|
+
|
162
|
+
assert_triggered msg do
|
163
|
+
@tc.assert_equal h1, h2
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_assert_equal_different_diff_deactivated
|
168
|
+
without_diff do
|
169
|
+
assert_triggered util_msg("haha" * 10, "blah" * 10) do
|
170
|
+
o1 = "haha" * 10
|
171
|
+
o2 = "blah" * 10
|
172
|
+
|
173
|
+
@tc.assert_equal o1, o2
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_assert_equal_different_message
|
179
|
+
assert_triggered "whoops.\nExpected: 1\n Actual: 2" do
|
180
|
+
@tc.assert_equal 1, 2, message { "whoops" }
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_assert_equal_different_lambda
|
185
|
+
assert_triggered "whoops.\nExpected: 1\n Actual: 2" do
|
186
|
+
@tc.assert_equal 1, 2, lambda { "whoops" }
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_assert_equal_different_hex
|
191
|
+
c = Class.new do
|
192
|
+
def initialize s; @name = s; end
|
193
|
+
end
|
194
|
+
|
195
|
+
o1 = c.new "a"
|
196
|
+
o2 = c.new "b"
|
197
|
+
msg = clean <<-EOS
|
198
|
+
--- expected
|
199
|
+
+++ actual
|
200
|
+
@@ -1 +1 @@
|
201
|
+
-#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"a\">
|
202
|
+
+#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"b\">
|
203
|
+
EOS
|
204
|
+
|
205
|
+
assert_triggered msg do
|
206
|
+
@tc.assert_equal o1, o2
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
def test_assert_equal_different_hex_invisible
|
211
|
+
o1 = Object.new
|
212
|
+
o2 = Object.new
|
213
|
+
|
214
|
+
msg = "No visible difference in the Object#inspect output.
|
215
|
+
You should look at the implementation of #== on Object or its members.
|
216
|
+
#<Object:0xXXXXXX>".gsub(/^ +/, "")
|
217
|
+
|
218
|
+
assert_triggered msg do
|
219
|
+
@tc.assert_equal o1, o2
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
def test_assert_equal_different_long
|
224
|
+
msg = "--- expected
|
225
|
+
+++ actual
|
226
|
+
@@ -1 +1 @@
|
227
|
+
-\"hahahahahahahahahahahahahahahahahahahaha\"
|
228
|
+
+\"blahblahblahblahblahblahblahblahblahblah\"
|
229
|
+
".gsub(/^ +/, "")
|
230
|
+
|
231
|
+
assert_triggered msg do
|
232
|
+
o1 = "haha" * 10
|
233
|
+
o2 = "blah" * 10
|
234
|
+
|
235
|
+
@tc.assert_equal o1, o2
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
def test_assert_equal_different_long_invisible
|
240
|
+
msg = "No visible difference in the String#inspect output.
|
241
|
+
You should look at the implementation of #== on String or its members.
|
242
|
+
\"blahblahblahblahblahblahblahblahblahblah\"".gsub(/^ +/, "")
|
243
|
+
|
244
|
+
assert_triggered msg do
|
245
|
+
o1 = "blah" * 10
|
246
|
+
o2 = "blah" * 10
|
247
|
+
def o1.== _
|
248
|
+
false
|
249
|
+
end
|
250
|
+
@tc.assert_equal o1, o2
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
def test_assert_equal_different_long_msg
|
255
|
+
msg = "message.
|
256
|
+
--- expected
|
257
|
+
+++ actual
|
258
|
+
@@ -1 +1 @@
|
259
|
+
-\"hahahahahahahahahahahahahahahahahahahaha\"
|
260
|
+
+\"blahblahblahblahblahblahblahblahblahblah\"
|
261
|
+
".gsub(/^ +/, "")
|
262
|
+
|
263
|
+
assert_triggered msg do
|
264
|
+
o1 = "haha" * 10
|
265
|
+
o2 = "blah" * 10
|
266
|
+
@tc.assert_equal o1, o2, "message"
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_assert_equal_different_short
|
271
|
+
assert_triggered util_msg(1, 2) do
|
272
|
+
@tc.assert_equal 1, 2
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
def test_assert_equal_different_short_msg
|
277
|
+
assert_triggered util_msg(1, 2, "message") do
|
278
|
+
@tc.assert_equal 1, 2, "message"
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
def test_assert_equal_different_short_multiline
|
283
|
+
msg = "--- expected\n+++ actual\n@@ -1,2 +1,2 @@\n \"a\n-b\"\n+c\"\n"
|
284
|
+
assert_triggered msg do
|
285
|
+
@tc.assert_equal "a\nb", "a\nc"
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
def test_assert_equal_does_not_allow_lhs_nil
|
290
|
+
if Minitest::VERSION =~ /^6/ then
|
291
|
+
warn "Time to strip the MT5 test"
|
292
|
+
|
293
|
+
@assertion_count += 1
|
294
|
+
assert_triggered(/Use assert_nil if expecting nil/) do
|
295
|
+
@tc.assert_equal nil, nil
|
296
|
+
end
|
297
|
+
else
|
298
|
+
err_re = /Use assert_nil if expecting nil from .*test_minitest_\w+.rb/
|
299
|
+
err_re = "" if $-w.nil?
|
300
|
+
|
301
|
+
assert_output "", err_re do
|
302
|
+
@tc.assert_equal nil, nil
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
def test_assert_equal_does_not_allow_lhs_nil_triggered
|
308
|
+
assert_triggered "Expected: nil\n Actual: false" do
|
309
|
+
@tc.assert_equal nil, false
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
def test_assert_equal_string_bug791
|
314
|
+
exp = <<-'EOF'.gsub(/^ {10}/, "") # note single quotes
|
315
|
+
--- expected
|
316
|
+
+++ actual
|
317
|
+
@@ -1,2 +1 @@
|
318
|
+
-"\\n
|
319
|
+
-"
|
320
|
+
+"\\\"
|
321
|
+
EOF
|
322
|
+
|
323
|
+
exp = "Expected: \"\\\\n\"\n Actual: \"\\\\\""
|
324
|
+
assert_triggered exp do
|
325
|
+
@tc.assert_equal "\\n", "\\"
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
def test_assert_equal_string_both_escaped_unescaped_newlines
|
330
|
+
msg = <<-EOM.gsub(/^ {10}/, "")
|
331
|
+
--- expected
|
332
|
+
+++ actual
|
333
|
+
@@ -1,2 +1 @@
|
334
|
+
-\"A\\n
|
335
|
+
-B\"
|
336
|
+
+\"A\\n\\\\nB\"
|
337
|
+
EOM
|
338
|
+
|
339
|
+
assert_triggered msg do
|
340
|
+
exp = "A\\nB"
|
341
|
+
act = "A\n\\nB"
|
342
|
+
|
343
|
+
@tc.assert_equal exp, act
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
def test_assert_equal_string_encodings
|
348
|
+
msg = <<-EOM.gsub(/^ {10}/, "")
|
349
|
+
--- expected
|
350
|
+
+++ actual
|
351
|
+
@@ -1,3 +1,3 @@
|
352
|
+
-# encoding: UTF-8
|
353
|
+
-# valid: false
|
354
|
+
+# encoding: ASCII-8BIT
|
355
|
+
+# valid: true
|
356
|
+
"bad-utf8-\\xF1.txt"
|
357
|
+
EOM
|
358
|
+
|
359
|
+
assert_triggered msg do
|
360
|
+
x = "bad-utf8-\xF1.txt"
|
361
|
+
y = x.dup.force_encoding "binary" # TODO: switch to .b when 1.9 dropped
|
362
|
+
@tc.assert_equal x, y
|
363
|
+
end
|
364
|
+
end unless RUBY18
|
365
|
+
|
366
|
+
def test_assert_equal_string_encodings_both_different
|
367
|
+
msg = <<-EOM.gsub(/^ {10}/, "")
|
368
|
+
--- expected
|
369
|
+
+++ actual
|
370
|
+
@@ -1,3 +1,3 @@
|
371
|
+
-# encoding: US-ASCII
|
372
|
+
-# valid: false
|
373
|
+
+# encoding: ASCII-8BIT
|
374
|
+
+# valid: true
|
375
|
+
"bad-utf8-\\xF1.txt"
|
376
|
+
EOM
|
377
|
+
|
378
|
+
assert_triggered msg do
|
379
|
+
x = "bad-utf8-\xF1.txt".force_encoding "ASCII"
|
380
|
+
y = x.dup.force_encoding "binary" # TODO: switch to .b when 1.9 dropped
|
381
|
+
@tc.assert_equal x, y
|
382
|
+
end
|
383
|
+
end unless RUBY18
|
384
|
+
|
385
|
+
def test_assert_equal_unescape_newlines
|
386
|
+
msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
|
387
|
+
--- expected
|
388
|
+
+++ actual
|
389
|
+
@@ -1,2 +1,2 @@
|
390
|
+
-"hello
|
391
|
+
+"hello\n
|
392
|
+
world"
|
393
|
+
EOM
|
394
|
+
|
395
|
+
assert_triggered msg do
|
396
|
+
exp = "hello\nworld"
|
397
|
+
act = 'hello\nworld' # notice single quotes
|
398
|
+
|
399
|
+
@tc.assert_equal exp, act
|
400
|
+
end
|
401
|
+
end
|
402
|
+
|
403
|
+
def test_assert_in_delta
|
404
|
+
@tc.assert_in_delta 0.0, 1.0 / 1000, 0.1
|
405
|
+
end
|
406
|
+
|
407
|
+
def test_assert_in_delta_triggered
|
408
|
+
x = "1.0e-06"
|
409
|
+
assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= #{x}." do
|
410
|
+
@tc.assert_in_delta 0.0, 1.0 / 1000, 0.000001
|
411
|
+
end
|
412
|
+
end
|
413
|
+
|
414
|
+
def test_assert_in_epsilon
|
415
|
+
@assertion_count = 10
|
416
|
+
|
417
|
+
@tc.assert_in_epsilon 10_000, 9991
|
418
|
+
@tc.assert_in_epsilon 9991, 10_000
|
419
|
+
@tc.assert_in_epsilon 1.0, 1.001
|
420
|
+
@tc.assert_in_epsilon 1.001, 1.0
|
421
|
+
|
422
|
+
@tc.assert_in_epsilon 10_000, 9999.1, 0.0001
|
423
|
+
@tc.assert_in_epsilon 9999.1, 10_000, 0.0001
|
424
|
+
@tc.assert_in_epsilon 1.0, 1.0001, 0.0001
|
425
|
+
@tc.assert_in_epsilon 1.0001, 1.0, 0.0001
|
426
|
+
|
427
|
+
@tc.assert_in_epsilon(-1, -1)
|
428
|
+
@tc.assert_in_epsilon(-10_000, -9991)
|
429
|
+
end
|
430
|
+
|
431
|
+
def test_assert_in_epsilon_triggered
|
432
|
+
assert_triggered "Expected |10000 - 9990| (10) to be <= 9.99." do
|
433
|
+
@tc.assert_in_epsilon 10_000, 9990
|
434
|
+
end
|
435
|
+
end
|
436
|
+
|
437
|
+
def test_assert_in_epsilon_triggered_negative_case
|
438
|
+
x = (RUBY18 and not maglev?) ? "0.1" : "0.100000xxx"
|
439
|
+
y = "0.1"
|
440
|
+
assert_triggered "Expected |-1.1 - -1| (#{x}) to be <= #{y}." do
|
441
|
+
@tc.assert_in_epsilon(-1.1, -1, 0.1)
|
442
|
+
end
|
443
|
+
end
|
444
|
+
|
445
|
+
def test_assert_includes
|
446
|
+
@assertion_count = 2
|
447
|
+
|
448
|
+
@tc.assert_includes [true], true
|
449
|
+
end
|
450
|
+
|
451
|
+
def test_assert_includes_triggered
|
452
|
+
@assertion_count = 3
|
453
|
+
|
454
|
+
e = @tc.assert_raises Minitest::Assertion do
|
455
|
+
@tc.assert_includes [true], false
|
456
|
+
end
|
457
|
+
|
458
|
+
expected = "Expected [true] to include false."
|
459
|
+
assert_equal expected, e.message
|
460
|
+
end
|
461
|
+
|
462
|
+
def test_assert_instance_of
|
463
|
+
@tc.assert_instance_of String, "blah"
|
464
|
+
end
|
465
|
+
|
466
|
+
def test_assert_instance_of_triggered
|
467
|
+
assert_triggered 'Expected "blah" to be an instance of Array, not String.' do
|
468
|
+
@tc.assert_instance_of Array, "blah"
|
469
|
+
end
|
470
|
+
end
|
471
|
+
|
472
|
+
def test_assert_kind_of
|
473
|
+
@tc.assert_kind_of String, "blah"
|
474
|
+
end
|
475
|
+
|
476
|
+
def test_assert_kind_of_triggered
|
477
|
+
assert_triggered 'Expected "blah" to be a kind of Array, not String.' do
|
478
|
+
@tc.assert_kind_of Array, "blah"
|
479
|
+
end
|
480
|
+
end
|
481
|
+
|
482
|
+
def test_assert_match
|
483
|
+
@assertion_count = 2
|
484
|
+
@tc.assert_match(/\w+/, "blah blah blah")
|
485
|
+
end
|
486
|
+
|
487
|
+
def test_assert_match_matchee_to_str
|
488
|
+
@assertion_count = 2
|
489
|
+
|
490
|
+
obj = Object.new
|
491
|
+
def obj.to_str; "blah" end
|
492
|
+
|
493
|
+
@tc.assert_match "blah", obj
|
494
|
+
end
|
495
|
+
|
496
|
+
def test_assert_match_matcher_object
|
497
|
+
@assertion_count = 2
|
498
|
+
|
499
|
+
pattern = Object.new
|
500
|
+
def pattern.=~ _; true end
|
501
|
+
|
502
|
+
@tc.assert_match pattern, 5
|
503
|
+
end
|
504
|
+
|
505
|
+
def test_assert_match_object_triggered
|
506
|
+
@assertion_count = 2
|
507
|
+
|
508
|
+
pattern = Object.new
|
509
|
+
def pattern.=~ _; false end
|
510
|
+
def pattern.inspect; "[Object]" end
|
511
|
+
|
512
|
+
assert_triggered "Expected [Object] to match 5." do
|
513
|
+
@tc.assert_match pattern, 5
|
514
|
+
end
|
515
|
+
end
|
516
|
+
|
517
|
+
def test_assert_match_triggered
|
518
|
+
@assertion_count = 2
|
519
|
+
assert_triggered 'Expected /\d+/ to match "blah blah blah".' do
|
520
|
+
@tc.assert_match(/\d+/, "blah blah blah")
|
521
|
+
end
|
522
|
+
end
|
523
|
+
|
524
|
+
def test_assert_nil
|
525
|
+
@tc.assert_nil nil
|
526
|
+
end
|
527
|
+
|
528
|
+
def test_assert_nil_triggered
|
529
|
+
assert_triggered "Expected 42 to be nil." do
|
530
|
+
@tc.assert_nil 42
|
531
|
+
end
|
532
|
+
end
|
533
|
+
|
534
|
+
def test_assert_operator
|
535
|
+
@tc.assert_operator 2, :>, 1
|
536
|
+
end
|
537
|
+
|
538
|
+
def test_assert_operator_bad_object
|
539
|
+
bad = Object.new
|
540
|
+
def bad.== _; true end
|
541
|
+
|
542
|
+
@tc.assert_operator bad, :equal?, bad
|
543
|
+
end
|
544
|
+
|
545
|
+
def test_assert_operator_triggered
|
546
|
+
assert_triggered "Expected 2 to be < 1." do
|
547
|
+
@tc.assert_operator 2, :<, 1
|
548
|
+
end
|
549
|
+
end
|
550
|
+
|
551
|
+
def test_assert_output_both
|
552
|
+
@assertion_count = 2
|
553
|
+
|
554
|
+
@tc.assert_output "yay", "blah" do
|
555
|
+
print "yay"
|
556
|
+
$stderr.print "blah"
|
557
|
+
end
|
558
|
+
end
|
559
|
+
|
560
|
+
def test_assert_output_both_regexps
|
561
|
+
@assertion_count = 4
|
562
|
+
|
563
|
+
@tc.assert_output(/y.y/, /bl.h/) do
|
564
|
+
print "yay"
|
565
|
+
$stderr.print "blah"
|
566
|
+
end
|
567
|
+
end
|
568
|
+
|
569
|
+
def test_assert_output_err
|
570
|
+
@tc.assert_output nil, "blah" do
|
571
|
+
$stderr.print "blah"
|
572
|
+
end
|
573
|
+
end
|
574
|
+
|
575
|
+
def test_assert_output_neither
|
576
|
+
@assertion_count = 0
|
577
|
+
|
578
|
+
@tc.assert_output do
|
579
|
+
# do nothing
|
580
|
+
end
|
581
|
+
end
|
582
|
+
|
583
|
+
def test_assert_output_out
|
584
|
+
@tc.assert_output "blah" do
|
585
|
+
print "blah"
|
586
|
+
end
|
587
|
+
end
|
588
|
+
|
589
|
+
def test_assert_output_triggered_both
|
590
|
+
assert_triggered util_msg("blah", "blah blah", "In stderr") do
|
591
|
+
@tc.assert_output "yay", "blah" do
|
592
|
+
print "boo"
|
593
|
+
$stderr.print "blah blah"
|
594
|
+
end
|
595
|
+
end
|
596
|
+
end
|
597
|
+
|
598
|
+
def test_assert_output_triggered_err
|
599
|
+
assert_triggered util_msg("blah", "blah blah", "In stderr") do
|
600
|
+
@tc.assert_output nil, "blah" do
|
601
|
+
$stderr.print "blah blah"
|
602
|
+
end
|
603
|
+
end
|
604
|
+
end
|
605
|
+
|
606
|
+
def test_assert_output_triggered_out
|
607
|
+
assert_triggered util_msg("blah", "blah blah", "In stdout") do
|
608
|
+
@tc.assert_output "blah" do
|
609
|
+
print "blah blah"
|
610
|
+
end
|
611
|
+
end
|
612
|
+
end
|
613
|
+
|
614
|
+
def test_assert_output_no_block
|
615
|
+
assert_triggered "assert_output requires a block to capture output." do
|
616
|
+
@tc.assert_output "blah"
|
617
|
+
end
|
618
|
+
end
|
619
|
+
|
620
|
+
def test_assert_output_nested_assert_uncaught
|
621
|
+
@assertion_count = 1
|
622
|
+
|
623
|
+
assert_triggered "Epic Fail!" do
|
624
|
+
@tc.assert_output "blah\n" do
|
625
|
+
puts "blah"
|
626
|
+
@tc.flunk
|
627
|
+
end
|
628
|
+
end
|
629
|
+
end
|
630
|
+
|
631
|
+
def test_assert_output_nested_raise
|
632
|
+
@assertion_count = 2
|
633
|
+
|
634
|
+
@tc.assert_output "blah\n" do
|
635
|
+
@tc.assert_raises RuntimeError do
|
636
|
+
puts "blah"
|
637
|
+
raise "boom!"
|
638
|
+
end
|
639
|
+
end
|
640
|
+
end
|
641
|
+
|
642
|
+
def test_assert_output_nested_raise_bad
|
643
|
+
@assertion_count = 0
|
644
|
+
|
645
|
+
assert_unexpected "boom!" do
|
646
|
+
@tc.assert_raises do # 2) bypassed via UnexpectedError
|
647
|
+
@tc.assert_output "blah\n" do # 1) captures and raises UnexpectedError
|
648
|
+
puts "not_blah"
|
649
|
+
raise "boom!"
|
650
|
+
end
|
651
|
+
end
|
652
|
+
end
|
653
|
+
end
|
654
|
+
|
655
|
+
def test_assert_output_nested_raise_mismatch
|
656
|
+
# this test is redundant, but illustrative
|
657
|
+
@assertion_count = 0
|
658
|
+
|
659
|
+
assert_unexpected "boom!" do
|
660
|
+
@tc.assert_raises RuntimeError do # 2) bypassed via UnexpectedError
|
661
|
+
@tc.assert_output "blah\n" do # 1) captures and raises UnexpectedError
|
662
|
+
puts "not_blah"
|
663
|
+
raise ArgumentError, "boom!"
|
664
|
+
end
|
665
|
+
end
|
666
|
+
end
|
667
|
+
end
|
668
|
+
|
669
|
+
def test_assert_output_nested_throw_caught
|
670
|
+
@assertion_count = 2
|
671
|
+
|
672
|
+
@tc.assert_output "blah\n" do
|
673
|
+
@tc.assert_throws :boom! do
|
674
|
+
puts "blah"
|
675
|
+
throw :boom!
|
676
|
+
end
|
677
|
+
end
|
678
|
+
end
|
679
|
+
|
680
|
+
def test_assert_output_nested_throw_caught_bad
|
681
|
+
@assertion_count = 1 # want 0; can't prevent throw from escaping :(
|
682
|
+
|
683
|
+
@tc.assert_throws :boom! do # 2) captured via catch
|
684
|
+
@tc.assert_output "blah\n" do # 1) bypassed via throw
|
685
|
+
puts "not_blah"
|
686
|
+
throw :boom!
|
687
|
+
end
|
688
|
+
end
|
689
|
+
end
|
690
|
+
|
691
|
+
def test_assert_output_nested_throw_mismatch
|
692
|
+
@assertion_count = 0
|
693
|
+
|
694
|
+
assert_unexpected "uncaught throw :boom!" do
|
695
|
+
@tc.assert_throws :not_boom! do # 2) captured via assert_throws+rescue
|
696
|
+
@tc.assert_output "blah\n" do # 1) bypassed via throw
|
697
|
+
puts "not_blah"
|
698
|
+
throw :boom!
|
699
|
+
end
|
700
|
+
end
|
701
|
+
end
|
702
|
+
end
|
703
|
+
|
704
|
+
def test_assert_output_uncaught_raise
|
705
|
+
@assertion_count = 0
|
706
|
+
|
707
|
+
assert_unexpected "RuntimeError: boom!" do
|
708
|
+
@tc.assert_output "blah\n" do
|
709
|
+
puts "not_blah"
|
710
|
+
raise "boom!"
|
711
|
+
end
|
712
|
+
end
|
713
|
+
end
|
714
|
+
|
715
|
+
def test_assert_output_uncaught_throw
|
716
|
+
@assertion_count = 0
|
717
|
+
|
718
|
+
assert_unexpected "uncaught throw :boom!" do
|
719
|
+
@tc.assert_output "blah\n" do
|
720
|
+
puts "not_blah"
|
721
|
+
throw :boom!
|
722
|
+
end
|
723
|
+
end
|
724
|
+
end
|
725
|
+
def test_assert_predicate
|
726
|
+
@tc.assert_predicate "", :empty?
|
727
|
+
end
|
728
|
+
|
729
|
+
def test_assert_predicate_triggered
|
730
|
+
assert_triggered 'Expected "blah" to be empty?.' do
|
731
|
+
@tc.assert_predicate "blah", :empty?
|
732
|
+
end
|
733
|
+
end
|
734
|
+
|
735
|
+
def test_assert_raises
|
736
|
+
@tc.assert_raises RuntimeError do
|
737
|
+
raise "blah"
|
738
|
+
end
|
739
|
+
end
|
740
|
+
|
741
|
+
def test_assert_raises_default
|
742
|
+
@tc.assert_raises do
|
743
|
+
raise StandardError, "blah"
|
744
|
+
end
|
745
|
+
end
|
746
|
+
|
747
|
+
def test_assert_raises_default_triggered
|
748
|
+
e = assert_raises Minitest::Assertion do
|
749
|
+
@tc.assert_raises do
|
750
|
+
raise SomeError, "blah"
|
751
|
+
end
|
752
|
+
end
|
753
|
+
|
754
|
+
expected = clean <<-EOM.chomp
|
755
|
+
[StandardError] exception expected, not
|
756
|
+
Class: <SomeError>
|
757
|
+
Message: <\"blah\">
|
758
|
+
---Backtrace---
|
759
|
+
FILE:LINE:in \`test_assert_raises_default_triggered\'
|
760
|
+
---------------
|
761
|
+
EOM
|
762
|
+
|
763
|
+
actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
|
764
|
+
actual.gsub!(/block \(\d+ levels\) in /, "") if RUBY_VERSION >= "1.9.0"
|
765
|
+
|
766
|
+
assert_equal expected, actual
|
767
|
+
end
|
768
|
+
|
769
|
+
def test_assert_raises_exit
|
770
|
+
@tc.assert_raises SystemExit do
|
771
|
+
exit 1
|
772
|
+
end
|
773
|
+
end
|
774
|
+
|
775
|
+
def test_assert_raises_module
|
776
|
+
@tc.assert_raises MyModule do
|
777
|
+
raise AnError
|
778
|
+
end
|
779
|
+
end
|
780
|
+
|
781
|
+
def test_assert_raises_signals
|
782
|
+
@tc.assert_raises SignalException do
|
783
|
+
raise SignalException, :INT
|
784
|
+
end
|
785
|
+
end
|
786
|
+
|
787
|
+
def test_assert_raises_throw_nested_bad
|
788
|
+
@assertion_count = 0
|
789
|
+
|
790
|
+
assert_unexpected "RuntimeError: boom!" do
|
791
|
+
@tc.assert_raises do
|
792
|
+
@tc.assert_throws :blah do
|
793
|
+
raise "boom!"
|
794
|
+
throw :not_blah
|
795
|
+
end
|
796
|
+
end
|
797
|
+
end
|
798
|
+
end
|
799
|
+
|
800
|
+
##
|
801
|
+
# *sigh* This is quite an odd scenario, but it is from real (albeit
|
802
|
+
# ugly) test code in ruby-core:
|
803
|
+
|
804
|
+
# http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29259
|
805
|
+
|
806
|
+
def test_assert_raises_skip
|
807
|
+
@assertion_count = 0
|
808
|
+
|
809
|
+
assert_triggered "skipped", Minitest::Skip do
|
810
|
+
@tc.assert_raises ArgumentError do
|
811
|
+
begin
|
812
|
+
raise "blah"
|
813
|
+
rescue
|
814
|
+
skip "skipped"
|
815
|
+
end
|
816
|
+
end
|
817
|
+
end
|
818
|
+
end
|
819
|
+
|
820
|
+
def test_assert_raises_subclass
|
821
|
+
@tc.assert_raises StandardError do
|
822
|
+
raise AnError
|
823
|
+
end
|
824
|
+
end
|
825
|
+
|
826
|
+
def test_assert_raises_subclass_triggered
|
827
|
+
e = assert_raises Minitest::Assertion do
|
828
|
+
@tc.assert_raises SomeError do
|
829
|
+
raise AnError, "some message"
|
830
|
+
end
|
831
|
+
end
|
832
|
+
|
833
|
+
expected = clean <<-EOM
|
834
|
+
[SomeError] exception expected, not
|
835
|
+
Class: <AnError>
|
836
|
+
Message: <\"some message\">
|
837
|
+
---Backtrace---
|
838
|
+
FILE:LINE:in \`test_assert_raises_subclass_triggered\'
|
839
|
+
---------------
|
840
|
+
EOM
|
841
|
+
|
842
|
+
actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
|
843
|
+
actual.gsub!(/block \(\d+ levels\) in /, "") if RUBY_VERSION >= "1.9.0"
|
844
|
+
|
845
|
+
assert_equal expected.chomp, actual
|
846
|
+
end
|
847
|
+
|
848
|
+
def test_assert_raises_triggered_different
|
849
|
+
e = assert_raises Minitest::Assertion do
|
850
|
+
@tc.assert_raises RuntimeError do
|
851
|
+
raise SyntaxError, "icky"
|
852
|
+
end
|
853
|
+
end
|
854
|
+
|
855
|
+
expected = clean <<-EOM.chomp
|
856
|
+
[RuntimeError] exception expected, not
|
857
|
+
Class: <SyntaxError>
|
858
|
+
Message: <\"icky\">
|
859
|
+
---Backtrace---
|
860
|
+
FILE:LINE:in \`test_assert_raises_triggered_different\'
|
861
|
+
---------------
|
862
|
+
EOM
|
863
|
+
|
864
|
+
actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
|
865
|
+
actual.gsub!(/block \(\d+ levels\) in /, "") if RUBY_VERSION >= "1.9.0"
|
866
|
+
|
867
|
+
assert_equal expected, actual
|
868
|
+
end
|
869
|
+
|
870
|
+
def test_assert_raises_triggered_different_msg
|
871
|
+
e = assert_raises Minitest::Assertion do
|
872
|
+
@tc.assert_raises RuntimeError, "XXX" do
|
873
|
+
raise SyntaxError, "icky"
|
874
|
+
end
|
875
|
+
end
|
876
|
+
|
877
|
+
expected = clean <<-EOM
|
878
|
+
XXX.
|
879
|
+
[RuntimeError] exception expected, not
|
880
|
+
Class: <SyntaxError>
|
881
|
+
Message: <\"icky\">
|
882
|
+
---Backtrace---
|
883
|
+
FILE:LINE:in \`test_assert_raises_triggered_different_msg\'
|
884
|
+
---------------
|
885
|
+
EOM
|
886
|
+
|
887
|
+
actual = e.message.gsub(/^.+:\d+/, "FILE:LINE")
|
888
|
+
actual.gsub!(/block \(\d+ levels\) in /, "") if RUBY_VERSION >= "1.9.0"
|
889
|
+
|
890
|
+
assert_equal expected.chomp, actual
|
891
|
+
end
|
892
|
+
|
893
|
+
def test_assert_raises_triggered_none
|
894
|
+
e = assert_raises Minitest::Assertion do
|
895
|
+
@tc.assert_raises Minitest::Assertion do
|
896
|
+
# do nothing
|
897
|
+
end
|
898
|
+
end
|
899
|
+
|
900
|
+
expected = "Minitest::Assertion expected but nothing was raised."
|
901
|
+
|
902
|
+
assert_equal expected, e.message
|
903
|
+
end
|
904
|
+
|
905
|
+
def test_assert_raises_triggered_none_msg
|
906
|
+
e = assert_raises Minitest::Assertion do
|
907
|
+
@tc.assert_raises Minitest::Assertion, "XXX" do
|
908
|
+
# do nothing
|
909
|
+
end
|
910
|
+
end
|
911
|
+
|
912
|
+
expected = "XXX.\nMinitest::Assertion expected but nothing was raised."
|
913
|
+
|
914
|
+
assert_equal expected, e.message
|
915
|
+
end
|
916
|
+
|
917
|
+
def test_assert_raises_without_block
|
918
|
+
assert_triggered "assert_raises requires a block to capture errors." do
|
919
|
+
@tc.assert_raises StandardError
|
920
|
+
end
|
921
|
+
end
|
922
|
+
|
923
|
+
def test_assert_respond_to
|
924
|
+
@tc.assert_respond_to "blah", :empty?
|
925
|
+
end
|
926
|
+
|
927
|
+
def test_assert_respond_to_triggered
|
928
|
+
assert_triggered 'Expected "blah" (String) to respond to #rawr!.' do
|
929
|
+
@tc.assert_respond_to "blah", :rawr!
|
930
|
+
end
|
931
|
+
end
|
932
|
+
|
933
|
+
def test_assert_same
|
934
|
+
@assertion_count = 3
|
935
|
+
|
936
|
+
o = "blah"
|
937
|
+
@tc.assert_same 1, 1
|
938
|
+
@tc.assert_same :blah, :blah
|
939
|
+
@tc.assert_same o, o
|
940
|
+
end
|
941
|
+
|
942
|
+
def test_assert_same_triggered
|
943
|
+
@assertion_count = 2
|
944
|
+
|
945
|
+
assert_triggered "Expected 2 (oid=N) to be the same as 1 (oid=N)." do
|
946
|
+
@tc.assert_same 1, 2
|
947
|
+
end
|
948
|
+
|
949
|
+
s1 = "blah"
|
950
|
+
s2 = "blah"
|
951
|
+
|
952
|
+
assert_triggered 'Expected "blah" (oid=N) to be the same as "blah" (oid=N).' do
|
953
|
+
@tc.assert_same s1, s2
|
954
|
+
end
|
955
|
+
end
|
956
|
+
|
957
|
+
def test_assert_send
|
958
|
+
assert_deprecated :assert_send do
|
959
|
+
@tc.assert_send [1, :<, 2]
|
960
|
+
end
|
961
|
+
end
|
962
|
+
|
963
|
+
def test_assert_send_bad
|
964
|
+
assert_deprecated :assert_send do
|
965
|
+
assert_triggered "Expected 1.>(*[2]) to return true." do
|
966
|
+
@tc.assert_send [1, :>, 2]
|
967
|
+
end
|
968
|
+
end
|
969
|
+
end
|
970
|
+
|
971
|
+
def test_assert_silent
|
972
|
+
@assertion_count = 2
|
973
|
+
|
974
|
+
@tc.assert_silent do
|
975
|
+
# do nothing
|
976
|
+
end
|
977
|
+
end
|
978
|
+
|
979
|
+
def test_assert_silent_triggered_err
|
980
|
+
assert_triggered util_msg("", "blah blah", "In stderr") do
|
981
|
+
@tc.assert_silent do
|
982
|
+
$stderr.print "blah blah"
|
983
|
+
end
|
984
|
+
end
|
985
|
+
end
|
986
|
+
|
987
|
+
def test_assert_silent_triggered_out
|
988
|
+
@assertion_count = 2
|
989
|
+
|
990
|
+
assert_triggered util_msg("", "blah blah", "In stdout") do
|
991
|
+
@tc.assert_silent do
|
992
|
+
print "blah blah"
|
993
|
+
end
|
994
|
+
end
|
995
|
+
end
|
996
|
+
|
997
|
+
def test_assert_throws
|
998
|
+
@tc.assert_throws :blah do
|
999
|
+
throw :blah
|
1000
|
+
end
|
1001
|
+
end
|
1002
|
+
|
1003
|
+
def test_assert_throws_argument_exception
|
1004
|
+
@assertion_count = 0
|
1005
|
+
|
1006
|
+
assert_unexpected "ArgumentError" do
|
1007
|
+
@tc.assert_throws :blah do
|
1008
|
+
raise ArgumentError
|
1009
|
+
end
|
1010
|
+
end
|
1011
|
+
end
|
1012
|
+
|
1013
|
+
def test_assert_throws_different
|
1014
|
+
assert_triggered "Expected :blah to have been thrown, not :not_blah." do
|
1015
|
+
@tc.assert_throws :blah do
|
1016
|
+
throw :not_blah
|
1017
|
+
end
|
1018
|
+
end
|
1019
|
+
end
|
1020
|
+
|
1021
|
+
def test_assert_throws_name_error
|
1022
|
+
@assertion_count = 0
|
1023
|
+
|
1024
|
+
assert_unexpected "NameError" do
|
1025
|
+
@tc.assert_throws :blah do
|
1026
|
+
raise NameError
|
1027
|
+
end
|
1028
|
+
end
|
1029
|
+
end
|
1030
|
+
|
1031
|
+
def test_assert_throws_unthrown
|
1032
|
+
assert_triggered "Expected :blah to have been thrown." do
|
1033
|
+
@tc.assert_throws :blah do
|
1034
|
+
# do nothing
|
1035
|
+
end
|
1036
|
+
end
|
1037
|
+
end
|
1038
|
+
|
1039
|
+
def test_assert_path_exists
|
1040
|
+
@tc.assert_path_exists __FILE__
|
1041
|
+
end
|
1042
|
+
|
1043
|
+
def test_assert_path_exists_triggered
|
1044
|
+
assert_triggered "Expected path 'blah' to exist." do
|
1045
|
+
@tc.assert_path_exists "blah"
|
1046
|
+
end
|
1047
|
+
end
|
1048
|
+
|
1049
|
+
def test_capture_io
|
1050
|
+
@assertion_count = 0
|
1051
|
+
|
1052
|
+
non_verbose do
|
1053
|
+
out, err = capture_io do
|
1054
|
+
puts "hi"
|
1055
|
+
$stderr.puts "bye!"
|
1056
|
+
end
|
1057
|
+
|
1058
|
+
assert_equal "hi\n", out
|
1059
|
+
assert_equal "bye!\n", err
|
1060
|
+
end
|
1061
|
+
end
|
1062
|
+
|
1063
|
+
def test_capture_subprocess_io
|
1064
|
+
@assertion_count = 0
|
1065
|
+
|
1066
|
+
non_verbose do
|
1067
|
+
out, err = capture_subprocess_io do
|
1068
|
+
system("echo hi")
|
1069
|
+
system("echo bye! 1>&2")
|
1070
|
+
end
|
1071
|
+
|
1072
|
+
assert_equal "hi\n", out
|
1073
|
+
assert_equal "bye!", err.strip
|
1074
|
+
end
|
1075
|
+
end
|
1076
|
+
|
1077
|
+
def test_class_asserts_match_refutes
|
1078
|
+
@assertion_count = 0
|
1079
|
+
|
1080
|
+
methods = Minitest::Assertions.public_instance_methods
|
1081
|
+
methods.map!(&:to_s) if Symbol === methods.first
|
1082
|
+
|
1083
|
+
# These don't have corresponding refutes _on purpose_. They're
|
1084
|
+
# useless and will never be added, so don't bother.
|
1085
|
+
ignores = %w[assert_output assert_raises assert_send
|
1086
|
+
assert_silent assert_throws assert_mock]
|
1087
|
+
|
1088
|
+
# These are test/unit methods. I'm not actually sure why they're still here
|
1089
|
+
ignores += %w[assert_no_match assert_not_equal assert_not_nil
|
1090
|
+
assert_not_same assert_nothing_raised
|
1091
|
+
assert_nothing_thrown assert_raise]
|
1092
|
+
|
1093
|
+
asserts = methods.grep(/^assert/).sort - ignores
|
1094
|
+
refutes = methods.grep(/^refute/).sort - ignores
|
1095
|
+
|
1096
|
+
assert_empty refutes.map { |n| n.sub(/^refute/, "assert") } - asserts
|
1097
|
+
assert_empty asserts.map { |n| n.sub(/^assert/, "refute") } - refutes
|
1098
|
+
end
|
1099
|
+
|
1100
|
+
def test_delta_consistency
|
1101
|
+
@assertion_count = 2
|
1102
|
+
|
1103
|
+
@tc.assert_in_delta 0, 1, 1
|
1104
|
+
|
1105
|
+
assert_triggered "Expected |0 - 1| (1) to not be <= 1." do
|
1106
|
+
@tc.refute_in_delta 0, 1, 1
|
1107
|
+
end
|
1108
|
+
end
|
1109
|
+
|
1110
|
+
def test_epsilon_consistency
|
1111
|
+
@assertion_count = 2
|
1112
|
+
|
1113
|
+
@tc.assert_in_epsilon 1.0, 1.001
|
1114
|
+
|
1115
|
+
msg = "Expected |1.0 - 1.001| (0.000999xxx) to not be <= 0.001."
|
1116
|
+
assert_triggered msg do
|
1117
|
+
@tc.refute_in_epsilon 1.0, 1.001
|
1118
|
+
end
|
1119
|
+
end
|
1120
|
+
|
1121
|
+
def assert_fail_after t
|
1122
|
+
@tc.fail_after t.year, t.month, t.day, "remove the deprecations"
|
1123
|
+
end
|
1124
|
+
|
1125
|
+
def test_fail_after
|
1126
|
+
d0 = Time.now
|
1127
|
+
d1 = d0 + 86_400 # I am an idiot
|
1128
|
+
|
1129
|
+
assert_silent do
|
1130
|
+
assert_fail_after d1
|
1131
|
+
end
|
1132
|
+
|
1133
|
+
assert_triggered "remove the deprecations" do
|
1134
|
+
assert_fail_after d0
|
1135
|
+
end
|
1136
|
+
end
|
1137
|
+
|
1138
|
+
def test_flunk
|
1139
|
+
assert_triggered "Epic Fail!" do
|
1140
|
+
@tc.flunk
|
1141
|
+
end
|
1142
|
+
end
|
1143
|
+
|
1144
|
+
def test_flunk_message
|
1145
|
+
assert_triggered @zomg do
|
1146
|
+
@tc.flunk @zomg
|
1147
|
+
end
|
1148
|
+
end
|
1149
|
+
|
1150
|
+
def test_pass
|
1151
|
+
@tc.pass
|
1152
|
+
end
|
1153
|
+
|
1154
|
+
def test_refute
|
1155
|
+
@assertion_count = 2
|
1156
|
+
|
1157
|
+
@tc.assert_equal true, @tc.refute(false), "returns true on success"
|
1158
|
+
end
|
1159
|
+
|
1160
|
+
def test_refute_empty
|
1161
|
+
@assertion_count = 2
|
1162
|
+
|
1163
|
+
@tc.refute_empty [1]
|
1164
|
+
end
|
1165
|
+
|
1166
|
+
def test_refute_empty_triggered
|
1167
|
+
@assertion_count = 2
|
1168
|
+
|
1169
|
+
assert_triggered "Expected [] to not be empty." do
|
1170
|
+
@tc.refute_empty []
|
1171
|
+
end
|
1172
|
+
end
|
1173
|
+
|
1174
|
+
def test_refute_equal
|
1175
|
+
@tc.refute_equal "blah", "yay"
|
1176
|
+
end
|
1177
|
+
|
1178
|
+
def test_refute_equal_triggered
|
1179
|
+
assert_triggered 'Expected "blah" to not be equal to "blah".' do
|
1180
|
+
@tc.refute_equal "blah", "blah"
|
1181
|
+
end
|
1182
|
+
end
|
1183
|
+
|
1184
|
+
def test_refute_in_delta
|
1185
|
+
@tc.refute_in_delta 0.0, 1.0 / 1000, 0.000001
|
1186
|
+
end
|
1187
|
+
|
1188
|
+
def test_refute_in_delta_triggered
|
1189
|
+
x = "0.1"
|
1190
|
+
assert_triggered "Expected |0.0 - 0.001| (0.001) to not be <= #{x}." do
|
1191
|
+
@tc.refute_in_delta 0.0, 1.0 / 1000, 0.1
|
1192
|
+
end
|
1193
|
+
end
|
1194
|
+
|
1195
|
+
def test_refute_in_epsilon
|
1196
|
+
@tc.refute_in_epsilon 10_000, 9990-1
|
1197
|
+
end
|
1198
|
+
|
1199
|
+
def test_refute_in_epsilon_triggered
|
1200
|
+
assert_triggered "Expected |10000 - 9990| (10) to not be <= 10.0." do
|
1201
|
+
@tc.refute_in_epsilon 10_000, 9990
|
1202
|
+
flunk
|
1203
|
+
end
|
1204
|
+
end
|
1205
|
+
|
1206
|
+
def test_refute_includes
|
1207
|
+
@assertion_count = 2
|
1208
|
+
|
1209
|
+
@tc.refute_includes [true], false
|
1210
|
+
end
|
1211
|
+
|
1212
|
+
def test_refute_includes_triggered
|
1213
|
+
@assertion_count = 3
|
1214
|
+
|
1215
|
+
e = @tc.assert_raises Minitest::Assertion do
|
1216
|
+
@tc.refute_includes [true], true
|
1217
|
+
end
|
1218
|
+
|
1219
|
+
expected = "Expected [true] to not include true."
|
1220
|
+
assert_equal expected, e.message
|
1221
|
+
end
|
1222
|
+
|
1223
|
+
def test_refute_instance_of
|
1224
|
+
@tc.refute_instance_of Array, "blah"
|
1225
|
+
end
|
1226
|
+
|
1227
|
+
def test_refute_instance_of_triggered
|
1228
|
+
assert_triggered 'Expected "blah" to not be an instance of String.' do
|
1229
|
+
@tc.refute_instance_of String, "blah"
|
1230
|
+
end
|
1231
|
+
end
|
1232
|
+
|
1233
|
+
def test_refute_kind_of
|
1234
|
+
@tc.refute_kind_of Array, "blah"
|
1235
|
+
end
|
1236
|
+
|
1237
|
+
def test_refute_kind_of_triggered
|
1238
|
+
assert_triggered 'Expected "blah" to not be a kind of String.' do
|
1239
|
+
@tc.refute_kind_of String, "blah"
|
1240
|
+
end
|
1241
|
+
end
|
1242
|
+
|
1243
|
+
def test_refute_match
|
1244
|
+
@assertion_count = 2
|
1245
|
+
@tc.refute_match(/\d+/, "blah blah blah")
|
1246
|
+
end
|
1247
|
+
|
1248
|
+
def test_refute_match_matcher_object
|
1249
|
+
@assertion_count = 2
|
1250
|
+
pattern = Object.new
|
1251
|
+
def pattern.=~ _; false end
|
1252
|
+
@tc.refute_match pattern, 5
|
1253
|
+
end
|
1254
|
+
|
1255
|
+
def test_refute_match_object_triggered
|
1256
|
+
@assertion_count = 2
|
1257
|
+
|
1258
|
+
pattern = Object.new
|
1259
|
+
def pattern.=~ _; true end
|
1260
|
+
def pattern.inspect; "[Object]" end
|
1261
|
+
|
1262
|
+
assert_triggered "Expected [Object] to not match 5." do
|
1263
|
+
@tc.refute_match pattern, 5
|
1264
|
+
end
|
1265
|
+
end
|
1266
|
+
|
1267
|
+
def test_refute_match_triggered
|
1268
|
+
@assertion_count = 2
|
1269
|
+
assert_triggered 'Expected /\w+/ to not match "blah blah blah".' do
|
1270
|
+
@tc.refute_match(/\w+/, "blah blah blah")
|
1271
|
+
end
|
1272
|
+
end
|
1273
|
+
|
1274
|
+
def test_refute_nil
|
1275
|
+
@tc.refute_nil 42
|
1276
|
+
end
|
1277
|
+
|
1278
|
+
def test_refute_nil_triggered
|
1279
|
+
assert_triggered "Expected nil to not be nil." do
|
1280
|
+
@tc.refute_nil nil
|
1281
|
+
end
|
1282
|
+
end
|
1283
|
+
|
1284
|
+
def test_refute_operator
|
1285
|
+
@tc.refute_operator 2, :<, 1
|
1286
|
+
end
|
1287
|
+
|
1288
|
+
def test_refute_operator_bad_object
|
1289
|
+
bad = Object.new
|
1290
|
+
def bad.== _; true end
|
1291
|
+
|
1292
|
+
@tc.refute_operator true, :equal?, bad
|
1293
|
+
end
|
1294
|
+
|
1295
|
+
def test_refute_operator_triggered
|
1296
|
+
assert_triggered "Expected 2 to not be > 1." do
|
1297
|
+
@tc.refute_operator 2, :>, 1
|
1298
|
+
end
|
1299
|
+
end
|
1300
|
+
|
1301
|
+
def test_refute_predicate
|
1302
|
+
@tc.refute_predicate "42", :empty?
|
1303
|
+
end
|
1304
|
+
|
1305
|
+
def test_refute_predicate_triggered
|
1306
|
+
assert_triggered 'Expected "" to not be empty?.' do
|
1307
|
+
@tc.refute_predicate "", :empty?
|
1308
|
+
end
|
1309
|
+
end
|
1310
|
+
|
1311
|
+
def test_refute_respond_to
|
1312
|
+
@tc.refute_respond_to "blah", :rawr!
|
1313
|
+
end
|
1314
|
+
|
1315
|
+
def test_refute_respond_to_triggered
|
1316
|
+
assert_triggered 'Expected "blah" to not respond to empty?.' do
|
1317
|
+
@tc.refute_respond_to "blah", :empty?
|
1318
|
+
end
|
1319
|
+
end
|
1320
|
+
|
1321
|
+
def test_refute_same
|
1322
|
+
@tc.refute_same 1, 2
|
1323
|
+
end
|
1324
|
+
|
1325
|
+
def test_refute_same_triggered
|
1326
|
+
assert_triggered "Expected 1 (oid=N) to not be the same as 1 (oid=N)." do
|
1327
|
+
@tc.refute_same 1, 1
|
1328
|
+
end
|
1329
|
+
end
|
1330
|
+
|
1331
|
+
def test_refute_path_exists
|
1332
|
+
@tc.refute_path_exists "blah"
|
1333
|
+
end
|
1334
|
+
|
1335
|
+
def test_refute_path_exists_triggered
|
1336
|
+
assert_triggered "Expected path '#{__FILE__}' to not exist." do
|
1337
|
+
@tc.refute_path_exists __FILE__
|
1338
|
+
end
|
1339
|
+
end
|
1340
|
+
|
1341
|
+
def test_skip
|
1342
|
+
@assertion_count = 0
|
1343
|
+
|
1344
|
+
assert_triggered "haha!", Minitest::Skip do
|
1345
|
+
@tc.skip "haha!"
|
1346
|
+
end
|
1347
|
+
end
|
1348
|
+
|
1349
|
+
def assert_skip_until t, msg
|
1350
|
+
@tc.skip_until t.year, t.month, t.day, msg
|
1351
|
+
end
|
1352
|
+
|
1353
|
+
def test_skip_until
|
1354
|
+
@assertion_count = 0
|
1355
|
+
|
1356
|
+
d0 = Time.now
|
1357
|
+
d1 = d0 + 86_400 # I am an idiot
|
1358
|
+
|
1359
|
+
assert_output "", /Stale skip_until \"not yet\" at .*?:\d+$/ do
|
1360
|
+
assert_skip_until d0, "not yet"
|
1361
|
+
end
|
1362
|
+
|
1363
|
+
assert_triggered "not ready yet", Minitest::Skip do
|
1364
|
+
assert_skip_until d1, "not ready yet"
|
1365
|
+
end
|
1366
|
+
end
|
1367
|
+
|
1368
|
+
def util_msg exp, act, msg = nil
|
1369
|
+
s = "Expected: #{exp.inspect}\n Actual: #{act.inspect}"
|
1370
|
+
s = "#{msg}.\n#{s}" if msg
|
1371
|
+
s
|
1372
|
+
end
|
1373
|
+
|
1374
|
+
def without_diff
|
1375
|
+
old_diff = Minitest::Assertions.diff
|
1376
|
+
Minitest::Assertions.diff = nil
|
1377
|
+
|
1378
|
+
yield
|
1379
|
+
ensure
|
1380
|
+
Minitest::Assertions.diff = old_diff
|
1381
|
+
end
|
1382
|
+
end
|
1383
|
+
|
1384
|
+
class TestMinitestAssertionHelpers < Minitest::Test
|
1385
|
+
def assert_mu_pp exp, input, raw = false
|
1386
|
+
act = mu_pp input
|
1387
|
+
|
1388
|
+
if String === input && !raw then
|
1389
|
+
assert_equal "\"#{exp}\"", act
|
1390
|
+
else
|
1391
|
+
assert_equal exp, act
|
1392
|
+
end
|
1393
|
+
end
|
1394
|
+
|
1395
|
+
def assert_mu_pp_for_diff exp, input, raw = false
|
1396
|
+
act = mu_pp_for_diff input
|
1397
|
+
|
1398
|
+
if String === input && !raw then
|
1399
|
+
assert_equal "\"#{exp}\"", act
|
1400
|
+
else
|
1401
|
+
assert_equal exp, act
|
1402
|
+
end
|
1403
|
+
end
|
1404
|
+
|
1405
|
+
def test_diff_equal
|
1406
|
+
msg = "No visible difference in the String#inspect output.
|
1407
|
+
You should look at the implementation of #== on String or its members.
|
1408
|
+
\"blahblahblahblahblahblahblahblahblahblah\"".gsub(/^ +/, "")
|
1409
|
+
|
1410
|
+
o1 = "blah" * 10
|
1411
|
+
o2 = "blah" * 10
|
1412
|
+
def o1.== _
|
1413
|
+
false
|
1414
|
+
end
|
1415
|
+
|
1416
|
+
assert_equal msg, diff(o1, o2)
|
1417
|
+
end
|
1418
|
+
|
1419
|
+
def test_diff_str_mixed
|
1420
|
+
msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
|
1421
|
+
--- expected
|
1422
|
+
+++ actual
|
1423
|
+
@@ -1 +1 @@
|
1424
|
+
-"A\\n\nB"
|
1425
|
+
+"A\n\\nB"
|
1426
|
+
EOM
|
1427
|
+
|
1428
|
+
exp = "A\\n\nB"
|
1429
|
+
act = "A\n\\nB"
|
1430
|
+
|
1431
|
+
assert_equal msg, diff(exp, act)
|
1432
|
+
end
|
1433
|
+
|
1434
|
+
def test_diff_str_multiline
|
1435
|
+
msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
|
1436
|
+
--- expected
|
1437
|
+
+++ actual
|
1438
|
+
@@ -1,2 +1,2 @@
|
1439
|
+
"A
|
1440
|
+
-B"
|
1441
|
+
+C"
|
1442
|
+
EOM
|
1443
|
+
|
1444
|
+
exp = "A\nB"
|
1445
|
+
act = "A\nC"
|
1446
|
+
|
1447
|
+
assert_equal msg, diff(exp, act)
|
1448
|
+
end
|
1449
|
+
|
1450
|
+
def test_diff_str_simple
|
1451
|
+
msg = <<-'EOM'.gsub(/^ {10}/, "").chomp # NOTE single quotes on heredoc
|
1452
|
+
Expected: "A"
|
1453
|
+
Actual: "B"
|
1454
|
+
EOM
|
1455
|
+
|
1456
|
+
exp = "A"
|
1457
|
+
act = "B"
|
1458
|
+
|
1459
|
+
assert_equal msg, diff(exp, act)
|
1460
|
+
end
|
1461
|
+
|
1462
|
+
def test_message
|
1463
|
+
assert_equal "blah2.", message { "blah2" }.call
|
1464
|
+
assert_equal "blah2.", message("") { "blah2" }.call
|
1465
|
+
assert_equal "blah1.\nblah2.", message(:blah1) { "blah2" }.call
|
1466
|
+
assert_equal "blah1.\nblah2.", message("blah1") { "blah2" }.call
|
1467
|
+
|
1468
|
+
message = proc { "blah1" }
|
1469
|
+
assert_equal "blah1.\nblah2.", message(message) { "blah2" }.call
|
1470
|
+
|
1471
|
+
message = message { "blah1" }
|
1472
|
+
assert_equal "blah1.\nblah2.", message(message) { "blah2" }.call
|
1473
|
+
end
|
1474
|
+
|
1475
|
+
def test_message_deferred
|
1476
|
+
var = nil
|
1477
|
+
|
1478
|
+
msg = message { var = "blah" }
|
1479
|
+
|
1480
|
+
assert_nil var
|
1481
|
+
|
1482
|
+
msg.call
|
1483
|
+
|
1484
|
+
assert_equal "blah", var
|
1485
|
+
end
|
1486
|
+
|
1487
|
+
def test_mu_pp
|
1488
|
+
assert_mu_pp 42.inspect, 42
|
1489
|
+
assert_mu_pp %w[a b c].inspect, %w[a b c]
|
1490
|
+
assert_mu_pp "A B", "A B"
|
1491
|
+
assert_mu_pp "A\\nB", "A\nB"
|
1492
|
+
assert_mu_pp "A\\\\nB", 'A\nB' # notice single quotes
|
1493
|
+
end
|
1494
|
+
|
1495
|
+
def test_mu_pp_for_diff
|
1496
|
+
assert_mu_pp_for_diff "#<Object:0xXXXXXX>", Object.new
|
1497
|
+
assert_mu_pp_for_diff "A B", "A B"
|
1498
|
+
assert_mu_pp_for_diff [1, 2, 3].inspect, [1, 2, 3]
|
1499
|
+
assert_mu_pp_for_diff "A\nB", "A\nB"
|
1500
|
+
end
|
1501
|
+
|
1502
|
+
def test_mu_pp_for_diff_str_bad_encoding
|
1503
|
+
str = "\666".force_encoding Encoding::UTF_8
|
1504
|
+
exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6\""
|
1505
|
+
|
1506
|
+
assert_mu_pp_for_diff exp, str, :raw
|
1507
|
+
end
|
1508
|
+
|
1509
|
+
def test_mu_pp_for_diff_str_bad_encoding_both
|
1510
|
+
str = "\666A\\n\nB".force_encoding Encoding::UTF_8
|
1511
|
+
exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6A\\\\n\\nB\""
|
1512
|
+
|
1513
|
+
assert_mu_pp_for_diff exp, str, :raw
|
1514
|
+
end
|
1515
|
+
|
1516
|
+
def test_mu_pp_for_diff_str_encoding
|
1517
|
+
str = "A\nB".b
|
1518
|
+
exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\nB\""
|
1519
|
+
|
1520
|
+
assert_mu_pp_for_diff exp, str, :raw
|
1521
|
+
end
|
1522
|
+
|
1523
|
+
def test_mu_pp_for_diff_str_encoding_both
|
1524
|
+
str = "A\\n\nB".b
|
1525
|
+
exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\\\\n\\nB\""
|
1526
|
+
|
1527
|
+
assert_mu_pp_for_diff exp, str, :raw
|
1528
|
+
end
|
1529
|
+
|
1530
|
+
def test_mu_pp_for_diff_str_nerd
|
1531
|
+
assert_mu_pp_for_diff "A\\nB\\\\nC", "A\nB\\nC"
|
1532
|
+
assert_mu_pp_for_diff "\\nB\\\\nC", "\nB\\nC"
|
1533
|
+
assert_mu_pp_for_diff "\\nB\\\\n", "\nB\\n"
|
1534
|
+
assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
|
1535
|
+
assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
|
1536
|
+
assert_mu_pp_for_diff "\\\\nB\\n", "\\nB\n"
|
1537
|
+
assert_mu_pp_for_diff "\\\\nB\\nC", "\\nB\nC"
|
1538
|
+
assert_mu_pp_for_diff "A\\\\n\\nB", "A\\n\nB"
|
1539
|
+
assert_mu_pp_for_diff "A\\n\\\\nB", "A\n\\nB"
|
1540
|
+
assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
|
1541
|
+
assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
|
1542
|
+
end
|
1543
|
+
|
1544
|
+
def test_mu_pp_for_diff_str_normal
|
1545
|
+
assert_mu_pp_for_diff "", ""
|
1546
|
+
assert_mu_pp_for_diff "A\\n\n", "A\\n"
|
1547
|
+
assert_mu_pp_for_diff "A\\n\nB", "A\\nB"
|
1548
|
+
assert_mu_pp_for_diff "A\n", "A\n"
|
1549
|
+
assert_mu_pp_for_diff "A\nB", "A\nB"
|
1550
|
+
assert_mu_pp_for_diff "\\n\n", "\\n"
|
1551
|
+
assert_mu_pp_for_diff "\n", "\n"
|
1552
|
+
assert_mu_pp_for_diff "\\n\nA", "\\nA"
|
1553
|
+
assert_mu_pp_for_diff "\nA", "\nA"
|
1554
|
+
end
|
1555
|
+
|
1556
|
+
def test_mu_pp_str_bad_encoding
|
1557
|
+
str = "\666".force_encoding Encoding::UTF_8
|
1558
|
+
exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6\""
|
1559
|
+
|
1560
|
+
assert_mu_pp exp, str, :raw
|
1561
|
+
end
|
1562
|
+
|
1563
|
+
def test_mu_pp_str_encoding
|
1564
|
+
str = "A\nB".b
|
1565
|
+
exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\\nB\""
|
1566
|
+
|
1567
|
+
assert_mu_pp exp, str, :raw
|
1568
|
+
end
|
1569
|
+
|
1570
|
+
def test_mu_pp_str_immutable
|
1571
|
+
printer = Class.new { extend Minitest::Assertions }
|
1572
|
+
str = "test".freeze
|
1573
|
+
assert_equal '"test"', printer.mu_pp(str)
|
1574
|
+
end
|
1575
|
+
end
|