Skip to content

Commit f4939b2

Browse files
kuei0221eregon
authored andcommitted
methods on array subclass return array instance
1 parent 5eae23f commit f4939b2

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

Diff for: core/array/drop_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative '../../spec_helper'
2+
require_relative 'fixtures/classes'
23

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

4950
-> { [1, 2].drop(obj) }.should raise_error(TypeError)
5051
end
52+
53+
ruby_version_is ''...'3.0' do
54+
it 'returns a subclass instance for Array subclasses' do
55+
ArraySpecs::MyArray[1, 2, 3, 4, 5].drop(1).should be_an_instance_of(ArraySpecs::MyArray)
56+
end
57+
end
58+
59+
ruby_version_is '3.0' do
60+
it 'returns a Array instance for Array subclasses' do
61+
ArraySpecs::MyArray[1, 2, 3, 4, 5].drop(1).should be_an_instance_of(Array)
62+
end
63+
end
5164
end

Diff for: core/array/drop_while_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative '../../spec_helper'
2+
require_relative 'fixtures/classes'
23

34
describe "Array#drop_while" do
45
it "removes elements from the start of the array while the block evaluates to true" do
@@ -12,4 +13,16 @@
1213
it "removes elements from the start of the array until the block returns false" do
1314
[1, 2, 3, false, 5].drop_while { |n| n }.should == [false, 5]
1415
end
16+
17+
ruby_version_is ''...'3.0' do
18+
it 'returns a subclass instance for Array subclasses' do
19+
ArraySpecs::MyArray[1, 2, 3, 4, 5].drop_while { |n| n < 4 }.should be_an_instance_of(ArraySpecs::MyArray)
20+
end
21+
end
22+
23+
ruby_version_is '3.0' do
24+
it 'returns a Array instance for Array subclasses' do
25+
ArraySpecs::MyArray[1, 2, 3, 4, 5].drop_while { |n| n < 4 }.should be_an_instance_of(Array)
26+
end
27+
end
1528
end

Diff for: core/array/slice_spec.rb

+58
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,64 @@ def to.to_int() -2 end
185185
a.should == [3, 4]
186186
end
187187
end
188+
189+
describe "with a subclass of Array" do
190+
before :each do
191+
@array = ArraySpecs::MyArray[1, 2, 3, 4, 5]
192+
end
193+
194+
ruby_version_is ''...'3.0' do
195+
it "returns a subclass instance with [n, m]" do
196+
@array.slice!(0, 2).should be_an_instance_of(ArraySpecs::MyArray)
197+
end
198+
199+
it "returns a subclass instance with [-n, m]" do
200+
@array.slice!(-3, 2).should be_an_instance_of(ArraySpecs::MyArray)
201+
end
202+
203+
it "returns a subclass instance with [n..m]" do
204+
@array.slice!(1..3).should be_an_instance_of(ArraySpecs::MyArray)
205+
end
206+
207+
it "returns a subclass instance with [n...m]" do
208+
@array.slice!(1...3).should be_an_instance_of(ArraySpecs::MyArray)
209+
end
210+
211+
it "returns a subclass instance with [-n..-m]" do
212+
@array.slice!(-3..-1).should be_an_instance_of(ArraySpecs::MyArray)
213+
end
214+
215+
it "returns a subclass instance with [-n...-m]" do
216+
@array.slice!(-3...-1).should be_an_instance_of(ArraySpecs::MyArray)
217+
end
218+
end
219+
220+
ruby_version_is '3.0' do
221+
it "returns a Array instance with [n, m]" do
222+
@array.slice!(0, 2).should be_an_instance_of(Array)
223+
end
224+
225+
it "returns a Array instance with [-n, m]" do
226+
@array.slice!(-3, 2).should be_an_instance_of(Array)
227+
end
228+
229+
it "returns a Array instance with [n..m]" do
230+
@array.slice!(1..3).should be_an_instance_of(Array)
231+
end
232+
233+
it "returns a Array instance with [n...m]" do
234+
@array.slice!(1...3).should be_an_instance_of(Array)
235+
end
236+
237+
it "returns a Array instance with [-n..-m]" do
238+
@array.slice!(-3..-1).should be_an_instance_of(Array)
239+
end
240+
241+
it "returns a Array instance with [-n...-m]" do
242+
@array.slice!(-3...-1).should be_an_instance_of(Array)
243+
end
244+
end
245+
end
188246
end
189247

190248
describe "Array#slice" do

Diff for: core/array/take_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative '../../spec_helper'
2+
require_relative 'fixtures/classes'
23

34
describe "Array#take" do
45
it "returns the first specified number of elements" do
@@ -24,4 +25,16 @@
2425
it "raises an ArgumentError when the argument is negative" do
2526
->{ [1].take(-3) }.should raise_error(ArgumentError)
2627
end
28+
29+
ruby_version_is ''...'3.0' do
30+
it 'returns a subclass instance for Array subclasses' do
31+
ArraySpecs::MyArray[1, 2, 3, 4, 5].take(1).should be_an_instance_of(ArraySpecs::MyArray)
32+
end
33+
end
34+
35+
ruby_version_is '3.0' do
36+
it 'returns a Array instance for Array subclasses' do
37+
ArraySpecs::MyArray[1, 2, 3, 4, 5].take(1).should be_an_instance_of(Array)
38+
end
39+
end
2740
end

Diff for: core/array/take_while_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative '../../spec_helper'
2+
require_relative 'fixtures/classes'
23

34
describe "Array#take_while" do
45
it "returns all elements until the block returns false" do
@@ -12,4 +13,16 @@
1213
it "returns all elements until the block returns false" do
1314
[1, 2, false, 4].take_while{ |element| element }.should == [1, 2]
1415
end
16+
17+
ruby_version_is ''...'3.0' do
18+
it 'returns a subclass instance for Array subclasses' do
19+
ArraySpecs::MyArray[1, 2, 3, 4, 5].take_while { |n| n < 4 }.should be_an_instance_of(ArraySpecs::MyArray)
20+
end
21+
end
22+
23+
ruby_version_is '3.0' do
24+
it 'returns a Array instance for Array subclasses' do
25+
ArraySpecs::MyArray[1, 2, 3, 4, 5].take_while { |n| n < 4 }.should be_an_instance_of(Array)
26+
end
27+
end
1528
end

0 commit comments

Comments
 (0)