Sha256: 2e197102502ecabc5db558a9093d05cdacfb35e2dfcedb9e78c90543e5e6879a

Contents?: true

Size: 1.89 KB

Versions: 23

Compression:

Stored size: 1.89 KB

Contents

describe "Array#insert" do
  it "returns self" do
    ary = []
    ary.insert(0).should equal(ary)
    ary.insert(0, :a).should equal(ary)
  end

  it "inserts objects before the element at index for non-negative index" do
    ary = []
    ary.insert(0, 3).should == [3]
    ary.insert(0, 1, 2).should == [1, 2, 3]
    ary.insert(0).should == [1, 2, 3]

    # Let's just assume insert() always modifies the array from now on.
    ary.insert(1, 'a').should == [1, 'a', 2, 3]
    ary.insert(0, 'b').should == ['b', 1, 'a', 2, 3]
    ary.insert(5, 'c').should == ['b', 1, 'a', 2, 3, 'c']
    ary.insert(7, 'd').should == ['b', 1, 'a', 2, 3, 'c', nil, 'd']
    ary.insert(10, 5, 4).should == ['b', 1, 'a', 2, 3, 'c', nil, 'd', nil, nil, 5, 4]
  end

  it "appends objects to the end of the array for index == -1" do
    [1, 3, 3].insert(-1, 2, 'x', 0.5).should == [1, 3, 3, 2, 'x', 0.5]
  end

  it "inserts objects after the element at index with negative index" do
    ary = []
    ary.insert(-1, 3).should == [3]
    ary.insert(-2, 2).should == [2, 3]
    ary.insert(-3, 1).should == [1, 2, 3]
    ary.insert(-2, -3).should == [1, 2, -3, 3]
    ary.insert(-1, []).should == [1, 2, -3, 3, []]
    ary.insert(-2, 'x', 'y').should == [1, 2, -3, 3, 'x', 'y', []]
    ary = [1, 2, 3]
  end

  it "pads with nils if the index to be inserted to is past the end" do
    [].insert(5, 5).should == [nil, nil, nil, nil, nil, 5]
  end

  it "can insert before the first element with a negative index" do
    [1, 2, 3].insert(-4, -3).should == [-3, 1, 2, 3]
  end

  it "raises an IndexError if the negative index is out of bounds" do
    lambda { [].insert(-2, 1) }.should raise_error(IndexError)
    lambda { [1].insert(-3, 2) }.should raise_error(IndexError)
  end

  it "does nothing if no object is passed" do
    [].insert(0).should == []
    [].insert(-1).should == []
    [].insert(10).should == []
    [].insert(-2).should == []
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

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