From 6ffa571eae6667ce944aa6c599144990f258c8bf Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sat, 16 Oct 2021 13:37:01 +0200 Subject: [PATCH] Add spec for Kernel#clone(freeze: true/false) calling #initialize_clone(freeze: true/false) --- core/kernel/clone_spec.rb | 92 ++++++++++++++++++++++++++------- core/kernel/fixtures/classes.rb | 8 ++- 2 files changed, 81 insertions(+), 19 deletions(-) diff --git a/core/kernel/clone_spec.rb b/core/kernel/clone_spec.rb index c18af4a490..f9daa2badc 100644 --- a/core/kernel/clone_spec.rb +++ b/core/kernel/clone_spec.rb @@ -28,27 +28,88 @@ def klass.allocate clone.class.should equal klass end - it "copies frozen state from the original" do - o2 = @obj.clone - @obj.freeze - o3 = @obj.clone + describe "with no arguments" do + it "copies frozen state from the original" do + o2 = @obj.clone + @obj.freeze + o3 = @obj.clone + + o2.should_not.frozen? + o3.should.frozen? + end - o2.should_not.frozen? - o3.should.frozen? + it 'copies frozen?' do + o = ''.freeze.clone + o.frozen?.should be_true + end end - ruby_version_is '3.0' do - it 'takes an freeze: true option to frozen copy' do - @obj.clone(freeze: true).should.frozen? + describe "with freeze: true" do + it 'makes a frozen copy if the original is frozen' do @obj.freeze @obj.clone(freeze: true).should.frozen? end + + ruby_version_is ''...'3.0' do + it 'does not freeze the copy even if the original is not frozen' do + @obj.clone(freeze: true).should_not.frozen? + end + + it "calls #initialize_clone with no kwargs" do + obj = KernelSpecs::CloneFreeze.new + obj.clone(freeze: true) + ScratchPad.recorded.should == [obj, {}] + end + end + + ruby_version_is '3.0' do + it 'freezes the copy even if the original was not frozen' do + @obj.clone(freeze: true).should.frozen? + end + + it "calls #initialize_clone with kwargs freeze: true" do + obj = KernelSpecs::CloneFreeze.new + obj.clone(freeze: true) + ScratchPad.recorded.should == [obj, { freeze: true }] + end + + it "calls #initialize_clone with kwargs freeze: true even if #initialize_clone only takes a single argument" do + obj = KernelSpecs::Clone.new + -> { obj.clone(freeze: true) }.should raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1)') + end + end end - it 'takes an freeze: false option to not return frozen copy' do - @obj.clone(freeze: false).should_not.frozen? - @obj.freeze - @obj.clone(freeze: false).should_not.frozen? + describe "with freeze: false" do + it 'does not freeze the copy if the original is frozen' do + @obj.freeze + @obj.clone(freeze: false).should_not.frozen? + end + + it 'does not freeze the copy if the original is not frozen' do + @obj.clone(freeze: false).should_not.frozen? + end + + ruby_version_is ''...'3.0' do + it "calls #initialize_clone with no kwargs" do + obj = KernelSpecs::CloneFreeze.new + obj.clone(freeze: false) + ScratchPad.recorded.should == [obj, {}] + end + end + + ruby_version_is '3.0' do + it "calls #initialize_clone with kwargs freeze: false" do + obj = KernelSpecs::CloneFreeze.new + obj.clone(freeze: false) + ScratchPad.recorded.should == [obj, { freeze: false }] + end + + it "calls #initialize_clone with kwargs freeze: false even if #initialize_clone only takes a single argument" do + obj = KernelSpecs::Clone.new + -> { obj.clone(freeze: false) }.should raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1)') + end + end end it "copies instance variables" do @@ -114,11 +175,6 @@ def bar cloned.bar.should == ['a'] end - it 'copies frozen?' do - o = ''.freeze.clone - o.frozen?.should be_true - end - ruby_version_is ''...'2.7' do it 'copies tainted?' do o = ''.taint.clone diff --git a/core/kernel/fixtures/classes.rb b/core/kernel/fixtures/classes.rb index 1bf5715c50..8de1407b92 100644 --- a/core/kernel/fixtures/classes.rb +++ b/core/kernel/fixtures/classes.rb @@ -288,7 +288,13 @@ def initialize_copy(other) class Clone def initialize_clone(other) - ScratchPad.record other.object_id + ScratchPad.record other + end + end + + class CloneFreeze + def initialize_clone(other, **kwargs) + ScratchPad.record([other, kwargs]) end end