File tree Expand file tree Collapse file tree 2 files changed +20
-13
lines changed
lib/active_support/testing Expand file tree Collapse file tree 2 files changed +20
-13
lines changed Original file line number Diff line number Diff line change @@ -299,17 +299,24 @@ def _assert_nothing_raised_or_warn(assertion, &block)
299
299
end
300
300
301
301
def _callable_to_source_string ( callable )
302
- if defined? ( RubyVM ::AbstractSyntaxTree ) && callable . is_a? ( Proc )
303
- ast = begin
304
- RubyVM ::AbstractSyntaxTree . of ( callable , keep_script_lines : true )
305
- rescue SystemCallError
306
- # Failed to get the source somehow
307
- return callable
308
- end
309
- return callable unless ast
302
+ if defined? ( RubyVM ::InstructionSequence ) && callable . is_a? ( Proc )
303
+ iseq = RubyVM ::InstructionSequence . of ( callable )
304
+ source =
305
+ if iseq . script_lines
306
+ iseq . script_lines . join ( "\n " )
307
+ elsif File . readable? ( iseq . absolute_path )
308
+ File . read ( iseq . absolute_path )
309
+ end
310
+
311
+ return callable unless source
312
+
313
+ location = iseq . to_a [ 4 ] [ :code_location ]
314
+ return callable unless location
310
315
311
- source = ast . source
312
- source . strip!
316
+ lines = source . lines [ ( location [ 0 ] - 1 ) ..( location [ 2 ] - 1 ) ]
317
+ lines [ -1 ] = lines [ -1 ] . byteslice ( ...location [ 3 ] )
318
+ lines [ 0 ] = lines [ 0 ] . byteslice ( location [ 1 ] ...)
319
+ source = lines . join . strip
313
320
314
321
# We ignore procs defined with do/end as they are likely multi-line anyway.
315
322
if source . start_with? ( "{" )
Original file line number Diff line number Diff line change @@ -171,7 +171,7 @@ def test_assert_difference_message_includes_change
171
171
end
172
172
173
173
def test_assert_difference_message_with_lambda
174
- skip if !defined? ( RubyVM ::AbstractSyntaxTree )
174
+ skip if !defined? ( RubyVM ::InstructionSequence )
175
175
176
176
error = assert_raises Minitest ::Assertion do
177
177
assert_difference ( -> { @object . num } , 1 , "Object Changed" ) do
@@ -247,7 +247,7 @@ def test_assert_changes_with_to_option_but_no_change_has_special_message
247
247
end
248
248
249
249
def test_assert_changes_message_with_lambda
250
- skip if !defined? ( RubyVM ::AbstractSyntaxTree )
250
+ skip if !defined? ( RubyVM ::InstructionSequence )
251
251
252
252
error = assert_raises Minitest ::Assertion do
253
253
assert_changes -> { @object . num } , to : 0 do
@@ -375,7 +375,7 @@ def test_assert_no_changes_with_message
375
375
end
376
376
377
377
def test_assert_no_changes_message_with_lambda
378
- skip if !defined? ( RubyVM ::AbstractSyntaxTree )
378
+ skip if !defined? ( RubyVM ::InstructionSequence )
379
379
380
380
error = assert_raises Minitest ::Assertion do
381
381
assert_no_changes -> { @object . num } do
You can’t perform that action at this time.
0 commit comments