Sha256: 7af9a3209c45a435595bc504921d178e2169a49aa28d4e69c1804d974d68a936

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

require "spec_helper"

require "hamster/list"

describe Hamster::List do
  [:reduce, :inject, :fold, :foldr].each do |method|
    describe "##{method}" do
      describe "on a really big list" do
        before do
          @list = Hamster.interval(0, STACK_OVERFLOW_DEPTH)
        end

        it "doesn't run out of stack" do
          -> { @list.reduce(&:+) }.should_not raise_error
        end
      end

      [
        [[], 10, 10],
        [[1], 10, 9],
        [[1, 2, 3], 10, 4],
      ].each do |values, initial, expected|

        describe "on #{values.inspect}" do
          before do
            @list = Hamster.list(*values)
          end

          describe "with an initial value of #{initial}" do
            describe "and a block" do
              it "returns #{expected.inspect}" do
                @list.send(method, initial) { |memo, item| memo - item }.should == expected
              end
            end

            describe "and no block" do
              it "returns the memo" do
                @list.send(method, initial).should == initial
              end
            end
          end
        end
      end

      [
        [[], nil],
        [[1], 1],
        [[1, 2, 3], -4],
      ].each do |values, expected|

        describe "on #{values.inspect}" do
          before do
            @list = Hamster.list(*values)
          end

          describe "with no initial value" do
            describe "and a block" do
              it "returns #{expected.inspect}" do
                @list.send(method) { |memo, item| memo - item }.should == expected
              end
            end

            describe "and no block" do
              it "returns nil" do
                @list.send(method).should be_nil
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hamster-1.0.1.pre.rc.1 spec/hamster/list/reduce_spec.rb