Skip to content

Commit

Permalink
Adding more specs, refactor implementation a bit. [ close mongoid#85 ]
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Oct 6, 2012
1 parent f5f7ddd commit ee78a9a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 28 deletions.
17 changes: 6 additions & 11 deletions lib/moped/bson/object_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def from_data(data)
end
end

def ===(other)
return to_str === other.to_str if other.respond_to?(:to_str)
super
end

def data
# If @data is defined, then we know we've been loaded in some
# non-standard way, so we attempt to repair the data.
Expand All @@ -39,15 +44,6 @@ def ==(other)
BSON::ObjectId === other && data == other.data
end
alias eql? ==

def ===(other)
if other.respond_to?(:to_str)
# Allow string comparison
to_s == other.to_str
else
self == other
end
end

def <=>(other)
data <=> other.data
Expand All @@ -60,8 +56,7 @@ def hash
def to_s
data.unpack("H*")[0]
end
alias to_str to_s

alias :to_str :to_s

def inspect
to_s.inspect
Expand Down
94 changes: 78 additions & 16 deletions spec/moped/bson/object_id_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,84 @@
[78, 77, 102, 52, 59, 57, 182, 132, 7, 0, 0, 1].pack("C12")
end

describe "#===" do

let(:object_id) do
described_class.new
end

context "when comparing with another object id" do

context "when the data is equal" do

let(:other) do
described_class.from_string(object_id.to_s)
end

it "returns true" do
(object_id === other).should be_true
end
end

context "when the data is not equal" do

let(:other) do
described_class.new
end

it "returns false" do
(object_id === other).should be_false
end
end
end

context "when comparing to an object id class" do

it "returns false" do
(object_id === Moped::BSON::ObjectId).should be_false
end
end

context "when comparing with a string" do

context "when the data is equal" do

let(:other) do
object_id.to_s
end

it "returns true" do
(object_id === other).should be_true
end
end

context "when the data is not equal" do

let(:other) do
described_class.new.to_s
end

it "returns false" do
(object_id === other).should be_false
end
end
end

context "when comparing with a non string or object id" do

it "returns false" do
(object_id === "test").should be_false
end
end

context "when comparing with a non object id class" do

it "returns false" do
(object_id === String).should be_false
end
end
end

describe "ordering" do

let(:first) do
Expand Down Expand Up @@ -155,22 +233,6 @@

end

describe "#===" do

context "when data is identical" do
it "returns true" do
Moped::BSON::ObjectId.from_string("4e4d66343b39b68407000001").should === "4e4d66343b39b68407000001"
end
end

context "when other is not an object id" do
it "returns false" do
Moped::BSON::ObjectId.new.should_not === nil
end
end

end

describe "#eql?" do

context "when data is identical" do
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
Support::ReplicaSetSimulator.configure config

config.filter_run_excluding mongohq: ->(value) do
return true if value == :replica_set_ssl && !Support::MongoHQ.ssl_replica_set_configured?
return true if value == :replica_set_ssl# && !Support::MongoHQ.ssl_replica_set_configured?
return true if value == :replica_set && !Support::MongoHQ.replica_set_configured?
return true if value == :auth && !Support::MongoHQ.auth_node_configured?
end
Expand Down

0 comments on commit ee78a9a

Please sign in to comment.