@@ -303,9 +303,35 @@ def assert_ruby_status(args, test_stdin="", message=nil, **opt)
303303
304304 def separated_runner ( token , out = nil )
305305 include ( *Test ::Unit ::TestCase . ancestors . select { |c | !c . is_a? ( Class ) } )
306+
306307 out = out ? IO . new ( out , 'w' ) : STDOUT
308+
309+ # avoid method redefinitions
310+ out_write = out . method ( :write )
311+ integer_to_s = Integer . instance_method ( :to_s )
312+ array_pack = Array . instance_method ( :pack )
313+ marshal_dump = Marshal . method ( :dump )
314+ assertions_ivar_set = Test ::Unit ::Assertions . method ( :instance_variable_set )
315+ assertions_ivar_get = Test ::Unit ::Assertions . method ( :instance_variable_get )
316+ Test ::Unit ::Assertions . module_eval do
317+ @_assertions = 0
318+
319+ undef _assertions =
320+ define_method ( :_assertions= , -> ( n ) { assertions_ivar_set . call ( :@_assertions , n ) } )
321+
322+ undef _assertions
323+ define_method ( :_assertions , -> { assertions_ivar_get . call ( :@_assertions ) } )
324+ end
325+ # assume Method#call and UnboundMethod#bind_call need to work as the original
326+
307327 at_exit {
308- out . puts "#{ token } <error>" , [ Marshal . dump ( $!) ] . pack ( 'm' ) , "#{ token } </error>" , "#{ token } assertions=#{ self . _assertions } "
328+ assertions = assertions_ivar_get . call ( :@_assertions )
329+ out_write . call <<~OUT
330+ #{ token } <error>
331+ #{ array_pack . bind_call ( [ marshal_dump . call ( $!) ] , 'm' ) }
332+ #{ token } </error>
333+ #{ token } assertions=#{ integer_to_s . bind_call ( assertions ) }
334+ OUT
309335 }
310336 if defined? ( Test ::Unit ::Runner )
311337 Test ::Unit ::Runner . class_variable_set ( :@@stop_auto_run , true )
@@ -360,7 +386,9 @@ def assert_separately(args, file = nil, line = nil, src, ignore_stderr: nil, **o
360386 raise if $!
361387 abort = status . coredump? || ( status . signaled? && ABORT_SIGNALS . include? ( status . termsig ) )
362388 assert ( !abort , FailDesc [ status , nil , stderr ] )
363- self . _assertions += res [ /^#{ token_re } assertions=(\d +)/ , 1 ] . to_i
389+ if ( assertions = res [ /^#{ token_re } assertions=(\d +)/ , 1 ] . to_i ) > 0
390+ self . _assertions += assertions
391+ end
364392 begin
365393 res = Marshal . load ( res [ /^#{ token_re } <error>\n \K .*\n (?=#{ token_re } <\/ error>$)/m ] . unpack1 ( "m" ) )
366394 rescue => marshal_error
0 commit comments