Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve DataKind eql? to consider object type #304

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ldclient-rb/impl/data_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions spec/impl/data_store_spec.rb
Original file line number Diff line number Diff line change
@@ -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