Refactor assert_forwarded so it does not need ObjectSpace._id2ref or eval
This commit is contained in:
parent
f0b50d3c6c
commit
af716bef32
|
@ -182,22 +182,21 @@ end
|
||||||
module ExtraAssertions
|
module ExtraAssertions
|
||||||
def assert_forwarded(object, method, ret_val, *expected_args)
|
def assert_forwarded(object, method, ret_val, *expected_args)
|
||||||
call_args = nil
|
call_args = nil
|
||||||
call_args_proc = proc { |args| call_args = args }
|
object.singleton_class.class_exec do
|
||||||
object.instance_eval <<-END_EVAL, __FILE__, __LINE__ + 1
|
alias_method :"#{method}_org", method
|
||||||
alias #{method}_org #{method}
|
define_method(method) do |*args|
|
||||||
def #{method}(*args)
|
call_args = args
|
||||||
ObjectSpace._id2ref(#{call_args_proc.object_id}).call(args)
|
ret_val
|
||||||
ObjectSpace._id2ref(#{ret_val.object_id})
|
end
|
||||||
end
|
end
|
||||||
END_EVAL
|
|
||||||
|
|
||||||
assert_equal(ret_val, yield) # Invoke test
|
assert_equal(ret_val, yield) # Invoke test
|
||||||
assert_equal(expected_args, call_args)
|
assert_equal(expected_args, call_args)
|
||||||
ensure
|
ensure
|
||||||
object.instance_eval <<-END_EVAL, __FILE__, __LINE__ + 1
|
object.singleton_class.class_exec do
|
||||||
undef #{method}
|
remove_method method
|
||||||
alias #{method} #{method}_org
|
alias_method method, :"#{method}_org"
|
||||||
END_EVAL
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue