summaryrefslogtreecommitdiff
path: root/spec/ruby/core/data/equal_value_spec.rb
blob: d9a0dcff3ecb8d76f31ccde2a88d5daaaa257e3c (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#==" do
  it "returns true if the other is the same object" do
    a = DataSpecs::Measure.new(42, "km")
    a.should == 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 == 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 == 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 == b
  end

  it "returns false if any corresponding elements are not equal with #==" do
    a = DataSpecs::Measure.new(42, "km")
    b = DataSpecs::Measure.new(42.0, "mi")
    a.should_not == 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 == 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 == b
    end

    it "returns false if any corresponding elements are not equal with #==" 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 == b
    end
  end
end