summaryrefslogtreecommitdiff
path: root/spec/ruby/core/data/eql_spec.rb
blob: 6958d5de4a9826491e398f978c75ddbb40d7774e (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
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Data#eql?" do
  it "returns true if the other is the same object" do
    a = DataSpecs::Measure.new(42, "km")
    a.should.eql?(a)
  end

  it "returns true if the other has all the same fields" do
    a = DataSpecs::Measure.new(42, "km")
    b = DataSpecs::Measure.new(42, "km")
    a.should.eql?(b)
  end

  it "returns false if the other is a different object or has different fields" do
    a = DataSpecs::Measure.new(42, "km")
    b = DataSpecs::Measure.new(42, "mi")
    a.should_not.eql?(b)
  end

  it "returns false if other is of a different class" do
    a = DataSpecs::Measure.new(42, "km")
    klass = Data.define(*DataSpecs::Measure.members)
    b = klass.new(42, "km")
    a.should_not.eql?(b)
  end

  it "returns false if any corresponding elements are not equal with #eql?" do
    a = DataSpecs::Measure.new(42, "km")
    b = DataSpecs::Measure.new(42.0, "mi")
    a.should_not.eql?(b)
  end

  context "recursive structure" do
    it "returns true the other is the same object" do
      a = DataSpecs::Measure.allocate
      a.send(:initialize, amount: 42, unit: a)

      a.should.eql?(a)
    end

    it "returns true if the other has all the same fields" do
      a = DataSpecs::Measure.allocate
      a.send(:initialize, amount: 42, unit: a)

      b = DataSpecs::Measure.allocate
      b.send(:initialize, amount: 42, unit: b)

      a.should.eql?(b)
    end

    it "returns false if any corresponding elements are not equal with #eql?" do
      a = DataSpecs::Measure.allocate
      a.send(:initialize, amount: a, unit: "km")

      b = DataSpecs::Measure.allocate
      b.send(:initialize, amount: b, unit: "mi")

      a.should_not.eql?(b)
    end
  end
end