@@ -32,11 +32,20 @@ def test_deferred_token_stream_materializes_registered_nodes_once
3232 context = RDoc ::Parser ::RubyColorizer ::DeferredContext . new ( code )
3333 loaders = nodes . drop ( 1 ) . map { |node | context . token_stream_loader ( node . node_id ) }
3434 parse_lex_calls = partial_colorize_calls = 0
35- count_parse_lex = -> ( original , *arguments ) { parse_lex_calls += 1 ; original . call ( *arguments ) }
36- count_partial_colorize = -> ( original , *arguments ) { partial_colorize_calls += 1 ; original . call ( *arguments ) }
35+ colorizer = RDoc ::Parser ::RubyColorizer
36+
37+ without_singleton_method ( Prism , :parse_lex ) do |parse_lex |
38+ Prism . define_singleton_method ( :parse_lex ) do |*arguments |
39+ parse_lex_calls += 1
40+ parse_lex . call ( *arguments )
41+ end
42+
43+ without_singleton_method ( colorizer , :partial_colorize ) do |partial_colorize |
44+ colorizer . define_singleton_method ( :partial_colorize ) do |*arguments |
45+ partial_colorize_calls += 1
46+ partial_colorize . call ( *arguments )
47+ end
3748
38- stub_singleton_method ( Prism , :parse_lex , count_parse_lex ) do
39- stub_singleton_method ( RDoc ::Parser ::RubyColorizer , :partial_colorize , count_partial_colorize ) do
4049 assert_equal %w[ first second ] , loaders . map { |loader | loader . call . map ( &:text ) . join }
4150 end
4251 end
@@ -62,15 +71,17 @@ def test_deferred_token_stream_retries_atomically
6271 nodes = Prism . parse ( code ) . value . statements . body
6372 context = RDoc ::Parser ::RubyColorizer ::DeferredContext . new ( code )
6473 loaders = nodes . map { |node | context . token_stream_loader ( node . node_id ) }
74+ colorizer = RDoc ::Parser ::RubyColorizer
6575 calls = 0
66- fail_second_call = lambda do |original , *arguments |
67- calls += 1
68- raise 'colorization failed' if calls == 2
6976
70- original . call ( *arguments )
71- end
77+ without_singleton_method ( colorizer , :partial_colorize ) do |partial_colorize |
78+ colorizer . define_singleton_method ( :partial_colorize ) do |*arguments |
79+ calls += 1
80+ raise 'colorization failed' if calls == 2
81+
82+ partial_colorize . call ( *arguments )
83+ end
7284
73- stub_singleton_method ( RDoc ::Parser ::RubyColorizer , :partial_colorize , fail_second_call ) do
7485 assert_raise ( RuntimeError ) { loaders . first . call }
7586 end
7687
@@ -314,18 +325,16 @@ def test_rational_imaginary
314325
315326 private
316327
317- # Replaces +object+.+name+ with +replacement+ while the block runs.
318- # +replacement+ receives the original method as its first argument.
319- # The original is removed before each redefinition to avoid method
320- # redefinition warnings.
321- def stub_singleton_method ( object , name , replacement )
328+ # Removes the singleton method +name+ from +object+ and yields the
329+ # original method. The block can define a replacement without a method
330+ # redefinition warning. The original method is restored after the block.
331+ def without_singleton_method ( object , name )
322332 original = object . method ( name )
323333 object . singleton_class . remove_method ( name )
324- object . define_singleton_method ( name ) { |*arguments | replacement . call ( original , *arguments ) }
325- begin
326- yield
327- ensure
328- object . singleton_class . remove_method ( name )
334+ yield original
335+ ensure
336+ if original
337+ object . singleton_class . remove_method ( name ) if object . singleton_class . method_defined? ( name , false )
329338 object . define_singleton_method ( name , original )
330339 end
331340 end
0 commit comments