From 112846044d39a20912528528308c473c240ce417 Mon Sep 17 00:00:00 2001 From: michaelhwang Date: Thu, 14 Jan 2021 00:47:42 +0800 Subject: [PATCH] methods on array subclass return array instance --- core/array/drop_spec.rb | 13 ++++++++ core/array/drop_while_spec.rb | 13 ++++++++ core/array/slice_spec.rb | 58 +++++++++++++++++++++++++++++++++++ core/array/take_spec.rb | 13 ++++++++ core/array/take_while_spec.rb | 13 ++++++++ 5 files changed, 110 insertions(+) diff --git a/core/array/drop_spec.rb b/core/array/drop_spec.rb index 84ea86b04c..f911fd9018 100644 --- a/core/array/drop_spec.rb +++ b/core/array/drop_spec.rb @@ -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 @@ -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 diff --git a/core/array/drop_while_spec.rb b/core/array/drop_while_spec.rb index cfb6b1e267..bb783d22a5 100644 --- a/core/array/drop_while_spec.rb +++ b/core/array/drop_while_spec.rb @@ -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 @@ -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 diff --git a/core/array/slice_spec.rb b/core/array/slice_spec.rb index f416d8d0ce..2f98df9488 100644 --- a/core/array/slice_spec.rb +++ b/core/array/slice_spec.rb @@ -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 diff --git a/core/array/take_spec.rb b/core/array/take_spec.rb index 0de99b0a7e..4fb6f0ce75 100644 --- a/core/array/take_spec.rb +++ b/core/array/take_spec.rb @@ -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 @@ -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 diff --git a/core/array/take_while_spec.rb b/core/array/take_while_spec.rb index f159e6f251..363419b265 100644 --- a/core/array/take_while_spec.rb +++ b/core/array/take_while_spec.rb @@ -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 @@ -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