minitest 5.10.3 → 5.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,6 +27,8 @@ module Minitest
27
27
  # figure out what diff to use.
28
28
 
29
29
  def self.diff
30
+ return @diff if defined? @diff
31
+
30
32
  @diff = if (RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ &&
31
33
  system("diff.exe", __FILE__, __FILE__)) then
32
34
  "diff.exe -u"
@@ -38,9 +40,7 @@ module Minitest
38
40
  "diff -u"
39
41
  else
40
42
  nil
41
- end unless defined? @diff
42
-
43
- @diff
43
+ end
44
44
  end
45
45
 
46
46
  ##
@@ -61,12 +61,15 @@ module Minitest
61
61
  butwas = mu_pp_for_diff act
62
62
  result = nil
63
63
 
64
+ e1, e2 = expect.include?("\n"), expect.include?("\\n")
65
+ b1, b2 = butwas.include?("\n"), butwas.include?("\\n")
66
+
64
67
  need_to_diff =
65
- (expect.include?("\n") ||
66
- butwas.include?("\n") ||
67
- expect.size > 30 ||
68
- butwas.size > 30 ||
69
- expect == butwas) &&
68
+ (e1 ^ e2 ||
69
+ b1 ^ b2 ||
70
+ expect.size > 30 ||
71
+ butwas.size > 30 ||
72
+ expect == butwas) &&
70
73
  Minitest::Assertions.diff
71
74
 
72
75
  return "Expected: #{mu_pp exp}\n Actual: #{mu_pp act}" unless
@@ -101,8 +104,10 @@ module Minitest
101
104
 
102
105
  ##
103
106
  # This returns a human-readable version of +obj+. By default
104
- # #inspect is called. You can override this to use #pretty_print
107
+ # #inspect is called. You can override this to use #pretty_inspect
105
108
  # if you want.
109
+ #
110
+ # See Minitest::Test.make_my_diffs_pretty!
106
111
 
107
112
  def mu_pp obj
108
113
  s = obj.inspect
@@ -110,8 +115,11 @@ module Minitest
110
115
  if defined? Encoding then
111
116
  s = s.encode Encoding.default_external
112
117
 
113
- if String === obj && obj.encoding != Encoding.default_external then
114
- s = "# encoding: #{obj.encoding}\n#{s}"
118
+ if String === obj && (obj.encoding != Encoding.default_external ||
119
+ !obj.valid_encoding?) then
120
+ enc = "# encoding: #{obj.encoding}"
121
+ val = "# valid: #{obj.valid_encoding?}"
122
+ s = "#{enc}\n#{val}\n#{s}"
115
123
  end
116
124
  end
117
125
 
@@ -119,13 +127,32 @@ module Minitest
119
127
  end
120
128
 
121
129
  ##
122
- # This returns a diff-able human-readable version of +obj+. This
123
- # differs from the regular mu_pp because it expands escaped
124
- # newlines and makes hex-values generic (like object_ids). This
130
+ # This returns a diff-able more human-readable version of +obj+.
131
+ # This differs from the regular mu_pp because it expands escaped
132
+ # newlines and makes hex-values (like object_ids) generic. This
125
133
  # uses mu_pp to do the first pass and then cleans it up.
126
134
 
127
135
  def mu_pp_for_diff obj
128
- mu_pp(obj).gsub(/\\n/, "\n").gsub(/:0x[a-fA-F0-9]{4,}/m, ":0xXXXXXX")
136
+ str = mu_pp obj
137
+
138
+ # both '\n' & '\\n' (_after_ mu_pp (aka inspect))
139
+ single = str.match?(/(?<!\\|^)\\n/)
140
+ double = str.match?(/(?<=\\|^)\\n/)
141
+
142
+ process =
143
+ if single ^ double then
144
+ if single then
145
+ lambda { |s| s == "\\n" ? "\n" : s } # unescape
146
+ else
147
+ lambda { |s| s == "\\\\n" ? "\\n\n" : s } # unescape a bit, add nls
148
+ end
149
+ else
150
+ :itself # leave it alone
151
+ end
152
+
153
+ str.
154
+ gsub(/\\?\\n/, &process).
155
+ gsub(/:0x[a-fA-F0-9]{4,}/m, ":0xXXXXXX") # anonymize hex values
129
156
  end
130
157
 
131
158
  ##
@@ -173,7 +200,7 @@ module Minitest
173
200
  msg = message(msg, E) { diff exp, act }
174
201
  result = assert exp == act, msg
175
202
 
176
- if exp.nil? then
203
+ if nil == exp then
177
204
  if Minitest::VERSION =~ /^6/ then
178
205
  refute_nil exp, "Use assert_nil if expecting nil."
179
206
  else
@@ -205,8 +232,8 @@ module Minitest
205
232
  # For comparing Floats. Fails unless +exp+ and +act+ have a relative
206
233
  # error less than +epsilon+.
207
234
 
208
- def assert_in_epsilon a, b, epsilon = 0.001, msg = nil
209
- assert_in_delta a, b, [a.abs, b.abs].min * epsilon, msg
235
+ def assert_in_epsilon exp, act, epsilon = 0.001, msg = nil
236
+ assert_in_delta exp, act, [exp.abs, act.abs].min * epsilon, msg
210
237
  end
211
238
 
212
239
  ##
@@ -283,6 +310,9 @@ module Minitest
283
310
  # See also: #assert_silent
284
311
 
285
312
  def assert_output stdout = nil, stderr = nil
313
+ flunk "assert_output requires a block to capture output." unless
314
+ block_given?
315
+
286
316
  out, err = capture_io do
287
317
  yield
288
318
  end
@@ -319,6 +349,9 @@ module Minitest
319
349
  # passed.
320
350
 
321
351
  def assert_raises *exp
352
+ flunk "assert_raises requires a block to capture errors." unless
353
+ block_given?
354
+
322
355
  msg = "#{exp.pop}.\n" if String === exp.last
323
356
  exp << StandardError if exp.empty?
324
357
 
@@ -318,7 +318,7 @@ module Minitest
318
318
  # Enumerates over +enum+ mapping +block+ if given, returning the
319
319
  # sum of the result. Eg:
320
320
  #
321
- # sigma([1, 2, 3]) # => 1 + 2 + 3 => 7
321
+ # sigma([1, 2, 3]) # => 1 + 2 + 3 => 6
322
322
  # sigma([1, 2, 3]) { |n| n ** 2 } # => 1 + 4 + 9 => 14
323
323
 
324
324
  def sigma enum, &block
@@ -418,6 +418,37 @@ module Minitest
418
418
  assert_performance_exponential threshold, &work
419
419
  end
420
420
  end
421
+
422
+
423
+ ##
424
+ # Create a benchmark that verifies that the performance is logarithmic.
425
+ #
426
+ # describe "my class Bench" do
427
+ # bench_performance_logarithmic "algorithm" do |n|
428
+ # @obj.algorithm(n)
429
+ # end
430
+ # end
431
+
432
+ def self.bench_performance_logarithmic name, threshold = 0.99, &work
433
+ bench name do
434
+ assert_performance_logarithmic threshold, &work
435
+ end
436
+ end
437
+
438
+ ##
439
+ # Create a benchmark that verifies that the performance is power.
440
+ #
441
+ # describe "my class Bench" do
442
+ # bench_performance_power "algorithm" do |n|
443
+ # @obj.algorithm(n)
444
+ # end
445
+ # end
446
+
447
+ def self.bench_performance_power name, threshold = 0.99, &work
448
+ bench name do
449
+ assert_performance_power threshold, &work
450
+ end
451
+ end
421
452
  end
422
453
 
423
454
  Minitest::Spec.register_spec_type(/Bench(mark)?$/, Minitest::BenchSpec)
@@ -223,15 +223,12 @@ class Object
223
223
  metaclass.send :alias_method, new_name, name
224
224
 
225
225
  metaclass.send :define_method, name do |*args, &blk|
226
- ret = if val_or_callable.respond_to? :call then
227
- val_or_callable.call(*args)
228
- else
229
- val_or_callable
230
- end
231
-
232
- blk.call(*block_args) if blk
233
-
234
- ret
226
+ if val_or_callable.respond_to? :call then
227
+ val_or_callable.call(*args, &blk)
228
+ else
229
+ blk.call(*block_args) if blk
230
+ val_or_callable
231
+ end
235
232
  end
236
233
 
237
234
  yield self
@@ -53,7 +53,7 @@ module Minitest
53
53
  end
54
54
  end
55
55
 
56
- module Test
56
+ module Test # :nodoc:
57
57
  def _synchronize; Minitest::Test.io_lock.synchronize { yield }; end # :nodoc:
58
58
 
59
59
  module ClassMethods # :nodoc:
@@ -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)
@@ -10,6 +10,11 @@ module Minitest
10
10
  class Test < Runnable
11
11
  require "minitest/assertions"
12
12
  include Minitest::Assertions
13
+ include Minitest::Reportable
14
+
15
+ def class_name # :nodoc:
16
+ self.class.name # for Minitest::Reportable
17
+ end
13
18
 
14
19
  PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, SystemExit] # :nodoc:
15
20
 
@@ -79,20 +84,6 @@ module Minitest
79
84
  :random
80
85
  end
81
86
 
82
- ##
83
- # The time it took to run this test.
84
-
85
- attr_accessor :time
86
-
87
- def marshal_dump # :nodoc:
88
- super << self.time
89
- end
90
-
91
- def marshal_load ary # :nodoc:
92
- self.time = ary.pop
93
- super
94
- end
95
-
96
87
  TEARDOWN_METHODS = %w[ before_teardown teardown after_teardown ] # :nodoc:
97
88
 
98
89
  ##
@@ -115,7 +106,7 @@ module Minitest
115
106
  end
116
107
  end
117
108
 
118
- self # per contract
109
+ Result.from self # per contract
119
110
  end
120
111
 
121
112
  ##
@@ -210,61 +201,6 @@ module Minitest
210
201
  self.failures << UnexpectedError.new(e)
211
202
  end
212
203
 
213
- ##
214
- # Did this run error?
215
-
216
- def error?
217
- self.failures.any? { |f| UnexpectedError === f }
218
- end
219
-
220
- ##
221
- # The location identifier of this test.
222
-
223
- def location
224
- loc = " [#{self.failure.location}]" unless passed? or error?
225
- "#{self.class}##{self.name}#{loc}"
226
- end
227
-
228
- ##
229
- # Did this run pass?
230
- #
231
- # Note: skipped runs are not considered passing, but they don't
232
- # cause the process to exit non-zero.
233
-
234
- def passed?
235
- not self.failure
236
- end
237
-
238
- ##
239
- # Returns ".", "F", or "E" based on the result of the run.
240
-
241
- def result_code
242
- self.failure and self.failure.result_code or "."
243
- end
244
-
245
- ##
246
- # Was this run skipped?
247
-
248
- def skipped?
249
- self.failure and Skip === self.failure
250
- end
251
-
252
- def time_it # :nodoc:
253
- t0 = Minitest.clock_time
254
-
255
- yield
256
- ensure
257
- self.time = Minitest.clock_time - t0
258
- end
259
-
260
- def to_s # :nodoc:
261
- return location if passed? and not skipped?
262
-
263
- failures.map { |failure|
264
- "#{failure.result_label}:\n#{self.location}:\n#{failure.message}\n"
265
- }.join "\n"
266
- end
267
-
268
204
  def with_info_handler &block # :nodoc:
269
205
  t0 = Minitest.clock_time
270
206
 
@@ -19,6 +19,9 @@ 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
 
@@ -68,6 +71,7 @@ class MetaMetaMetaTestCase < Minitest::Test
68
71
  output.sub!(/Finished in .*/, "Finished in 0.00")
69
72
  output.sub!(/Loaded suite .*/, "Loaded suite blah")
70
73
 
74
+ output.gsub!(/FakeNamedTest\d+/, "FakeNamedTestXX")
71
75
  output.gsub!(/ = \d+.\d\d s = /, " = 0.00 s = ")
72
76
  output.gsub!(/0x[A-Fa-f0-9]+/, "0xXXX")
73
77
  output.gsub!(/ +$/, "")
@@ -0,0 +1,1389 @@
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 = maglev? ? "9.999999xxxe-07" : "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 = maglev? ? "0.100000xxx" : "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_capture_io
910
+ @assertion_count = 0
911
+
912
+ non_verbose do
913
+ out, err = capture_io do
914
+ puts "hi"
915
+ $stderr.puts "bye!"
916
+ end
917
+
918
+ assert_equal "hi\n", out
919
+ assert_equal "bye!\n", err
920
+ end
921
+ end
922
+
923
+ def test_capture_subprocess_io
924
+ @assertion_count = 0
925
+
926
+ non_verbose do
927
+ out, err = capture_subprocess_io do
928
+ system("echo hi")
929
+ system("echo bye! 1>&2")
930
+ end
931
+
932
+ assert_equal "hi\n", out
933
+ assert_equal "bye!", err.strip
934
+ end
935
+ end
936
+
937
+ def test_class_asserts_match_refutes
938
+ @assertion_count = 0
939
+
940
+ methods = Minitest::Assertions.public_instance_methods
941
+ methods.map!(&:to_s) if Symbol === methods.first
942
+
943
+ # These don't have corresponding refutes _on purpose_. They're
944
+ # useless and will never be added, so don't bother.
945
+ ignores = %w[assert_output assert_raises assert_send
946
+ assert_silent assert_throws assert_mock]
947
+
948
+ # These are test/unit methods. I'm not actually sure why they're still here
949
+ ignores += %w[assert_no_match assert_not_equal assert_not_nil
950
+ assert_not_same assert_nothing_raised
951
+ assert_nothing_thrown assert_raise]
952
+
953
+ asserts = methods.grep(/^assert/).sort - ignores
954
+ refutes = methods.grep(/^refute/).sort - ignores
955
+
956
+ assert_empty refutes.map { |n| n.sub(/^refute/, "assert") } - asserts
957
+ assert_empty asserts.map { |n| n.sub(/^assert/, "refute") } - refutes
958
+ end
959
+
960
+ def test_delta_consistency
961
+ @assertion_count = 2
962
+
963
+ @tc.assert_in_delta 0, 1, 1
964
+
965
+ assert_triggered "Expected |0 - 1| (1) to not be <= 1." do
966
+ @tc.refute_in_delta 0, 1, 1
967
+ end
968
+ end
969
+
970
+ def test_epsilon_consistency
971
+ @assertion_count = 2
972
+
973
+ @tc.assert_in_epsilon 1.0, 1.001
974
+
975
+ msg = "Expected |1.0 - 1.001| (0.000999xxx) to not be <= 0.001."
976
+ assert_triggered msg do
977
+ @tc.refute_in_epsilon 1.0, 1.001
978
+ end
979
+ end
980
+
981
+ def test_flunk
982
+ assert_triggered "Epic Fail!" do
983
+ @tc.flunk
984
+ end
985
+ end
986
+
987
+ def test_flunk_message
988
+ assert_triggered @zomg do
989
+ @tc.flunk @zomg
990
+ end
991
+ end
992
+
993
+ def test_pass
994
+ @tc.pass
995
+ end
996
+
997
+ def test_refute
998
+ @assertion_count = 2
999
+
1000
+ @tc.assert_equal false, @tc.refute(false), "returns false on success"
1001
+ end
1002
+
1003
+ def test_refute_empty
1004
+ @assertion_count = 2
1005
+
1006
+ @tc.refute_empty [1]
1007
+ end
1008
+
1009
+ def test_refute_empty_triggered
1010
+ @assertion_count = 2
1011
+
1012
+ assert_triggered "Expected [] to not be empty." do
1013
+ @tc.refute_empty []
1014
+ end
1015
+ end
1016
+
1017
+ def test_refute_equal
1018
+ @tc.refute_equal "blah", "yay"
1019
+ end
1020
+
1021
+ def test_refute_equal_triggered
1022
+ assert_triggered 'Expected "blah" to not be equal to "blah".' do
1023
+ @tc.refute_equal "blah", "blah"
1024
+ end
1025
+ end
1026
+
1027
+ def test_refute_in_delta
1028
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.000001
1029
+ end
1030
+
1031
+ def test_refute_in_delta_triggered
1032
+ x = maglev? ? "0.100000xxx" : "0.1"
1033
+ assert_triggered "Expected |0.0 - 0.001| (0.001) to not be <= #{x}." do
1034
+ @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1
1035
+ end
1036
+ end
1037
+
1038
+ def test_refute_in_epsilon
1039
+ @tc.refute_in_epsilon 10_000, 9990-1
1040
+ end
1041
+
1042
+ def test_refute_in_epsilon_triggered
1043
+ assert_triggered "Expected |10000 - 9990| (10) to not be <= 10.0." do
1044
+ @tc.refute_in_epsilon 10_000, 9990
1045
+ flunk
1046
+ end
1047
+ end
1048
+
1049
+ def test_refute_includes
1050
+ @assertion_count = 2
1051
+
1052
+ @tc.refute_includes [true], false
1053
+ end
1054
+
1055
+ def test_refute_includes_triggered
1056
+ @assertion_count = 3
1057
+
1058
+ e = @tc.assert_raises Minitest::Assertion do
1059
+ @tc.refute_includes [true], true
1060
+ end
1061
+
1062
+ expected = "Expected [true] to not include true."
1063
+ assert_equal expected, e.message
1064
+ end
1065
+
1066
+ def test_refute_instance_of
1067
+ @tc.refute_instance_of Array, "blah"
1068
+ end
1069
+
1070
+ def test_refute_instance_of_triggered
1071
+ assert_triggered 'Expected "blah" to not be an instance of String.' do
1072
+ @tc.refute_instance_of String, "blah"
1073
+ end
1074
+ end
1075
+
1076
+ def test_refute_kind_of
1077
+ @tc.refute_kind_of Array, "blah"
1078
+ end
1079
+
1080
+ def test_refute_kind_of_triggered
1081
+ assert_triggered 'Expected "blah" to not be a kind of String.' do
1082
+ @tc.refute_kind_of String, "blah"
1083
+ end
1084
+ end
1085
+
1086
+ def test_refute_match
1087
+ @assertion_count = 2
1088
+ @tc.refute_match(/\d+/, "blah blah blah")
1089
+ end
1090
+
1091
+ def test_refute_match_matcher_object
1092
+ @assertion_count = 2
1093
+ pattern = Object.new
1094
+ def pattern.=~ _; false end
1095
+ @tc.refute_match pattern, 5
1096
+ end
1097
+
1098
+ def test_refute_match_object_triggered
1099
+ @assertion_count = 2
1100
+
1101
+ pattern = Object.new
1102
+ def pattern.=~ _; true end
1103
+ def pattern.inspect; "[Object]" end
1104
+
1105
+ assert_triggered "Expected [Object] to not match 5." do
1106
+ @tc.refute_match pattern, 5
1107
+ end
1108
+ end
1109
+
1110
+ def test_refute_match_triggered
1111
+ @assertion_count = 2
1112
+ assert_triggered 'Expected /\w+/ to not match "blah blah blah".' do
1113
+ @tc.refute_match(/\w+/, "blah blah blah")
1114
+ end
1115
+ end
1116
+
1117
+ def test_refute_nil
1118
+ @tc.refute_nil 42
1119
+ end
1120
+
1121
+ def test_refute_nil_triggered
1122
+ assert_triggered "Expected nil to not be nil." do
1123
+ @tc.refute_nil nil
1124
+ end
1125
+ end
1126
+
1127
+ def test_refute_operator
1128
+ @tc.refute_operator 2, :<, 1
1129
+ end
1130
+
1131
+ def test_refute_operator_bad_object
1132
+ bad = Object.new
1133
+ def bad.== _; true end
1134
+
1135
+ @tc.refute_operator true, :equal?, bad
1136
+ end
1137
+
1138
+ def test_refute_operator_triggered
1139
+ assert_triggered "Expected 2 to not be > 1." do
1140
+ @tc.refute_operator 2, :>, 1
1141
+ end
1142
+ end
1143
+
1144
+ def test_refute_predicate
1145
+ @tc.refute_predicate "42", :empty?
1146
+ end
1147
+
1148
+ def test_refute_predicate_triggered
1149
+ assert_triggered 'Expected "" to not be empty?.' do
1150
+ @tc.refute_predicate "", :empty?
1151
+ end
1152
+ end
1153
+
1154
+ def test_refute_respond_to
1155
+ @tc.refute_respond_to "blah", :rawr!
1156
+ end
1157
+
1158
+ def test_refute_respond_to_triggered
1159
+ assert_triggered 'Expected "blah" to not respond to empty?.' do
1160
+ @tc.refute_respond_to "blah", :empty?
1161
+ end
1162
+ end
1163
+
1164
+ def test_refute_same
1165
+ @tc.refute_same 1, 2
1166
+ end
1167
+
1168
+ def test_refute_same_triggered
1169
+ assert_triggered "Expected 1 (oid=N) to not be the same as 1 (oid=N)." do
1170
+ @tc.refute_same 1, 1
1171
+ end
1172
+ end
1173
+
1174
+ def test_skip
1175
+ @assertion_count = 0
1176
+
1177
+ assert_triggered "haha!", Minitest::Skip do
1178
+ @tc.skip "haha!"
1179
+ end
1180
+ end
1181
+
1182
+ def util_msg exp, act, msg = nil
1183
+ s = "Expected: #{exp.inspect}\n Actual: #{act.inspect}"
1184
+ s = "#{msg}.\n#{s}" if msg
1185
+ s
1186
+ end
1187
+
1188
+ def without_diff
1189
+ old_diff = Minitest::Assertions.diff
1190
+ Minitest::Assertions.diff = nil
1191
+
1192
+ yield
1193
+ ensure
1194
+ Minitest::Assertions.diff = old_diff
1195
+ end
1196
+ end
1197
+
1198
+ class TestMinitestAssertionHelpers < Minitest::Test
1199
+ def assert_mu_pp exp, input, raw = false
1200
+ act = mu_pp input
1201
+
1202
+ if String === input && !raw then
1203
+ assert_equal "\"#{exp}\"", act
1204
+ else
1205
+ assert_equal exp, act
1206
+ end
1207
+ end
1208
+
1209
+ def assert_mu_pp_for_diff exp, input, raw = false
1210
+ act = mu_pp_for_diff input
1211
+
1212
+ if String === input && !raw then
1213
+ assert_equal "\"#{exp}\"", act
1214
+ else
1215
+ assert_equal exp, act
1216
+ end
1217
+ end
1218
+
1219
+ def test_diff_equal
1220
+ msg = "No visible difference in the String#inspect output.
1221
+ You should look at the implementation of #== on String or its members.
1222
+ \"blahblahblahblahblahblahblahblahblahblah\"".gsub(/^ +/, "")
1223
+
1224
+ o1 = "blah" * 10
1225
+ o2 = "blah" * 10
1226
+ def o1.== _
1227
+ false
1228
+ end
1229
+
1230
+ assert_equal msg, diff(o1, o2)
1231
+ end
1232
+
1233
+ def test_diff_str_mixed
1234
+ msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
1235
+ --- expected
1236
+ +++ actual
1237
+ @@ -1 +1 @@
1238
+ -"A\\n\nB"
1239
+ +"A\n\\nB"
1240
+ EOM
1241
+
1242
+ exp = "A\\n\nB"
1243
+ act = "A\n\\nB"
1244
+
1245
+ assert_equal msg, diff(exp, act)
1246
+ end
1247
+
1248
+ def test_diff_str_multiline
1249
+ msg = <<-'EOM'.gsub(/^ {10}/, "") # NOTE single quotes on heredoc
1250
+ --- expected
1251
+ +++ actual
1252
+ @@ -1,2 +1,2 @@
1253
+ "A
1254
+ -B"
1255
+ +C"
1256
+ EOM
1257
+
1258
+ exp = "A\nB"
1259
+ act = "A\nC"
1260
+
1261
+ assert_equal msg, diff(exp, act)
1262
+ end
1263
+
1264
+ def test_diff_str_simple
1265
+ msg = <<-'EOM'.gsub(/^ {10}/, "").chomp # NOTE single quotes on heredoc
1266
+ Expected: "A"
1267
+ Actual: "B"
1268
+ EOM
1269
+
1270
+ exp = "A"
1271
+ act = "B"
1272
+
1273
+ assert_equal msg, diff(exp, act)
1274
+ end
1275
+
1276
+ def test_message
1277
+ assert_equal "blah2.", message { "blah2" }.call
1278
+ assert_equal "blah2.", message("") { "blah2" }.call
1279
+ assert_equal "blah1.\nblah2.", message(:blah1) { "blah2" }.call
1280
+ assert_equal "blah1.\nblah2.", message("blah1") { "blah2" }.call
1281
+
1282
+ message = proc { "blah1" }
1283
+ assert_equal "blah1.\nblah2.", message(message) { "blah2" }.call
1284
+
1285
+ message = message { "blah1" }
1286
+ assert_equal "blah1.\nblah2.", message(message) { "blah2" }.call
1287
+ end
1288
+
1289
+ def test_message_deferred
1290
+ var = nil
1291
+
1292
+ msg = message { var = "blah" }
1293
+
1294
+ assert_nil var
1295
+
1296
+ msg.call
1297
+
1298
+ assert_equal "blah", var
1299
+ end
1300
+
1301
+ def test_mu_pp
1302
+ assert_mu_pp 42.inspect, 42
1303
+ assert_mu_pp %w[a b c].inspect, %w[a b c]
1304
+ assert_mu_pp "A B", "A B"
1305
+ assert_mu_pp "A\\nB", "A\nB"
1306
+ assert_mu_pp "A\\\\nB", 'A\nB' # notice single quotes
1307
+ end
1308
+
1309
+ def test_mu_pp_for_diff
1310
+ assert_mu_pp_for_diff "#<Object:0xXXXXXX>", Object.new
1311
+ assert_mu_pp_for_diff "A B", "A B"
1312
+ assert_mu_pp_for_diff [1, 2, 3].inspect, [1, 2, 3]
1313
+ assert_mu_pp_for_diff "A\nB", "A\nB"
1314
+ end
1315
+
1316
+ def test_mu_pp_for_diff_str_bad_encoding
1317
+ str = "\666".force_encoding Encoding::UTF_8
1318
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6\""
1319
+
1320
+ assert_mu_pp_for_diff exp, str, :raw
1321
+ end
1322
+
1323
+ def test_mu_pp_for_diff_str_bad_encoding_both
1324
+ str = "\666A\\n\nB".force_encoding Encoding::UTF_8
1325
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6A\\\\n\\nB\""
1326
+
1327
+ assert_mu_pp_for_diff exp, str, :raw
1328
+ end
1329
+
1330
+ def test_mu_pp_for_diff_str_encoding
1331
+ str = "A\nB".b
1332
+ exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\nB\""
1333
+
1334
+ assert_mu_pp_for_diff exp, str, :raw
1335
+ end
1336
+
1337
+ def test_mu_pp_for_diff_str_encoding_both
1338
+ str = "A\\n\nB".b
1339
+ exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\\\\n\\nB\""
1340
+
1341
+ assert_mu_pp_for_diff exp, str, :raw
1342
+ end
1343
+
1344
+ def test_mu_pp_for_diff_str_nerd
1345
+ assert_mu_pp_for_diff "A\\nB\\\\nC", "A\nB\\nC"
1346
+ assert_mu_pp_for_diff "\\nB\\\\nC", "\nB\\nC"
1347
+ assert_mu_pp_for_diff "\\nB\\\\n", "\nB\\n"
1348
+ assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
1349
+ assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
1350
+ assert_mu_pp_for_diff "\\\\nB\\n", "\\nB\n"
1351
+ assert_mu_pp_for_diff "\\\\nB\\nC", "\\nB\nC"
1352
+ assert_mu_pp_for_diff "A\\\\n\\nB", "A\\n\nB"
1353
+ assert_mu_pp_for_diff "A\\n\\\\nB", "A\n\\nB"
1354
+ assert_mu_pp_for_diff "\\\\n\\n", "\\n\n"
1355
+ assert_mu_pp_for_diff "\\n\\\\n", "\n\\n"
1356
+ end
1357
+
1358
+ def test_mu_pp_for_diff_str_normal
1359
+ assert_mu_pp_for_diff "", ""
1360
+ assert_mu_pp_for_diff "A\\n\n", "A\\n"
1361
+ assert_mu_pp_for_diff "A\\n\nB", "A\\nB"
1362
+ assert_mu_pp_for_diff "A\n", "A\n"
1363
+ assert_mu_pp_for_diff "A\nB", "A\nB"
1364
+ assert_mu_pp_for_diff "\\n\n", "\\n"
1365
+ assert_mu_pp_for_diff "\n", "\n"
1366
+ assert_mu_pp_for_diff "\\n\nA", "\\nA"
1367
+ assert_mu_pp_for_diff "\nA", "\nA"
1368
+ end
1369
+
1370
+ def test_mu_pp_str_bad_encoding
1371
+ str = "\666".force_encoding Encoding::UTF_8
1372
+ exp = "# encoding: UTF-8\n# valid: false\n\"\\xB6\""
1373
+
1374
+ assert_mu_pp exp, str, :raw
1375
+ end
1376
+
1377
+ def test_mu_pp_str_encoding
1378
+ str = "A\nB".b
1379
+ exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\\nB\""
1380
+
1381
+ assert_mu_pp exp, str, :raw
1382
+ end
1383
+
1384
+ def test_mu_pp_str_immutable
1385
+ printer = Class.new { extend Minitest::Assertions }
1386
+ str = "test".freeze
1387
+ assert_equal '"test"', printer.mu_pp(str)
1388
+ end
1389
+ end