Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add specs to cover the fix for [Bug #14266] #858

Merged
merged 3 commits into from
Oct 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 17 additions & 0 deletions library/set/initialize_clone_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative '../../spec_helper'
require 'set'

describe "Set#initialize_clone" do
ruby_version_is "3.0" do
it "does not freeze the new Set when called from clone(freeze: false)" do
set1 = Set[1, 2]
set1.freeze
set2 = set1.clone(freeze: false)
set1.frozen?.should == true
set2.frozen?.should == false
set2.add 3
set1.should == Set[1, 2]
set2.should == Set[1, 2, 3]
end
end
end