aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/enumerable_test.rb
blob: 6ca41f9116e485b9abfead915cb60bb10fa8fecd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require 'test/unit'
require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/enumerable'

class EnumerableTests < Test::Unit::TestCase
  
  def test_first_match_no_match
    [[1, 2, 3, 4, 5], (1..9)].each {|a| a.first_match {|x| x > 10}}
  end
  
  def test_first_match_with_match
    assert_equal true, [1, 2, 3, 4, 5, 6].first_match {|x| x > 4}
    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

  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