PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/gems/facets-2.4.5/test/core/enumerable/test_group_by.rb

https://bitbucket.org/mediashelf/fedora-migrator
Ruby | 31 lines | 24 code | 7 blank | 0 comment | 0 complexity | 800855aa9b4873987d4276b51d762080 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, IPL-1.0, AGPL-1.0, LGPL-3.0
  1. require 'facets/enumerable/group_by'
  2. require 'test/unit'
  3. class TC_Enumerable < Test::Unit::TestCase
  4. def test_group_by_for_array
  5. a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  6. r = {0=>[0, 2, 4, 6, 8], 1=>[1, 3, 5, 7, 9]}
  7. assert_equal(r, a.group_by{ |e| e % 2 }) #.each{|k, v| v.sort!})
  8. end
  9. def test_group_by_for_hash
  10. h = {0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5, 6=>6, 7=>7, 8=>8, 9=>9}
  11. r = {0=>[[0, 0], [2, 2], [4, 4], [6, 6], [8, 8]], 1=>[[1, 1], [3, 3], [5, 5], [7, 7], [9, 9]]}
  12. assert_equal(r, h.group_by{|k, v| v%2}.each{|k, v| v.sort!})
  13. end
  14. def test_group_by_for_range
  15. x = (1..5).group_by{ |n| n % 3 }
  16. o = { 0 => [3], 1 => [1, 4], 2 => [2,5] }
  17. assert_equal( o, x )
  18. end
  19. def test_group_by_for_array_of_string
  20. x = ["I had", 1, "dollar and", 50, "cents"].group_by{ |e| e.class }
  21. o = { String => ["I had","dollar and","cents"], Fixnum => [1,50] }
  22. assert_equal( o, x )
  23. end
  24. end