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