Sha256: 5815ca80e9d3080643dbc7ee62a83d9bcd57bf46963b5382182d61c29818abc6

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

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

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/hamster-3.0.0/spec/lib/hamster/list/insert_spec.rb
hamster-3.0.0 spec/lib/hamster/list/insert_spec.rb
hamster-2.0.0 spec/lib/hamster/list/insert_spec.rb