diff options
Diffstat (limited to 'spec/ruby/core/array/slice_spec.rb')
| -rw-r--r-- | spec/ruby/core/array/slice_spec.rb | 74 |
1 files changed, 66 insertions, 8 deletions
diff --git a/spec/ruby/core/array/slice_spec.rb b/spec/ruby/core/array/slice_spec.rb index f6cbd1bcc4..731c129251 100644 --- a/spec/ruby/core/array/slice_spec.rb +++ b/spec/ruby/core/array/slice_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/classes', __FILE__) -require File.expand_path('../shared/slice', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/classes' +require_relative 'shared/slice' describe "Array#slice!" do it "removes and return the element at index" do @@ -116,8 +116,8 @@ describe "Array#slice!" do a.slice!(from .. to).should == [2, 3, 4] a.should == [1, 5] - lambda { a.slice!("a" .. "b") }.should raise_error(TypeError) - lambda { a.slice!(from .. "b") }.should raise_error(TypeError) + -> { a.slice!("a" .. "b") }.should raise_error(TypeError) + -> { a.slice!(from .. "b") }.should raise_error(TypeError) end it "returns last element for consecutive calls at zero index" do @@ -150,11 +150,69 @@ describe "Array#slice!" do a.should == [1, 2] end - it "raises a RuntimeError on a frozen array" do - lambda { ArraySpecs.frozen_array.slice!(0, 0) }.should raise_error(RuntimeError) + it "raises a FrozenError on a frozen array" do + -> { ArraySpecs.frozen_array.slice!(0, 0) }.should raise_error(FrozenError) + end + + 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] + + 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 + + it "works with beginless ranges" do + a = [0,1,2,3,4] + a.slice!((..3)).should == [0, 1, 2, 3] + a.should == [4] + + a = [0,1,2,3,4] + a.slice!((...-2)).should == [0, 1, 2] + a.should == [3, 4] + end + + describe "with a subclass of Array" do + before :each do + @array = ArraySpecs::MyArray[1, 2, 3, 4, 5] + end + + 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 describe "Array#slice" do - it_behaves_like(:array_slice, :slice) + it_behaves_like :array_slice, :slice end |
