minitest 5.12.2

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