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

methods on array subclass return array instance #827

Merged
merged 1 commit into from
Jan 30, 2021

Conversation

kuei0221
Copy link
Contributor

HI, here is a spec for #823,

The following methods now return Array instances instead of
subclass instances when called on subclass instances:
[Bug #6087]

Array#drop
Array#drop_while
Array#flatten
Array#slice!
Array#slice / Array#[]
Array#take
Array#take_while
Array#uniq
Array#*

This PR will add spec to cover drop, drop_while, take, take_while, slice!
Other methods are already covered with spec:

Array#flatten

ruby_version_is ''...'3.0' do
it "returns subclass instance for Array subclasses" do
ArraySpecs::MyArray[].flatten.should be_an_instance_of(ArraySpecs::MyArray)
ArraySpecs::MyArray[1, 2, 3].flatten.should be_an_instance_of(ArraySpecs::MyArray)
ArraySpecs::MyArray[1, [2], 3].flatten.should be_an_instance_of(ArraySpecs::MyArray)
ArraySpecs::MyArray[1, [2, 3], 4].flatten.should == ArraySpecs::MyArray[1, 2, 3, 4]
[ArraySpecs::MyArray[1, 2, 3]].flatten.should be_an_instance_of(Array)
end
end
ruby_version_is '3.0' do
it "returns Array instance for Array subclasses" do
ArraySpecs::MyArray[].flatten.should be_an_instance_of(Array)
ArraySpecs::MyArray[1, 2, 3].flatten.should be_an_instance_of(Array)
ArraySpecs::MyArray[1, [2], 3].flatten.should be_an_instance_of(Array)
ArraySpecs::MyArray[1, [2, 3], 4].flatten.should == [1, 2, 3, 4]
[ArraySpecs::MyArray[1, 2, 3]].flatten.should be_an_instance_of(Array)
end
end

Array#slice

describe "with a subclass of Array" do
before :each do
ScratchPad.clear
@array = ArraySpecs::MyArray[1, 2, 3, 4, 5]
end
ruby_version_is ''...'3.0' do
it "returns a subclass instance with [n, m]" do
@array.send(@method, 0, 2).should be_an_instance_of(ArraySpecs::MyArray)
end
it "returns a subclass instance with [-n, m]" do
@array.send(@method, -3, 2).should be_an_instance_of(ArraySpecs::MyArray)
end
it "returns a subclass instance with [n..m]" do
@array.send(@method, 1..3).should be_an_instance_of(ArraySpecs::MyArray)
end
it "returns a subclass instance with [n...m]" do
@array.send(@method, 1...3).should be_an_instance_of(ArraySpecs::MyArray)
end
it "returns a subclass instance with [-n..-m]" do
@array.send(@method, -3..-1).should be_an_instance_of(ArraySpecs::MyArray)
end
it "returns a subclass instance with [-n...-m]" do
@array.send(@method, -3...-1).should be_an_instance_of(ArraySpecs::MyArray)
end
end
ruby_version_is '3.0' do
it "returns a Array instance with [n, m]" do
@array.send(@method, 0, 2).should be_an_instance_of(Array)
end
it "returns a Array instance with [-n, m]" do
@array.send(@method, -3, 2).should be_an_instance_of(Array)
end
it "returns a Array instance with [n..m]" do
@array.send(@method, 1..3).should be_an_instance_of(Array)
end
it "returns a Array instance with [n...m]" do
@array.send(@method, 1...3).should be_an_instance_of(Array)
end
it "returns a Array instance with [-n..-m]" do
@array.send(@method, -3..-1).should be_an_instance_of(Array)
end
it "returns a Array instance with [-n...-m]" do
@array.send(@method, -3...-1).should be_an_instance_of(Array)
end
end

Array#uniq

ruby_version_is ''...'3.0' do
it "returns subclass instance on Array subclasses" do
ArraySpecs::MyArray[1, 2, 3].uniq.should be_an_instance_of(ArraySpecs::MyArray)
end
end
ruby_version_is '3.0' do
it "returns Array instance on Array subclasses" do
ArraySpecs::MyArray[1, 2, 3].uniq.should be_an_instance_of(Array)
end
end

Array#*

ruby_version_is ''...'3.0' do
it "returns a subclass instance" do
(@array * 0).should be_an_instance_of(ArraySpecs::MyArray)
(@array * 1).should be_an_instance_of(ArraySpecs::MyArray)
(@array * 2).should be_an_instance_of(ArraySpecs::MyArray)
end
end
ruby_version_is '3.0' do
it "returns an Array instance" do
(@array * 0).should be_an_instance_of(Array)
(@array * 1).should be_an_instance_of(Array)
(@array * 2).should be_an_instance_of(Array)
end
end

Thanks 👍

Copy link
Member

@eregon eregon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you!

Sorry for the late review.

@eregon eregon merged commit f4939b2 into ruby:master Jan 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants