Skip to content

Commit

Permalink
Add spec for Kernel#clone(freeze: true/false) calling #initialize_clo…
Browse files Browse the repository at this point in the history
…ne(freeze: true/false)
  • Loading branch information
eregon committed Oct 16, 2021
1 parent e27c3b4 commit 6ffa571
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 19 deletions.
92 changes: 74 additions & 18 deletions core/kernel/clone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion core/kernel/fixtures/classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 6ffa571

Please sign in to comment.