Skip to content

Commit

Permalink
feat(unique_value): add == consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Mar 14, 2023
1 parent cf02a50 commit 26c8686
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/convenient_service/support/unique_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ def initialize(label = default_label)
#
# @internal
# NOTE: Every single Ruby object responds to `object_id`.
# NOTE: NO `return unless other.instance_of?(self.class)` for performance.
#
def ==(other)
return unless other.instance_of?(self.class)

object_id == other.object_id
end

Expand Down
24 changes: 24 additions & 0 deletions spec/lib/convenient_service/support/unique_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
describe "#==" do
let(:unique_value) { described_class.new(:foo) }

context "when `other` has different class" do
let(:other) { 42 }

it "returns `false`" do
expect(unique_value == other).to be_nil
end
end

context "when `other` has different `object_id`" do
let(:other) { described_class.new(:bar) }

Expand All @@ -81,6 +89,14 @@
describe "#===" do
let(:unique_value) { described_class.new(:foo) }

context "when `other` has different class" do
let(:other) { 42 }

it "returns `false`" do
expect(unique_value === other).to be_nil
end
end

context "when `other` has different `object_id`" do
let(:other) { described_class.new(:bar) }

Expand All @@ -101,6 +117,14 @@
describe ".eql?" do
let(:unique_value) { described_class.new(:foo) }

context "when `other` has different class" do
let(:other) { 42 }

it "returns `false`" do
expect(unique_value.eql?(other)).to be_nil
end
end

context "when `other` has different `object_id`" do
let(:other) { described_class.new(:bar) }

Expand Down

0 comments on commit 26c8686

Please sign in to comment.