@@ -24,23 +24,8 @@ def message msg = nil, ending = nil, &default
24
24
end
25
25
26
26
module CoreAssertions
27
- if defined? ( MiniTest )
28
- require_relative '../../envutil'
29
- # for ruby core testing
30
- include MiniTest ::Assertions
31
-
32
- # Compatibility hack for assert_raise
33
- Test ::Unit ::AssertionFailedError = MiniTest ::Assertion
34
- else
35
- module MiniTest
36
- class Assertion < Exception ; end
37
- class Skip < Assertion ; end
38
- end
39
-
40
- require 'pp'
41
- require_relative 'envutil'
42
- include Test ::Unit ::Assertions
43
- end
27
+ require_relative 'envutil'
28
+ require 'pp'
44
29
45
30
def mu_pp ( obj ) #:nodoc:
46
31
obj . pretty_inspect . chomp
@@ -115,18 +100,18 @@ def syntax_check(code, fname, line)
115
100
116
101
def assert_no_memory_leak ( args , prepare , code , message = nil , limit : 2.0 , rss : false , **opt )
117
102
# TODO: consider choosing some appropriate limit for MJIT and stop skipping this once it does not randomly fail
118
- pend 'assert_no_memory_leak may consider MJIT memory usage as leak' if defined? ( RubyVM ::MJIT ) && RubyVM ::MJIT . enabled?
103
+ pend 'assert_no_memory_leak may consider MJIT memory usage as leak' if defined? ( RubyVM ::JIT ) && RubyVM ::JIT . enabled?
119
104
120
- require_relative '../../ memory_status'
121
- raise MiniTest :: Skip , "unsupported platform" unless defined? ( Memory ::Status )
105
+ require_relative 'memory_status'
106
+ raise Test :: Unit :: PendedError , "unsupported platform" unless defined? ( Memory ::Status )
122
107
123
108
token = "\e [7;1m#{ $$. to_s } :#{ Time . now . strftime ( '%s.%L' ) } :#{ rand ( 0x10000 ) . to_s ( 16 ) } :\e [m"
124
109
token_dump = token . dump
125
110
token_re = Regexp . quote ( token )
126
111
envs = args . shift if Array === args and Hash === args . first
127
112
args = [
128
113
"--disable=gems" ,
129
- "-r" , File . expand_path ( "../../../ memory_status" , __FILE__ ) ,
114
+ "-r" , File . expand_path ( "../memory_status" , __FILE__ ) ,
130
115
*args ,
131
116
"-v" , "-" ,
132
117
]
@@ -183,11 +168,11 @@ def assert_nothing_raised(*args)
183
168
end
184
169
begin
185
170
line = __LINE__ ; yield
186
- rescue MiniTest :: Skip
171
+ rescue Test :: Unit :: PendedError
187
172
raise
188
173
rescue Exception => e
189
174
bt = e . backtrace
190
- as = e . instance_of? ( MiniTest :: Assertion )
175
+ as = e . instance_of? ( Test :: Unit :: AssertionFailedError )
191
176
if as
192
177
ans = /\A #{ Regexp . quote ( __FILE__ ) } :#{ line } :in /o
193
178
bt . reject! { |ln | ans =~ ln }
@@ -199,7 +184,7 @@ def assert_nothing_raised(*args)
199
184
"Backtrace:\n " +
200
185
e . backtrace . map { |frame | " #{ frame } " } . join ( "\n " )
201
186
}
202
- raise MiniTest :: Assertion , msg . call , bt
187
+ raise Test :: Unit :: AssertionFailedError , msg . call , bt
203
188
else
204
189
raise
205
190
end
@@ -260,11 +245,12 @@ def assert_ruby_status(args, test_stdin="", message=nil, **opt)
260
245
ABORT_SIGNALS = Signal . list . values_at ( *%w" ILL ABRT BUS SEGV TERM " )
261
246
262
247
def separated_runner ( out = nil )
248
+ include ( *Test ::Unit ::TestCase . ancestors . select { |c | !c . is_a? ( Class ) } )
263
249
out = out ? IO . new ( out , 'w' ) : STDOUT
264
250
at_exit {
265
- out . puts [ Marshal . dump ( $!) ] . pack ( 'm' ) , "assertions=\ # {self._assertions}"
251
+ out . puts [ Marshal . dump ( $!) ] . pack ( 'm' ) , "assertions=#{ self . _assertions } "
266
252
}
267
- Test ::Unit ::Runner . class_variable_set ( :@@stop_auto_run , true )
253
+ Test ::Unit ::Runner . class_variable_set ( :@@stop_auto_run , true ) if defined? ( Test :: Unit :: Runner )
268
254
end
269
255
270
256
def assert_separately ( args , file = nil , line = nil , src , ignore_stderr : nil , **opt )
@@ -276,14 +262,14 @@ def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **o
276
262
capture_stdout = true
277
263
unless /mswin|mingw/ =~ RUBY_PLATFORM
278
264
capture_stdout = false
279
- opt [ :out ] = MiniTest ::Unit . output
265
+ opt [ :out ] = Test ::Unit :: Runner . output if defined? ( Test :: Unit :: Runner )
280
266
res_p , res_c = IO . pipe
281
- opt [ res_c . fileno ] = res_c . fileno
267
+ opt [ :ios ] = [ res_c ]
282
268
end
283
269
src = <<eom
284
270
# -*- coding: #{ line += __LINE__ ; src . encoding } ; -*-
285
271
BEGIN {
286
- require "test/unit";include Test::Unit::Assertions;require #{ ( __dir__ + "/core_assertions" ) . dump } ;include Test::Unit::CoreAssertions
272
+ require "test/unit";include Test::Unit::Assertions;require #{ __FILE__ . dump } ;include Test::Unit::CoreAssertions
287
273
separated_runner #{ res_c &.fileno }
288
274
}
289
275
#{ line -= __LINE__ ; src }
@@ -330,6 +316,27 @@ def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **o
330
316
raise marshal_error if marshal_error
331
317
end
332
318
319
+ # Run Ractor-related test without influencing the main test suite
320
+ def assert_ractor ( src , args : [ ] , require : nil , require_relative : nil , file : nil , line : nil , ignore_stderr : nil , **opt )
321
+ return unless defined? ( Ractor )
322
+
323
+ require = "require #{ require . inspect } " if require
324
+ if require_relative
325
+ dir = File . dirname ( caller_locations [ 0 , 1 ] [ 0 ] . absolute_path )
326
+ full_path = File . expand_path ( require_relative , dir )
327
+ require = "#{ require } ; require #{ full_path . inspect } "
328
+ end
329
+
330
+ assert_separately ( args , file , line , <<~RUBY , ignore_stderr : ignore_stderr , **opt )
331
+ #{ require }
332
+ previous_verbose = $VERBOSE
333
+ $VERBOSE = nil
334
+ Ractor.new {} # trigger initial warning
335
+ $VERBOSE = previous_verbose
336
+ #{ src }
337
+ RUBY
338
+ end
339
+
333
340
# :call-seq:
334
341
# assert_throw( tag, failure_message = nil, &block )
335
342
#
@@ -380,8 +387,8 @@ def assert_raise(*exp, &b)
380
387
381
388
begin
382
389
yield
383
- rescue MiniTest :: Skip => e
384
- return e if exp . include? MiniTest :: Skip
390
+ rescue Test :: Unit :: PendedError => e
391
+ return e if exp . include? Test :: Unit :: PendedError
385
392
raise e
386
393
rescue Exception => e
387
394
expected = exp . any? { |ex |
@@ -456,6 +463,78 @@ def assert_raise_with_message(exception, expected, msg = nil, &block)
456
463
ex
457
464
end
458
465
466
+ MINI_DIR = File . join ( File . dirname ( File . expand_path ( __FILE__ ) ) , "minitest" ) #:nodoc:
467
+
468
+ # :call-seq:
469
+ # assert(test, [failure_message])
470
+ #
471
+ #Tests if +test+ is true.
472
+ #
473
+ #+msg+ may be a String or a Proc. If +msg+ is a String, it will be used
474
+ #as the failure message. Otherwise, the result of calling +msg+ will be
475
+ #used as the message if the assertion fails.
476
+ #
477
+ #If no +msg+ is given, a default message will be used.
478
+ #
479
+ # assert(false, "This was expected to be true")
480
+ def assert ( test , *msgs )
481
+ case msg = msgs . first
482
+ when String , Proc
483
+ when nil
484
+ msgs . shift
485
+ else
486
+ bt = caller . reject { |s | s . start_with? ( MINI_DIR ) }
487
+ raise ArgumentError , "assertion message must be String or Proc, but #{ msg . class } was given." , bt
488
+ end unless msgs . empty?
489
+ super
490
+ end
491
+
492
+ # :call-seq:
493
+ # assert_respond_to( object, method, failure_message = nil )
494
+ #
495
+ #Tests if the given Object responds to +method+.
496
+ #
497
+ #An optional failure message may be provided as the final argument.
498
+ #
499
+ # assert_respond_to("hello", :reverse) #Succeeds
500
+ # assert_respond_to("hello", :does_not_exist) #Fails
501
+ def assert_respond_to ( obj , ( meth , *priv ) , msg = nil )
502
+ unless priv . empty?
503
+ msg = message ( msg ) {
504
+ "Expected #{ mu_pp ( obj ) } (#{ obj . class } ) to respond to ##{ meth } #{ " privately" if priv [ 0 ] } "
505
+ }
506
+ return assert obj . respond_to? ( meth , *priv ) , msg
507
+ end
508
+ #get rid of overcounting
509
+ if caller_locations ( 1 , 1 ) [ 0 ] . path . start_with? ( MINI_DIR )
510
+ return if obj . respond_to? ( meth )
511
+ end
512
+ super ( obj , meth , msg )
513
+ end
514
+
515
+ # :call-seq:
516
+ # assert_not_respond_to( object, method, failure_message = nil )
517
+ #
518
+ #Tests if the given Object does not respond to +method+.
519
+ #
520
+ #An optional failure message may be provided as the final argument.
521
+ #
522
+ # assert_not_respond_to("hello", :reverse) #Fails
523
+ # assert_not_respond_to("hello", :does_not_exist) #Succeeds
524
+ def assert_not_respond_to ( obj , ( meth , *priv ) , msg = nil )
525
+ unless priv . empty?
526
+ msg = message ( msg ) {
527
+ "Expected #{ mu_pp ( obj ) } (#{ obj . class } ) to not respond to ##{ meth } #{ " privately" if priv [ 0 ] } "
528
+ }
529
+ return assert !obj . respond_to? ( meth , *priv ) , msg
530
+ end
531
+ #get rid of overcounting
532
+ if caller_locations ( 1 , 1 ) [ 0 ] . path . start_with? ( MINI_DIR )
533
+ return unless obj . respond_to? ( meth )
534
+ end
535
+ refute_respond_to ( obj , meth , msg )
536
+ end
537
+
459
538
# pattern_list is an array which contains regexp and :*.
460
539
# :* means any sequence.
461
540
#
@@ -515,7 +594,22 @@ def assert_warn(*args)
515
594
assert_warning ( *args ) { $VERBOSE = false ; yield }
516
595
end
517
596
597
+ def assert_deprecated_warning ( mesg = /deprecated/ )
598
+ assert_warning ( mesg ) do
599
+ Warning [ :deprecated ] = true
600
+ yield
601
+ end
602
+ end
603
+
604
+ def assert_deprecated_warn ( mesg = /deprecated/ )
605
+ assert_warn ( mesg ) do
606
+ Warning [ :deprecated ] = true
607
+ yield
608
+ end
609
+ end
610
+
518
611
class << ( AssertFile = Struct . new ( :failure_message ) . new )
612
+ include Assertions
519
613
include CoreAssertions
520
614
def assert_file_predicate ( predicate , *args )
521
615
if /\A not_/ =~ predicate
@@ -606,10 +700,20 @@ def assert_join_threads(threads, message = nil)
606
700
if message
607
701
msg = "#{ message } \n #{ msg } "
608
702
end
609
- raise MiniTest :: Assertion , msg
703
+ raise Test :: Unit :: AssertionFailedError , msg
610
704
end
611
705
end
612
706
707
+ def assert_all? ( obj , m = nil , &blk )
708
+ failed = [ ]
709
+ obj . each do |*a , &b |
710
+ unless blk . call ( *a , &b )
711
+ failed << ( a . size > 1 ? a : a [ 0 ] )
712
+ end
713
+ end
714
+ assert ( failed . empty? , message ( m ) { failed . pretty_inspect } )
715
+ end
716
+
613
717
def assert_all_assertions ( msg = nil )
614
718
all = AllFailures . new
615
719
yield all
@@ -618,6 +722,14 @@ def assert_all_assertions(msg = nil)
618
722
end
619
723
alias all_assertions assert_all_assertions
620
724
725
+ def assert_all_assertions_foreach ( msg = nil , *keys , &block )
726
+ all = AllFailures . new
727
+ all . foreach ( *keys , &block )
728
+ ensure
729
+ assert ( all . pass? , message ( msg ) { all . message . chomp ( "." ) } )
730
+ end
731
+ alias all_assertions_foreach assert_all_assertions_foreach
732
+
621
733
def message ( msg = nil , *args , &default ) # :nodoc:
622
734
if Proc === msg
623
735
super ( nil , *args ) do
@@ -635,6 +747,22 @@ def message(msg = nil, *args, &default) # :nodoc:
635
747
super
636
748
end
637
749
end
750
+
751
+ def diff ( exp , act )
752
+ require 'pp'
753
+ q = PP . new ( +"" )
754
+ q . guard_inspect_key do
755
+ q . group ( 2 , "expected: " ) do
756
+ q . pp exp
757
+ end
758
+ q . text q . newline
759
+ q . group ( 2 , "actual: " ) do
760
+ q . pp act
761
+ end
762
+ q . flush
763
+ end
764
+ q . output
765
+ end
638
766
end
639
767
end
640
768
end
0 commit comments