@@ -17,11 +17,11 @@ def test_new_with_invalid_capacity
1717 end
1818
1919 def test_capture
20- buffer = StackFrames ::Buffer . new ( 1 )
20+ buffer = StackFrames ::Buffer . new ( 2 )
2121 expected_line = __LINE__ + 1
2222 frames_length = buffer . capture
23- assert_equal ( 1 , frames_length )
24- frame = buffer [ 0 ]
23+ assert_equal ( 2 , frames_length )
24+ frame = buffer [ skipping_c_frames? ? 0 : 1 ]
2525 assert_equal ( 'test_capture' , frame . method_name )
2626 assert_equal ( true , frame . method_name . frozen? )
2727 assert_equal ( __FILE__ , frame . path )
@@ -36,13 +36,16 @@ def test_no_object_allocations
3636 got_path = nil
3737 got_lineno = nil
3838 got_method_name = nil
39- num_allocations = count_allocations do
39+ frame_index = skipping_c_frames? ? 1 : 3
40+ instrumented_code = lambda do
4041 buffer . capture
41- frame = buffer [ 1 ]
42+ frame = buffer [ frame_index ]
4243 got_path = frame . path
4344 got_lineno = frame . lineno
4445 got_method_name = frame . method_name
4546 end
47+ count_allocations ( &instrumented_code ) # allow lazy memoized allocations
48+ num_allocations = count_allocations ( &instrumented_code )
4649 assert_equal ( 0 , num_allocations )
4750 assert_equal ( 'count_allocations' , got_method_name )
4851 assert_equal ( method ( :count_allocations ) . source_location [ 1 ] + 1 , got_lineno )
@@ -55,12 +58,19 @@ def test_index_lookup
5558 frame1 do
5659 buffer . capture
5760 end
61+ skipped = 0
5862 [
59- [ "test_index_lookup" , capture_lineno ] ,
60- [ "frame1" , method ( :frame1 ) . source_location [ 1 ] + 1 ] ,
61- [ "test_index_lookup" , capture_lineno - 1 ] ,
62- ] . each_with_index do |( method_name , lineno ) , i |
63- frame = buffer [ i ]
63+ [ :c , "capture" , 0 ] ,
64+ [ :ruby , "test_index_lookup" , capture_lineno ] ,
65+ [ :ruby , "frame1" , method ( :frame1 ) . source_location [ 1 ] + 1 ] ,
66+ [ :ruby , "test_index_lookup" , capture_lineno - 1 ] ,
67+ ] . each_with_index do |( lang , method_name , lineno ) , i |
68+ if skipping_c_frames? && lang == :c
69+ skipped += 1
70+ next
71+ end
72+ buffer_idx = i - skipped
73+ frame = buffer [ buffer_idx ]
6474 assert_equal ( method_name , frame . method_name , "frame #{ i } " )
6575 assert_equal ( lineno , frame . lineno , "frame #{ i } " )
6676 end
@@ -107,6 +117,10 @@ def test_gc_stress
107117
108118 private
109119
120+ def skipping_c_frames?
121+ Gem ::Version . new ( RUBY_VERSION ) < Gem ::Version . new ( "3" )
122+ end
123+
110124 def frame1
111125 yield
112126 end
0 commit comments