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 (76 lines) | stat: -rw-r--r-- 2,190 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
76
require 'spec_helper'

describe Immutable::Vector do
  describe '#eql' do
    let(:vector) { V['A', 'B', 'C'] }

    it 'returns false when comparing with an array with the same contents' do
      vector.eql?(%w[A B C]).should == false
    end

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

    it 'returns false when comparing an empty vector with an empty array' do
      V.empty.eql?([]).should == false
    end

    it 'returns false when comparing with a subclass of Immutable::Vector' do
      vector.eql?(Class.new(Immutable::Vector).new(%w[A B C])).should == false
    end
  end

  describe '#==' do
    let(:vector) { V['A', 'B', 'C'] }

    it 'returns true when comparing with an array with the same contents' do
      (vector == %w[A B C]).should == true
    end

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

    it 'returns true when comparing an empty vector with an empty array' do
      (V.empty == []).should == true
    end

    it 'returns true when comparing with a subclass of Immutable::Vector' do
      (vector == Class.new(Immutable::Vector).new(%w[A B C])).should == true
    end

    it 'works on larger vectors' do
      array = 2000.times.map { rand(10000) }
      (V.new(array.dup) == array).should == true
    end
  end

  [:eql?, :==].each do |method|
    describe "##{method}" do
      [
        [[], [], true],
        [[], [nil], false],
        [['A'], [], false],
        [['A'], ['A'], true],
        [['A'], ['B'], false],
        [%w[A B], ['A'], false],
        [%w[A B C], %w[A B C], true],
        [%w[C A B], %w[A B C], false],
      ].each do |a, b, expected|
        describe "returns #{expected.inspect}" do
          let(:vector_a) { V[*a] }
          let(:vector_b) { V[*b] }

          it "for vectors #{a.inspect} and #{b.inspect}" do
            vector_a.send(method, vector_b).should == expected
          end

          it "for vectors #{b.inspect} and #{a.inspect}" do
            vector_b.send(method, vector_a).should == expected
          end
        end
      end
    end
  end
end