File: insert_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (90 lines) | stat: -rw-r--r-- 3,165 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

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 of no object is passed" do
    [].insert(0).should == []
    [].insert(-1).should == []
    [].insert(10).should == []
    [].insert(-2).should == []
  end

  it "tries to convert the passed position argument to an Integer using #to_int" do
    obj = mock('2')
    obj.should_receive(:to_int).and_return(2)
    [].insert(obj, 'x').should == [nil, nil, 'x']
  end

  it "raises an ArgumentError if no argument passed" do
    lambda { [].insert() }.should raise_error(ArgumentError)
  end

  ruby_version_is ""..."1.9" do
    it "raises a TypeError on frozen arrays when the array is modified" do
      lambda { ArraySpecs.frozen_array.insert(0, 'x') }.should raise_error(TypeError)
    end

    it "does not raise on frozen arrays when the array would not be modified" do
      ArraySpecs.frozen_array.insert(0).should == [1, 2, 3]
    end
  end

  ruby_version_is "1.9" do
    it "raises a RuntimeError on frozen arrays when the array is modified" do
      lambda { ArraySpecs.frozen_array.insert(0, 'x') }.should raise_error(RuntimeError)
    end

    # see [ruby-core:23666]
    it "raises a RuntimeError on frozen arrays when the array would not be modified" do
      lambda { ArraySpecs.frozen_array.insert(0)      }.should raise_error(RuntimeError)
    end
  end
end