Sha256: 74e69a2730bb131c929f9b4ac445890bd0c86e175ebdd8f1ded55a47afe8f003

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

require 'hamster/list'

describe Hamster::List do

  describe "#span" do

    it "is lazy" do
      lambda { Hamster.stream { fail }.span { true } }.should_not raise_error
    end

    [
      [[], [], []],
      [[1], [1], []],
      [[1, 2], [1, 2], []],
      [[1, 2, 3], [1, 2], [3]],
      [[1, 2, 3, 4], [1, 2], [3, 4]],
      [[2, 3, 4], [2], [3, 4]],
      [[3, 4], [], [3, 4]],
      [[4], [], [4]],
    ].each do |values, expected_prefix, expected_remainder|

      describe "on #{values.inspect}" do

        before do
          @original = Hamster.list(*values)
        end

        describe "with a block" do

          before do
            @result = @original.span { |item| item <= 2 }
            @prefix = @result.car
            @remainder = @result.cadr
          end

          it "preserves the original" do
            @original.should == Hamster.list(*values)
          end

          it "returns a list with two items" do
            @result.size.should == 2
          end

          it "correctly identifies the prefix" do
            @prefix.should == Hamster.list(*expected_prefix)
          end

          it "correctly identifies the remainder" do
            @remainder.should == Hamster.list(*expected_remainder)
          end

        end

        describe "without a block" do

          before do
            @result = @original.span
            @prefix = @result.car
            @remainder = @result.cadr
          end

          it "returns a list with two items" do
            @result.size.should == 2
          end

          it "returns self as the prefix" do
            @prefix.should equal(@original)
          end

          it "leaves the remainder empty" do
            @remainder.should be_empty
          end

        end

      end

    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hamster-0.2.1 spec/hamster/list/span_spec.rb
hamster-0.2.0 spec/hamster/list/span_spec.rb