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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
# frozen_string_literal: true
require "spec/spec_helper"
RSpec.describe Equatable, "#include" do
let(:name) { "Value" }
let(:object) { described_class }
context "without attributes" do
let(:klass) { ::Class.new }
subject { klass.new }
before {
allow(klass).to receive(:name).and_return(name)
klass.send(:include, object)
}
it { is_expected.to respond_to(:compare?) }
it { is_expected.to be_instance_of(klass) }
it "has no attribute names" do
expect(klass.comparison_attrs).to eq([])
end
describe "#inspect" do
it { expect(subject.inspect).to eql("#<Value>") }
end
describe "#hash" do
it { expect(subject.hash).to eql([klass].hash) }
end
describe "#eql?" do
context "when objects are similar" do
let(:other) { subject.dup }
it { expect(subject.eql?(other)).to eql(true) }
end
context "when objects are different" do
let(:other) { double("other") }
it { expect(subject.eql?(other)).to eql(false) }
end
end
describe "#==" do
context "when objects are similar" do
let(:other) { subject.dup }
it { expect(subject == other).to eql(true) }
end
context "when objects are different" do
let(:other) { double("other") }
it { expect(subject == other).to eql(false) }
end
end
context "equivalence relation" do
let(:other) { subject.dup }
let(:another) { other.dup }
it "is not equal to nil reference" do
expect(subject.eql?(nil)).to eql(false)
end
it "is reflexive" do
expect(subject.eql?(subject)).to eql(true)
end
it "is symmetric" do
expect(subject.eql?(other)).to eql(other.eql?(subject))
end
it "is transitive" do
expect(subject.eql?(other) && other.eql?(another))
.to eql(subject.eql?(another))
end
end
end
context "with attributes" do
let(:value) { 11 }
let(:klass) {
::Class.new do
include Equatable
attr_reader :value
def initialize(value)
@value = value
end
end
}
before { allow(klass).to receive(:name).and_return(name) }
subject { klass.new(value) }
it "dynamically defines #hash method" do
expect(klass.method_defined?(:hash)).to eql(true)
end
it "dynamically defines #inspect method" do
expect(klass.method_defined?(:inspect)).to eql(true)
end
it { is_expected.to respond_to(:compare?) }
it { is_expected.to respond_to(:eql?) }
it "has comparison attribute names" do
expect(klass.comparison_attrs).to eq([:value])
end
describe "#eql?" do
context "when objects are similar" do
let(:other) { subject.dup }
it { expect(subject.eql?(other)).to eql(true) }
end
context "when objects are different" do
let(:other) { double("other") }
it { expect(subject.eql?(other)).to eql(false) }
end
end
describe "#==" do
context "when objects are similar" do
let(:other) { subject.dup }
it { expect(subject == other).to eql(true) }
end
context "when objects are different" do
let(:other) { double("other") }
it { expect(subject == other).to eql(false) }
end
end
describe "#inspect" do
it { expect(subject.inspect).to eql("#<Value value=11>") }
end
describe "#hash" do
it { expect(subject.hash).to eql(([klass] + [value]).hash) }
end
context "equivalence relation" do
let(:other) { subject.dup }
let(:another) { other.dup }
it "is not equal to nil reference" do
expect(subject.eql?(nil)).to eql(false)
end
it "is reflexive" do
expect(subject.eql?(subject)).to eql(true)
end
it "is symmetric" do
expect(subject.eql?(other)).to eql(other.eql?(subject))
end
it "is transitive" do
expect(subject.eql?(other) && other.eql?(another))
.to eql(subject.eql?(another))
end
end
end
end
|