opal-minitest 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,666 @@
1
+ # OMT: removed
2
+ #require "rbconfig"
3
+
4
+ module Minitest
5
+ ##
6
+ # Minitest Assertions. All assertion methods accept a +msg+ which is
7
+ # printed if the assertion fails.
8
+ #
9
+ # Protocol: Nearly everything here boils up to +assert+, which
10
+ # expects to be able to increment an instance accessor named
11
+ # +assertions+. This is not provided by Assertions and must be
12
+ # provided by the thing including Assertions. See Minitest::Runnable
13
+ # for an example.
14
+
15
+ module Assertions
16
+ UNDEFINED = Object.new # :nodoc:
17
+
18
+ def UNDEFINED.inspect # :nodoc:
19
+ "UNDEFINED" # again with the rdoc bugs... :(
20
+ end
21
+
22
+ ##
23
+ # Returns the diff command to use in #diff. Tries to intelligently
24
+ # figure out what diff to use.
25
+
26
+ def self.diff
27
+ # OMT: unsupported
28
+ # @diff = if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ &&
29
+ # system("diff.exe", __FILE__, __FILE__)) then
30
+ # "diff.exe -u"
31
+ # elsif Minitest::Test.maglev? then
32
+ # "diff -u"
33
+ # elsif system("gdiff", __FILE__, __FILE__)
34
+ # "gdiff -u" # solaris and kin suck
35
+ # elsif system("diff", __FILE__, __FILE__)
36
+ # "diff -u"
37
+ # else
38
+ # nil
39
+ # end unless defined? @diff
40
+ #
41
+ @diff
42
+ end
43
+
44
+ ##
45
+ # Set the diff command to use in #diff.
46
+
47
+ def self.diff= o
48
+ @diff = o
49
+ end
50
+
51
+ ##
52
+ # Returns a diff between +exp+ and +act+. If there is no known
53
+ # diff command or if it doesn't make sense to diff the output
54
+ # (single line, short output), then it simply returns a basic
55
+ # comparison between the two.
56
+
57
+ def diff exp, act
58
+ # OMT: changed
59
+ # require "tempfile"
60
+ #
61
+ # expect = mu_pp_for_diff exp
62
+ # butwas = mu_pp_for_diff act
63
+ # result = nil
64
+ #
65
+ # need_to_diff =
66
+ # Minitest::Assertions.diff &&
67
+ # (expect.include?("\n") ||
68
+ # butwas.include?("\n") ||
69
+ # expect.size > 30 ||
70
+ # butwas.size > 30 ||
71
+ # expect == butwas)
72
+ #
73
+ # return "Expected: #{mu_pp exp}\n Actual: #{mu_pp act}" unless
74
+ # need_to_diff
75
+ #
76
+ # Tempfile.open("expect") do |a|
77
+ # a.puts expect
78
+ # a.flush
79
+ #
80
+ # Tempfile.open("butwas") do |b|
81
+ # b.puts butwas
82
+ # b.flush
83
+ #
84
+ # result = `#{Minitest::Assertions.diff} #{a.path} #{b.path}`
85
+ # result.sub!(/^\-\-\- .+/, "--- expected")
86
+ # result.sub!(/^\+\+\+ .+/, "+++ actual")
87
+ #
88
+ # if result.empty? then
89
+ # klass = exp.class
90
+ # result = [
91
+ # "No visible difference in the #{klass}#inspect output.\n",
92
+ # "You should look at the implementation of #== on ",
93
+ # "#{klass} or its members.\n",
94
+ # expect,
95
+ # ].join
96
+ # end
97
+ # end
98
+ # end
99
+ #
100
+ # result
101
+ "Expected: #{mu_pp exp}\n Actual: #{mu_pp act}"
102
+ end
103
+
104
+ ##
105
+ # This returns a human-readable version of +obj+. By default
106
+ # #inspect is called. You can override this to use #pretty_print
107
+ # if you want.
108
+
109
+ def mu_pp obj
110
+ s = obj.inspect
111
+ s = s.encode Encoding.default_external if defined? Encoding
112
+ s
113
+ end
114
+
115
+ ##
116
+ # This returns a diff-able human-readable version of +obj+. This
117
+ # differs from the regular mu_pp because it expands escaped
118
+ # newlines and makes hex-values generic (like object_ids). This
119
+ # uses mu_pp to do the first pass and then cleans it up.
120
+
121
+ def mu_pp_for_diff obj
122
+ mu_pp(obj).gsub(/\\n/, "\n").gsub(/:0x[a-fA-F0-9]{4,}/m, ':0xXXXXXX')
123
+ end
124
+
125
+ ##
126
+ # Fails unless +test+ is truthy.
127
+
128
+ def assert test, msg = nil
129
+ msg ||= "Failed assertion, no message given."
130
+ self.assertions += 1
131
+ unless test then
132
+ msg = msg.call if Proc === msg
133
+ raise Minitest::Assertion, msg
134
+ end
135
+ true
136
+ end
137
+
138
+ def _synchronize # :nodoc:
139
+ yield
140
+ end
141
+
142
+ ##
143
+ # Fails unless +obj+ is empty.
144
+
145
+ def assert_empty obj, msg = nil
146
+ msg = message(msg) { "Expected #{mu_pp(obj)} to be empty" }
147
+ assert_respond_to obj, :empty?
148
+ assert obj.empty?, msg
149
+ end
150
+
151
+ ##
152
+ # Fails unless <tt>exp == act</tt> printing the difference between
153
+ # the two, if possible.
154
+ #
155
+ # If there is no visible difference but the assertion fails, you
156
+ # should suspect that your #== is buggy, or your inspect output is
157
+ # missing crucial details.
158
+ #
159
+ # For floats use assert_in_delta.
160
+ #
161
+ # See also: Minitest::Assertions.diff
162
+
163
+ def assert_equal exp, act, msg = nil
164
+ msg = message(msg, "") { diff exp, act }
165
+ assert exp == act, msg
166
+ end
167
+
168
+ ##
169
+ # For comparing Floats. Fails unless +exp+ and +act+ are within +delta+
170
+ # of each other.
171
+ #
172
+ # assert_in_delta Math::PI, (22.0 / 7.0), 0.01
173
+
174
+ def assert_in_delta exp, act, delta = 0.001, msg = nil
175
+ n = (exp - act).abs
176
+ msg = message(msg) {
177
+ "Expected |#{exp} - #{act}| (#{n}) to be <= #{delta}"
178
+ }
179
+ assert delta >= n, msg
180
+ end
181
+
182
+ ##
183
+ # For comparing Floats. Fails unless +exp+ and +act+ have a relative
184
+ # error less than +epsilon+.
185
+
186
+ def assert_in_epsilon a, b, epsilon = 0.001, msg = nil
187
+ assert_in_delta a, b, [a.abs, b.abs].min * epsilon, msg
188
+ end
189
+
190
+ ##
191
+ # Fails unless +collection+ includes +obj+.
192
+
193
+ def assert_includes collection, obj, msg = nil
194
+ msg = message(msg) {
195
+ "Expected #{mu_pp(collection)} to include #{mu_pp(obj)}"
196
+ }
197
+ assert_respond_to collection, :include?
198
+ assert collection.include?(obj), msg
199
+ end
200
+
201
+ ##
202
+ # Fails unless +obj+ is an instance of +cls+.
203
+
204
+ def assert_instance_of cls, obj, msg = nil
205
+ msg = message(msg) {
206
+ "Expected #{mu_pp(obj)} to be an instance of #{cls}, not #{obj.class}"
207
+ }
208
+
209
+ assert obj.instance_of?(cls), msg
210
+ end
211
+
212
+ ##
213
+ # Fails unless +obj+ is a kind of +cls+.
214
+
215
+ def assert_kind_of cls, obj, msg = nil
216
+ msg = message(msg) {
217
+ "Expected #{mu_pp(obj)} to be a kind of #{cls}, not #{obj.class}" }
218
+
219
+ assert obj.kind_of?(cls), msg
220
+ end
221
+
222
+ ##
223
+ # Fails unless +matcher+ <tt>=~</tt> +obj+.
224
+
225
+ def assert_match matcher, obj, msg = nil
226
+ msg = message(msg) { "Expected #{mu_pp matcher} to match #{mu_pp obj}" }
227
+ assert_respond_to matcher, :"=~"
228
+ matcher = Regexp.new Regexp.escape matcher if String === matcher
229
+ assert matcher =~ obj, msg
230
+ end
231
+
232
+ ##
233
+ # Fails unless +obj+ is nil
234
+
235
+ def assert_nil obj, msg = nil
236
+ msg = message(msg) { "Expected #{mu_pp(obj)} to be nil" }
237
+ assert obj.nil?, msg
238
+ end
239
+
240
+ ##
241
+ # For testing with binary operators. Eg:
242
+ #
243
+ # assert_operator 5, :<=, 4
244
+
245
+ def assert_operator o1, op, o2 = UNDEFINED, msg = nil
246
+ return assert_predicate o1, op, msg if UNDEFINED == o2
247
+ msg = message(msg) { "Expected #{mu_pp(o1)} to be #{op} #{mu_pp(o2)}" }
248
+ assert o1.__send__(op, o2), msg
249
+ end
250
+
251
+ ##
252
+ # Fails if stdout or stderr do not output the expected results.
253
+ # Pass in nil if you don't care about that streams output. Pass in
254
+ # "" if you require it to be silent. Pass in a regexp if you want
255
+ # to pattern match.
256
+ #
257
+ # NOTE: this uses #capture_io, not #capture_subprocess_io.
258
+ #
259
+ # See also: #assert_silent
260
+
261
+ def assert_output stdout = nil, stderr = nil
262
+ out, err = capture_io do
263
+ yield
264
+ end
265
+
266
+ err_msg = Regexp === stderr ? :assert_match : :assert_equal if stderr
267
+ out_msg = Regexp === stdout ? :assert_match : :assert_equal if stdout
268
+
269
+ y = send err_msg, stderr, err, "In stderr" if err_msg
270
+ x = send out_msg, stdout, out, "In stdout" if out_msg
271
+
272
+ (!stdout || x) && (!stderr || y)
273
+ end
274
+
275
+ ##
276
+ # For testing with predicates. Eg:
277
+ #
278
+ # assert_predicate str, :empty?
279
+ #
280
+ # This is really meant for specs and is front-ended by assert_operator:
281
+ #
282
+ # str.must_be :empty?
283
+
284
+ def assert_predicate o1, op, msg = nil
285
+ msg = message(msg) { "Expected #{mu_pp(o1)} to be #{op}" }
286
+ assert o1.__send__(op), msg
287
+ end
288
+
289
+ ##
290
+ # Fails unless the block raises one of +exp+. Returns the
291
+ # exception matched so you can check the message, attributes, etc.
292
+
293
+ def assert_raises *exp
294
+ msg = "#{exp.pop}.\n" if String === exp.last
295
+
296
+ begin
297
+ yield
298
+ rescue Minitest::Skip => e
299
+ return e if exp.include? Minitest::Skip
300
+ raise e
301
+ rescue Exception => e
302
+ expected = exp.any? { |ex|
303
+ if ex.instance_of? Module then
304
+ e.kind_of? ex
305
+ else
306
+ e.instance_of? ex
307
+ end
308
+ }
309
+
310
+ assert expected, proc {
311
+ exception_details(e, "#{msg}#{mu_pp(exp)} exception expected, not")
312
+ }
313
+
314
+ return e
315
+ end
316
+
317
+ exp = exp.first if exp.size == 1
318
+
319
+ flunk "#{msg}#{mu_pp(exp)} expected but nothing was raised."
320
+ end
321
+
322
+ ##
323
+ # Fails unless +obj+ responds to +meth+.
324
+
325
+ def assert_respond_to obj, meth, msg = nil
326
+ msg = message(msg) {
327
+ "Expected #{mu_pp(obj)} (#{obj.class}) to respond to ##{meth}"
328
+ }
329
+ assert obj.respond_to?(meth), msg
330
+ end
331
+
332
+ ##
333
+ # Fails unless +exp+ and +act+ are #equal?
334
+
335
+ def assert_same exp, act, msg = nil
336
+ msg = message(msg) {
337
+ data = [mu_pp(act), act.object_id, mu_pp(exp), exp.object_id]
338
+ "Expected %s (oid=%d) to be the same as %s (oid=%d)" % data
339
+ }
340
+ assert exp.equal?(act), msg
341
+ end
342
+
343
+ ##
344
+ # +send_ary+ is a receiver, message and arguments.
345
+ #
346
+ # Fails unless the call returns a true value
347
+
348
+ def assert_send send_ary, m = nil
349
+ recv, msg, *args = send_ary
350
+ m = message(m) {
351
+ "Expected #{mu_pp(recv)}.#{msg}(*#{mu_pp(args)}) to return true" }
352
+ assert recv.__send__(msg, *args), m
353
+ end
354
+
355
+ ##
356
+ # Fails if the block outputs anything to stderr or stdout.
357
+ #
358
+ # See also: #assert_output
359
+
360
+ def assert_silent
361
+ assert_output "", "" do
362
+ yield
363
+ end
364
+ end
365
+
366
+ ##
367
+ # Fails unless the block throws +sym+
368
+
369
+ def assert_throws sym, msg = nil
370
+ default = "Expected #{mu_pp(sym)} to have been thrown"
371
+ caught = true
372
+ catch(sym) do
373
+ begin
374
+ yield
375
+ rescue ThreadError => e # wtf?!? 1.8 + threads == suck
376
+ default += ", not \:#{e.message[/uncaught throw \`(\w+?)\'/, 1]}"
377
+ rescue ArgumentError => e # 1.9 exception
378
+ default += ", not #{e.message.split(/ /).last}"
379
+ rescue NameError => e # 1.8 exception
380
+ default += ", not #{e.name.inspect}"
381
+ end
382
+ caught = false
383
+ end
384
+
385
+ assert caught, message(msg) { default }
386
+ end
387
+
388
+ ##
389
+ # Captures $stdout and $stderr into strings:
390
+ #
391
+ # out, err = capture_io do
392
+ # puts "Some info"
393
+ # warn "You did a bad thing"
394
+ # end
395
+ #
396
+ # assert_match %r%info%, out
397
+ # assert_match %r%bad%, err
398
+ #
399
+ # NOTE: For efficiency, this method uses StringIO and does not
400
+ # capture IO for subprocesses. Use #capture_subprocess_io for
401
+ # that.
402
+
403
+ def capture_io
404
+ _synchronize do
405
+ begin
406
+ require 'stringio'
407
+
408
+ captured_stdout, captured_stderr = StringIO.new, StringIO.new
409
+
410
+ orig_stdout, orig_stderr = $stdout, $stderr
411
+ $stdout, $stderr = captured_stdout, captured_stderr
412
+
413
+ yield
414
+
415
+ return captured_stdout.string, captured_stderr.string
416
+ ensure
417
+ $stdout = orig_stdout
418
+ $stderr = orig_stderr
419
+ end
420
+ end
421
+ end
422
+
423
+ ##
424
+ # Captures $stdout and $stderr into strings, using Tempfile to
425
+ # ensure that subprocess IO is captured as well.
426
+ #
427
+ # out, err = capture_subprocess_io do
428
+ # system "echo Some info"
429
+ # system "echo You did a bad thing 1>&2"
430
+ # end
431
+ #
432
+ # assert_match %r%info%, out
433
+ # assert_match %r%bad%, err
434
+ #
435
+ # NOTE: This method is approximately 10x slower than #capture_io so
436
+ # only use it when you need to test the output of a subprocess.
437
+ # OMT: unsupported
438
+
439
+ # def capture_subprocess_io
440
+ # _synchronize do
441
+ # begin
442
+ # require 'tempfile'
443
+ #
444
+ # captured_stdout, captured_stderr = Tempfile.new("out"), Tempfile.new("err")
445
+ #
446
+ # orig_stdout, orig_stderr = $stdout.dup, $stderr.dup
447
+ # $stdout.reopen captured_stdout
448
+ # $stderr.reopen captured_stderr
449
+ #
450
+ # yield
451
+ #
452
+ # $stdout.rewind
453
+ # $stderr.rewind
454
+ #
455
+ # return captured_stdout.read, captured_stderr.read
456
+ # ensure
457
+ # captured_stdout.unlink
458
+ # captured_stderr.unlink
459
+ # $stdout.reopen orig_stdout
460
+ # $stderr.reopen orig_stderr
461
+ # end
462
+ # end
463
+ # end
464
+
465
+ ##
466
+ # Returns details for exception +e+
467
+
468
+ def exception_details e, msg
469
+ [
470
+ "#{msg}",
471
+ "Class: <#{e.class}>",
472
+ "Message: <#{e.message.inspect}>",
473
+ "---Backtrace---",
474
+ "#{Minitest::filter_backtrace(e.backtrace).join("\n")}",
475
+ "---------------",
476
+ ].join "\n"
477
+ end
478
+
479
+ ##
480
+ # Fails with +msg+
481
+
482
+ def flunk msg = nil
483
+ msg ||= "Epic Fail!"
484
+ assert false, msg
485
+ end
486
+
487
+ ##
488
+ # Returns a proc that will output +msg+ along with the default message.
489
+
490
+ def message msg = nil, ending = ".", &default
491
+ proc {
492
+ msg = msg.call.chomp(".") if Proc === msg
493
+ custom_message = "#{msg}.\n" unless msg.nil? or msg.to_s.empty?
494
+ "#{custom_message}#{default.call}#{ending}"
495
+ }
496
+ end
497
+
498
+ ##
499
+ # used for counting assertions
500
+
501
+ def pass msg = nil
502
+ assert true
503
+ end
504
+
505
+ ##
506
+ # Fails if +test+ is truthy.
507
+
508
+ def refute test, msg = nil
509
+ msg ||= "Failed refutation, no message given"
510
+ not assert(! test, msg)
511
+ end
512
+
513
+ ##
514
+ # Fails if +obj+ is empty.
515
+
516
+ def refute_empty obj, msg = nil
517
+ msg = message(msg) { "Expected #{mu_pp(obj)} to not be empty" }
518
+ assert_respond_to obj, :empty?
519
+ refute obj.empty?, msg
520
+ end
521
+
522
+ ##
523
+ # Fails if <tt>exp == act</tt>.
524
+ #
525
+ # For floats use refute_in_delta.
526
+
527
+ def refute_equal exp, act, msg = nil
528
+ msg = message(msg) {
529
+ "Expected #{mu_pp(act)} to not be equal to #{mu_pp(exp)}"
530
+ }
531
+ refute exp == act, msg
532
+ end
533
+
534
+ ##
535
+ # For comparing Floats. Fails if +exp+ is within +delta+ of +act+.
536
+ #
537
+ # refute_in_delta Math::PI, (22.0 / 7.0)
538
+
539
+ def refute_in_delta exp, act, delta = 0.001, msg = nil
540
+ n = (exp - act).abs
541
+ msg = message(msg) {
542
+ "Expected |#{exp} - #{act}| (#{n}) to not be <= #{delta}"
543
+ }
544
+ refute delta >= n, msg
545
+ end
546
+
547
+ ##
548
+ # For comparing Floats. Fails if +exp+ and +act+ have a relative error
549
+ # less than +epsilon+.
550
+
551
+ def refute_in_epsilon a, b, epsilon = 0.001, msg = nil
552
+ refute_in_delta a, b, a * epsilon, msg
553
+ end
554
+
555
+ ##
556
+ # Fails if +collection+ includes +obj+.
557
+
558
+ def refute_includes collection, obj, msg = nil
559
+ msg = message(msg) {
560
+ "Expected #{mu_pp(collection)} to not include #{mu_pp(obj)}"
561
+ }
562
+ assert_respond_to collection, :include?
563
+ refute collection.include?(obj), msg
564
+ end
565
+
566
+ ##
567
+ # Fails if +obj+ is an instance of +cls+.
568
+
569
+ def refute_instance_of cls, obj, msg = nil
570
+ msg = message(msg) {
571
+ "Expected #{mu_pp(obj)} to not be an instance of #{cls}"
572
+ }
573
+ refute obj.instance_of?(cls), msg
574
+ end
575
+
576
+ ##
577
+ # Fails if +obj+ is a kind of +cls+.
578
+
579
+ def refute_kind_of cls, obj, msg = nil
580
+ msg = message(msg) { "Expected #{mu_pp(obj)} to not be a kind of #{cls}" }
581
+ refute obj.kind_of?(cls), msg
582
+ end
583
+
584
+ ##
585
+ # Fails if +matcher+ <tt>=~</tt> +obj+.
586
+
587
+ def refute_match matcher, obj, msg = nil
588
+ msg = message(msg) {"Expected #{mu_pp matcher} to not match #{mu_pp obj}"}
589
+ assert_respond_to matcher, :"=~"
590
+ matcher = Regexp.new Regexp.escape matcher if String === matcher
591
+ refute matcher =~ obj, msg
592
+ end
593
+
594
+ ##
595
+ # Fails if +obj+ is nil.
596
+
597
+ def refute_nil obj, msg = nil
598
+ msg = message(msg) { "Expected #{mu_pp(obj)} to not be nil" }
599
+ refute obj.nil?, msg
600
+ end
601
+
602
+ ##
603
+ # Fails if +o1+ is not +op+ +o2+. Eg:
604
+ #
605
+ # refute_operator 1, :>, 2 #=> pass
606
+ # refute_operator 1, :<, 2 #=> fail
607
+
608
+ def refute_operator o1, op, o2 = UNDEFINED, msg = nil
609
+ return refute_predicate o1, op, msg if UNDEFINED == o2
610
+ msg = message(msg) { "Expected #{mu_pp(o1)} to not be #{op} #{mu_pp(o2)}"}
611
+ refute o1.__send__(op, o2), msg
612
+ end
613
+
614
+ ##
615
+ # For testing with predicates.
616
+ #
617
+ # refute_predicate str, :empty?
618
+ #
619
+ # This is really meant for specs and is front-ended by refute_operator:
620
+ #
621
+ # str.wont_be :empty?
622
+
623
+ def refute_predicate o1, op, msg = nil
624
+ msg = message(msg) { "Expected #{mu_pp(o1)} to not be #{op}" }
625
+ refute o1.__send__(op), msg
626
+ end
627
+
628
+ ##
629
+ # Fails if +obj+ responds to the message +meth+.
630
+
631
+ def refute_respond_to obj, meth, msg = nil
632
+ msg = message(msg) { "Expected #{mu_pp(obj)} to not respond to #{meth}" }
633
+
634
+ refute obj.respond_to?(meth), msg
635
+ end
636
+
637
+ ##
638
+ # Fails if +exp+ is the same (by object identity) as +act+.
639
+
640
+ def refute_same exp, act, msg = nil
641
+ msg = message(msg) {
642
+ data = [mu_pp(act), act.object_id, mu_pp(exp), exp.object_id]
643
+ "Expected %s (oid=%d) to not be the same as %s (oid=%d)" % data
644
+ }
645
+ refute exp.equal?(act), msg
646
+ end
647
+
648
+ ##
649
+ # Skips the current run. If run in verbose-mode, the skipped run
650
+ # gets listed at the end of the run but doesn't cause a failure
651
+ # exit code.
652
+
653
+ def skip msg = nil, bt = caller
654
+ msg ||= "Skipped, no message given"
655
+ @skip = true
656
+ raise Minitest::Skip, msg, bt
657
+ end
658
+
659
+ ##
660
+ # Was this testcase skipped? Meant for #teardown.
661
+
662
+ def skipped?
663
+ defined?(@skip) and @skip
664
+ end
665
+ end
666
+ end