Sha256: 89a5338116ace0fefe0129fd291f18262705e80649e483ce2cfe5fdbe06de6b8

Contents?: true

Size: 1.58 KB

Versions: 12

Compression:

Stored size: 1.58 KB

Contents

require File.expand_path('../../../spec_helper', __FILE__)

require 'hamster/tuple'
require 'hamster/list'

describe Hamster::List do

  describe "#partition" do

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

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

      describe "on #{values.inspect}" do

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

        describe "with a block" do

          before do
            @result = @original.partition(&:odd?)
            @matches = @result.first
            @remainder = @result.last
          end

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

          it "returns a tuple with two items" do
            @result.is_a?(Hamster::Tuple).should == true
          end

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

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

        end

        describe "without a block" do

          before do
            @result = @original.partition
          end

          it "returns self" do
            @result.should equal(@original)
          end

        end

      end

    end

  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
hamster-0.3.6 spec/hamster/list/partition_spec.rb
hamster-0.3.5 spec/hamster/list/partition_spec.rb
hamster-0.3.4 spec/hamster/list/partition_spec.rb
hamster-0.3.3 spec/hamster/list/partition_spec.rb
hamster-0.3.2 spec/hamster/list/partition_spec.rb
hamster-0.3.1 spec/hamster/list/partition_spec.rb
hamster-0.3.0 spec/hamster/list/partition_spec.rb
hamster-0.2.13 spec/hamster/list/partition_spec.rb
hamster-0.2.12 spec/hamster/list/partition_spec.rb
hamster-0.2.11 spec/hamster/list/partition_spec.rb
hamster-0.2.9 spec/hamster/list/partition_spec.rb
hamster-0.2.8 spec/hamster/list/partition_spec.rb