minitest 5.11.3 → 5.14.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -109,8 +109,8 @@ module Minitest
109
109
  # is applied against the slope itself. As such, you probably want
110
110
  # to tighten it from the default.
111
111
  #
112
- # See http://www.graphpad.com/curvefit/goodness_of_fit.htm for
113
- # more details.
112
+ # See https://www.graphpad.com/guides/prism/8/curve-fitting/reg_intepretingnonlinr2.htm
113
+ # for more details.
114
114
  #
115
115
  # Fit is calculated by #fit_linear.
116
116
  #
@@ -9,10 +9,11 @@
9
9
  #
10
10
  # it "should still work in threads" do
11
11
  # my_threaded_thingy do
12
- # (1+1).must_equal 2 # bad
13
- # assert_equal 2, 1+1 # good
14
- # _(1 + 1).must_equal 2 # good
15
- # value(1 + 1).must_equal 2 # good, also #expect
12
+ # (1+1).must_equal 2 # bad
13
+ # assert_equal 2, 1+1 # good
14
+ # _(1 + 1).must_equal 2 # good
15
+ # value(1 + 1).must_equal 2 # good, also #expect
16
+ # _ { 1 + "1" }.must_raise TypeError # good
16
17
  # end
17
18
  # end
18
19
 
@@ -45,7 +46,7 @@ module Minitest::Expectations
45
46
 
46
47
  infect_an_assertion :assert_in_delta, :must_be_close_to
47
48
 
48
- alias :must_be_within_delta :must_be_close_to # :nodoc:
49
+ infect_an_assertion :assert_in_delta, :must_be_within_delta # :nodoc:
49
50
 
50
51
  ##
51
52
  # See Minitest::Assertions#assert_in_epsilon
@@ -168,6 +169,24 @@ module Minitest::Expectations
168
169
 
169
170
  infect_an_assertion :assert_throws, :must_throw, :block
170
171
 
172
+ ##
173
+ # See Minitest::Assertions#assert_path_exists
174
+ #
175
+ # _(some_path).path_must_exist
176
+ #
177
+ # :method: path_must_exist
178
+
179
+ infect_an_assertion :assert_path_exists, :path_must_exist, :unary
180
+
181
+ ##
182
+ # See Minitest::Assertions#refute_path_exists
183
+ #
184
+ # _(some_path).path_wont_exist
185
+ #
186
+ # :method: path_wont_exist
187
+
188
+ infect_an_assertion :refute_path_exists, :path_wont_exist, :unary
189
+
171
190
  ##
172
191
  # See Minitest::Assertions#refute_empty
173
192
  #
@@ -195,7 +214,7 @@ module Minitest::Expectations
195
214
 
196
215
  infect_an_assertion :refute_in_delta, :wont_be_close_to
197
216
 
198
- alias :wont_be_within_delta :wont_be_close_to # :nodoc:
217
+ infect_an_assertion :refute_in_delta, :wont_be_within_delta # :nodoc:
199
218
 
200
219
  ##
201
220
  # See Minitest::Assertions#refute_in_epsilon
@@ -8,12 +8,16 @@ class Module # :nodoc:
8
8
  # warn "%-22p -> %p %p" % [meth, new_name, dont_flip]
9
9
  self.class_eval <<-EOM, __FILE__, __LINE__ + 1
10
10
  def #{new_name} *args
11
+ where = Minitest.filter_backtrace(caller).first
12
+ where = where.split(/:in /, 2).first # clean up noise
13
+ warn "DEPRECATED: global use of #{new_name} from #\{where}. Use _(obj).#{new_name} instead. This will fail in Minitest 6."
11
14
  Minitest::Expectation.new(self, Minitest::Spec.current).#{new_name}(*args)
12
15
  end
13
16
  EOM
14
17
 
15
18
  Minitest::Expectation.class_eval <<-EOM, __FILE__, __LINE__ + 1
16
19
  def #{new_name} *args
20
+ raise "Calling ##{new_name} outside of test." unless ctx
17
21
  case
18
22
  when #{!!dont_flip} then
19
23
  ctx.#{meth}(target, *args)
@@ -285,21 +289,28 @@ class Minitest::Spec < Minitest::Test
285
289
 
286
290
  module InstanceMethods
287
291
  ##
288
- # Returns a value monad that has all of Expectations methods
289
- # available to it.
292
+ # Takes a value or a block and returns a value monad that has
293
+ # all of Expectations methods available to it.
290
294
  #
291
- # Also aliased to #value and #expect for your aesthetic pleasure:
295
+ # _(1 + 1).must_equal 2
292
296
  #
293
- # _(1 + 1).must_equal 2
294
- # value(1 + 1).must_equal 2
295
- # expect(1 + 1).must_equal 2
297
+ # And for blocks:
298
+ #
299
+ # _ { 1 + "1" }.must_raise TypeError
296
300
  #
297
301
  # This method of expectation-based testing is preferable to
298
302
  # straight-expectation methods (on Object) because it stores its
299
303
  # test context, bypassing our hacky use of thread-local variables.
300
304
  #
301
- # At some point, the methods on Object will be deprecated and then
302
- # removed.
305
+ # NOTE: At some point, the methods on Object will be deprecated
306
+ # and then removed.
307
+ #
308
+ # It is also aliased to #value and #expect for your aesthetic
309
+ # pleasure:
310
+ #
311
+ # _(1 + 1).must_equal 2
312
+ # value(1 + 1).must_equal 2
313
+ # expect(1 + 1).must_equal 2
303
314
 
304
315
  def _ value = nil, &block
305
316
  Minitest::Expectation.new block || value, self
@@ -19,9 +19,20 @@ class FakeNamedTest < Minitest::Test
19
19
  end
20
20
  end
21
21
 
22
+ module MyModule; end
23
+ class AnError < StandardError; include MyModule; end
24
+
22
25
  class MetaMetaMetaTestCase < Minitest::Test
23
26
  attr_accessor :reporter, :output, :tu
24
27
 
28
+ def with_stderr err
29
+ old = $stderr
30
+ $stderr = err
31
+ yield
32
+ ensure
33
+ $stderr = old
34
+ end
35
+
25
36
  def run_tu_with_fresh_reporter flags = %w[--seed 42]
26
37
  options = Minitest.process_args flags
27
38
 
@@ -31,18 +42,20 @@ class MetaMetaMetaTestCase < Minitest::Test
31
42
  reporter << Minitest::SummaryReporter.new(@output, options)
32
43
  reporter << Minitest::ProgressReporter.new(@output, options)
33
44
 
34
- reporter.start
45
+ with_stderr @output do
46
+ reporter.start
35
47
 
36
- yield(reporter) if block_given?
48
+ yield(reporter) if block_given?
37
49
 
38
- @tus ||= [@tu]
39
- @tus.each do |tu|
40
- Minitest::Runnable.runnables.delete tu
50
+ @tus ||= [@tu]
51
+ @tus.each do |tu|
52
+ Minitest::Runnable.runnables.delete tu
41
53
 
42
- tu.run reporter, options
43
- end
54
+ tu.run reporter, options
55
+ end
44
56
 
45
- reporter.report
57
+ reporter.report
58
+ end
46
59
  end
47
60
 
48
61
  def first_reporter
@@ -81,6 +94,8 @@ class MetaMetaMetaTestCase < Minitest::Test
81
94
  output.gsub!(/^(\s+)[^:]+:\d+:in/, '\1FILE:LINE:in')
82
95
  end
83
96
 
97
+ output.gsub!(/( at )[^:]+:\d+/, '\1[FILE:LINE]')
98
+
84
99
  output
85
100
  end
86
101
 
@@ -0,0 +1,1567 @@
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 test_fail_after
1122
+ t = Time.now
1123
+ y, m, d = t.year, t.month, t.day
1124
+
1125
+ assert_silent do
1126
+ @tc.fail_after y, m, d+1, "remove the deprecations"
1127
+ end
1128
+
1129
+ assert_triggered "remove the deprecations" do
1130
+ @tc.fail_after y, m, d, "remove the deprecations"
1131
+ end
1132
+ end
1133
+
1134
+ def test_flunk
1135
+ assert_triggered "Epic Fail!" do
1136
+ @tc.flunk
1137
+ end
1138
+ end
1139
+
1140
+ def test_flunk_message
1141
+ assert_triggered @zomg do
1142
+ @tc.flunk @zomg
1143
+ end
1144
+ end
1145
+
1146
+ def test_pass
1147
+ @tc.pass
1148
+ end
1149
+
1150
+ def test_refute
1151
+ @assertion_count = 2
1152
+
1153
+ @tc.assert_equal false, @tc.refute(false), "returns false on success"
1154
+ end
1155
+
1156
+ def test_refute_empty
1157
+ @assertion_count = 2
1158
+
1159
+ @tc.refute_empty [1]
1160
+ end
1161
+
1162
+ def test_refute_empty_triggered
1163
+ @assertion_count = 2
1164
+
1165
+ assert_triggered "Expected [] to not be empty." do
1166
+ @tc.refute_empty []
1167
+ end
1168
+ end
1169
+
1170
+ def test_refute_equal
1171
+ @tc.refute_equal "blah", "yay"
1172
+ end
1173
+
1174
+ def test_refute_equal_triggered
1175
+ assert_triggered 'Expected "blah" to not be equal to "blah".' do
1176
+ @tc.refute_equal "blah", "blah"
1177
+ end
1178
+ end
1179
+
1180
+ def test_refute_in_delta
1181
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.000001
1182
+ end
1183
+
1184
+ def test_refute_in_delta_triggered
1185
+ x = "0.1"
1186
+ assert_triggered "Expected |0.0 - 0.001| (0.001) to not be <= #{x}." do
1187
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1
1188
+ end
1189
+ end
1190
+
1191
+ def test_refute_in_epsilon
1192
+ @tc.refute_in_epsilon 10_000, 9990-1
1193
+ end
1194
+
1195
+ def test_refute_in_epsilon_triggered
1196
+ assert_triggered "Expected |10000 - 9990| (10) to not be <= 10.0." do
1197
+ @tc.refute_in_epsilon 10_000, 9990
1198
+ flunk
1199
+ end
1200
+ end
1201
+
1202
+ def test_refute_includes
1203
+ @assertion_count = 2
1204
+
1205
+ @tc.refute_includes [true], false
1206
+ end
1207
+
1208
+ def test_refute_includes_triggered
1209
+ @assertion_count = 3
1210
+
1211
+ e = @tc.assert_raises Minitest::Assertion do
1212
+ @tc.refute_includes [true], true
1213
+ end
1214
+
1215
+ expected = "Expected [true] to not include true."
1216
+ assert_equal expected, e.message
1217
+ end
1218
+
1219
+ def test_refute_instance_of
1220
+ @tc.refute_instance_of Array, "blah"
1221
+ end
1222
+
1223
+ def test_refute_instance_of_triggered
1224
+ assert_triggered 'Expected "blah" to not be an instance of String.' do
1225
+ @tc.refute_instance_of String, "blah"
1226
+ end
1227
+ end
1228
+
1229
+ def test_refute_kind_of
1230
+ @tc.refute_kind_of Array, "blah"
1231
+ end
1232
+
1233
+ def test_refute_kind_of_triggered
1234
+ assert_triggered 'Expected "blah" to not be a kind of String.' do
1235
+ @tc.refute_kind_of String, "blah"
1236
+ end
1237
+ end
1238
+
1239
+ def test_refute_match
1240
+ @assertion_count = 2
1241
+ @tc.refute_match(/\d+/, "blah blah blah")
1242
+ end
1243
+
1244
+ def test_refute_match_matcher_object
1245
+ @assertion_count = 2
1246
+ pattern = Object.new
1247
+ def pattern.=~ _; false end
1248
+ @tc.refute_match pattern, 5
1249
+ end
1250
+
1251
+ def test_refute_match_object_triggered
1252
+ @assertion_count = 2
1253
+
1254
+ pattern = Object.new
1255
+ def pattern.=~ _; true end
1256
+ def pattern.inspect; "[Object]" end
1257
+
1258
+ assert_triggered "Expected [Object] to not match 5." do
1259
+ @tc.refute_match pattern, 5
1260
+ end
1261
+ end
1262
+
1263
+ def test_refute_match_triggered
1264
+ @assertion_count = 2
1265
+ assert_triggered 'Expected /\w+/ to not match "blah blah blah".' do
1266
+ @tc.refute_match(/\w+/, "blah blah blah")
1267
+ end
1268
+ end
1269
+
1270
+ def test_refute_nil
1271
+ @tc.refute_nil 42
1272
+ end
1273
+
1274
+ def test_refute_nil_triggered
1275
+ assert_triggered "Expected nil to not be nil." do
1276
+ @tc.refute_nil nil
1277
+ end
1278
+ end
1279
+
1280
+ def test_refute_operator
1281
+ @tc.refute_operator 2, :<, 1
1282
+ end
1283
+
1284
+ def test_refute_operator_bad_object
1285
+ bad = Object.new
1286
+ def bad.== _; true end
1287
+
1288
+ @tc.refute_operator true, :equal?, bad
1289
+ end
1290
+
1291
+ def test_refute_operator_triggered
1292
+ assert_triggered "Expected 2 to not be > 1." do
1293
+ @tc.refute_operator 2, :>, 1
1294
+ end
1295
+ end
1296
+
1297
+ def test_refute_predicate
1298
+ @tc.refute_predicate "42", :empty?
1299
+ end
1300
+
1301
+ def test_refute_predicate_triggered
1302
+ assert_triggered 'Expected "" to not be empty?.' do
1303
+ @tc.refute_predicate "", :empty?
1304
+ end
1305
+ end
1306
+
1307
+ def test_refute_respond_to
1308
+ @tc.refute_respond_to "blah", :rawr!
1309
+ end
1310
+
1311
+ def test_refute_respond_to_triggered
1312
+ assert_triggered 'Expected "blah" to not respond to empty?.' do
1313
+ @tc.refute_respond_to "blah", :empty?
1314
+ end
1315
+ end
1316
+
1317
+ def test_refute_same
1318
+ @tc.refute_same 1, 2
1319
+ end
1320
+
1321
+ def test_refute_same_triggered
1322
+ assert_triggered "Expected 1 (oid=N) to not be the same as 1 (oid=N)." do
1323
+ @tc.refute_same 1, 1
1324
+ end
1325
+ end
1326
+
1327
+ def test_refute_path_exists
1328
+ @tc.refute_path_exists "blah"
1329
+ end
1330
+
1331
+ def test_refute_path_exists_triggered
1332
+ assert_triggered "Expected path '#{__FILE__}' to not exist." do
1333
+ @tc.refute_path_exists __FILE__
1334
+ end
1335
+ end
1336
+
1337
+ def test_skip
1338
+ @assertion_count = 0
1339
+
1340
+ assert_triggered "haha!", Minitest::Skip do
1341
+ @tc.skip "haha!"
1342
+ end
1343
+ end
1344
+
1345
+ def test_skip_until
1346
+ @assertion_count = 0
1347
+
1348
+ t = Time.now
1349
+ y, m, d = t.year, t.month, t.day
1350
+
1351
+ assert_output "", /Stale skip_until \"not yet\" at .*?:\d+$/ do
1352
+ @tc.skip_until y, m, d, "not yet"
1353
+ end
1354
+
1355
+ assert_triggered "not ready yet", Minitest::Skip do
1356
+ @tc.skip_until y, m, d+1, "not ready yet"
1357
+ end
1358
+ end
1359
+
1360
+ def util_msg exp, act, msg = nil
1361
+ s = "Expected: #{exp.inspect}\n Actual: #{act.inspect}"
1362
+ s = "#{msg}.\n#{s}" if msg
1363
+ s
1364
+ end
1365
+
1366
+ def without_diff
1367
+ old_diff = Minitest::Assertions.diff
1368
+ Minitest::Assertions.diff = nil
1369
+
1370
+ yield
1371
+ ensure
1372
+ Minitest::Assertions.diff = old_diff
1373
+ end
1374
+ end
1375
+
1376
+ class TestMinitestAssertionHelpers < Minitest::Test
1377
+ def assert_mu_pp exp, input, raw = false
1378
+ act = mu_pp input
1379
+
1380
+ if String === input && !raw then
1381
+ assert_equal "\"#{exp}\"", act
1382
+ else
1383
+ assert_equal exp, act
1384
+ end
1385
+ end
1386
+
1387
+ def assert_mu_pp_for_diff exp, input, raw = false
1388
+ act = mu_pp_for_diff input
1389
+
1390
+ if String === input && !raw then
1391
+ assert_equal "\"#{exp}\"", act
1392
+ else
1393
+ assert_equal exp, act
1394
+ end
1395
+ end
1396
+
1397
+ def test_diff_equal
1398
+ msg = "No visible difference in the String#inspect output.
1399
+ You should look at the implementation of #== on String or its members.
1400
+ \"blahblahblahblahblahblahblahblahblahblah\"".gsub(/^ +/, "")
1401
+
1402
+ o1 = "blah" * 10
1403
+ o2 = "blah" * 10
1404
+ def o1.== _
1405
+ false
1406
+ end
1407
+
1408
+ assert_equal msg, diff(o1, o2)
1409
+ end
1410
+
1411
+ def test_diff_str_mixed
1412
+ msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
1413
+ --- expected
1414
+ +++ actual
1415
+ @@ -1 +1 @@
1416
+ -"A\\n\nB"
1417
+ +"A\n\\nB"
1418
+ EOM
1419
+
1420
+ exp = "A\\n\nB"
1421
+ act = "A\n\\nB"
1422
+
1423
+ assert_equal msg, diff(exp, act)
1424
+ end
1425
+
1426
+ def test_diff_str_multiline
1427
+ msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
1428
+ --- expected
1429
+ +++ actual
1430
+ @@ -1,2 +1,2 @@
1431
+ "A
1432
+ -B"
1433
+ +C"
1434
+ EOM
1435
+
1436
+ exp = "A\nB"
1437
+ act = "A\nC"
1438
+
1439
+ assert_equal msg, diff(exp, act)
1440
+ end
1441
+
1442
+ def test_diff_str_simple
1443
+ msg = <<-'EOM'.gsub(/^ {10}/, "").chomp # NOTE single quotes on heredoc
1444
+ Expected: "A"
1445
+ Actual: "B"
1446
+ EOM
1447
+
1448
+ exp = "A"
1449
+ act = "B"
1450
+
1451
+ assert_equal msg, diff(exp, act)
1452
+ end
1453
+
1454
+ def test_message
1455
+ assert_equal "blah2.", message { "blah2" }.call
1456
+ assert_equal "blah2.", message("") { "blah2" }.call
1457
+ assert_equal "blah1.\nblah2.", message(:blah1) { "blah2" }.call
1458
+ assert_equal "blah1.\nblah2.", message("blah1") { "blah2" }.call
1459
+
1460
+ message = proc { "blah1" }
1461
+ assert_equal "blah1.\nblah2.", message(message) { "blah2" }.call
1462
+
1463
+ message = message { "blah1" }
1464
+ assert_equal "blah1.\nblah2.", message(message) { "blah2" }.call
1465
+ end
1466
+
1467
+ def test_message_deferred
1468
+ var = nil
1469
+
1470
+ msg = message { var = "blah" }
1471
+
1472
+ assert_nil var
1473
+
1474
+ msg.call
1475
+
1476
+ assert_equal "blah", var
1477
+ end
1478
+
1479
+ def test_mu_pp
1480
+ assert_mu_pp 42.inspect, 42
1481
+ assert_mu_pp %w[a b c].inspect, %w[a b c]
1482
+ assert_mu_pp "A B", "A B"
1483
+ assert_mu_pp "A\\nB", "A\nB"
1484
+ assert_mu_pp "A\\\\nB", 'A\nB' # notice single quotes
1485
+ end
1486
+
1487
+ def test_mu_pp_for_diff
1488
+ assert_mu_pp_for_diff "#<Object:0xXXXXXX>", Object.new
1489
+ assert_mu_pp_for_diff "A B", "A B"
1490
+ assert_mu_pp_for_diff [1, 2, 3].inspect, [1, 2, 3]
1491
+ assert_mu_pp_for_diff "A\nB", "A\nB"
1492
+ end
1493
+
1494
+ def test_mu_pp_for_diff_str_bad_encoding
1495
+ str = "\666".force_encoding Encoding::UTF_8
1496
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6\""
1497
+
1498
+ assert_mu_pp_for_diff exp, str, :raw
1499
+ end
1500
+
1501
+ def test_mu_pp_for_diff_str_bad_encoding_both
1502
+ str = "\666A\\n\nB".force_encoding Encoding::UTF_8
1503
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6A\\\\n\\nB\""
1504
+
1505
+ assert_mu_pp_for_diff exp, str, :raw
1506
+ end
1507
+
1508
+ def test_mu_pp_for_diff_str_encoding
1509
+ str = "A\nB".b
1510
+ exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\nB\""
1511
+
1512
+ assert_mu_pp_for_diff exp, str, :raw
1513
+ end
1514
+
1515
+ def test_mu_pp_for_diff_str_encoding_both
1516
+ str = "A\\n\nB".b
1517
+ exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\\\\n\\nB\""
1518
+
1519
+ assert_mu_pp_for_diff exp, str, :raw
1520
+ end
1521
+
1522
+ def test_mu_pp_for_diff_str_nerd
1523
+ assert_mu_pp_for_diff "A\\nB\\\\nC", "A\nB\\nC"
1524
+ assert_mu_pp_for_diff "\\nB\\\\nC", "\nB\\nC"
1525
+ assert_mu_pp_for_diff "\\nB\\\\n", "\nB\\n"
1526
+ assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
1527
+ assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
1528
+ assert_mu_pp_for_diff "\\\\nB\\n", "\\nB\n"
1529
+ assert_mu_pp_for_diff "\\\\nB\\nC", "\\nB\nC"
1530
+ assert_mu_pp_for_diff "A\\\\n\\nB", "A\\n\nB"
1531
+ assert_mu_pp_for_diff "A\\n\\\\nB", "A\n\\nB"
1532
+ assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
1533
+ assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
1534
+ end
1535
+
1536
+ def test_mu_pp_for_diff_str_normal
1537
+ assert_mu_pp_for_diff "", ""
1538
+ assert_mu_pp_for_diff "A\\n\n", "A\\n"
1539
+ assert_mu_pp_for_diff "A\\n\nB", "A\\nB"
1540
+ assert_mu_pp_for_diff "A\n", "A\n"
1541
+ assert_mu_pp_for_diff "A\nB", "A\nB"
1542
+ assert_mu_pp_for_diff "\\n\n", "\\n"
1543
+ assert_mu_pp_for_diff "\n", "\n"
1544
+ assert_mu_pp_for_diff "\\n\nA", "\\nA"
1545
+ assert_mu_pp_for_diff "\nA", "\nA"
1546
+ end
1547
+
1548
+ def test_mu_pp_str_bad_encoding
1549
+ str = "\666".force_encoding Encoding::UTF_8
1550
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6\""
1551
+
1552
+ assert_mu_pp exp, str, :raw
1553
+ end
1554
+
1555
+ def test_mu_pp_str_encoding
1556
+ str = "A\nB".b
1557
+ exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\\nB\""
1558
+
1559
+ assert_mu_pp exp, str, :raw
1560
+ end
1561
+
1562
+ def test_mu_pp_str_immutable
1563
+ printer = Class.new { extend Minitest::Assertions }
1564
+ str = "test".freeze
1565
+ assert_equal '"test"', printer.mu_pp(str)
1566
+ end
1567
+ end