summaryrefslogtreecommitdiff
path: root/spec/ruby/core/enumerable/tally_spec.rb
blob: 95c64c12949505285cccdfc41d93dd2ee3f5e6d8 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Enumerable#tally" do
  before :each do
    ScratchPad.record []
  end

  it "returns a hash with counts according to the value" do
    enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz')
    enum.tally.should == { 'foo' => 2, 'bar' => 1, 'baz' => 1}
  end

  it "returns a hash without default" do
    hash = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz').tally
    hash.default_proc.should be_nil
    hash.default.should be_nil
  end

  it "returns an empty hash for empty enumerables" do
    EnumerableSpecs::Empty.new.tally.should == {}
  end

  it "counts values as gathered array when yielded with multiple arguments" do
    EnumerableSpecs::YieldsMixed2.new.tally.should == EnumerableSpecs::YieldsMixed2.gathered_yields.group_by(&:itself).transform_values(&:size)
  end

  it "does not call given block" do
    enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz')
    enum.tally { |v| ScratchPad << v }
    ScratchPad.recorded.should == []
  end
end

describe "Enumerable#tally with a hash" do
  before :each do
    ScratchPad.record []
  end

  it "returns a hash with counts according to the value" do
    enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz')
    enum.tally({ 'foo' => 1 }).should == { 'foo' => 3, 'bar' => 1, 'baz' => 1}
  end

  it "returns the given hash" do
    enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz')
    hash = { 'foo' => 1 }
    enum.tally(hash).should equal(hash)
  end

  it "calls #to_hash to convert argument to Hash implicitly if passed not a Hash" do
    enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz')
    object = Object.new
    def object.to_hash; { 'foo' => 1 }; end
    enum.tally(object).should == { 'foo' => 3, 'bar' => 1, 'baz' => 1}
  end

  it "raises a FrozenError and does not update the given hash when the hash is frozen" do
    enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz')
    hash = { 'foo' => 1 }.freeze
    -> { enum.tally(hash) }.should raise_error(FrozenError)
    hash.should == { 'foo' => 1 }
  end

  it "raises a FrozenError even if enumerable is empty" do
    enum = EnumerableSpecs::Numerous.new()
    hash = { 'foo' => 1 }.freeze
    -> { enum.tally(hash) }.should raise_error(FrozenError)
  end

  it "does not call given block" do
    enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz')
    enum.tally({ 'foo' => 1 }) { |v| ScratchPad << v }
    ScratchPad.recorded.should == []
  end

  it "ignores the default value" do
    enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz')
    enum.tally(Hash.new(100)).should == { 'foo' => 2, 'bar' => 1, 'baz' => 1}
  end

  it "ignores the default proc" do
    enum = EnumerableSpecs::Numerous.new('foo', 'bar', 'foo', 'baz')
    enum.tally(Hash.new {100}).should == { 'foo' => 2, 'bar' => 1, 'baz' => 1}
  end

  it "needs the values counting each elements to be an integer" do
    enum = EnumerableSpecs::Numerous.new('foo')
    -> { enum.tally({ 'foo' => 'bar' }) }.should raise_error(TypeError)
  end
end