minitest 5.11.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,299 @@
1
+ require "minitest/autorun"
2
+ require "minitest/metametameta"
3
+
4
+ class Runnable
5
+ def woot
6
+ assert true
7
+ end
8
+ end
9
+
10
+ class TestMinitestReporter < MetaMetaMetaTestCase
11
+
12
+ attr_accessor :r, :io
13
+
14
+ def new_composite_reporter
15
+ reporter = Minitest::CompositeReporter.new
16
+ reporter << Minitest::SummaryReporter.new(self.io)
17
+ reporter << Minitest::ProgressReporter.new(self.io)
18
+
19
+ def reporter.first
20
+ reporters.first
21
+ end
22
+
23
+ def reporter.results
24
+ first.results
25
+ end
26
+
27
+ def reporter.count
28
+ first.count
29
+ end
30
+
31
+ def reporter.assertions
32
+ first.assertions
33
+ end
34
+
35
+ reporter
36
+ end
37
+
38
+ def setup
39
+ self.io = StringIO.new("")
40
+ self.r = new_composite_reporter
41
+ end
42
+
43
+ def error_test
44
+ unless defined? @et then
45
+ @et = Minitest::Test.new(:woot)
46
+ @et.failures << Minitest::UnexpectedError.new(begin
47
+ raise "no"
48
+ rescue => e
49
+ e
50
+ end)
51
+ @et = Minitest::Result.from @et
52
+ end
53
+ @et
54
+ end
55
+
56
+ def fail_test
57
+ unless defined? @ft then
58
+ @ft = Minitest::Test.new(:woot)
59
+ @ft.failures << begin
60
+ raise Minitest::Assertion, "boo"
61
+ rescue Minitest::Assertion => e
62
+ e
63
+ end
64
+ @ft = Minitest::Result.from @ft
65
+ end
66
+ @ft
67
+ end
68
+
69
+ def passing_test
70
+ @pt ||= Minitest::Result.from Minitest::Test.new(:woot)
71
+ end
72
+
73
+ def skip_test
74
+ unless defined? @st then
75
+ @st = Minitest::Test.new(:woot)
76
+ @st.failures << begin
77
+ raise Minitest::Skip
78
+ rescue Minitest::Assertion => e
79
+ e
80
+ end
81
+ @st = Minitest::Result.from @st
82
+ end
83
+ @st
84
+ end
85
+
86
+ def test_to_s
87
+ r.record passing_test
88
+ r.record fail_test
89
+ assert_match "woot", r.first.to_s
90
+ end
91
+
92
+ def test_passed_eh_empty
93
+ assert_predicate r, :passed?
94
+ end
95
+
96
+ def test_passed_eh_failure
97
+ r.results << fail_test
98
+
99
+ refute_predicate r, :passed?
100
+ end
101
+
102
+ SKIP_MSG = "\n\nYou have skipped tests. Run with --verbose for details."
103
+
104
+ def test_passed_eh_error
105
+ r.start
106
+
107
+ r.results << error_test
108
+
109
+ refute_predicate r, :passed?
110
+
111
+ r.report
112
+
113
+ refute_match SKIP_MSG, io.string
114
+ end
115
+
116
+ def test_passed_eh_skipped
117
+ r.start
118
+ r.results << skip_test
119
+ assert r.passed?
120
+
121
+ restore_env do
122
+ r.report
123
+ end
124
+
125
+ assert_match SKIP_MSG, io.string
126
+ end
127
+
128
+ def test_passed_eh_skipped_verbose
129
+ r.first.options[:verbose] = true
130
+
131
+ r.start
132
+ r.results << skip_test
133
+ assert r.passed?
134
+ r.report
135
+
136
+ refute_match SKIP_MSG, io.string
137
+ end
138
+
139
+ def test_start
140
+ r.start
141
+
142
+ exp = "Run options: \n\n# Running:\n\n"
143
+
144
+ assert_equal exp, io.string
145
+ end
146
+
147
+ def test_record_pass
148
+ r.record passing_test
149
+
150
+ assert_equal ".", io.string
151
+ assert_empty r.results
152
+ assert_equal 1, r.count
153
+ assert_equal 0, r.assertions
154
+ end
155
+
156
+ def test_record_fail
157
+ fail_test = self.fail_test
158
+ r.record fail_test
159
+
160
+ assert_equal "F", io.string
161
+ assert_equal [fail_test], r.results
162
+ assert_equal 1, r.count
163
+ assert_equal 0, r.assertions
164
+ end
165
+
166
+ def test_record_error
167
+ error_test = self.error_test
168
+ r.record error_test
169
+
170
+ assert_equal "E", io.string
171
+ assert_equal [error_test], r.results
172
+ assert_equal 1, r.count
173
+ assert_equal 0, r.assertions
174
+ end
175
+
176
+ def test_record_skip
177
+ skip_test = self.skip_test
178
+ r.record skip_test
179
+
180
+ assert_equal "S", io.string
181
+ assert_equal [skip_test], r.results
182
+ assert_equal 1, r.count
183
+ assert_equal 0, r.assertions
184
+ end
185
+
186
+ def test_report_empty
187
+ r.start
188
+ r.report
189
+
190
+ exp = clean <<-EOM
191
+ Run options:
192
+
193
+ # Running:
194
+
195
+
196
+
197
+ Finished in 0.00
198
+
199
+ 0 runs, 0 assertions, 0 failures, 0 errors, 0 skips
200
+ EOM
201
+
202
+ assert_equal exp, normalize_output(io.string)
203
+ end
204
+
205
+ def test_report_passing
206
+ r.start
207
+ r.record passing_test
208
+ r.report
209
+
210
+ exp = clean <<-EOM
211
+ Run options:
212
+
213
+ # Running:
214
+
215
+ .
216
+
217
+ Finished in 0.00
218
+
219
+ 1 runs, 0 assertions, 0 failures, 0 errors, 0 skips
220
+ EOM
221
+
222
+ assert_equal exp, normalize_output(io.string)
223
+ end
224
+
225
+ def test_report_failure
226
+ r.start
227
+ r.record fail_test
228
+ r.report
229
+
230
+ exp = clean <<-EOM
231
+ Run options:
232
+
233
+ # Running:
234
+
235
+ F
236
+
237
+ Finished in 0.00
238
+
239
+ 1) Failure:
240
+ Minitest::Test#woot [FILE:LINE]:
241
+ boo
242
+
243
+ 1 runs, 0 assertions, 1 failures, 0 errors, 0 skips
244
+ EOM
245
+
246
+ assert_equal exp, normalize_output(io.string)
247
+ end
248
+
249
+ def test_report_error
250
+ r.start
251
+ r.record error_test
252
+ r.report
253
+
254
+ exp = clean <<-EOM
255
+ Run options:
256
+
257
+ # Running:
258
+
259
+ E
260
+
261
+ Finished in 0.00
262
+
263
+ 1) Error:
264
+ Minitest::Test#woot:
265
+ RuntimeError: no
266
+ FILE:LINE:in `error_test'
267
+ FILE:LINE:in `test_report_error'
268
+
269
+ 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
270
+ EOM
271
+
272
+ assert_equal exp, normalize_output(io.string)
273
+ end
274
+
275
+ def test_report_skipped
276
+ r.start
277
+ r.record skip_test
278
+
279
+ restore_env do
280
+ r.report
281
+ end
282
+
283
+ exp = clean <<-EOM
284
+ Run options:
285
+
286
+ # Running:
287
+
288
+ S
289
+
290
+ Finished in 0.00
291
+
292
+ 1 runs, 0 assertions, 0 failures, 0 errors, 1 skips
293
+
294
+ You have skipped tests. Run with --verbose for details.
295
+ EOM
296
+
297
+ assert_equal exp, normalize_output(io.string)
298
+ end
299
+ end
@@ -0,0 +1,987 @@
1
+ # encoding: utf-8
2
+ require "minitest/autorun"
3
+ require "stringio"
4
+
5
+ class MiniSpecA < Minitest::Spec; end
6
+ class MiniSpecB < Minitest::Test; extend Minitest::Spec::DSL; end
7
+ class MiniSpecC < MiniSpecB; end
8
+ class NamedExampleA < MiniSpecA; end
9
+ class NamedExampleB < MiniSpecB; end
10
+ class NamedExampleC < MiniSpecC; end
11
+ class ExampleA; end
12
+ class ExampleB < ExampleA; end
13
+
14
+ describe Minitest::Spec do
15
+ # helps to deal with 2.4 deprecation of Fixnum for Integer
16
+ Int = 1.class
17
+
18
+ # do not parallelize this suite... it just can"t handle it.
19
+
20
+ def assert_triggered expected = "blah", klass = Minitest::Assertion
21
+ @assertion_count += 1
22
+
23
+ e = assert_raises(klass) do
24
+ yield
25
+ end
26
+
27
+ msg = e.message.sub(/(---Backtrace---).*/m, '\1')
28
+ msg.gsub!(/\(oid=[-0-9]+\)/, "(oid=N)")
29
+ msg.gsub!(/@.+>/, "@PATH>")
30
+ msg.gsub!(/(\d\.\d{6})\d+/, '\1xxx') # normalize: ruby version, impl, platform
31
+ msg.gsub!(/:0x[a-fA-F0-9]{4,}/m, ":0xXXXXXX")
32
+
33
+ if expected
34
+ @assertion_count += 1
35
+ case expected
36
+ when String then
37
+ assert_equal expected, msg
38
+ when Regexp then
39
+ @assertion_count += 1
40
+ assert_match expected, msg
41
+ else
42
+ flunk "Unknown: #{expected.inspect}"
43
+ end
44
+ end
45
+ end
46
+
47
+ before do
48
+ @assertion_count = 4
49
+ end
50
+
51
+ after do
52
+ _(self.assertions).must_equal @assertion_count if passed? and not skipped?
53
+ end
54
+
55
+ it "needs to be able to catch a Minitest::Assertion exception" do
56
+ @assertion_count = 1
57
+
58
+ assert_triggered "Expected 1 to not be equal to 1." do
59
+ 1.wont_equal 1
60
+ end
61
+ end
62
+
63
+ it "needs to be sensible about must_include order" do
64
+ @assertion_count += 3 # must_include is 2 assertions
65
+
66
+ [1, 2, 3].must_include(2).must_equal true
67
+
68
+ assert_triggered "Expected [1, 2, 3] to include 5." do
69
+ [1, 2, 3].must_include 5
70
+ end
71
+
72
+ assert_triggered "msg.\nExpected [1, 2, 3] to include 5." do
73
+ [1, 2, 3].must_include 5, "msg"
74
+ end
75
+ end
76
+
77
+ it "needs to be sensible about wont_include order" do
78
+ @assertion_count += 3 # wont_include is 2 assertions
79
+
80
+ [1, 2, 3].wont_include(5).must_equal false
81
+
82
+ assert_triggered "Expected [1, 2, 3] to not include 2." do
83
+ [1, 2, 3].wont_include 2
84
+ end
85
+
86
+ assert_triggered "msg.\nExpected [1, 2, 3] to not include 2." do
87
+ [1, 2, 3].wont_include 2, "msg"
88
+ end
89
+ end
90
+
91
+ it "needs to catch an expected exception" do
92
+ @assertion_count = 2
93
+
94
+ proc { raise "blah" }.must_raise RuntimeError
95
+ proc { raise Minitest::Assertion }.must_raise Minitest::Assertion
96
+ end
97
+
98
+ it "needs to catch an unexpected exception" do
99
+ @assertion_count -= 2 # no positive
100
+
101
+ msg = <<-EOM.gsub(/^ {6}/, "").chomp
102
+ [RuntimeError] exception expected, not
103
+ Class: <StandardError>
104
+ Message: <"woot">
105
+ ---Backtrace---
106
+ EOM
107
+
108
+ assert_triggered msg do
109
+ proc { raise StandardError, "woot" }.must_raise RuntimeError
110
+ end
111
+
112
+ assert_triggered "msg.\n#{msg}" do
113
+ proc { raise StandardError, "woot" }.must_raise RuntimeError, "msg"
114
+ end
115
+ end
116
+
117
+ it "needs to ensure silence" do
118
+ @assertion_count -= 1 # no msg
119
+ @assertion_count += 2 # assert_output is 2 assertions
120
+
121
+ proc {}.must_be_silent.must_equal true
122
+
123
+ assert_triggered "In stdout.\nExpected: \"\"\n Actual: \"xxx\"" do
124
+ proc { print "xxx" }.must_be_silent
125
+ end
126
+ end
127
+
128
+ it "needs to have all methods named well" do
129
+ @assertion_count = 2
130
+
131
+ methods = Object.public_instance_methods.find_all { |n| n =~ /^must|^wont/ }
132
+ methods.map!(&:to_s) if Symbol === methods.first
133
+
134
+ musts, wonts = methods.sort.partition { |m| m =~ /^must/ }
135
+
136
+ expected_musts = %w[must_be
137
+ must_be_close_to
138
+ must_be_empty
139
+ must_be_instance_of
140
+ must_be_kind_of
141
+ must_be_nil
142
+ must_be_same_as
143
+ must_be_silent
144
+ must_be_within_delta
145
+ must_be_within_epsilon
146
+ must_equal
147
+ must_include
148
+ must_match
149
+ must_output
150
+ must_raise
151
+ must_respond_to
152
+ must_throw]
153
+
154
+ bad = %w[not raise throw send output be_silent]
155
+
156
+ expected_wonts = expected_musts.map { |m| m.sub(/^must/, "wont") }
157
+ expected_wonts.reject! { |m| m =~ /wont_#{Regexp.union(*bad)}/ }
158
+
159
+ musts.must_equal expected_musts
160
+ wonts.must_equal expected_wonts
161
+ end
162
+
163
+ it "needs to raise if an expected exception is not raised" do
164
+ @assertion_count -= 2 # no positive test
165
+
166
+ assert_triggered "RuntimeError expected but nothing was raised." do
167
+ proc { 42 }.must_raise RuntimeError
168
+ end
169
+
170
+ assert_triggered "msg.\nRuntimeError expected but nothing was raised." do
171
+ proc { 42 }.must_raise RuntimeError, "msg"
172
+ end
173
+ end
174
+
175
+ it "needs to verify binary messages" do
176
+ 42.wont_be(:<, 24).must_equal false
177
+
178
+ assert_triggered "Expected 24 to not be < 42." do
179
+ 24.wont_be :<, 42
180
+ end
181
+
182
+ assert_triggered "msg.\nExpected 24 to not be < 42." do
183
+ 24.wont_be :<, 42, "msg"
184
+ end
185
+ end
186
+
187
+ it "needs to verify emptyness" do
188
+ @assertion_count += 3 # empty is 2 assertions
189
+
190
+ [].must_be_empty.must_equal true
191
+
192
+ assert_triggered "Expected [42] to be empty." do
193
+ [42].must_be_empty
194
+ end
195
+
196
+ assert_triggered "msg.\nExpected [42] to be empty." do
197
+ [42].must_be_empty "msg"
198
+ end
199
+ end
200
+
201
+ it "needs to verify equality" do
202
+ @assertion_count += 1
203
+
204
+ (6 * 7).must_equal(42).must_equal true
205
+
206
+ assert_triggered "Expected: 42\n Actual: 54" do
207
+ (6 * 9).must_equal 42
208
+ end
209
+
210
+ assert_triggered "msg.\nExpected: 42\n Actual: 54" do
211
+ (6 * 9).must_equal 42, "msg"
212
+ end
213
+
214
+ assert_triggered(/^-42\n\+#<Proc:0xXXXXXX@PATH>\n/) do
215
+ proc { 42 }.must_equal 42 # proc isn't called, so expectation fails
216
+ end
217
+ end
218
+
219
+ it "needs to warn on equality with nil" do
220
+ @assertion_count += 1 # extra test
221
+
222
+ out, err = capture_io do
223
+ nil.must_equal(nil).must_equal true
224
+ end
225
+
226
+ exp = "DEPRECATED: Use assert_nil if expecting nil from #{__FILE__}:#{__LINE__-3}. " \
227
+ "This will fail in Minitest 6.\n"
228
+ exp = "" if $-w.nil?
229
+
230
+ assert_empty out
231
+ assert_equal exp, err
232
+ end
233
+
234
+ it "needs to verify floats outside a delta" do
235
+ @assertion_count += 1 # extra test
236
+
237
+ 24.wont_be_close_to(42).must_equal false
238
+
239
+ assert_triggered "Expected |42 - 42.0| (0.0) to not be <= 0.001." do
240
+ (6 * 7.0).wont_be_close_to 42
241
+ end
242
+
243
+ x = maglev? ? "1.0000000000000001e-05" : "1.0e-05"
244
+ assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do
245
+ (6 * 7.0).wont_be_close_to 42, 0.00001
246
+ end
247
+
248
+ assert_triggered "msg.\nExpected |42 - 42.0| (0.0) to not be <= #{x}." do
249
+ (6 * 7.0).wont_be_close_to 42, 0.00001, "msg"
250
+ end
251
+ end
252
+
253
+ it "needs to verify floats outside an epsilon" do
254
+ @assertion_count += 1 # extra test
255
+
256
+ 24.wont_be_within_epsilon(42).must_equal false
257
+
258
+ x = maglev? ? "0.042000000000000003" : "0.042"
259
+ assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do
260
+ (6 * 7.0).wont_be_within_epsilon 42
261
+ end
262
+
263
+ x = maglev? ? "0.00042000000000000002" : "0.00042"
264
+ assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do
265
+ (6 * 7.0).wont_be_within_epsilon 42, 0.00001
266
+ end
267
+
268
+ assert_triggered "msg.\nExpected |42 - 42.0| (0.0) to not be <= #{x}." do
269
+ (6 * 7.0).wont_be_within_epsilon 42, 0.00001, "msg"
270
+ end
271
+ end
272
+
273
+ it "needs to verify floats within a delta" do
274
+ @assertion_count += 1 # extra test
275
+
276
+ (6.0 * 7).must_be_close_to(42.0).must_equal true
277
+
278
+ assert_triggered "Expected |0.0 - 0.01| (0.01) to be <= 0.001." do
279
+ (1.0 / 100).must_be_close_to 0.0
280
+ end
281
+
282
+ x = maglev? ? "9.9999999999999995e-07" : "1.0e-06"
283
+ assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= #{x}." do
284
+ (1.0 / 1000).must_be_close_to 0.0, 0.000001
285
+ end
286
+
287
+ assert_triggered "msg.\nExpected |0.0 - 0.001| (0.001) to be <= #{x}." do
288
+ (1.0 / 1000).must_be_close_to 0.0, 0.000001, "msg"
289
+ end
290
+ end
291
+
292
+ it "needs to verify floats within an epsilon" do
293
+ @assertion_count += 1 # extra test
294
+
295
+ (6.0 * 7).must_be_within_epsilon(42.0).must_equal true
296
+
297
+ assert_triggered "Expected |0.0 - 0.01| (0.01) to be <= 0.0." do
298
+ (1.0 / 100).must_be_within_epsilon 0.0
299
+ end
300
+
301
+ assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= 0.0." do
302
+ (1.0 / 1000).must_be_within_epsilon 0.0, 0.000001
303
+ end
304
+
305
+ assert_triggered "msg.\nExpected |0.0 - 0.001| (0.001) to be <= 0.0." do
306
+ (1.0 / 1000).must_be_within_epsilon 0.0, 0.000001, "msg"
307
+ end
308
+ end
309
+
310
+ it "needs to verify identity" do
311
+ 1.must_be_same_as(1).must_equal true
312
+
313
+ assert_triggered "Expected 1 (oid=N) to be the same as 2 (oid=N)." do
314
+ 1.must_be_same_as 2
315
+ end
316
+
317
+ assert_triggered "msg.\nExpected 1 (oid=N) to be the same as 2 (oid=N)." do
318
+ 1.must_be_same_as 2, "msg"
319
+ end
320
+ end
321
+
322
+ it "needs to verify inequality" do
323
+ @assertion_count += 2
324
+ 42.wont_equal(6 * 9).must_equal false
325
+ proc {}.wont_equal(42).must_equal false
326
+
327
+ assert_triggered "Expected 1 to not be equal to 1." do
328
+ 1.wont_equal 1
329
+ end
330
+
331
+ assert_triggered "msg.\nExpected 1 to not be equal to 1." do
332
+ 1.wont_equal 1, "msg"
333
+ end
334
+ end
335
+
336
+ it "needs to verify instances of a class" do
337
+ 42.wont_be_instance_of(String).must_equal false
338
+
339
+ assert_triggered "Expected 42 to not be a kind of #{Int.name}." do
340
+ 42.wont_be_kind_of Int
341
+ end
342
+
343
+ assert_triggered "msg.\nExpected 42 to not be an instance of #{Int.name}." do
344
+ 42.wont_be_instance_of Int, "msg"
345
+ end
346
+ end
347
+
348
+ it "needs to verify kinds of a class" do
349
+ @assertion_count += 2
350
+
351
+ 42.wont_be_kind_of(String).must_equal false
352
+ proc {}.wont_be_kind_of(String).must_equal false
353
+
354
+ assert_triggered "Expected 42 to not be a kind of #{Int.name}." do
355
+ 42.wont_be_kind_of Int
356
+ end
357
+
358
+ assert_triggered "msg.\nExpected 42 to not be a kind of #{Int.name}." do
359
+ 42.wont_be_kind_of Int, "msg"
360
+ end
361
+ end
362
+
363
+ it "needs to verify kinds of objects" do
364
+ @assertion_count += 3 # extra test
365
+
366
+ (6 * 7).must_be_kind_of(Int).must_equal true
367
+ (6 * 7).must_be_kind_of(Numeric).must_equal true
368
+
369
+ assert_triggered "Expected 42 to be a kind of String, not #{Int.name}." do
370
+ (6 * 7).must_be_kind_of String
371
+ end
372
+
373
+ assert_triggered "msg.\nExpected 42 to be a kind of String, not #{Int.name}." do
374
+ (6 * 7).must_be_kind_of String, "msg"
375
+ end
376
+
377
+ exp = "Expected #<Proc:0xXXXXXX@PATH> to be a kind of String, not Proc."
378
+ assert_triggered exp do
379
+ proc {}.must_be_kind_of String
380
+ end
381
+ end
382
+
383
+ it "needs to verify mismatch" do
384
+ @assertion_count += 3 # match is 2
385
+
386
+ "blah".wont_match(/\d+/).must_equal false
387
+
388
+ assert_triggered "Expected /\\w+/ to not match \"blah\"." do
389
+ "blah".wont_match(/\w+/)
390
+ end
391
+
392
+ assert_triggered "msg.\nExpected /\\w+/ to not match \"blah\"." do
393
+ "blah".wont_match(/\w+/, "msg")
394
+ end
395
+ end
396
+
397
+ it "needs to verify nil" do
398
+ nil.must_be_nil.must_equal true
399
+
400
+ assert_triggered "Expected 42 to be nil." do
401
+ 42.must_be_nil
402
+ end
403
+
404
+ assert_triggered "msg.\nExpected 42 to be nil." do
405
+ 42.must_be_nil "msg"
406
+ end
407
+ end
408
+
409
+ it "needs to verify non-emptyness" do
410
+ @assertion_count += 3 # empty is 2 assertions
411
+
412
+ ["some item"].wont_be_empty.must_equal false
413
+
414
+ assert_triggered "Expected [] to not be empty." do
415
+ [].wont_be_empty
416
+ end
417
+
418
+ assert_triggered "msg.\nExpected [] to not be empty." do
419
+ [].wont_be_empty "msg"
420
+ end
421
+ end
422
+
423
+ it "needs to verify non-identity" do
424
+ 1.wont_be_same_as(2).must_equal false
425
+
426
+ assert_triggered "Expected 1 (oid=N) to not be the same as 1 (oid=N)." do
427
+ 1.wont_be_same_as 1
428
+ end
429
+
430
+ assert_triggered "msg.\nExpected 1 (oid=N) to not be the same as 1 (oid=N)." do
431
+ 1.wont_be_same_as 1, "msg"
432
+ end
433
+ end
434
+
435
+ it "needs to verify non-nil" do
436
+ 42.wont_be_nil.must_equal false
437
+
438
+ assert_triggered "Expected nil to not be nil." do
439
+ nil.wont_be_nil
440
+ end
441
+
442
+ assert_triggered "msg.\nExpected nil to not be nil." do
443
+ nil.wont_be_nil "msg"
444
+ end
445
+ end
446
+
447
+ it "needs to verify objects not responding to a message" do
448
+ "".wont_respond_to(:woot!).must_equal false
449
+
450
+ assert_triggered "Expected \"\" to not respond to to_s." do
451
+ "".wont_respond_to :to_s
452
+ end
453
+
454
+ assert_triggered "msg.\nExpected \"\" to not respond to to_s." do
455
+ "".wont_respond_to :to_s, "msg"
456
+ end
457
+ end
458
+
459
+ it "needs to verify output in stderr" do
460
+ @assertion_count -= 1 # no msg
461
+
462
+ proc { $stderr.print "blah" }.must_output(nil, "blah").must_equal true
463
+
464
+ assert_triggered "In stderr.\nExpected: \"blah\"\n Actual: \"xxx\"" do
465
+ proc { $stderr.print "xxx" }.must_output(nil, "blah")
466
+ end
467
+ end
468
+
469
+ it "needs to verify output in stdout" do
470
+ @assertion_count -= 1 # no msg
471
+
472
+ proc { print "blah" }.must_output("blah").must_equal true
473
+
474
+ assert_triggered "In stdout.\nExpected: \"blah\"\n Actual: \"xxx\"" do
475
+ proc { print "xxx" }.must_output("blah")
476
+ end
477
+ end
478
+
479
+ it "needs to verify regexp matches" do
480
+ @assertion_count += 3 # must_match is 2 assertions
481
+
482
+ "blah".must_match(/\w+/).must_equal true
483
+
484
+ assert_triggered "Expected /\\d+/ to match \"blah\"." do
485
+ "blah".must_match(/\d+/)
486
+ end
487
+
488
+ assert_triggered "msg.\nExpected /\\d+/ to match \"blah\"." do
489
+ "blah".must_match(/\d+/, "msg")
490
+ end
491
+ end
492
+
493
+ describe "expect" do
494
+ before do
495
+ @assertion_count -= 3
496
+ end
497
+
498
+ it "can use expect" do
499
+ _(1 + 1).must_equal 2
500
+ end
501
+
502
+ it "can use expect with a lambda" do
503
+ _ { raise "blah" }.must_raise RuntimeError
504
+ end
505
+
506
+ it "can use expect in a thread" do
507
+ Thread.new { _(1 + 1).must_equal 2 }.join
508
+ end
509
+
510
+ it "can NOT use must_equal in a thread. It must use expect in a thread" do
511
+ assert_raises NoMethodError do
512
+ capture_io do
513
+ Thread.new { (1 + 1).must_equal 2 }.join
514
+ end
515
+ end
516
+ end
517
+ end
518
+
519
+ it "needs to verify throw" do
520
+ @assertion_count += 2 # 2 extra tests
521
+
522
+ proc { throw :blah }.must_throw(:blah).must_equal true
523
+
524
+ assert_triggered "Expected :blah to have been thrown." do
525
+ proc {}.must_throw :blah
526
+ end
527
+
528
+ assert_triggered "Expected :blah to have been thrown, not :xxx." do
529
+ proc { throw :xxx }.must_throw :blah
530
+ end
531
+
532
+ assert_triggered "msg.\nExpected :blah to have been thrown." do
533
+ proc {}.must_throw :blah, "msg"
534
+ end
535
+
536
+ assert_triggered "msg.\nExpected :blah to have been thrown, not :xxx." do
537
+ proc { throw :xxx }.must_throw :blah, "msg"
538
+ end
539
+ end
540
+
541
+ it "needs to verify types of objects" do
542
+ (6 * 7).must_be_instance_of(Int).must_equal true
543
+
544
+ exp = "Expected 42 to be an instance of String, not #{Int.name}."
545
+
546
+ assert_triggered exp do
547
+ (6 * 7).must_be_instance_of String
548
+ end
549
+
550
+ assert_triggered "msg.\n#{exp}" do
551
+ (6 * 7).must_be_instance_of String, "msg"
552
+ end
553
+ end
554
+
555
+ it "needs to verify using any (negative) predicate" do
556
+ @assertion_count -= 1 # doesn"t take a message
557
+
558
+ "blah".wont_be(:empty?).must_equal false
559
+
560
+ assert_triggered "Expected \"\" to not be empty?." do
561
+ "".wont_be :empty?
562
+ end
563
+ end
564
+
565
+ it "needs to verify using any binary operator" do
566
+ @assertion_count -= 1 # no msg
567
+
568
+ 41.must_be(:<, 42).must_equal true
569
+
570
+ assert_triggered "Expected 42 to be < 41." do
571
+ 42.must_be(:<, 41)
572
+ end
573
+ end
574
+
575
+ it "needs to verify using any predicate" do
576
+ @assertion_count -= 1 # no msg
577
+
578
+ "".must_be(:empty?).must_equal true
579
+
580
+ assert_triggered "Expected \"blah\" to be empty?." do
581
+ "blah".must_be :empty?
582
+ end
583
+ end
584
+
585
+ it "needs to verify using respond_to" do
586
+ 42.must_respond_to(:+).must_equal true
587
+
588
+ assert_triggered "Expected 42 (#{Int.name}) to respond to #clear." do
589
+ 42.must_respond_to :clear
590
+ end
591
+
592
+ assert_triggered "msg.\nExpected 42 (#{Int.name}) to respond to #clear." do
593
+ 42.must_respond_to :clear, "msg"
594
+ end
595
+ end
596
+ end
597
+
598
+ describe Minitest::Spec, :let do
599
+ i_suck_and_my_tests_are_order_dependent!
600
+
601
+ def _count
602
+ $let_count ||= 0
603
+ end
604
+
605
+ let :count do
606
+ $let_count += 1
607
+ $let_count
608
+ end
609
+
610
+ it "is evaluated once per example" do
611
+ _count.must_equal 0
612
+
613
+ count.must_equal 1
614
+ count.must_equal 1
615
+
616
+ _count.must_equal 1
617
+ end
618
+
619
+ it "is REALLY evaluated once per example" do
620
+ _count.must_equal 1
621
+
622
+ count.must_equal 2
623
+ count.must_equal 2
624
+
625
+ _count.must_equal 2
626
+ end
627
+
628
+ it 'raises an error if the name begins with "test"' do
629
+ proc { self.class.let(:test_value) { true } }.must_raise ArgumentError
630
+ end
631
+
632
+ it "raises an error if the name shadows a normal instance method" do
633
+ proc { self.class.let(:message) { true } }.must_raise ArgumentError
634
+ end
635
+
636
+ it "doesn't raise an error if it is just another let" do
637
+ proc do
638
+ describe :outer do
639
+ let(:bar)
640
+ describe :inner do
641
+ let(:bar)
642
+ end
643
+ end
644
+ :good
645
+ end.call.must_equal :good
646
+ end
647
+
648
+ it "procs come after dont_flip" do
649
+ p = proc {}
650
+ assert_respond_to p, :call
651
+ p.must_respond_to :call
652
+ end
653
+ end
654
+
655
+ describe Minitest::Spec, :subject do
656
+ attr_reader :subject_evaluation_count
657
+
658
+ subject do
659
+ @subject_evaluation_count ||= 0
660
+ @subject_evaluation_count += 1
661
+ @subject_evaluation_count
662
+ end
663
+
664
+ it "is evaluated once per example" do
665
+ subject.must_equal 1
666
+ subject.must_equal 1
667
+ subject_evaluation_count.must_equal 1
668
+ end
669
+ end
670
+
671
+ class TestMetaStatic < Minitest::Test
672
+ def test_children
673
+ Minitest::Spec.children.clear # prevents parallel run
674
+
675
+ y = z = nil
676
+ x = describe "top-level thingy" do
677
+ y = describe "first thingy" do end
678
+
679
+ it "top-level-it" do end
680
+
681
+ z = describe "second thingy" do end
682
+ end
683
+
684
+ assert_equal [x], Minitest::Spec.children
685
+ assert_equal [y, z], x.children
686
+ assert_equal [], y.children
687
+ assert_equal [], z.children
688
+ end
689
+
690
+ def test_it_wont_remove_existing_child_test_methods
691
+ Minitest::Spec.children.clear # prevents parallel run
692
+
693
+ inner = nil
694
+ outer = describe "outer" do
695
+ inner = describe "inner" do
696
+ it do
697
+ assert true
698
+ end
699
+ end
700
+ it do
701
+ assert true
702
+ end
703
+ end
704
+
705
+ assert_equal 1, outer.public_instance_methods.grep(/^test_/).count
706
+ assert_equal 1, inner.public_instance_methods.grep(/^test_/).count
707
+ end
708
+
709
+ def test_it_wont_add_test_methods_to_children
710
+ Minitest::Spec.children.clear # prevents parallel run
711
+
712
+ inner = nil
713
+ outer = describe "outer" do
714
+ inner = describe "inner" do end
715
+ it do
716
+ assert true
717
+ end
718
+ end
719
+
720
+ assert_equal 1, outer.public_instance_methods.grep(/^test_/).count
721
+ assert_equal 0, inner.public_instance_methods.grep(/^test_/).count
722
+ end
723
+ end
724
+
725
+ require "minitest/metametameta"
726
+
727
+ class TestMeta < MetaMetaMetaTestCase
728
+ parallelize_me!
729
+
730
+ def util_structure
731
+ y = z = nil
732
+ before_list = []
733
+ after_list = []
734
+ x = describe "top-level thingy" do
735
+ before { before_list << 1 }
736
+ after { after_list << 1 }
737
+
738
+ it "top-level-it" do end
739
+
740
+ y = describe "inner thingy" do
741
+ before { before_list << 2 }
742
+ after { after_list << 2 }
743
+ it "inner-it" do end
744
+
745
+ z = describe "very inner thingy" do
746
+ before { before_list << 3 }
747
+ after { after_list << 3 }
748
+ it "inner-it" do end
749
+
750
+ it { } # ignore me
751
+ specify { } # anonymous it
752
+ end
753
+ end
754
+ end
755
+
756
+ return x, y, z, before_list, after_list
757
+ end
758
+
759
+ def test_register_spec_type
760
+ original_types = Minitest::Spec::TYPES.dup
761
+
762
+ assert_includes Minitest::Spec::TYPES, [//, Minitest::Spec]
763
+
764
+ Minitest::Spec.register_spec_type(/woot/, TestMeta)
765
+
766
+ p = lambda do |_| true end
767
+ Minitest::Spec.register_spec_type TestMeta, &p
768
+
769
+ keys = Minitest::Spec::TYPES.map(&:first)
770
+
771
+ assert_includes keys, /woot/
772
+ assert_includes keys, p
773
+ ensure
774
+ Minitest::Spec::TYPES.replace original_types
775
+ end
776
+
777
+ def test_spec_type
778
+ original_types = Minitest::Spec::TYPES.dup
779
+
780
+ Minitest::Spec.register_spec_type(/A$/, MiniSpecA)
781
+ Minitest::Spec.register_spec_type MiniSpecB do |desc|
782
+ desc.superclass == ExampleA
783
+ end
784
+ Minitest::Spec.register_spec_type MiniSpecC do |_desc, *addl|
785
+ addl.include? :woot
786
+ end
787
+
788
+ assert_equal MiniSpecA, Minitest::Spec.spec_type(ExampleA)
789
+ assert_equal MiniSpecB, Minitest::Spec.spec_type(ExampleB)
790
+ assert_equal MiniSpecC, Minitest::Spec.spec_type(ExampleB, :woot)
791
+ ensure
792
+ Minitest::Spec::TYPES.replace original_types
793
+ end
794
+
795
+ def test_bug_dsl_expectations
796
+ spec_class = Class.new MiniSpecB do
797
+ it "should work" do
798
+ 0.must_equal 0
799
+ end
800
+ end
801
+
802
+ test_name = spec_class.instance_methods.sort.grep(/test/).first
803
+
804
+ spec = spec_class.new test_name
805
+
806
+ result = spec.run
807
+
808
+ assert spec.passed?
809
+ assert result.passed?
810
+ assert_equal 1, result.assertions
811
+ end
812
+
813
+ def test_name
814
+ spec_a = describe ExampleA do; end
815
+ spec_b = describe ExampleB, :random_method do; end
816
+ spec_c = describe ExampleB, :random_method, :addl_context do; end
817
+
818
+ assert_equal "ExampleA", spec_a.name
819
+ assert_equal "ExampleB::random_method", spec_b.name
820
+ assert_equal "ExampleB::random_method::addl_context", spec_c.name
821
+ end
822
+
823
+ def test_name2
824
+ assert_equal "NamedExampleA", NamedExampleA.name
825
+ assert_equal "NamedExampleB", NamedExampleB.name
826
+ assert_equal "NamedExampleC", NamedExampleC.name
827
+
828
+ spec_a = describe ExampleA do; end
829
+ spec_b = describe ExampleB, :random_method do; end
830
+
831
+ assert_equal "ExampleA", spec_a.name
832
+ assert_equal "ExampleB::random_method", spec_b.name
833
+ end
834
+
835
+ def test_structure
836
+ x, y, z, * = util_structure
837
+
838
+ assert_equal "top-level thingy", x.to_s
839
+ assert_equal "top-level thingy::inner thingy", y.to_s
840
+ assert_equal "top-level thingy::inner thingy::very inner thingy", z.to_s
841
+
842
+ assert_equal "top-level thingy", x.desc
843
+ assert_equal "inner thingy", y.desc
844
+ assert_equal "very inner thingy", z.desc
845
+
846
+ top_methods = %w[setup teardown test_0001_top-level-it]
847
+ inner_methods1 = %w[setup teardown test_0001_inner-it]
848
+ inner_methods2 = inner_methods1 +
849
+ %w[test_0002_anonymous test_0003_anonymous]
850
+
851
+ assert_equal top_methods, x.instance_methods(false).sort.map(&:to_s)
852
+ assert_equal inner_methods1, y.instance_methods(false).sort.map(&:to_s)
853
+ assert_equal inner_methods2, z.instance_methods(false).sort.map(&:to_s)
854
+ end
855
+
856
+ def test_structure_postfix_it
857
+ z = nil
858
+ y = describe "outer" do
859
+ # NOT here, below the inner-describe!
860
+ # it "inner-it" do end
861
+
862
+ z = describe "inner" do
863
+ it "inner-it" do end
864
+ end
865
+
866
+ # defined AFTER inner describe means we'll try to wipe out the inner-it
867
+ it "inner-it" do end
868
+ end
869
+
870
+ assert_equal %w[test_0001_inner-it], y.instance_methods(false).map(&:to_s)
871
+ assert_equal %w[test_0001_inner-it], z.instance_methods(false).map(&:to_s)
872
+ end
873
+
874
+ def test_setup_teardown_behavior
875
+ _, _, z, before_list, after_list = util_structure
876
+
877
+ @tu = z
878
+
879
+ run_tu_with_fresh_reporter
880
+
881
+ size = z.runnable_methods.size
882
+ assert_equal [1, 2, 3] * size, before_list
883
+ assert_equal [3, 2, 1] * size, after_list
884
+ end
885
+
886
+ def test_describe_first_structure
887
+ x1 = x2 = y = z = nil
888
+ x = describe "top-level thingy" do
889
+ y = describe "first thingy" do end
890
+
891
+ x1 = it "top level it" do end
892
+ x2 = it "не латинские &いった α, β, γ, δ, ε hello!!! world" do end
893
+
894
+ z = describe "second thingy" do end
895
+ end
896
+
897
+ test_methods = ["test_0001_top level it",
898
+ "test_0002_не латинские &いった α, β, γ, δ, ε hello!!! world",
899
+ ].sort
900
+
901
+ assert_equal test_methods, [x1, x2]
902
+ assert_equal test_methods, x.instance_methods.grep(/^test/).map(&:to_s).sort
903
+ assert_equal [], y.instance_methods.grep(/^test/)
904
+ assert_equal [], z.instance_methods.grep(/^test/)
905
+ end
906
+
907
+ def test_structure_subclasses
908
+ z = nil
909
+ x = Class.new Minitest::Spec do
910
+ def xyz; end
911
+ end
912
+ y = Class.new x do
913
+ z = describe("inner") { }
914
+ end
915
+
916
+ assert_respond_to x.new(nil), "xyz"
917
+ assert_respond_to y.new(nil), "xyz"
918
+ assert_respond_to z.new(nil), "xyz"
919
+ end
920
+ end
921
+
922
+ class TestSpecInTestCase < MetaMetaMetaTestCase
923
+ def setup
924
+ super
925
+
926
+ Thread.current[:current_spec] = self
927
+ @tc = self
928
+ @assertion_count = 2
929
+ end
930
+
931
+ def assert_triggered expected, klass = Minitest::Assertion
932
+ @assertion_count += 1
933
+
934
+ e = assert_raises klass do
935
+ yield
936
+ end
937
+
938
+ msg = e.message.sub(/(---Backtrace---).*/m, "\1")
939
+ msg.gsub!(/\(oid=[-0-9]+\)/, "(oid=N)")
940
+
941
+ assert_equal expected, msg
942
+ end
943
+
944
+ def teardown
945
+ msg = "expected #{@assertion_count} assertions, not #{@tc.assertions}"
946
+ assert_equal @assertion_count, @tc.assertions, msg
947
+ end
948
+
949
+ def test_expectation
950
+ @tc.assert_equal true, 1.must_equal(1)
951
+ end
952
+
953
+ def test_expectation_triggered
954
+ assert_triggered "Expected: 2\n Actual: 1" do
955
+ 1.must_equal 2
956
+ end
957
+ end
958
+
959
+ def test_expectation_with_a_message
960
+ assert_triggered "woot.\nExpected: 2\n Actual: 1" do
961
+ 1.must_equal 2, "woot"
962
+ end
963
+ end
964
+ end
965
+
966
+ class ValueMonadTest < Minitest::Test
967
+ attr_accessor :struct
968
+
969
+ def setup
970
+ @struct = { :_ => "a", :value => "b", :expect => "c" }
971
+ def @struct.method_missing k # think openstruct
972
+ self[k]
973
+ end
974
+ end
975
+
976
+ def test_value_monad_method
977
+ assert_equal "a", struct._
978
+ end
979
+
980
+ def test_value_monad_value_alias
981
+ assert_equal "b", struct.value
982
+ end
983
+
984
+ def test_value_monad_expect_alias
985
+ assert_equal "c", struct.expect
986
+ end
987
+ end