File: equality_operator_spec.rb

package info (click to toggle)
ruby-dry-equalizer 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 172 kB
  • sloc: ruby: 400; makefile: 4
file content (86 lines) | stat: -rw-r--r-- 1,817 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
77
78
79
80
81
82
83
84
85
86
require 'spec_helper'

RSpec.describe Dry::Equalizer::Methods, '#==' do
  subject { object == other }

  let(:object)          { described_class.new(true) }
  let(:described_class) { Class.new(super_class)    }

  let(:super_class) do
    Class.new do
      include Dry::Equalizer::Methods

      attr_reader :boolean

      def initialize(boolean)
        @boolean = boolean
      end

      def cmp?(comparator, other)
        boolean.send(comparator, other.boolean)
      end
    end
  end

  context 'with the same object' do
    let(:other) { object }

    it { should be(true) }

    it 'is symmetric' do
      should eql(other == object)
    end
  end

  context 'with an equivalent object' do
    let(:other) { object.dup }

    it { should be(true) }

    it 'is symmetric' do
      should eql(other == object)
    end
  end

  context 'with a subclass instance having equivalent obervable state' do
    let(:other) { Class.new(described_class).new(true) }

    it { should be(true) }

    it 'is not symmetric' do
      # the subclass instance should maintain substitutability with the object
      # (in the LSP sense) the reverse is not true.
      should_not eql(other == object)
    end
  end

  context 'with a superclass instance having equivalent observable state' do
    let(:other) { super_class.new(true) }

    it { should be(false) }

    it 'is not symmetric' do
      should_not eql(other == object)
    end
  end

  context 'with an object of another class' do
    let(:other) { Class.new.new }

    it { should be(false) }

    it 'is symmetric' do
      should eql(other == object)
    end
  end

  context 'with a different object' do
    let(:other) { described_class.new(false) }

    it { should be(false) }

    it 'is symmetric' do
      should eql(other == object)
    end
  end
end