Skip to content

Commit

Permalink
methods on array subclass return array instance
Browse files Browse the repository at this point in the history
  • Loading branch information
kuei0221 authored and eregon committed Jan 30, 2021
1 parent 5eae23f commit f4939b2
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
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

0 comments on commit f4939b2

Please sign in to comment.