File: equal_spec.rb

package info (click to toggle)
ruby-equatable 0.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 160 kB
  • sloc: ruby: 394; makefile: 4
file content (84 lines) | stat: -rw-r--r-- 1,681 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
# frozen_string_literal: true

require "spec/spec_helper"

RSpec.describe Equatable, '#==' do
  let(:name) { 'Value' }
  let(:value) { 11 }

  let(:super_klass) {
    ::Class.new do
      include Equatable

      attr_reader :value

      def initialize(value)
        @value = value
      end
    end
  }

  let(:klass) { Class.new(super_klass) }

  let(:object) { klass.new(value) }

  subject { object == other }

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

    it { is_expected.to eql(true) }

    it 'is symmetric' do
      is_expected.to eql(other == object)
    end
  end

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

    it { is_expected.to eql(true) }

    it 'is symmetric' do
      is_expected.to eql(other == object)
    end
  end

  context 'with an equivalent object of a subclass' do
    let(:other) { ::Class.new(klass).new(value) }

    it { is_expected.to eql(true) }

    it 'is not symmetric' do
      # LSP, any equality for type should work for subtype but
      # not the other way
      is_expected.not_to eql(other == object)
    end
  end

  context 'with an equivalent object of a superclass' do
    let(:other) { super_klass.new(value) }

    it { is_expected.to eql(false) }

    it 'is not symmetric' do
      is_expected.not_to eql(other == object)
    end
  end

  context 'with an object with a different interface' do
    let(:other) { Object.new }

    it { is_expected.to eql(false) }
  end

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

    it { is_expected.to eql(false) }

    it 'is symmetric' do
      is_expected.to eql(other == object)
    end
  end
end