diff options
Diffstat (limited to 'spec/ruby/core/array')
-rw-r--r-- | spec/ruby/core/array/deconstruct_spec.rb | 11 | ||||
-rw-r--r-- | spec/ruby/core/array/element_set_spec.rb | 47 | ||||
-rw-r--r-- | spec/ruby/core/array/fill_spec.rb | 9 | ||||
-rw-r--r-- | spec/ruby/core/array/intersection_spec.rb | 88 | ||||
-rw-r--r-- | spec/ruby/core/array/shared/intersection.rb | 84 | ||||
-rw-r--r-- | spec/ruby/core/array/shared/join.rb | 14 | ||||
-rw-r--r-- | spec/ruby/core/array/slice_spec.rb | 12 | ||||
-rw-r--r-- | spec/ruby/core/array/values_at_spec.rb | 7 |
8 files changed, 195 insertions, 77 deletions
diff --git a/spec/ruby/core/array/deconstruct_spec.rb b/spec/ruby/core/array/deconstruct_spec.rb new file mode 100644 index 0000000000..2b07152dfc --- /dev/null +++ b/spec/ruby/core/array/deconstruct_spec.rb @@ -0,0 +1,11 @@ +require_relative '../../spec_helper' + +ruby_version_is "2.7" do + describe "Array#deconstruct" do + it "returns self" do + array = [1] + + array.deconstruct.should equal array + end + end +end diff --git a/spec/ruby/core/array/element_set_spec.rb b/spec/ruby/core/array/element_set_spec.rb index 9375ff9b80..f58f0ffdc4 100644 --- a/spec/ruby/core/array/element_set_spec.rb +++ b/spec/ruby/core/array/element_set_spec.rb @@ -396,6 +396,14 @@ describe "Array#[]= with [m..n]" do a.should == [1, 2, 3, 8, 4, 5] end + it "inserts at the end if m > the array size" do + a = [1, 2, 3] + a[3..3] = [4] + a.should == [1, 2, 3, 4] + a[5..7] = [6] + a.should == [1, 2, 3, 4, nil, 6] + end + describe "Range subclasses" do before :each do @range_incl = ArraySpecs::MyRange.new(1, 2) @@ -425,6 +433,45 @@ describe "Array#[]= with [m..n]" do end end +ruby_version_is "2.6" do + describe "Array#[]= with [m..]" do + + it "just sets the section defined by range to nil even if the rhs is nil" do + a = [1, 2, 3, 4, 5] + a[eval("(2..)")] = nil + a.should == [1, 2, nil] + end + + it "just sets the section defined by range to nil if m and n < 0 and the rhs is nil" do + a = [1, 2, 3, 4, 5] + a[eval("(-3..)")] = nil + a.should == [1, 2, nil] + end + + it "replaces the section defined by range" do + a = [6, 5, 4, 3, 2, 1] + a[eval("(3...)")] = 9 + a.should == [6, 5, 4, 9] + a[eval("(2..)")] = [7, 7, 7] + a.should == [6, 5, 7, 7, 7] + end + + it "replaces the section if m and n < 0" do + a = [1, 2, 3, 4, 5] + a[eval("(-3..)")] = [7, 8, 9] + a.should == [1, 2, 7, 8, 9] + end + + it "inserts at the end if m > the array size" do + a = [1, 2, 3] + a[eval("(3..)")] = [4] + a.should == [1, 2, 3, 4] + a[eval("(5..)")] = [6] + a.should == [1, 2, 3, 4, nil, 6] + end + end +end + describe "Array#[] after a shift" do it "works for insertion" do a = [1,2] diff --git a/spec/ruby/core/array/fill_spec.rb b/spec/ruby/core/array/fill_spec.rb index 1c1beef25e..0f44d3c010 100644 --- a/spec/ruby/core/array/fill_spec.rb +++ b/spec/ruby/core/array/fill_spec.rb @@ -314,4 +314,13 @@ describe "Array#fill with (filler, range)" do def obj.<=>(rhs); rhs == self ? 0 : nil end -> { [].fill('a', obj..obj) }.should raise_error(TypeError) end + + ruby_version_is "2.6" do + it "works with endless ranges" do + [1, 2, 3, 4].fill('x', eval("(1..)")).should == [1, 'x', 'x', 'x'] + [1, 2, 3, 4].fill('x', eval("(3...)")).should == [1, 2, 3, 'x'] + [1, 2, 3, 4].fill(eval("(1..)")) { |x| x + 2 }.should == [1, 3, 4, 5] + [1, 2, 3, 4].fill(eval("(3...)")) { |x| x + 2 }.should == [1, 2, 3, 5] + end + end end diff --git a/spec/ruby/core/array/intersection_spec.rb b/spec/ruby/core/array/intersection_spec.rb index 7bf2ec4dbe..27d90f1e44 100644 --- a/spec/ruby/core/array/intersection_spec.rb +++ b/spec/ruby/core/array/intersection_spec.rb @@ -1,87 +1,21 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative 'shared/intersection' describe "Array#&" do - it "creates an array with elements common to both arrays (intersection)" do - ([] & []).should == [] - ([1, 2] & []).should == [] - ([] & [1, 2]).should == [] - ([ 1, 3, 5 ] & [ 1, 2, 3 ]).should == [1, 3] - end - - it "creates an array with no duplicates" do - ([ 1, 1, 3, 5 ] & [ 1, 2, 3 ]).uniq!.should == nil - end - - it "creates an array with elements in order they are first encountered" do - ([ 1, 2, 3, 2, 5 ] & [ 5, 2, 3, 4 ]).should == [2, 3, 5] - end - - it "does not modify the original Array" do - a = [1, 1, 3, 5] - (a & [1, 2, 3]).should == [1, 3] - a.should == [1, 1, 3, 5] - end - - it "properly handles recursive arrays" do - empty = ArraySpecs.empty_recursive_array - (empty & empty).should == empty - - (ArraySpecs.recursive_array & []).should == [] - ([] & ArraySpecs.recursive_array).should == [] - - (ArraySpecs.recursive_array & ArraySpecs.recursive_array).should == [1, 'two', 3.0, ArraySpecs.recursive_array] - end + it_behaves_like :array_intersection, :& +end - it "tries to convert the passed argument to an Array using #to_ary" do - obj = mock('[1,2,3]') - obj.should_receive(:to_ary).and_return([1, 2, 3]) - ([1, 2] & obj).should == ([1, 2]) - end +ruby_version_is "2.7" do + describe "Array#intersection" do + it_behaves_like :array_intersection, :intersection - it "determines equivalence between elements in the sense of eql?" do - not_supported_on :opal do - ([5.0, 4.0] & [5, 4]).should == [] + it "accepts multiple arguments" do + [1, 2, 3, 4].intersection([1, 2, 3], [2, 3, 4]).should == [2, 3] end - str = "x" - ([str] & [str.dup]).should == [str] - - obj1 = mock('1') - obj2 = mock('2') - obj1.stub!(:hash).and_return(0) - obj2.stub!(:hash).and_return(0) - obj1.should_receive(:eql?).at_least(1).and_return(true) - obj2.stub!(:eql?).and_return(true) - - ([obj1] & [obj2]).should == [obj1] - ([obj1, obj1, obj2, obj2] & [obj2]).should == [obj1] - - obj1 = mock('3') - obj2 = mock('4') - obj1.stub!(:hash).and_return(0) - obj2.stub!(:hash).and_return(0) - obj1.should_receive(:eql?).at_least(1).and_return(false) - - ([obj1] & [obj2]).should == [] - ([obj1, obj1, obj2, obj2] & [obj2]).should == [obj2] - end - - it "does return subclass instances for Array subclasses" do - (ArraySpecs::MyArray[1, 2, 3] & []).should be_an_instance_of(Array) - (ArraySpecs::MyArray[1, 2, 3] & ArraySpecs::MyArray[1, 2, 3]).should be_an_instance_of(Array) - ([] & ArraySpecs::MyArray[1, 2, 3]).should be_an_instance_of(Array) - end - - it "does not call to_ary on array subclasses" do - ([5, 6] & ArraySpecs::ToAryArray[1, 2, 5, 6]).should == [5, 6] - end - - it "properly handles an identical item even when its #eql? isn't reflexive" do - x = mock('x') - x.stub!(:hash).and_return(42) - x.stub!(:eql?).and_return(false) # Stubbed for clarity and latitude in implementation; not actually sent by MRI. - - ([x] & [x]).should == [x] + it "preserves elements order from original array" do + [1, 2, 3, 4].intersection([3, 2, 1]).should == [1, 2, 3] + end end end diff --git a/spec/ruby/core/array/shared/intersection.rb b/spec/ruby/core/array/shared/intersection.rb new file mode 100644 index 0000000000..49849b08c2 --- /dev/null +++ b/spec/ruby/core/array/shared/intersection.rb @@ -0,0 +1,84 @@ +describe :array_intersection, shared: true do + it "creates an array with elements common to both arrays (intersection)" do + [].send(@method, []).should == [] + [1, 2].send(@method, []).should == [] + [].send(@method, [1, 2]).should == [] + [ 1, 3, 5 ].send(@method, [ 1, 2, 3 ]).should == [1, 3] + end + + it "creates an array with no duplicates" do + [ 1, 1, 3, 5 ].send(@method, [ 1, 2, 3 ]).uniq!.should == nil + end + + it "creates an array with elements in order they are first encountered" do + [ 1, 2, 3, 2, 5 ].send(@method, [ 5, 2, 3, 4 ]).should == [2, 3, 5] + end + + it "does not modify the original Array" do + a = [1, 1, 3, 5] + a.send(@method, [1, 2, 3]).should == [1, 3] + a.should == [1, 1, 3, 5] + end + + it "properly handles recursive arrays" do + empty = ArraySpecs.empty_recursive_array + empty.send(@method, empty).should == empty + + ArraySpecs.recursive_array.send(@method, []).should == [] + [].send(@method, ArraySpecs.recursive_array).should == [] + + ArraySpecs.recursive_array.send(@method, ArraySpecs.recursive_array).should == [1, 'two', 3.0, ArraySpecs.recursive_array] + end + + it "tries to convert the passed argument to an Array using #to_ary" do + obj = mock('[1,2,3]') + obj.should_receive(:to_ary).and_return([1, 2, 3]) + [1, 2].send(@method, obj).should == ([1, 2]) + end + + it "determines equivalence between elements in the sense of eql?" do + not_supported_on :opal do + [5.0, 4.0].send(@method, [5, 4]).should == [] + end + + str = "x" + [str].send(@method, [str.dup]).should == [str] + + obj1 = mock('1') + obj2 = mock('2') + obj1.stub!(:hash).and_return(0) + obj2.stub!(:hash).and_return(0) + obj1.should_receive(:eql?).at_least(1).and_return(true) + obj2.stub!(:eql?).and_return(true) + + [obj1].send(@method, [obj2]).should == [obj1] + [obj1, obj1, obj2, obj2].send(@method, [obj2]).should == [obj1] + + obj1 = mock('3') + obj2 = mock('4') + obj1.stub!(:hash).and_return(0) + obj2.stub!(:hash).and_return(0) + obj1.should_receive(:eql?).at_least(1).and_return(false) + + [obj1].send(@method, [obj2]).should == [] + [obj1, obj1, obj2, obj2].send(@method, [obj2]).should == [obj2] + end + + it "does return subclass instances for Array subclasses" do + ArraySpecs::MyArray[1, 2, 3].send(@method, []).should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].send(@method, ArraySpecs::MyArray[1, 2, 3]).should be_an_instance_of(Array) + [].send(@method, ArraySpecs::MyArray[1, 2, 3]).should be_an_instance_of(Array) + end + + it "does not call to_ary on array subclasses" do + [5, 6].send(@method, ArraySpecs::ToAryArray[1, 2, 5, 6]).should == [5, 6] + end + + it "properly handles an identical item even when its #eql? isn't reflexive" do + x = mock('x') + x.stub!(:hash).and_return(42) + x.stub!(:eql?).and_return(false) # Stubbed for clarity and latitude in implementation; not actually sent by MRI. + + [x].send(@method, [x]).should == [x] + end +end diff --git a/spec/ruby/core/array/shared/join.rb b/spec/ruby/core/array/shared/join.rb index 5e7193de8a..c443d9a628 100644 --- a/spec/ruby/core/array/shared/join.rb +++ b/spec/ruby/core/array/shared/join.rb @@ -113,6 +113,20 @@ describe :array_join_with_default_separator, shared: true do -> { ary_utf8_bad_binary.send(@method) }.should raise_error(EncodingError) end + + ruby_version_is "2.7" do + context "when $, is not nil" do + before do + suppress_warning do + $, = '*' + end + end + + it "warns" do + -> { [].join }.should complain(/warning: \$, is set to non-nil value/) + end + end + end end describe :array_join_with_string_separator, shared: true do diff --git a/spec/ruby/core/array/slice_spec.rb b/spec/ruby/core/array/slice_spec.rb index 16220bdf0d..2e9cfd39b3 100644 --- a/spec/ruby/core/array/slice_spec.rb +++ b/spec/ruby/core/array/slice_spec.rb @@ -153,6 +153,18 @@ describe "Array#slice!" do it "raises a #{frozen_error_class} on a frozen array" do -> { ArraySpecs.frozen_array.slice!(0, 0) }.should raise_error(frozen_error_class) end + + ruby_version_is "2.6" do + it "works with endless ranges" do + a = [1, 2, 3] + a.slice!(eval("(1..)")).should == [2, 3] + a.should == [1] + + a = [1, 2, 3] + a.slice!(eval("(2...)")).should == [3] + a.should == [1, 2] + end + end end describe "Array#slice" do diff --git a/spec/ruby/core/array/values_at_spec.rb b/spec/ruby/core/array/values_at_spec.rb index 13860150bb..f6801335f8 100644 --- a/spec/ruby/core/array/values_at_spec.rb +++ b/spec/ruby/core/array/values_at_spec.rb @@ -60,4 +60,11 @@ describe "Array#values_at" do it "does not return subclass instance on Array subclasses" do ArraySpecs::MyArray[1, 2, 3].values_at(0, 1..2, 1).should be_an_instance_of(Array) end + + ruby_version_is "2.6" do + it "works when given endless ranges" do + [1, 2, 3, 4].values_at(eval("(1..)")).should == [2, 3, 4] + [1, 2, 3, 4].values_at(eval("(3...)")).should == [4] + end + end end |