File: eql_spec.rb

package info (click to toggle)
ruby-immutable-ruby 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,852 kB
  • sloc: ruby: 16,556; makefile: 4
file content (75 lines) | stat: -rw-r--r-- 2,626 bytes parent folder | download
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
require 'spec_helper'
require 'bigdecimal'

describe Immutable::Hash do
  let(:hash) { H['A' => 'aye', 'B' => 'bee', 'C' => 'see'] }

  describe '#eql?' do
    it 'returns false when comparing with a standard hash' do
      hash.eql?('A' => 'aye', 'B' => 'bee', 'C' => 'see').should == false
    end

    it 'returns false when comparing with an arbitrary object' do
      hash.eql?(Object.new).should == false
    end

    it 'returns false when comparing with a subclass of Immutable::Hash' do
      subclass = Class.new(Immutable::Hash)
      instance = subclass.new('A' => 'aye', 'B' => 'bee', 'C' => 'see')
      hash.eql?(instance).should == false
    end
  end

  describe '#==' do
    it 'returns true when comparing with a standard hash' do
      (hash == {'A' => 'aye', 'B' => 'bee', 'C' => 'see'}).should == true
    end

    it 'returns false when comparing with an arbitrary object' do
      (hash == Object.new).should == false
    end

    it 'returns true when comparing with a subclass of Immutable::Hash' do
      subclass = Class.new(Immutable::Hash)
      instance = subclass.new('A' => 'aye', 'B' => 'bee', 'C' => 'see')
      (hash == instance).should == true
    end

    it 'performs numeric conversions between floats and BigDecimals' do
      expect(H[a: 0.0] == H[a: BigDecimal('0.0')]).to be true
      expect(H[a: BigDecimal('0.0')] == H[a: 0.0]).to be true
    end
  end

  [:eql?, :==].each do |method|
    describe "##{method}" do
      [
        [{}, {}, true],
        [{ 'A' => 'aye' }, {}, false],
        [{}, { 'A' => 'aye' }, false],
        [{ 'A' => 'aye' }, { 'A' => 'aye' }, true],
        [{ 'A' => 'aye' }, { 'B' => 'bee' }, false],
        [{ 'A' => 'aye', 'B' => 'bee' }, { 'A' => 'aye' }, false],
        [{ 'A' => 'aye' }, { 'A' => 'aye', 'B' => 'bee' }, false],
        [{ 'A' => 'aye', 'B' => 'bee', 'C' => 'see' }, { 'A' => 'aye', 'B' => 'bee', 'C' => 'see' }, true],
        [{ 'C' => 'see', 'A' => 'aye', 'B' => 'bee' }, { 'A' => 'aye', 'B' => 'bee', 'C' => 'see' }, true],
      ].each do |a, b, expected|
        describe "returns #{expected.inspect}" do
          it "for #{a.inspect} and #{b.inspect}" do
            H[a].send(method, H[b]).should == expected
          end

          it "for #{b.inspect} and #{a.inspect}" do
            H[b].send(method, H[a]).should == expected
          end
        end
      end
    end
  end

  it 'returns true on a large hash which is modified and then modified back again' do
    hash = H.new((1..1000).zip(2..1001))
    hash.put('a', 1).delete('a').should == hash
    hash.put('b', 2).delete('b').should eql(hash)
  end
end