test-unit 2.5.5 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,18 @@
1
1
  # Author:: Nathaniel Talbott.
2
2
  # Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
3
- # Copyright (c) 2009-2012 Kouhei Sutou. All rights reserved.
3
+ # Copyright (c) 2009-2013 Kouhei Sutou. All rights reserved.
4
4
  # License:: Ruby license.
5
5
 
6
- require 'test/unit/assertionfailederror'
6
+ require 'test/unit/assertion-failed-error'
7
7
  require 'test/unit/util/backtracefilter'
8
8
  require 'test/unit/util/method-owner-finder'
9
9
  require 'test/unit/diff'
10
10
 
11
+ begin
12
+ require 'power_assert'
13
+ rescue LoadError, SyntaxError
14
+ end
15
+
11
16
  module Test
12
17
  module Unit
13
18
 
@@ -20,11 +25,12 @@ module Test
20
25
  # override add_assertion to get notified whenever an assertion is made.
21
26
  #
22
27
  # Notes:
28
+ #
23
29
  # * The message to each assertion, if given, will be propagated with the
24
30
  # failure.
25
31
  # * It is easy to add your own assertions based on assert_block().
26
32
  #
27
- # = Example Custom Assertion
33
+ # @example Example Custom Assertion
28
34
  #
29
35
  # def deny(boolean, message = nil)
30
36
  # message = build_message message, '<?> is not false or nil.', boolean
@@ -39,13 +45,11 @@ module Test
39
45
  # The assertion upon which all other assertions are based. Passes if the
40
46
  # block yields true.
41
47
  #
42
- # Example:
48
+ # @example
43
49
  # assert_block "Couldn't do the thing" do
44
50
  # do_the_thing
45
51
  # end
46
-
47
- public
48
- def assert_block(message="assert_block failed.") # :yields:
52
+ def assert_block(message="assert_block failed.")
49
53
  _wrap_assertion do
50
54
  if (! yield)
51
55
  raise AssertionFailedError.new(message.to_s)
@@ -53,34 +57,112 @@ module Test
53
57
  end
54
58
  end
55
59
 
56
- ##
57
- # Asserts that +boolean+ is not false or nil.
58
- #
59
- # Example:
60
- # assert [1, 2].include?(5)
61
-
62
- public
63
- def assert(boolean, message=nil)
60
+ # @private
61
+ NOT_SPECIFIED = Object.new
62
+
63
+ # @overload assert(object, message=nil)
64
+ #
65
+ # Asserts that `object` is not false nor nil.
66
+ #
67
+ # Normally, you don't need to use this assertion. Use more
68
+ # specific assertions such as #assert_equal and
69
+ # #assert_include.
70
+ #
71
+ # @example Pass patterns
72
+ # assert(true) # => pass
73
+ # assert([1, 2].include?(1)) # => pass
74
+ #
75
+ # @example Failure patterns
76
+ # assert(nil) # => failure
77
+ # assert(false) # => failure
78
+ # assert([1, 2].include?(5)) # => failure
79
+ #
80
+ # @param [Object] object The check target.
81
+ # @param [String] message The additional user message. It is
82
+ # showed when the assertion is failed.
83
+ # @return [void]
84
+ #
85
+ # @overload assert(message=nil) {}
86
+ #
87
+ # Asserts that the givens block returns not false nor nil.
88
+ #
89
+ # This style uses Power Assert. It means that you can see each
90
+ # object values in method chains on failure. See the following
91
+ # example about Power Assert.
92
+ #
93
+ # @example Power Assert
94
+ # coins = [1, 5, 50]
95
+ # target_coin = 10
96
+ # assert do
97
+ # coins.include?(target_coin)
98
+ # end
99
+ # # =>
100
+ # # coins.include?(target_coin)
101
+ # # | | |
102
+ # # | | 10
103
+ # # | false
104
+ # # [1, 5, 50]
105
+ #
106
+ # We recommend you to use Power Assert for predicate method
107
+ # checks rather than existing assertions such as
108
+ # #assert_include and #assert_predicate. Power Assert shows
109
+ # useful message for debugging.
110
+ #
111
+ # We don't recommend you use Power Assert for equality
112
+ # check. You should use #assert_equal for the case. Because
113
+ # #assert_equal shows more useful message for debugging.
114
+ #
115
+ # @example Pass patterns
116
+ # assert {true} # => pass
117
+ # assert {[1, 2].include?(1)} # => pass
118
+ #
119
+ # @example Failure patterns
120
+ # assert {nil} # => failure
121
+ # assert {false} # => failure
122
+ # assert {[1, 2].include?(5)} # => failure
123
+ #
124
+ # @param [String] message The additional user message. It is
125
+ # showed when the assertion is failed.
126
+ # @yield [] Given no parameters to the block.
127
+ # @yieldreturn [Object] The checked object.
128
+ # @return [void]
129
+ def assert(object=NOT_SPECIFIED, message=nil, &block)
64
130
  _wrap_assertion do
65
- assertion_message = nil
66
- case message
67
- when nil, String, Proc
68
- when AssertionMessage
69
- assertion_message = message
131
+ have_object = (object != NOT_SPECIFIED)
132
+ if block
133
+ message = object if have_object
134
+ if defined?(PowerAssert)
135
+ PowerAssert.start(block, assertion_method: __callee__) do |pa|
136
+ pa_message = AssertionMessage.delayed_literal(&pa.message_proc)
137
+ assertion_message = build_message(message, "?", pa_message)
138
+ assert_block(assertion_message) do
139
+ pa.yield
140
+ end
141
+ end
142
+ else
143
+ assert(yield, message)
144
+ end
70
145
  else
71
- error_message = "assertion message must be String, Proc or "
72
- error_message << "#{AssertionMessage}: "
73
- error_message << "<#{message.inspect}>(<#{message.class}>)"
74
- raise ArgumentError, error_message, filter_backtrace(caller)
75
- end
76
- assert_block("assert should not be called with a block.") do
77
- !block_given?
78
- end
79
- assertion_message ||= build_message(message,
80
- "<?> is not true.",
81
- boolean)
82
- assert_block(assertion_message) do
83
- boolean
146
+ unless have_object
147
+ raise ArgumentError, "wrong number of arguments (0 for 1..2)"
148
+ end
149
+ assertion_message = nil
150
+ case message
151
+ when nil, String, Proc
152
+ when AssertionMessage
153
+ assertion_message = message
154
+ else
155
+ error_message = "assertion message must be String, Proc or "
156
+ error_message << "#{AssertionMessage}: "
157
+ error_message << "<#{message.inspect}>(<#{message.class}>)"
158
+ raise ArgumentError, error_message, filter_backtrace(caller)
159
+ end
160
+ assertion_message ||= build_message(message,
161
+ "<?> is not true.",
162
+ object)
163
+ assert_block(assertion_message) do
164
+ object
165
+ end
84
166
  end
85
167
  end
86
168
  end
@@ -133,10 +215,8 @@ module Test
133
215
  # error message is generated when this one fails that tells you the
134
216
  # values of expected and actual.
135
217
  #
136
- # Example:
218
+ # @example
137
219
  # assert_equal 'MY STRING', 'my string'.upcase
138
-
139
- public
140
220
  def assert_equal(expected, actual, message=nil)
141
221
  diff = AssertionMessage.delayed_diff(expected, actual)
142
222
  if expected.respond_to?(:encoding) and
@@ -169,7 +249,7 @@ EOT
169
249
  # exceptions. When an expected exception is an Exception
170
250
  # object, passes if expected_exception == actual_exception.
171
251
  #
172
- # Example:
252
+ # @example
173
253
  # assert_raise(RuntimeError, LoadError) do
174
254
  # raise 'Boom!!!'
175
255
  # end # -> pass
@@ -181,7 +261,6 @@ EOT
181
261
  # assert_raise(RuntimeError.new("XXX")) {raise "XXX"} # -> pass
182
262
  # assert_raise(MyError.new("XXX")) {raise "XXX"} # -> fail
183
263
  # assert_raise(RuntimeError.new("ZZZ")) {raise "XXX"} # -> fail
184
- public
185
264
  def assert_raise(*args, &block)
186
265
  assert_expected_exception = Proc.new do |*_args|
187
266
  message, assert_exception_helper, actual_exception = _args
@@ -211,7 +290,7 @@ EOT
211
290
  # Passes if the block raises one of the given
212
291
  # exceptions or sub exceptions of the given exceptions.
213
292
  #
214
- # Example:
293
+ # @example
215
294
  # assert_raise_kind_of(SystemCallError) do
216
295
  # raise Errno::EACCES
217
296
  # end
@@ -236,49 +315,81 @@ EOT
236
315
  # an array of classes, it passes if any class
237
316
  # satisfies +object.instance_of?(class).
238
317
  #
239
- # Example:
318
+ # @example
240
319
  # assert_instance_of(String, 'foo') # -> pass
241
320
  # assert_instance_of([Fixnum, NilClass], 100) # -> pass
242
321
  # assert_instance_of([Numeric, NilClass], 100) # -> fail
243
-
244
- public
245
322
  def assert_instance_of(klass, object, message="")
246
323
  _wrap_assertion do
247
- klasses = nil
248
- klasses = klass if klass.is_a?(Array)
324
+ if klass.is_a?(Array)
325
+ klasses = klass
326
+ else
327
+ klasses = [klass]
328
+ end
249
329
  assert_block("The first parameter to assert_instance_of should be " +
250
330
  "a Class or an Array of Class.") do
251
- if klasses
252
- klasses.all? {|k| k.is_a?(Class)}
253
- else
254
- klass.is_a?(Class)
255
- end
331
+ klasses.all? {|k| k.is_a?(Class)}
256
332
  end
257
333
  klass_message = AssertionMessage.maybe_container(klass) do |value|
258
334
  "<#{value}>"
259
335
  end
260
336
  full_message = build_message(message, <<EOT, object, klass_message, object.class)
261
- <?> expected to be an instance of
337
+ <?> expected to be instance_of\\?
262
338
  ? but was
263
339
  <?>.
264
340
  EOT
265
341
  assert_block(full_message) do
266
- if klasses
267
- klasses.any? {|k| object.instance_of?(k)}
268
- else
269
- object.instance_of?(klass)
270
- end
342
+ klasses.any? {|k| object.instance_of?(k)}
343
+ end
344
+ end
345
+ end
346
+
347
+ ##
348
+ # Passes if +object+.instance_of?(+klass+) does not hold.
349
+ # When +klass+ is an array of classes, it passes if no class
350
+ # satisfies +object.instance_of?(class).
351
+ #
352
+ # @example
353
+ # assert_not_instance_of(String, 100) # -> pass
354
+ # assert_not_instance_of([Fixnum, NilClass], '100') # -> pass
355
+ # assert_not_instance_of([Numeric, NilClass], 100) # -> fail
356
+ #
357
+ # @since 3.0.0
358
+ def assert_not_instance_of(klass, object, message="")
359
+ _wrap_assertion do
360
+ if klass.is_a?(Array)
361
+ klasses = klass
362
+ else
363
+ klasses = [klass]
364
+ end
365
+ assert_block("The first parameter to assert_not_instance_of should be " <<
366
+ "a Class or an Array of Class.") do
367
+ klasses.all? {|k| k.is_a?(Class)}
368
+ end
369
+ klass_message = AssertionMessage.maybe_container(klass) do |value|
370
+ "<#{value}>"
371
+ end
372
+ full_message = build_message(message,
373
+ "<?> expected to not be instance_of\\?\n" +
374
+ "? but was.",
375
+ object,
376
+ klass_message)
377
+ assert_block(full_message) do
378
+ klasses.none? {|k| object.instance_of?(k)}
271
379
  end
272
380
  end
273
381
  end
274
382
 
383
+ # Just for minitest compatibility. :<
384
+ #
385
+ # @since 3.0.0
386
+ alias_method :refute_instance_of, :assert_not_instance_of
387
+
275
388
  ##
276
389
  # Passes if +object+ is nil.
277
390
  #
278
- # Example:
391
+ # @example
279
392
  # assert_nil [1, 2].uniq!
280
-
281
- public
282
393
  def assert_nil(object, message="")
283
394
  full_message = build_message(message, <<EOT, object)
284
395
  <?> expected to be nil.
@@ -291,23 +402,20 @@ EOT
291
402
  # an array of classes or modules, it passes if any
292
403
  # class or module satisfies +object.kind_of?(class_or_module).
293
404
  #
294
- # Example:
405
+ # @example
295
406
  # assert_kind_of(Object, 'foo') # -> pass
296
407
  # assert_kind_of([Fixnum, NilClass], 100) # -> pass
297
408
  # assert_kind_of([Fixnum, NilClass], "string") # -> fail
298
-
299
- public
300
409
  def assert_kind_of(klass, object, message="")
301
410
  _wrap_assertion do
302
- klasses = nil
303
- klasses = klass if klass.is_a?(Array)
411
+ if klass.is_a?(Array)
412
+ klasses = klass
413
+ else
414
+ klasses = [klass]
415
+ end
304
416
  assert_block("The first parameter to assert_kind_of should be " +
305
417
  "a kind_of Module or an Array of a kind_of Module.") do
306
- if klasses
307
- klasses.all? {|k| k.kind_of?(Module)}
308
- else
309
- klass.kind_of?(Module)
310
- end
418
+ klasses.all? {|k| k.kind_of?(Module)}
311
419
  end
312
420
  klass_message = AssertionMessage.maybe_container(klass) do |value|
313
421
  "<#{value}>"
@@ -320,22 +428,57 @@ EOT
320
428
  klass_message,
321
429
  object.class)
322
430
  assert_block(full_message) do
323
- if klasses
324
- klasses.any? {|k| object.kind_of?(k)}
325
- else
326
- object.kind_of?(klass)
327
- end
431
+ klasses.any? {|k| object.kind_of?(k)}
432
+ end
433
+ end
434
+ end
435
+
436
+ ##
437
+ # Passes if +object+.kind_of?(+klass+) does not hold.
438
+ # When +klass+ is an array of classes or modules, it passes only if all
439
+ # classes (and modules) do not satisfy +object.kind_of?(class_or_module).
440
+ #
441
+ # @example
442
+ # assert_not_kind_of(Fixnum, 'foo') # -> pass
443
+ # assert_not_kind_of([Fixnum, NilClass], '0') # -> pass
444
+ # assert_not_kind_of([Fixnum, NilClass], 100) # -> fail
445
+ #
446
+ # @since 3.0.0
447
+ def assert_not_kind_of(klass, object, message="")
448
+ _wrap_assertion do
449
+ if klass.is_a?(Array)
450
+ klasses = klass
451
+ else
452
+ klasses = [klass]
453
+ end
454
+ assert_block("The first parameter to assert_not_kind_of should be " +
455
+ "a kind_of Module or an Array of a kind_of Module.") do
456
+ klasses.all? {|k| k.kind_of?(Module)}
457
+ end
458
+ klass_message = AssertionMessage.maybe_container(klass) do |value|
459
+ "<#{value}>"
460
+ end
461
+ full_message = build_message(message,
462
+ "<?> expected to not be kind_of\\?\n" +
463
+ "? but was.",
464
+ object,
465
+ klass_message)
466
+ assert_block(full_message) do
467
+ klasses.none? {|k| object.kind_of?(k)}
328
468
  end
329
469
  end
330
470
  end
331
471
 
472
+ # Just for minitest compatibility. :<
473
+ #
474
+ # @since 3.0.0
475
+ alias_method :refute_kind_of, :assert_not_kind_of
476
+
332
477
  ##
333
478
  # Passes if +object+ .respond_to? +method+
334
479
  #
335
- # Example:
480
+ # @example
336
481
  # assert_respond_to 'bugbear', :slice
337
-
338
- public
339
482
  def assert_respond_to(object, method, message="")
340
483
  _wrap_assertion do
341
484
  full_message = build_message(message,
@@ -356,11 +499,9 @@ EOT
356
499
  ##
357
500
  # Passes if +object+ does not .respond_to? +method+.
358
501
  #
359
- # Example:
502
+ # @example
360
503
  # assert_not_respond_to('bugbear', :nonexistence) # -> pass
361
504
  # assert_not_respond_to('bugbear', :size) # -> fail
362
-
363
- public
364
505
  def assert_not_respond_to(object, method, message="")
365
506
  _wrap_assertion do
366
507
  full_message = build_message(message,
@@ -386,10 +527,8 @@ EOT
386
527
  ##
387
528
  # Passes if +string+ =~ +pattern+.
388
529
  #
389
- # Example:
530
+ # @example
390
531
  # assert_match(/\d+/, 'five, 6, seven')
391
-
392
- public
393
532
  def assert_match(pattern, string, message="")
394
533
  _wrap_assertion do
395
534
  pattern = case(pattern)
@@ -407,11 +546,9 @@ EOT
407
546
  # Passes if +actual+ .equal? +expected+ (i.e. they are the same
408
547
  # instance).
409
548
  #
410
- # Example:
549
+ # @example
411
550
  # o = Object.new
412
551
  # assert_same o, o
413
-
414
- public
415
552
  def assert_same(expected, actual, message="")
416
553
  full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
417
554
  <?>
@@ -427,10 +564,8 @@ EOT
427
564
  #
428
565
  # Passes if object1.__send__(operator, object2) is true.
429
566
  #
430
- # Example:
567
+ # @example
431
568
  # assert_operator 5, :>=, 4
432
-
433
- public
434
569
  def assert_operator(object1, operator, object2, message="")
435
570
  _wrap_assertion do
436
571
  full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
@@ -444,15 +579,41 @@ EOT
444
579
  end
445
580
  end
446
581
 
582
+ ##
583
+ # Compares the +object1+ with +object2+ using +operator+.
584
+ #
585
+ # Passes if object1.__send__(operator, object2) is not true.
586
+ #
587
+ # @example
588
+ # assert_not_operator(5, :<, 4) # => pass
589
+ # assert_not_operator(5, :>, 4) # => fail
590
+ #
591
+ # @since 3.0.0
592
+ def assert_not_operator(object1, operator, object2, message="")
593
+ _wrap_assertion do
594
+ full_message = build_message(nil, "<?>\ngiven as the operator for #assert_not_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
595
+ assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
596
+ full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
597
+ <?> expected to not be
598
+ ?
599
+ <?>.
600
+ EOT
601
+ assert_block(full_message) { ! object1.__send__(operator, object2) }
602
+ end
603
+ end
604
+
605
+ # Just for minitest compatibility. :<
606
+ #
607
+ # @since 3.0.0
608
+ alias_method :refute_operator, :assert_not_operator
609
+
447
610
  ##
448
611
  # Passes if block does not raise an exception.
449
612
  #
450
- # Example:
613
+ # @example
451
614
  # assert_nothing_raised do
452
615
  # [1, 2].uniq
453
616
  # end
454
-
455
- public
456
617
  def assert_nothing_raised(*args)
457
618
  _wrap_assertion do
458
619
  if args.last.is_a?(String)
@@ -480,10 +641,8 @@ EOT
480
641
  ##
481
642
  # Flunk always fails.
482
643
  #
483
- # Example:
644
+ # @example
484
645
  # flunk 'Not done testing yet.'
485
-
486
- public
487
646
  def flunk(message="Flunked")
488
647
  assert_block(build_message(message)){false}
489
648
  end
@@ -491,10 +650,8 @@ EOT
491
650
  ##
492
651
  # Passes if ! +actual+ .equal? +expected+
493
652
  #
494
- # Example:
653
+ # @example
495
654
  # assert_not_same Object.new, Object.new
496
-
497
- public
498
655
  def assert_not_same(expected, actual, message="")
499
656
  full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
500
657
  <?>
@@ -513,10 +670,8 @@ EOT
513
670
  ##
514
671
  # Passes if +expected+ != +actual+
515
672
  #
516
- # Example:
673
+ # @example
517
674
  # assert_not_equal 'some string', 5
518
-
519
- public
520
675
  def assert_not_equal(expected, actual, message="")
521
676
  full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual)
522
677
  assert_block(full_message) { expected != actual }
@@ -530,10 +685,8 @@ EOT
530
685
  ##
531
686
  # Passes if ! +object+ .nil?
532
687
  #
533
- # Example:
688
+ # @example
534
689
  # assert_not_nil '1 two 3'.sub!(/two/, '2')
535
-
536
- public
537
690
  def assert_not_nil(object, message="")
538
691
  full_message = build_message(message, "<?> expected to not be nil.", object)
539
692
  assert_block(full_message){!object.nil?}
@@ -547,11 +700,9 @@ EOT
547
700
  ##
548
701
  # Passes if +regexp+ !~ +string+
549
702
  #
550
- # Example:
703
+ # @example
551
704
  # assert_not_match(/two/, 'one 2 three') # -> pass
552
705
  # assert_not_match(/three/, 'one 2 three') # -> fail
553
-
554
- public
555
706
  def assert_not_match(regexp, string, message="")
556
707
  _wrap_assertion do
557
708
  assert_instance_of(Regexp, regexp,
@@ -574,11 +725,9 @@ EOT
574
725
  #
575
726
  # Passes if +regexp+ !~ +string+
576
727
  #
577
- # Example:
728
+ # @example
578
729
  # assert_no_match(/two/, 'one 2 three') # -> pass
579
730
  # assert_no_match(/three/, 'one 2 three') # -> fail
580
-
581
- public
582
731
  def assert_no_match(regexp, string, message="")
583
732
  _wrap_assertion do
584
733
  assert_instance_of(Regexp, regexp,
@@ -588,21 +737,20 @@ EOT
588
737
  end
589
738
  end
590
739
 
740
+ # @private
591
741
  UncaughtThrow = {
592
- NameError => /^uncaught throw \`(.+)\'$/, #`
593
- ArgumentError => /^uncaught throw (.+)$/,
594
- ThreadError => /^uncaught throw \`(.+)\' in thread / #`
742
+ NameError => /^uncaught throw `(.+)'$/,
743
+ ArgumentError => /^uncaught throw (`.+'|.+)$/,
744
+ ThreadError => /^uncaught throw `(.+)' in thread /,
595
745
  }
596
746
 
597
747
  ##
598
748
  # Passes if the block throws +expected_object+
599
749
  #
600
- # Example:
750
+ # @example
601
751
  # assert_throw(:done) do
602
752
  # throw(:done)
603
753
  # end
604
-
605
- public
606
754
  def assert_throw(expected_object, message="", &proc)
607
755
  _wrap_assertion do
608
756
  begin
@@ -626,8 +774,7 @@ EOT
626
774
  assert_block(full_message) {caught}
627
775
  rescue NameError, ArgumentError, ThreadError => error
628
776
  raise unless UncaughtThrow[error.class] =~ error.message
629
- tag = $1
630
- tag = tag[1..-1].intern if tag[0, 1] == ":"
777
+ tag = AssertionMessage.normalize_tag($1)
631
778
  full_message = build_message(message,
632
779
  "<?> expected to be thrown but\n" +
633
780
  "<?> was thrown.",
@@ -645,12 +792,10 @@ EOT
645
792
  ##
646
793
  # Passes if block does not throw anything.
647
794
  #
648
- # Example:
795
+ # @example
649
796
  # assert_nothing_thrown do
650
797
  # [1, 2].uniq
651
798
  # end
652
-
653
- public
654
799
  def assert_nothing_thrown(message="", &proc)
655
800
  _wrap_assertion do
656
801
  assert(block_given?, "Should have passed a block to assert_nothing_thrown")
@@ -658,8 +803,7 @@ EOT
658
803
  proc.call
659
804
  rescue NameError, ArgumentError, ThreadError => error
660
805
  raise unless UncaughtThrow[error.class] =~ error.message
661
- tag = $1
662
- tag = tag[1..-1].intern if tag[0, 1] == ":"
806
+ tag = AssertionMessage.normalize_tag($1)
663
807
  full_message = build_message(message,
664
808
  "<?> was thrown when nothing was expected",
665
809
  tag)
@@ -673,10 +817,8 @@ EOT
673
817
  # Passes if +expected_float+ and +actual_float+ are equal
674
818
  # within +delta+ tolerance.
675
819
  #
676
- # Example:
820
+ # @example
677
821
  # assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
678
-
679
- public
680
822
  def assert_in_delta(expected_float, actual_float, delta=0.001, message="")
681
823
  _wrap_assertion do
682
824
  _assert_in_delta_validate_arguments(expected_float,
@@ -696,11 +838,9 @@ EOT
696
838
  # Passes if +expected_float+ and +actual_float+ are
697
839
  # not equal within +delta+ tolerance.
698
840
  #
699
- # Example:
841
+ # @example
700
842
  # assert_not_in_delta(0.05, (50000.0 / 10**6), 0.00002) # -> pass
701
843
  # assert_not_in_delta(0.05, (50000.0 / 10**6), 0.00001) # -> fail
702
-
703
- public
704
844
  def assert_not_in_delta(expected_float, actual_float, delta=0.001, message="")
705
845
  _wrap_assertion do
706
846
  _assert_in_delta_validate_arguments(expected_float,
@@ -722,7 +862,6 @@ EOT
722
862
  # @since 2.5.3
723
863
  alias_method :refute_in_delta, :assert_not_in_delta
724
864
 
725
- # :stopdoc:
726
865
  private
727
866
  def _assert_in_delta_validate_arguments(expected_float,
728
867
  actual_float,
@@ -795,17 +934,13 @@ EOT
795
934
  end
796
935
 
797
936
  public
798
- # :startdoc:
799
-
800
937
  ##
801
938
  # Passes if +expected_float+ and +actual_float+ are equal
802
939
  # within +epsilon+ relative error of +expected_float+.
803
940
  #
804
- # Example:
941
+ # @example
805
942
  # assert_in_epsilon(10000.0, 9900.0, 0.1) # -> pass
806
943
  # assert_in_epsilon(10000.0, 9899.0, 0.1) # -> fail
807
-
808
- public
809
944
  def assert_in_epsilon(expected_float, actual_float, epsilon=0.001,
810
945
  message="")
811
946
  _wrap_assertion do
@@ -834,11 +969,9 @@ EOT
834
969
  # not equal within +epsilon+ relative error of
835
970
  # +expected_float+.
836
971
  #
837
- # Example:
972
+ # @example
838
973
  # assert_not_in_epsilon(10000.0, 9900.0, 0.1) # -> fail
839
974
  # assert_not_in_epsilon(10000.0, 9899.0, 0.1) # -> pass
840
-
841
- public
842
975
  def assert_not_in_epsilon(expected_float, actual_float, epsilon=0.001,
843
976
  message="")
844
977
  _wrap_assertion do
@@ -858,7 +991,11 @@ EOT
858
991
  end
859
992
  end
860
993
 
861
- # :stopdoc:
994
+ # Just for minitest compatibility. :<
995
+ #
996
+ # @since 3.0.0
997
+ alias_method :refute_in_epsilon, :assert_not_in_epsilon
998
+
862
999
  private
863
1000
  def _assert_in_epsilon_validate_arguments(expected_float,
864
1001
  actual_float,
@@ -935,8 +1072,6 @@ EOT
935
1072
  end
936
1073
 
937
1074
  public
938
- # :startdoc:
939
-
940
1075
  ##
941
1076
  # Passes if the method send returns a true value.
942
1077
  #
@@ -945,11 +1080,9 @@ EOT
945
1080
  # * A method
946
1081
  # * Arguments to the method
947
1082
  #
948
- # Example:
1083
+ # @example
949
1084
  # assert_send([[1, 2], :member?, 1]) # -> pass
950
1085
  # assert_send([[1, 2], :member?, 4]) # -> fail
951
-
952
- public
953
1086
  def assert_send(send_array, message=nil)
954
1087
  _wrap_assertion do
955
1088
  assert_instance_of(Array, send_array,
@@ -987,7 +1120,7 @@ EOT
987
1120
  # * A method
988
1121
  # * Arguments to the method
989
1122
  #
990
- # Example:
1123
+ # @example
991
1124
  # assert_not_send([[1, 2], :member?, 1]) # -> fail
992
1125
  # assert_not_send([[1, 2], :member?, 4]) # -> pass
993
1126
  def assert_not_send(send_array, message=nil)
@@ -1022,7 +1155,7 @@ EOT
1022
1155
  ##
1023
1156
  # Passes if +actual+ is a boolean value.
1024
1157
  #
1025
- # Example:
1158
+ # @example
1026
1159
  # assert_boolean(true) # -> pass
1027
1160
  # assert_boolean(nil) # -> fail
1028
1161
  def assert_boolean(actual, message=nil)
@@ -1038,7 +1171,7 @@ EOT
1038
1171
  ##
1039
1172
  # Passes if +actual+ is true.
1040
1173
  #
1041
- # Example:
1174
+ # @example
1042
1175
  # assert_true(true) # -> pass
1043
1176
  # assert_true(:true) # -> fail
1044
1177
  def assert_true(actual, message=nil)
@@ -1054,7 +1187,7 @@ EOT
1054
1187
  ##
1055
1188
  # Passes if +actual+ is false.
1056
1189
  #
1057
- # Example:
1190
+ # @example
1058
1191
  # assert_false(false) # -> pass
1059
1192
  # assert_false(nil) # -> fail
1060
1193
  def assert_false(actual, message=nil)
@@ -1071,7 +1204,7 @@ EOT
1071
1204
  # Passes if expression "+expected+ +operator+
1072
1205
  # +actual+" is true.
1073
1206
  #
1074
- # Example:
1207
+ # @example
1075
1208
  # assert_compare(1, "<", 10) # -> pass
1076
1209
  # assert_compare(1, ">=", 10) # -> fail
1077
1210
  def assert_compare(expected, operator, actual, message=nil)
@@ -1104,7 +1237,7 @@ EOT
1104
1237
  ##
1105
1238
  # Passes if assertion is failed in block.
1106
1239
  #
1107
- # Example:
1240
+ # @example
1108
1241
  # assert_fail_assertion {assert_equal("A", "B")} # -> pass
1109
1242
  # assert_fail_assertion {assert_equal("A", "A")} # -> fail
1110
1243
  def assert_fail_assertion(message=nil)
@@ -1126,7 +1259,7 @@ EOT
1126
1259
  # Passes if an exception is raised in block and its
1127
1260
  # message is +expected+.
1128
1261
  #
1129
- # Example:
1262
+ # @example
1130
1263
  # assert_raise_message("exception") {raise "exception"} # -> pass
1131
1264
  # assert_raise_message(/exc/i) {raise "exception"} # -> pass
1132
1265
  # assert_raise_message("exception") {raise "EXCEPTION"} # -> fail
@@ -1166,7 +1299,7 @@ EOT
1166
1299
  ##
1167
1300
  # Passes if +object+.const_defined?(+constant_name+)
1168
1301
  #
1169
- # Example:
1302
+ # @example
1170
1303
  # assert_const_defined(Test, :Unit) # -> pass
1171
1304
  # assert_const_defined(Object, :Nonexistent) # -> fail
1172
1305
  def assert_const_defined(object, constant_name, message=nil)
@@ -1183,7 +1316,7 @@ EOT
1183
1316
  ##
1184
1317
  # Passes if !+object+.const_defined?(+constant_name+)
1185
1318
  #
1186
- # Example:
1319
+ # @example
1187
1320
  # assert_not_const_defined(Object, :Nonexistent) # -> pass
1188
1321
  # assert_not_const_defined(Test, :Unit) # -> fail
1189
1322
  def assert_not_const_defined(object, constant_name, message=nil)
@@ -1200,7 +1333,7 @@ EOT
1200
1333
  ##
1201
1334
  # Passes if +object+.+predicate+ is _true_.
1202
1335
  #
1203
- # Example:
1336
+ # @example
1204
1337
  # assert_predicate([], :empty?) # -> pass
1205
1338
  # assert_predicate([1], :empty?) # -> fail
1206
1339
  def assert_predicate(object, predicate, message=nil)
@@ -1222,7 +1355,7 @@ EOT
1222
1355
  ##
1223
1356
  # Passes if +object+.+predicate+ is not _true_.
1224
1357
  #
1225
- # Example:
1358
+ # @example
1226
1359
  # assert_not_predicate([1], :empty?) # -> pass
1227
1360
  # assert_not_predicate([], :empty?) # -> fail
1228
1361
  def assert_not_predicate(object, predicate, message=nil)
@@ -1241,11 +1374,16 @@ EOT
1241
1374
  end
1242
1375
  end
1243
1376
 
1377
+ # Just for minitest compatibility. :<
1378
+ #
1379
+ # @since 3.0.0
1380
+ alias_method :refute_predicate, :assert_not_predicate
1381
+
1244
1382
  ##
1245
1383
  # Passes if +object+#+alias_name+ is an alias method of
1246
1384
  # +object+#+original_name+.
1247
1385
  #
1248
- # Example:
1386
+ # @example
1249
1387
  # assert_alias_method([], :length, :size) # -> pass
1250
1388
  # assert_alias_method([], :size, :length) # -> pass
1251
1389
  # assert_alias_method([], :each, :size) # -> fail
@@ -1292,7 +1430,7 @@ EOT
1292
1430
  ##
1293
1431
  # Passes if +path+ exists.
1294
1432
  #
1295
- # Example:
1433
+ # @example
1296
1434
  # assert_path_exist("/tmp") # -> pass
1297
1435
  # assert_path_exist("/bin/sh") # -> pass
1298
1436
  # assert_path_exist("/nonexistent") # -> fail
@@ -1310,7 +1448,7 @@ EOT
1310
1448
  ##
1311
1449
  # Passes if +path+ doesn't exist.
1312
1450
  #
1313
- # Example:
1451
+ # @example
1314
1452
  # assert_path_not_exist("/nonexistent") # -> pass
1315
1453
  # assert_path_not_exist("/tmp") # -> fail
1316
1454
  # assert_path_not_exist("/bin/sh") # -> fail
@@ -1328,7 +1466,7 @@ EOT
1328
1466
  ##
1329
1467
  # Passes if +collection+ includes +object+.
1330
1468
  #
1331
- # Example:
1469
+ # @example
1332
1470
  # assert_include([1, 10], 1) # -> pass
1333
1471
  # assert_include(1..10, 5) # -> pass
1334
1472
  # assert_include([1, 10], 5) # -> fail
@@ -1355,7 +1493,7 @@ EOT
1355
1493
  ##
1356
1494
  # Passes if +collection+ doesn't include +object+.
1357
1495
  #
1358
- # Example:
1496
+ # @example
1359
1497
  # assert_not_include([1, 10], 5) # -> pass
1360
1498
  # assert_not_include(1..10, 20) # -> pass
1361
1499
  # assert_not_include([1, 10], 1) # -> fail
@@ -1374,10 +1512,20 @@ EOT
1374
1512
  end
1375
1513
  end
1376
1514
 
1515
+ # Just for minitest compatibility. :<
1516
+ #
1517
+ # @since 3.0.0
1518
+ alias_method :assert_not_includes, :assert_not_include
1519
+
1520
+ # Just for minitest compatibility. :<
1521
+ #
1522
+ # @since 3.0.0
1523
+ alias_method :refute_includes, :assert_not_include
1524
+
1377
1525
  ##
1378
1526
  # Passes if +object+ is empty.
1379
1527
  #
1380
- # Example:
1528
+ # @example
1381
1529
  # assert_empty("") # -> pass
1382
1530
  # assert_empty([]) # -> pass
1383
1531
  # assert_empty({}) # -> pass
@@ -1400,7 +1548,7 @@ EOT
1400
1548
  ##
1401
1549
  # Passes if +object+ is not empty.
1402
1550
  #
1403
- # Example:
1551
+ # @example
1404
1552
  # assert_not_empty(" ") # -> pass
1405
1553
  # assert_not_empty([nil]) # -> pass
1406
1554
  # assert_not_empty({1 => 2}) # -> pass
@@ -1420,11 +1568,14 @@ EOT
1420
1568
  end
1421
1569
  end
1422
1570
 
1571
+ # Just for minitest compatibility. :<
1572
+ #
1573
+ # @since 3.0.0
1574
+ alias_method :refute_empty, :assert_not_empty
1575
+
1423
1576
  ##
1424
1577
  # Builds a failure message. +head+ is added before the +template+ and
1425
1578
  # +arguments+ replaces the '?'s positionally in the template.
1426
-
1427
- public
1428
1579
  def build_message(head, template=nil, *arguments)
1429
1580
  template &&= template.chomp
1430
1581
  return AssertionMessage.new(head, template, arguments)
@@ -1460,13 +1611,10 @@ EOT
1460
1611
  ##
1461
1612
  # Select whether or not to use the pretty-printer. If this option is set
1462
1613
  # to false before any assertions are made, pp.rb will not be required.
1463
-
1464
- public
1465
1614
  def self.use_pp=(value)
1466
1615
  AssertionMessage.use_pp = value
1467
1616
  end
1468
1617
 
1469
- # :stopdoc:
1470
1618
  private
1471
1619
  def _assert_raise(assert_expected_exception, *args, &block)
1472
1620
  _wrap_assertion do
@@ -1497,7 +1645,6 @@ EOT
1497
1645
  end
1498
1646
  end
1499
1647
 
1500
- private
1501
1648
  def _set_failed_information(failure, expected, actual, user_message)
1502
1649
  failure.expected = expected
1503
1650
  failure.actual = actual
@@ -1523,6 +1670,19 @@ EOT
1523
1670
  MaybeContainer.new(value, &formatter)
1524
1671
  end
1525
1672
 
1673
+ def normalize_tag(tag)
1674
+ case tag
1675
+ when /\A:/
1676
+ tag[1..-1].intern
1677
+ when /\A`(.+)'\z/
1678
+ $1.intern
1679
+ when String
1680
+ tag.intern
1681
+ else
1682
+ tag
1683
+ end
1684
+ end
1685
+
1526
1686
  MAX_DIFF_TARGET_STRING_SIZE = 1000
1527
1687
  def max_diff_target_string_size
1528
1688
  return @@max_diff_target_string_size if @@max_diff_target_string_size
@@ -1680,6 +1840,33 @@ EOT
1680
1840
  end
1681
1841
  end
1682
1842
 
1843
+ class NumericInspector
1844
+ Inspector.register_inspector_class(self)
1845
+
1846
+ class << self
1847
+ def target?(object)
1848
+ object.is_a?(Numeric)
1849
+ end
1850
+ end
1851
+
1852
+ def initialize(numeric, inspected_objects)
1853
+ @inspected_objects = inspected_objects
1854
+ @numeric = numeric
1855
+ end
1856
+
1857
+ def inspect
1858
+ @numeric.to_s
1859
+ end
1860
+
1861
+ def pretty_print(q)
1862
+ q.text(@numeric.to_s)
1863
+ end
1864
+
1865
+ def pretty_print_cycle(q)
1866
+ q.text(@numeric.to_s)
1867
+ end
1868
+ end
1869
+
1683
1870
  class HashInspector
1684
1871
  Inspector.register_inspector_class(self)
1685
1872