diff options
author | Marcel Molina <marcel@vernix.org> | 2006-03-01 20:32:10 +0000 |
---|---|---|
committer | Marcel Molina <marcel@vernix.org> | 2006-03-01 20:32:10 +0000 |
commit | 1fdf578c17b38fc5391996d2016f4170bc3bf5ba (patch) | |
tree | 6063dbbf0043d0f187d6655c0ccbade2b1fc4ca4 /activesupport/test | |
parent | 8368b5960b5128deed17fcf9d7cda5c70da9d972 (diff) | |
download | rails-1fdf578c17b38fc5391996d2016f4170bc3bf5ba.tar.gz rails-1fdf578c17b38fc5391996d2016f4170bc3bf5ba.tar.bz2 rails-1fdf578c17b38fc5391996d2016f4170bc3bf5ba.zip |
Add Enumerable#group_by and Array#in_groups_of
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3726 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/core_ext/array_ext_test.rb | 31 | ||||
-rw-r--r-- | activesupport/test/core_ext/enumerable_test.rb | 17 |
2 files changed, 47 insertions, 1 deletions
diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 159dc2a478..9e23eed450 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -34,3 +34,34 @@ class ArrayExtConversionTests < Test::Unit::TestCase assert_equal "one", ['one'].to_sentence end end + +class ArrayExtGroupingTests < Test::Unit::TestCase + def test_group_by_with_perfect_fit + groups = [] + ('a'..'i').to_a.in_groups_of(3) do |group| + groups << group + end + + assert_equal [%w(a b c), %w(d e f), %w(g h i)], groups + end + + def test_group_by_with_padding + groups = [] + ('a'..'g').to_a.in_groups_of(3) do |group| + groups << group + end + + assert_equal [%w(a b c), %w(d e f), ['g', nil, nil]], groups + end + + def test_group_by_pads_with_specified_values + groups = [] + + ('a'..'g').to_a.in_groups_of(3, false) do |group| + groups << group + end + + assert_equal [%w(a b c), %w(d e f), ['g', false, false]], groups + end + +end diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index 1fcaa9163e..6ca41f9116 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -12,4 +12,19 @@ class EnumerableTests < Test::Unit::TestCase assert_equal true, (1..10).first_match {|x| x > 9} assert_equal :aba, {:a => 10, :aba => 50, :bac => 40}.first_match {|k, v| k if v > 45} end -end
\ No newline at end of file + + def test_group_by + names = %w(marcel sam david jeremy) + klass = Class.new + klass.send(:attr_accessor, :name) + objects = (1..50).inject([]) do |people,| + p = klass.new + p.name = names.sort_by { rand }.first + people << p + end + + objects.group_by {|object| object.name}.each do |name, group| + assert group.all? {|person| person.name == name} + end + end +end |