2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative 'fixtures/classes'
|
|
|
|
require_relative 'shared/dup_clone'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
|
|
|
describe "Kernel#clone" do
|
|
|
|
it_behaves_like :kernel_dup_clone, :clone
|
|
|
|
|
|
|
|
before :each do
|
|
|
|
ScratchPad.clear
|
|
|
|
@obj = KernelSpecs::Duplicate.new 1, :a
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls #initialize_copy on the new instance" do
|
|
|
|
clone = @obj.clone
|
|
|
|
ScratchPad.recorded.should_not == @obj.object_id
|
|
|
|
ScratchPad.recorded.should == clone.object_id
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the internal allocator and does not call #allocate" do
|
|
|
|
klass = Class.new
|
|
|
|
instance = klass.new
|
|
|
|
|
|
|
|
def klass.allocate
|
|
|
|
raise "allocate should not be called"
|
|
|
|
end
|
|
|
|
|
|
|
|
clone = instance.clone
|
|
|
|
clone.class.should equal klass
|
|
|
|
end
|
|
|
|
|
|
|
|
it "copies frozen state from the original" do
|
|
|
|
o2 = @obj.clone
|
|
|
|
@obj.freeze
|
|
|
|
o3 = @obj.clone
|
|
|
|
|
2020-05-03 06:28:29 -04:00
|
|
|
o2.should_not.frozen?
|
|
|
|
o3.should.frozen?
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
2020-09-15 15:54:31 -04:00
|
|
|
ruby_version_is '3.0' do
|
2019-09-23 19:03:15 -04:00
|
|
|
it 'takes an freeze: true option to frozen copy' do
|
2020-05-03 06:28:29 -04:00
|
|
|
@obj.clone(freeze: true).should.frozen?
|
2019-09-23 19:03:15 -04:00
|
|
|
@obj.freeze
|
2020-05-03 06:28:29 -04:00
|
|
|
@obj.clone(freeze: true).should.frozen?
|
2019-09-23 19:03:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'takes an freeze: false option to not return frozen copy' do
|
2020-05-03 06:28:29 -04:00
|
|
|
@obj.clone(freeze: false).should_not.frozen?
|
2019-04-27 12:53:23 -04:00
|
|
|
@obj.freeze
|
2020-05-03 06:28:29 -04:00
|
|
|
@obj.clone(freeze: false).should_not.frozen?
|
2017-06-29 10:35:37 -04:00
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
it "copies instance variables" do
|
|
|
|
clone = @obj.clone
|
|
|
|
clone.one.should == 1
|
|
|
|
clone.two.should == :a
|
|
|
|
end
|
|
|
|
|
|
|
|
it "copies singleton methods" do
|
|
|
|
def @obj.special() :the_one end
|
|
|
|
clone = @obj.clone
|
|
|
|
clone.special.should == :the_one
|
|
|
|
end
|
|
|
|
|
|
|
|
it "copies modules included in the singleton class" do
|
|
|
|
class << @obj
|
|
|
|
include KernelSpecs::DuplicateM
|
|
|
|
end
|
|
|
|
|
|
|
|
clone = @obj.clone
|
|
|
|
clone.repr.should == "KernelSpecs::Duplicate"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "copies constants defined in the singleton class" do
|
|
|
|
class << @obj
|
|
|
|
CLONE = :clone
|
|
|
|
end
|
|
|
|
|
|
|
|
clone = @obj.clone
|
|
|
|
class << clone
|
|
|
|
CLONE.should == :clone
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "replaces a singleton object's metaclass with a new copy with the same superclass" do
|
|
|
|
cls = Class.new do
|
|
|
|
def bar
|
|
|
|
['a']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
object = cls.new
|
|
|
|
object.define_singleton_method(:bar) do
|
|
|
|
['b', *super()]
|
|
|
|
end
|
|
|
|
object.bar.should == ['b', 'a']
|
|
|
|
|
|
|
|
cloned = object.clone
|
|
|
|
|
|
|
|
cloned.singleton_methods.should == [:bar]
|
|
|
|
|
|
|
|
# bar should replace previous one
|
|
|
|
cloned.define_singleton_method(:bar) do
|
|
|
|
['c', *super()]
|
|
|
|
end
|
|
|
|
cloned.bar.should == ['c', 'a']
|
|
|
|
|
|
|
|
# bar should be removed and call through to superclass
|
|
|
|
cloned.singleton_class.class_eval do
|
|
|
|
remove_method :bar
|
|
|
|
end
|
|
|
|
|
|
|
|
cloned.bar.should == ['a']
|
|
|
|
end
|
|
|
|
|
2019-09-24 23:59:12 -04:00
|
|
|
it 'copies frozen?' do
|
|
|
|
o = ''.freeze.clone
|
2017-05-07 08:04:49 -04:00
|
|
|
o.frozen?.should be_true
|
2019-09-24 23:59:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
ruby_version_is ''...'2.7' do
|
|
|
|
it 'copies tainted?' do
|
|
|
|
o = ''.taint.clone
|
|
|
|
o.tainted?.should be_true
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|