minitest 5.12.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,220 @@
1
+ require "minitest" unless defined? Minitest::Runnable
2
+
3
+ module Minitest
4
+ ##
5
+ # Subclass Test to create your own tests. Typically you'll want a
6
+ # Test subclass per implementation class.
7
+ #
8
+ # See Minitest::Assertions
9
+
10
+ class Test < Runnable
11
+ require "minitest/assertions"
12
+ include Minitest::Assertions
13
+ include Minitest::Reportable
14
+
15
+ def class_name # :nodoc:
16
+ self.class.name # for Minitest::Reportable
17
+ end
18
+
19
+ PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, SystemExit] # :nodoc:
20
+
21
+ # :stopdoc:
22
+ class << self; attr_accessor :io_lock; end
23
+ self.io_lock = Mutex.new
24
+ # :startdoc:
25
+
26
+ ##
27
+ # Call this at the top of your tests when you absolutely
28
+ # positively need to have ordered tests. In doing so, you're
29
+ # admitting that you suck and your tests are weak.
30
+
31
+ def self.i_suck_and_my_tests_are_order_dependent!
32
+ class << self
33
+ undef_method :test_order if method_defined? :test_order
34
+ define_method :test_order do :alpha end
35
+ end
36
+ end
37
+
38
+ ##
39
+ # Make diffs for this Test use #pretty_inspect so that diff
40
+ # in assert_equal can have more details. NOTE: this is much slower
41
+ # than the regular inspect but much more usable for complex
42
+ # objects.
43
+
44
+ def self.make_my_diffs_pretty!
45
+ require "pp"
46
+
47
+ define_method :mu_pp, &:pretty_inspect
48
+ end
49
+
50
+ ##
51
+ # Call this at the top of your tests when you want to run your
52
+ # tests in parallel. In doing so, you're admitting that you rule
53
+ # and your tests are awesome.
54
+
55
+ def self.parallelize_me!
56
+ include Minitest::Parallel::Test
57
+ extend Minitest::Parallel::Test::ClassMethods
58
+ end
59
+
60
+ ##
61
+ # Returns all instance methods starting with "test_". Based on
62
+ # #test_order, the methods are either sorted, randomized
63
+ # (default), or run in parallel.
64
+
65
+ def self.runnable_methods
66
+ methods = methods_matching(/^test_/)
67
+
68
+ case self.test_order
69
+ when :random, :parallel then
70
+ max = methods.size
71
+ methods.sort.sort_by { rand max }
72
+ when :alpha, :sorted then
73
+ methods.sort
74
+ else
75
+ raise "Unknown test_order: #{self.test_order.inspect}"
76
+ end
77
+ end
78
+
79
+ ##
80
+ # Defines the order to run tests (:random by default). Override
81
+ # this or use a convenience method to change it for your tests.
82
+
83
+ def self.test_order
84
+ :random
85
+ end
86
+
87
+ TEARDOWN_METHODS = %w[ before_teardown teardown after_teardown ] # :nodoc:
88
+
89
+ ##
90
+ # Runs a single test with setup/teardown hooks.
91
+
92
+ def run
93
+ with_info_handler do
94
+ time_it do
95
+ capture_exceptions do
96
+ before_setup; setup; after_setup
97
+
98
+ self.send self.name
99
+ end
100
+
101
+ TEARDOWN_METHODS.each do |hook|
102
+ capture_exceptions do
103
+ self.send hook
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ Result.from self # per contract
110
+ end
111
+
112
+ ##
113
+ # Provides before/after hooks for setup and teardown. These are
114
+ # meant for library writers, NOT for regular test authors. See
115
+ # #before_setup for an example.
116
+
117
+ module LifecycleHooks
118
+
119
+ ##
120
+ # Runs before every test, before setup. This hook is meant for
121
+ # libraries to extend minitest. It is not meant to be used by
122
+ # test developers.
123
+ #
124
+ # As a simplistic example:
125
+ #
126
+ # module MyMinitestPlugin
127
+ # def before_setup
128
+ # super
129
+ # # ... stuff to do before setup is run
130
+ # end
131
+ #
132
+ # def after_setup
133
+ # # ... stuff to do after setup is run
134
+ # super
135
+ # end
136
+ #
137
+ # def before_teardown
138
+ # super
139
+ # # ... stuff to do before teardown is run
140
+ # end
141
+ #
142
+ # def after_teardown
143
+ # # ... stuff to do after teardown is run
144
+ # super
145
+ # end
146
+ # end
147
+ #
148
+ # class MiniTest::Test
149
+ # include MyMinitestPlugin
150
+ # end
151
+
152
+ def before_setup; end
153
+
154
+ ##
155
+ # Runs before every test. Use this to set up before each test
156
+ # run.
157
+
158
+ def setup; end
159
+
160
+ ##
161
+ # Runs before every test, after setup. This hook is meant for
162
+ # libraries to extend minitest. It is not meant to be used by
163
+ # test developers.
164
+ #
165
+ # See #before_setup for an example.
166
+
167
+ def after_setup; end
168
+
169
+ ##
170
+ # Runs after every test, before teardown. This hook is meant for
171
+ # libraries to extend minitest. It is not meant to be used by
172
+ # test developers.
173
+ #
174
+ # See #before_setup for an example.
175
+
176
+ def before_teardown; end
177
+
178
+ ##
179
+ # Runs after every test. Use this to clean up after each test
180
+ # run.
181
+
182
+ def teardown; end
183
+
184
+ ##
185
+ # Runs after every test, after teardown. This hook is meant for
186
+ # libraries to extend minitest. It is not meant to be used by
187
+ # test developers.
188
+ #
189
+ # See #before_setup for an example.
190
+
191
+ def after_teardown; end
192
+ end # LifecycleHooks
193
+
194
+ def capture_exceptions # :nodoc:
195
+ yield
196
+ rescue *PASSTHROUGH_EXCEPTIONS
197
+ raise
198
+ rescue Assertion => e
199
+ self.failures << e
200
+ rescue Exception => e
201
+ self.failures << UnexpectedError.new(e)
202
+ end
203
+
204
+ def with_info_handler &block # :nodoc:
205
+ t0 = Minitest.clock_time
206
+
207
+ handler = lambda do
208
+ warn "\nCurrent: %s#%s %.2fs" % [self.class, self.name, Minitest.clock_time - t0]
209
+ end
210
+
211
+ self.class.on_signal ::Minitest.info_signal, handler, &block
212
+ end
213
+
214
+ include LifecycleHooks
215
+ include Guard
216
+ extend Guard
217
+ end # Test
218
+ end
219
+
220
+ require "minitest/unit" unless defined?(MiniTest) # compatibility layer only
@@ -0,0 +1,45 @@
1
+ # :stopdoc:
2
+
3
+ unless defined?(Minitest) then
4
+ # all of this crap is just to avoid circular requires and is only
5
+ # needed if a user requires "minitest/unit" directly instead of
6
+ # "minitest/autorun", so we also warn
7
+
8
+ from = caller.reject { |s| s =~ /rubygems/ }.join("\n ")
9
+ warn "Warning: you should require 'minitest/autorun' instead."
10
+ warn %(Warning: or add 'gem "minitest"' before 'require "minitest/autorun"')
11
+ warn "From:\n #{from}"
12
+
13
+ module Minitest; end
14
+ MiniTest = Minitest # prevents minitest.rb from requiring back to us
15
+ require "minitest"
16
+ end
17
+
18
+ MiniTest = Minitest unless defined?(MiniTest)
19
+
20
+ module Minitest
21
+ class Unit
22
+ VERSION = Minitest::VERSION
23
+ class TestCase < Minitest::Test
24
+ def self.inherited klass # :nodoc:
25
+ from = caller.first
26
+ warn "MiniTest::Unit::TestCase is now Minitest::Test. From #{from}"
27
+ super
28
+ end
29
+ end
30
+
31
+ def self.autorun # :nodoc:
32
+ from = caller.first
33
+ warn "MiniTest::Unit.autorun is now Minitest.autorun. From #{from}"
34
+ Minitest.autorun
35
+ end
36
+
37
+ def self.after_tests &b # :nodoc:
38
+ from = caller.first
39
+ warn "MiniTest::Unit.after_tests is now Minitest.after_run. From #{from}"
40
+ Minitest.after_run(&b)
41
+ end
42
+ end
43
+ end
44
+
45
+ # :startdoc:
@@ -0,0 +1,105 @@
1
+ require "tempfile"
2
+ require "stringio"
3
+ require "minitest/autorun"
4
+
5
+ class Minitest::Test
6
+ def clean s
7
+ s.gsub(/^ {6}/, "")
8
+ end
9
+ end
10
+
11
+ class FakeNamedTest < Minitest::Test
12
+ @@count = 0
13
+
14
+ def self.name
15
+ @fake_name ||= begin
16
+ @@count += 1
17
+ "FakeNamedTest%02d" % @@count
18
+ end
19
+ end
20
+ end
21
+
22
+ module MyModule; end
23
+ class AnError < StandardError; include MyModule; end
24
+
25
+ class MetaMetaMetaTestCase < Minitest::Test
26
+ attr_accessor :reporter, :output, :tu
27
+
28
+ def run_tu_with_fresh_reporter flags = %w[--seed 42]
29
+ options = Minitest.process_args flags
30
+
31
+ @output = StringIO.new("".encode('UTF-8'))
32
+
33
+ self.reporter = Minitest::CompositeReporter.new
34
+ reporter << Minitest::SummaryReporter.new(@output, options)
35
+ reporter << Minitest::ProgressReporter.new(@output, options)
36
+
37
+ reporter.start
38
+
39
+ yield(reporter) if block_given?
40
+
41
+ @tus ||= [@tu]
42
+ @tus.each do |tu|
43
+ Minitest::Runnable.runnables.delete tu
44
+
45
+ tu.run reporter, options
46
+ end
47
+
48
+ reporter.report
49
+ end
50
+
51
+ def first_reporter
52
+ reporter.reporters.first
53
+ end
54
+
55
+ def assert_report expected, flags = %w[--seed 42], &block
56
+ header = clean <<-EOM
57
+ Run options: #{flags.map { |s| s =~ /\|/ ? s.inspect : s }.join " "}
58
+
59
+ # Running:
60
+
61
+ EOM
62
+
63
+ run_tu_with_fresh_reporter flags, &block
64
+
65
+ output = normalize_output @output.string.dup
66
+
67
+ assert_equal header + expected, output
68
+ end
69
+
70
+ def normalize_output output
71
+ output.sub!(/Finished in .*/, "Finished in 0.00")
72
+ output.sub!(/Loaded suite .*/, "Loaded suite blah")
73
+
74
+ output.gsub!(/FakeNamedTest\d+/, "FakeNamedTestXX")
75
+ output.gsub!(/ = \d+.\d\d s = /, " = 0.00 s = ")
76
+ output.gsub!(/0x[A-Fa-f0-9]+/, "0xXXX")
77
+ output.gsub!(/ +$/, "")
78
+
79
+ if windows? then
80
+ output.gsub!(/\[(?:[A-Za-z]:)?[^\]:]+:\d+\]/, "[FILE:LINE]")
81
+ output.gsub!(/^(\s+)(?:[A-Za-z]:)?[^:]+:\d+:in/, '\1FILE:LINE:in')
82
+ else
83
+ output.gsub!(/\[[^\]:]+:\d+\]/, "[FILE:LINE]")
84
+ output.gsub!(/^(\s+)[^:]+:\d+:in/, '\1FILE:LINE:in')
85
+ end
86
+
87
+ output
88
+ end
89
+
90
+ def restore_env
91
+ old_value = ENV["MT_NO_SKIP_MSG"]
92
+ ENV.delete "MT_NO_SKIP_MSG"
93
+
94
+ yield
95
+ ensure
96
+ ENV["MT_NO_SKIP_MSG"] = old_value
97
+ end
98
+
99
+ def setup
100
+ super
101
+ srand 42
102
+ Minitest::Test.reset
103
+ @tu = nil
104
+ end
105
+ end
@@ -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