File: insert_spec.rb

package info (click to toggle)
ruby-hamster 3.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,932 kB
  • sloc: ruby: 16,915; makefile: 4
file content (47 lines) | stat: -rw-r--r-- 1,313 bytes parent folder | download | duplicates (2)
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
require "spec_helper"
require "hamster/list"

describe Hamster::List do
  describe "#insert" do
    let(:original) { L[1, 2, 3] }

    it "can add items at the beginning of a list" do
      list = original.insert(0, :a, :b)
      list.size.should be(5)
      list.at(0).should be(:a)
      list.at(2).should be(1)
    end

    it "can add items in the middle of a list" do
      list = original.insert(1, :a, :b, :c)
      list.size.should be(6)
      list.to_a.should == [1, :a, :b, :c, 2, 3]
    end

    it "can add items at the end of a list" do
      list = original.insert(3, :a, :b, :c)
      list.size.should be(6)
      list.to_a.should == [1, 2, 3, :a, :b, :c]
    end

    it "can add items past the end of a list" do
      list = original.insert(6, :a, :b)
      list.size.should be(8)
      list.to_a.should == [1, 2, 3, nil, nil, nil, :a, :b]
    end

    it "accepts a negative index, which counts back from the end of the list" do
      list = original.insert(-2, :a)
      list.size.should be(4)
      list.to_a.should == [1, :a, 2, 3]
    end

    it "raises IndexError if a negative index is too great" do
      expect { original.insert(-4, :a) }.to raise_error(IndexError)
    end

    it "is lazy" do
      -> { Hamster.stream { fail }.insert(0, :a) }.should_not raise_error
    end
  end
end