Sha256: f305a1d600b905e9dc204a6119ed244fb4011623159106ef434e8f7889a07764

Contents?: true

Size: 1.72 KB

Versions: 7

Compression:

Stored size: 1.72 KB

Contents

require 'spec_helper'
require 'gorillib/enumerable/sum'

Payment = Struct.new(:price)
class SummablePayment < Payment
  def +(val) self.class.new(price + val.price) end
end

describe Enumerable, :simple_spec => true do
  describe '#sum' do
    it 'sums lists of numbers to a number' do
      [5, 15, 10].sum        .should == 30
      [5, 15, 10].sum{|i| i }.should == 30
    end

    it 'sums list of strings to a string' do

      %w(a b c).sum        .should == 'abc'
      %w(a b c).sum{|i| i }.should == 'abc'
    end

    it 'sums list of objects with a &:method' do
      payments = [ Payment.new(5), Payment.new(15), Payment.new(10) ]
      payments.sum(&:price)           .should == 30
      payments.sum { |p| p.price * 2 }.should == 60
    end

    it 'sums object with a synthetic "+" method' do
      payments = [ SummablePayment.new(5), SummablePayment.new(15) ]
      payments.sum            .should == SummablePayment.new(20)
      payments.sum{|val| val }.should == SummablePayment.new(20)
    end

    it 'handles nil sums' do
      lambda{ [5, 15, nil].sum }.should raise_error(TypeError)

      payments = [ Payment.new(5), Payment.new(15), Payment.new(10), Payment.new(nil) ]
      lambda{ payments.sum(&:price) }.should raise_error(TypeError)
      payments.sum{|p| p.price.to_i * 2 }.should == 60
    end

    it 'handles empty sums' do
      [].sum        .should == 0
      [].sum{|i| i }.should == 0
      [].sum(Payment.new(0)).should == Payment.new(0)
    end

    it 'behaves the same on ranges' do
      (1..4).sum{|i| i * 2 }.should == 20
      (1..4).sum            .should == 10
      (1..4.5).sum          .should == 10
      (1...4).sum           .should ==  6
      ('a'..'c').sum        .should == 'abc'
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gorillib-0.6.0 spec/gorillib/enumerable/sum_spec.rb
gorillib-0.5.2 spec/gorillib/enumerable/sum_spec.rb
gorillib-0.5.0 spec/gorillib/enumerable/sum_spec.rb
gorillib-0.4.2 spec/gorillib/enumerable/sum_spec.rb
gorillib-0.4.2pre spec/gorillib/enumerable/sum_spec.rb
gorillib-0.4.0pre spec/gorillib/enumerable/sum_spec.rb
gorillib-0.4.1pre spec/gorillib/enumerable/sum_spec.rb