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
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
13 changes: 13 additions & 0 deletions core/array/drop_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Array#drop" do
it "removes the specified number of elements from the start of the array" do
Expand Down Expand Up @@ -48,4 +49,16 @@

-> { [1, 2].drop(obj) }.should raise_error(TypeError)
end

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

ruby_version_is '3.0' do
it 'returns a Array instance for Array subclasses' do
ArraySpecs::MyArray[1, 2, 3, 4, 5].drop(1).should be_an_instance_of(Array)
end
end
end
13 changes: 13 additions & 0 deletions core/array/drop_while_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Array#drop_while" do
it "removes elements from the start of the array while the block evaluates to true" do
Expand All @@ -12,4 +13,16 @@
it "removes elements from the start of the array until the block returns false" do
[1, 2, 3, false, 5].drop_while { |n| n }.should == [false, 5]
end

ruby_version_is ''...'3.0' do
it 'returns a subclass instance for Array subclasses' do
ArraySpecs::MyArray[1, 2, 3, 4, 5].drop_while { |n| n < 4 }.should be_an_instance_of(ArraySpecs::MyArray)
end
end

ruby_version_is '3.0' do
it 'returns a Array instance for Array subclasses' do
ArraySpecs::MyArray[1, 2, 3, 4, 5].drop_while { |n| n < 4 }.should be_an_instance_of(Array)
end
end
end
58 changes: 58 additions & 0 deletions core/array/slice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,64 @@ def to.to_int() -2 end
a.should == [3, 4]
end
end

describe "with a subclass of Array" do
before :each do
@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.slice!(0, 2).should be_an_instance_of(ArraySpecs::MyArray)
end

it "returns a subclass instance with [-n, m]" do
@array.slice!(-3, 2).should be_an_instance_of(ArraySpecs::MyArray)
end

it "returns a subclass instance with [n..m]" do
@array.slice!(1..3).should be_an_instance_of(ArraySpecs::MyArray)
end

it "returns a subclass instance with [n...m]" do
@array.slice!(1...3).should be_an_instance_of(ArraySpecs::MyArray)
end

it "returns a subclass instance with [-n..-m]" do
@array.slice!(-3..-1).should be_an_instance_of(ArraySpecs::MyArray)
end

it "returns a subclass instance with [-n...-m]" do
@array.slice!(-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.slice!(0, 2).should be_an_instance_of(Array)
end

it "returns a Array instance with [-n, m]" do
@array.slice!(-3, 2).should be_an_instance_of(Array)
end

it "returns a Array instance with [n..m]" do
@array.slice!(1..3).should be_an_instance_of(Array)
end

it "returns a Array instance with [n...m]" do
@array.slice!(1...3).should be_an_instance_of(Array)
end

it "returns a Array instance with [-n..-m]" do
@array.slice!(-3..-1).should be_an_instance_of(Array)
end

it "returns a Array instance with [-n...-m]" do
@array.slice!(-3...-1).should be_an_instance_of(Array)
end
end
end
end

describe "Array#slice" do
Expand Down
13 changes: 13 additions & 0 deletions core/array/take_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Array#take" do
it "returns the first specified number of elements" do
Expand All @@ -24,4 +25,16 @@
it "raises an ArgumentError when the argument is negative" do
->{ [1].take(-3) }.should raise_error(ArgumentError)
end

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

ruby_version_is '3.0' do
it 'returns a Array instance for Array subclasses' do
ArraySpecs::MyArray[1, 2, 3, 4, 5].take(1).should be_an_instance_of(Array)
end
end
end
13 changes: 13 additions & 0 deletions core/array/take_while_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Array#take_while" do
it "returns all elements until the block returns false" do
Expand All @@ -12,4 +13,16 @@
it "returns all elements until the block returns false" do
[1, 2, false, 4].take_while{ |element| element }.should == [1, 2]
end

ruby_version_is ''...'3.0' do
it 'returns a subclass instance for Array subclasses' do
ArraySpecs::MyArray[1, 2, 3, 4, 5].take_while { |n| n < 4 }.should be_an_instance_of(ArraySpecs::MyArray)
end
end

ruby_version_is '3.0' do
it 'returns a Array instance for Array subclasses' do
ArraySpecs::MyArray[1, 2, 3, 4, 5].take_while { |n| n < 4 }.should be_an_instance_of(Array)
end
end
end