diff --git a/lib/ldclient-rb/impl/data_store.rb b/lib/ldclient-rb/impl/data_store.rb index 00043597..8897ccaa 100644 --- a/lib/ldclient-rb/impl/data_store.rb +++ b/lib/ldclient-rb/impl/data_store.rb @@ -46,7 +46,7 @@ def get_dependency_keys_fn() end def eql?(other) - namespace == other.namespace && priority == other.priority + other.is_a?(DataKind) && namespace == other.namespace && priority == other.priority end def hash diff --git a/spec/impl/data_store_spec.rb b/spec/impl/data_store_spec.rb new file mode 100644 index 00000000..738c6fbb --- /dev/null +++ b/spec/impl/data_store_spec.rb @@ -0,0 +1,63 @@ +require "spec_helper" + +module LaunchDarkly + module Impl + module DataStore + describe DataKind do + describe "eql?" do + it "constant instances are equal to themselves" do + expect(LaunchDarkly::FEATURES.eql?(LaunchDarkly::FEATURES)).to be true + expect(LaunchDarkly::SEGMENTS.eql?(LaunchDarkly::SEGMENTS)).to be true + end + + it "same constructions are equal" do + expect(LaunchDarkly::FEATURES.eql?(DataKind.new(namespace: "features", priority: 1))).to be true + expect(DataKind.new(namespace: "features", priority: 1).eql?(DataKind.new(namespace: "features", priority: 1))).to be true + + expect(LaunchDarkly::SEGMENTS.eql?(DataKind.new(namespace: "segments", priority: 0))).to be true + expect(DataKind.new(namespace: "segments", priority: 0).eql?(DataKind.new(namespace: "segments", priority: 0))).to be true + end + + it "distinct namespaces are not equal" do + expect(DataKind.new(namespace: "features", priority: 1).eql?(DataKind.new(namespace: "segments", priority: 1))).to be false + end + + it "distinct priorities are not equal" do + expect(DataKind.new(namespace: "features", priority: 1).eql?(DataKind.new(namespace: "features", priority: 2))).to be false + expect(DataKind.new(namespace: "segments", priority: 1).eql?(DataKind.new(namespace: "segments", priority: 2))).to be false + end + + it "handles non-DataKind objects" do + ["example", true, 1, 1.0, [], {}].each do |obj| + expect(LaunchDarkly::FEATURES.eql?(obj)).to be false + end + end + end + + describe "hash" do + it "constant instances are equal to themselves" do + expect(LaunchDarkly::FEATURES.hash).to be LaunchDarkly::FEATURES.hash + expect(LaunchDarkly::SEGMENTS.hash).to be LaunchDarkly::SEGMENTS.hash + end + + it "same constructions are equal" do + expect(LaunchDarkly::FEATURES.hash).to be DataKind.new(namespace: "features", priority: 1).hash + expect(DataKind.new(namespace: "features", priority: 1).hash).to be DataKind.new(namespace: "features", priority: 1).hash + + expect(LaunchDarkly::SEGMENTS.hash).to be DataKind.new(namespace: "segments", priority: 0).hash + expect(DataKind.new(namespace: "segments", priority: 0).hash).to be DataKind.new(namespace: "segments", priority: 0).hash + end + + it "distinct namespaces are not equal" do + expect(DataKind.new(namespace: "features", priority: 1).hash).not_to be DataKind.new(namespace: "segments", priority: 1).hash + end + + it "distinct priorities are not equal" do + expect(DataKind.new(namespace: "features", priority: 1).hash).not_to be DataKind.new(namespace: "features", priority: 2).hash + expect(DataKind.new(namespace: "segments", priority: 1).hash).not_to be DataKind.new(namespace: "segments", priority: 2).hash + end + end + end + end + end +end