Skip to content

Commit 5893245

Browse files
authored
fix: Improve DataKind eql? to consider object type (#304)
1 parent 76d06bc commit 5893245

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

lib/ldclient-rb/impl/data_store.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get_dependency_keys_fn()
4646
end
4747

4848
def eql?(other)
49-
namespace == other.namespace && priority == other.priority
49+
other.is_a?(DataKind) && namespace == other.namespace && priority == other.priority
5050
end
5151

5252
def hash

spec/impl/data_store_spec.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require "spec_helper"
2+
3+
module LaunchDarkly
4+
module Impl
5+
module DataStore
6+
describe DataKind do
7+
describe "eql?" do
8+
it "constant instances are equal to themselves" do
9+
expect(LaunchDarkly::FEATURES.eql?(LaunchDarkly::FEATURES)).to be true
10+
expect(LaunchDarkly::SEGMENTS.eql?(LaunchDarkly::SEGMENTS)).to be true
11+
end
12+
13+
it "same constructions are equal" do
14+
expect(LaunchDarkly::FEATURES.eql?(DataKind.new(namespace: "features", priority: 1))).to be true
15+
expect(DataKind.new(namespace: "features", priority: 1).eql?(DataKind.new(namespace: "features", priority: 1))).to be true
16+
17+
expect(LaunchDarkly::SEGMENTS.eql?(DataKind.new(namespace: "segments", priority: 0))).to be true
18+
expect(DataKind.new(namespace: "segments", priority: 0).eql?(DataKind.new(namespace: "segments", priority: 0))).to be true
19+
end
20+
21+
it "distinct namespaces are not equal" do
22+
expect(DataKind.new(namespace: "features", priority: 1).eql?(DataKind.new(namespace: "segments", priority: 1))).to be false
23+
end
24+
25+
it "distinct priorities are not equal" do
26+
expect(DataKind.new(namespace: "features", priority: 1).eql?(DataKind.new(namespace: "features", priority: 2))).to be false
27+
expect(DataKind.new(namespace: "segments", priority: 1).eql?(DataKind.new(namespace: "segments", priority: 2))).to be false
28+
end
29+
30+
it "handles non-DataKind objects" do
31+
["example", true, 1, 1.0, [], {}].each do |obj|
32+
expect(LaunchDarkly::FEATURES.eql?(obj)).to be false
33+
end
34+
end
35+
end
36+
37+
describe "hash" do
38+
it "constant instances are equal to themselves" do
39+
expect(LaunchDarkly::FEATURES.hash).to be LaunchDarkly::FEATURES.hash
40+
expect(LaunchDarkly::SEGMENTS.hash).to be LaunchDarkly::SEGMENTS.hash
41+
end
42+
43+
it "same constructions are equal" do
44+
expect(LaunchDarkly::FEATURES.hash).to be DataKind.new(namespace: "features", priority: 1).hash
45+
expect(DataKind.new(namespace: "features", priority: 1).hash).to be DataKind.new(namespace: "features", priority: 1).hash
46+
47+
expect(LaunchDarkly::SEGMENTS.hash).to be DataKind.new(namespace: "segments", priority: 0).hash
48+
expect(DataKind.new(namespace: "segments", priority: 0).hash).to be DataKind.new(namespace: "segments", priority: 0).hash
49+
end
50+
51+
it "distinct namespaces are not equal" do
52+
expect(DataKind.new(namespace: "features", priority: 1).hash).not_to be DataKind.new(namespace: "segments", priority: 1).hash
53+
end
54+
55+
it "distinct priorities are not equal" do
56+
expect(DataKind.new(namespace: "features", priority: 1).hash).not_to be DataKind.new(namespace: "features", priority: 2).hash
57+
expect(DataKind.new(namespace: "segments", priority: 1).hash).not_to be DataKind.new(namespace: "segments", priority: 2).hash
58+
end
59+
end
60+
end
61+
end
62+
end
63+
end

0 commit comments

Comments
 (0)