diff options
author | Benoit Daloze <[email protected]> | 2020-12-27 17:35:32 +0100 |
---|---|---|
committer | Benoit Daloze <[email protected]> | 2020-12-27 17:35:32 +0100 |
commit | 727c97da1977544c91b9b3677811da3a44af7d53 (patch) | |
tree | 4f027117edad10789db57ff4b83242753a89e39d /spec/ruby/core/array | |
parent | 267bed0cd91711e2a8c79219e97431ba22137b01 (diff) |
Update to ruby/spec@4ce9f41
Diffstat (limited to 'spec/ruby/core/array')
-rw-r--r-- | spec/ruby/core/array/element_set_spec.rb | 48 | ||||
-rw-r--r-- | spec/ruby/core/array/fill_spec.rb | 7 | ||||
-rw-r--r-- | spec/ruby/core/array/first_spec.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/array/shared/slice.rb | 41 | ||||
-rw-r--r-- | spec/ruby/core/array/slice_spec.rb | 20 | ||||
-rw-r--r-- | spec/ruby/core/array/values_at_spec.rb | 7 |
6 files changed, 121 insertions, 4 deletions
diff --git a/spec/ruby/core/array/element_set_spec.rb b/spec/ruby/core/array/element_set_spec.rb index 2e01e838e9..1c143f3e37 100644 --- a/spec/ruby/core/array/element_set_spec.rb +++ b/spec/ruby/core/array/element_set_spec.rb @@ -439,7 +439,6 @@ 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 @@ -476,6 +475,53 @@ ruby_version_is "2.6" do end end +ruby_version_is "2.7" do + describe "Array#[]= with [..n] and [...n]" 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 == [nil, 4, 5] + a[eval("(...2)")] = nil + a.should == [nil, 5] + end + + it "just sets the section defined by range to nil if n < 0 and the rhs is nil" do + a = [1, 2, 3, 4, 5] + a[eval("(..-3)")] = nil + a.should == [nil, 4, 5] + a[eval("(...-1)")] = [nil, 5] + end + + it "replaces the section defined by range" do + a = [6, 5, 4, 3, 2, 1] + a[eval("(...3)")] = 9 + a.should == [9, 3, 2, 1] + a[eval("(..2)")] = [7, 7, 7, 7, 7] + a.should == [7, 7, 7, 7, 7, 1] + end + + it "replaces the section if n < 0" do + a = [1, 2, 3, 4, 5] + a[eval("(..-2)")] = [7, 8, 9] + a.should == [7, 8, 9, 5] + end + + it "replaces everything if n > the array size" do + a = [1, 2, 3] + a[eval("(...7)")] = [4] + a.should == [4] + end + + it "inserts at the beginning if n < negative the array size" do + a = [1, 2, 3] + a[eval("(..-7)")] = [4] + a.should == [4, 1, 2, 3] + a[eval("(...-10)")] = [6] + a.should == [6, 4, 1, 2, 3] + 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 6745bc8d09..8f0814a5da 100644 --- a/spec/ruby/core/array/fill_spec.rb +++ b/spec/ruby/core/array/fill_spec.rb @@ -324,4 +324,11 @@ describe "Array#fill with (filler, range)" do [1, 2, 3, 4].fill(eval("(3...)")) { |x| x + 2 }.should == [1, 2, 3, 5] end end + + ruby_version_is "2.7" do + it "works with beginless ranges" do + [1, 2, 3, 4].fill('x', eval("(..2)")).should == ["x", "x", "x", 4] + [1, 2, 3, 4].fill(eval("(...2)")) { |x| x + 2 }.should == [2, 3, 3, 4] + end + end end diff --git a/spec/ruby/core/array/first_spec.rb b/spec/ruby/core/array/first_spec.rb index 463da829a2..66eeba6565 100644 --- a/spec/ruby/core/array/first_spec.rb +++ b/spec/ruby/core/array/first_spec.rb @@ -33,7 +33,7 @@ describe "Array#first" do -> { [1, 2].first(-1) }.should raise_error(ArgumentError) end - it "raises a RangeError when count is an Integer" do + it "raises a RangeError when count is a Bignum" do -> { [].first(bignum_value) }.should raise_error(RangeError) end diff --git a/spec/ruby/core/array/shared/slice.rb b/spec/ruby/core/array/shared/slice.rb index 820047cdb2..6818badeda 100644 --- a/spec/ruby/core/array/shared/slice.rb +++ b/spec/ruby/core/array/shared/slice.rb @@ -486,7 +486,7 @@ describe :array_slice, shared: true do end end - it "raises a RangeError when the start index is out of range of Integer" do + it "raises a RangeError when the start index is out of range of Fixnum" do array = [1, 2, 3, 4, 5, 6] obj = mock('large value') obj.should_receive(:to_int).and_return(bignum_value) @@ -502,7 +502,7 @@ describe :array_slice, shared: true do array.send(@method, max_long.to_f.prev_float).should == nil end - it "raises a RangeError when the length is out of range of Integer" do + it "raises a RangeError when the length is out of range of Fixnum" do array = [1, 2, 3, 4, 5, 6] obj = mock('large value') obj.should_receive(:to_int).and_return(bignum_value) @@ -520,4 +520,41 @@ describe :array_slice, shared: true do -> { "hello".send(@method, bignum_value..(bignum_value + 1)) }.should raise_error(RangeError) -> { "hello".send(@method, 0..bignum_value) }.should raise_error(RangeError) end + + ruby_version_is "2.6" do + it "can accept endless ranges" do + a = [0, 1, 2, 3, 4, 5] + a.send(@method, eval("(2..)")).should == [2, 3, 4, 5] + a.send(@method, eval("(2...)")).should == [2, 3, 4, 5] + a.send(@method, eval("(-2..)")).should == [4, 5] + a.send(@method, eval("(-2...)")).should == [4, 5] + a.send(@method, eval("(9..)")).should == nil + a.send(@method, eval("(9...)")).should == nil + a.send(@method, eval("(-9..)")).should == nil + a.send(@method, eval("(-9...)")).should == nil + end + end + + ruby_version_is "2.7" do + it "can accept beginless ranges" do + a = [0, 1, 2, 3, 4, 5] + a.send(@method, eval("(..3)")).should == [0, 1, 2, 3] + a.send(@method, eval("(...3)")).should == [0, 1, 2] + a.send(@method, eval("(..-3)")).should == [0, 1, 2, 3] + a.send(@method, eval("(...-3)")).should == [0, 1, 2] + a.send(@method, eval("(..0)")).should == [0] + a.send(@method, eval("(...0)")).should == [] + a.send(@method, eval("(..9)")).should == [0, 1, 2, 3, 4, 5] + a.send(@method, eval("(...9)")).should == [0, 1, 2, 3, 4, 5] + a.send(@method, eval("(..-9)")).should == [] + a.send(@method, eval("(...-9)")).should == [] + end + + it "can accept nil...nil ranges" do + a = [0, 1, 2, 3, 4, 5] + a.send(@method, eval("(nil...nil)")).should == a + a.send(@method, eval("(...nil)")).should == a + a.send(@method, eval("(nil..)")).should == a + end + end end diff --git a/spec/ruby/core/array/slice_spec.rb b/spec/ruby/core/array/slice_spec.rb index 3829794938..f416d8d0ce 100644 --- a/spec/ruby/core/array/slice_spec.rb +++ b/spec/ruby/core/array/slice_spec.rb @@ -163,6 +163,26 @@ describe "Array#slice!" do a = [1, 2, 3] a.slice!(eval("(2...)")).should == [3] a.should == [1, 2] + + a = [1, 2, 3] + a.slice!(eval("(-2..)")).should == [2, 3] + a.should == [1] + + a = [1, 2, 3] + a.slice!(eval("(-1...)")).should == [3] + a.should == [1, 2] + end + end + + ruby_version_is "2.7" do + it "works with beginless ranges" do + a = [0,1,2,3,4] + a.slice!(eval("(..3)")).should == [0, 1, 2, 3] + a.should == [4] + + a = [0,1,2,3,4] + a.slice!(eval("(...-2)")).should == [0, 1, 2] + a.should == [3, 4] end end end diff --git a/spec/ruby/core/array/values_at_spec.rb b/spec/ruby/core/array/values_at_spec.rb index f6801335f8..019152c51c 100644 --- a/spec/ruby/core/array/values_at_spec.rb +++ b/spec/ruby/core/array/values_at_spec.rb @@ -67,4 +67,11 @@ describe "Array#values_at" do [1, 2, 3, 4].values_at(eval("(3...)")).should == [4] end end + + ruby_version_is "2.7" do + it "works when given beginless ranges" do + [1, 2, 3, 4].values_at(eval("(..2)")).should == [1, 2, 3] + [1, 2, 3, 4].values_at(eval("(...2)")).should == [1, 2] + end + end end |