Sha256: afadf37e6895d391d9c132b5cef1578c93c0d42c35d8b78792cb7e7c73417f8e

Contents?: true

Size: 1.35 KB

Versions: 23

Compression:

Stored size: 1.35 KB

Contents

describe "Array#last" do
  it "returns the last element" do
    [1, 1, 1, 1, 2].last.should == 2
  end

  it "returns nil if self is empty" do
    [].last.should == nil
  end

  it "returns the last count elements if given a count" do
    [1, 2, 3, 4, 5, 9].last(3).should == [4, 5, 9]
  end

  it "returns an empty array when passed a count on an empty array" do
    [].last(0).should == []
    [].last(1).should == []
  end

  it "returns an empty array when count == 0" do
    [1, 2, 3, 4, 5].last(0).should == []
  end

  it "returns an array containing the last element when passed count == 1" do
    [1, 2, 3, 4, 5].last(1).should == [5]
  end

  it "raises an ArgumentError when count is negative" do
    lambda { [1, 2].last(-1) }.should raise_error(ArgumentError)
  end

  it "returns the entire array when count > length" do
    [1, 2, 3, 4, 5, 9].last(10).should == [1, 2, 3, 4, 5, 9]
  end

  it "returns an array which is independent to the original when passed count" do
    ary = [1, 2, 3, 4, 5]
    ary.last(0).replace([1,2])
    ary.should == [1, 2, 3, 4, 5]
    ary.last(1).replace([1,2])
    ary.should == [1, 2, 3, 4, 5]
    ary.last(6).replace([1,2])
    ary.should == [1, 2, 3, 4, 5]
  end

  it "is not destructive" do
    a = [1, 2, 3]
    a.last
    a.should == [1, 2, 3]
    a.last(2)
    a.should == [1, 2, 3]
    a.last(3)
    a.should == [1, 2, 3]
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
opal-0.3.41 spec/core/array/last_spec.rb
opal-0.3.40 spec/core/array/last_spec.rb
opal-0.3.39 spec/core/array/last_spec.rb
opal-0.3.38 spec/core/array/last_spec.rb
opal-0.3.37 spec/core/array/last_spec.rb
opal-0.3.36 spec/core/array/last_spec.rb
opal-0.3.35 spec/core/array/last_spec.rb
opal-0.3.34 spec/core/array/last_spec.rb
opal-0.3.33 spec/core/array/last_spec.rb
opal-0.3.32 spec/core/array/last_spec.rb
opal-0.3.31 spec/core/array/last_spec.rb
opal-0.3.30 spec/core/array/last_spec.rb
opal-0.3.29 spec/core/array/last_spec.rb
opal-0.3.28 spec/core/array/last_spec.rb
opal-0.3.27 spec/core/array/last_spec.rb
opal-0.3.26 spec/core/array/last_spec.rb
opal-0.3.25 spec/core/array/last_spec.rb
opal-0.3.22 spec/core/array/last_spec.rb
opal-0.3.21 test/core/array/last_spec.rb
opal-0.3.20 test/core/array/last_spec.rb