mirror of https://github.com/rails/rails
Update Method#duplicable? to be consistent with Ruby 3.4
Fix: https://github.com/rails/rails/issues/51075 `Method` and `UnboundMethod` used to raise on `#dup`, but not `#clone`, this wasn't so much a feature, but a bug. It was fixed in https://github.com/ruby/ruby/pull/9926.
This commit is contained in:
parent
9b343c2879
commit
ca6995a80c
|
@ -28,7 +28,15 @@ class Object
|
|||
end
|
||||
end
|
||||
|
||||
class Method
|
||||
methods_are_duplicable = begin
|
||||
Object.instance_method(:duplicable?).dup
|
||||
true
|
||||
rescue TypeError
|
||||
false
|
||||
end
|
||||
|
||||
unless methods_are_duplicable
|
||||
class Method
|
||||
# Methods are not duplicable:
|
||||
#
|
||||
# method(:puts).duplicable? # => false
|
||||
|
@ -36,9 +44,9 @@ class Method
|
|||
def duplicable?
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class UnboundMethod
|
||||
class UnboundMethod
|
||||
# Unbound methods are not duplicable:
|
||||
#
|
||||
# method(:puts).unbind.duplicable? # => false
|
||||
|
@ -46,6 +54,7 @@ class UnboundMethod
|
|||
def duplicable?
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require "singleton"
|
||||
|
|
|
@ -6,18 +6,26 @@ require "active_support/core_ext/object/duplicable"
|
|||
require "active_support/core_ext/numeric/time"
|
||||
|
||||
class DuplicableTest < ActiveSupport::TestCase
|
||||
RAISE_DUP = [method(:puts), method(:puts).unbind, Class.new.include(Singleton).instance]
|
||||
ALLOW_DUP = ["1", "symbol_from_string".to_sym, Object.new, /foo/, [], {}, Time.now, Class.new, Module.new, BigDecimal("4.56"), nil, false, true, 1, 2.3, Complex(1), Rational(1)]
|
||||
OBJECTS = [
|
||||
method(:puts), method(:puts).unbind, Class.new.include(Singleton).instance,
|
||||
"1", "symbol_from_string".to_sym, Object.new, /foo/, [], {}, Time.now, Class.new,
|
||||
Module.new, BigDecimal("4.56"), nil, false, true, 1, 2.3, Complex(1), Rational(1),
|
||||
]
|
||||
|
||||
def test_duplicable
|
||||
RAISE_DUP.each do |v|
|
||||
assert_not v.duplicable?, "#{ v.inspect } should not be duplicable"
|
||||
assert_raises(TypeError, v.class.name) { v.dup }
|
||||
OBJECTS.each do |v|
|
||||
test "#{v.class}#duplicable? matches #{v.class}#dup behavior" do
|
||||
duplicable = begin
|
||||
v.dup
|
||||
true
|
||||
rescue TypeError
|
||||
false
|
||||
end
|
||||
|
||||
ALLOW_DUP.each do |v|
|
||||
assert_predicate v, :duplicable?, "#{ v.class } should be duplicable"
|
||||
assert_nothing_raised { v.dup }
|
||||
if duplicable
|
||||
assert_predicate v, :duplicable?
|
||||
else
|
||||
assert_not_predicate v, :duplicable?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue