diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-06-25 19:11:09 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-06-25 19:11:09 +0000 |
commit | 236c7325df4ca2783c92dffc0f0b9592f822d95a (patch) | |
tree | 09c0e8e9e49cfd3b9a0cb3bc4bd68bbbea90214e /activesupport/test/core_ext | |
parent | 42775686d2b32b66c971c0560ba2497c7f8703b2 (diff) | |
download | rails-236c7325df4ca2783c92dffc0f0b9592f822d95a.tar.gz rails-236c7325df4ca2783c92dffc0f0b9592f822d95a.tar.bz2 rails-236c7325df4ca2783c92dffc0f0b9592f822d95a.zip |
Enumerable#sum without blocks. Closes #5505. Don't assume 0 identity for sum.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4495 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/test/core_ext')
-rw-r--r-- | activesupport/test/core_ext/enumerable_test.rb | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index dada058d56..3d116e8d93 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -3,6 +3,9 @@ require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/symbol' require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/enumerable' Payment = Struct.new(:price) +class SummablePayment < Payment + def +(p) self.class.new(price + p.price) end +end class EnumerableTests < Test::Unit::TestCase def test_group_by @@ -19,13 +22,31 @@ class EnumerableTests < Test::Unit::TestCase assert group.all? {|person| person.name == name} end end - + def test_sums + assert_equal 30, [5, 15, 10].sum + assert_equal 30, [5, 15, 10].sum { |i| i } + + assert_equal 'abc', %w(a b c).sum + assert_equal 'abc', %w(a b c).sum { |i| i } + payments = [ Payment.new(5), Payment.new(15), Payment.new(10) ] assert_equal 30, payments.sum(&:price) assert_equal 60, payments.sum { |p| p.price * 2 } + + payments = [ SummablePayment.new(5), SummablePayment.new(15) ] + assert_equal SummablePayment.new(20), payments.sum + assert_equal SummablePayment.new(20), payments.sum { |p| p } end - + + def test_nil_sums + assert_raise(TypeError) { [5, 15, nil].sum } + + payments = [ Payment.new(5), Payment.new(15), Payment.new(10), Payment.new(nil) ] + assert_raise(TypeError) { payments.sum(&:price) } + assert_equal 60, payments.sum { |p| p.price.to_i * 2 } + end + def test_index_by payments = [ Payment.new(5), Payment.new(15), Payment.new(10) ] assert_equal( @@ -33,5 +54,4 @@ class EnumerableTests < Test::Unit::TestCase payments.index_by(&:price) ) end - end |