Permalink
Browse files

Improve equality checking on CodeObjects and add #hash/#equal? methods

Closes gh-119
  • Loading branch information...
1 parent 551cbe3 commit d489b2aefa74efea5020bb115a865c9410e07fc5 @lsegal lsegal committed Jun 15, 2010
@@ -248,13 +248,18 @@ def line
# @param [Base, Proxy] other if other is a {Proxy}, tests if
# the paths are equal
# @return [Boolean] whether or not the objects are considered the same
- def ==(other)
- if other.is_a?(Proxy)
+ def equal?(other)
+ if other.is_a?(Base) || other.is_a?(Proxy)
path == other.path
else
super
end
end
+ alias == equal?
+ alias eql? equal?
+
+ # @return [Integer] the object's hash value (for equality checking)
+ def hash; path.hash end
# Accesses a custom attribute on the object
# @param [#to_s] key the name of the custom attribute
@@ -125,13 +125,17 @@ def <=>(other)
end
# @return [Boolean]
- def ==(other)
+ def equal?(other)
if other.respond_to? :path
path == other.path
else
false
end
end
+ alias == equal?
+
+ # @return [Integer] the object's hash value (for equality checking)
+ def hash; path.hash end
# Returns the class name of the object the proxy is mimicking, if
# resolved. Otherwise returns +Proxy+.
@@ -6,6 +6,11 @@ class RootObject < ModuleObject
def path; "" end
def inspect; "#<yardoc root>" end
def root?; true end
+ def equal?(other)
+ other == :root ? true : super(other)
+ end
+
+ def hash; :root.hash end
end
end
end
View
@@ -1,6 +1,24 @@
require 'fileutils'
module YARD
+ # A TemporaryObject class is temporarily created during deserialization of
+ # an object from disk. An instance of this class is inserted into the
+ # {RegistryStore} until the object is resolved and helps the object resolve
+ # any references it might have to itself.
+ #
+ # @note This class should never be used directly.
+ # @private
+ class TemporaryObject < CodeObjects::Base
+ def self.__setup__(path)
+ object = allocate
+ object.path = path
+ object
+ end
+
+ attr_accessor :path
+ def hash; path.hash end
+ end
+
# The data store for the {Registry}.
#
# @see Registry
@@ -31,11 +49,13 @@ def get(key)
# check disk
return if @notfound[key]
+ put(key, TemporaryObject.__setup__(key))
if obj = @serializer.deserialize(key)
@loaded_objects += 1
put(key, obj)
else
@notfound[key] = true
+ delete(key)
nil
end
end
@@ -48,6 +68,7 @@ def put(key, value)
if key == ''
@store[:root] = value
else
+ @notfound.delete(key.to_sym)
@store[key.to_sym] = value
end
end
View
@@ -184,15 +184,20 @@
end
describe '#load_yardoc' do
- before do
- @store = RegistryStore.new
- RegistryStore.should_receive(:new).and_return(@store)
- end
-
it "should delegate load to RegistryStore" do
- @store.should_receive(:load).with('foo')
+ store = RegistryStore.new
+ store.should_receive(:load).with('foo')
+ RegistryStore.should_receive(:new).and_return(store)
Registry.yardoc_file = 'foo'
Registry.load_yardoc
end
+
+ it "should maintain hash key equality on loaded objects" do
+ Registry.clear
+ Registry.load!(File.dirname(__FILE__) + '/serializers/data/serialized_yardoc')
+ baz = Registry.at('Foo#baz')
+ Registry.at('Foo').aliases.keys.should include(baz)
+ Registry.at('Foo').aliases.has_key?(baz).should == true
+ end
end
end
@@ -0,0 +1 @@
+test.rb 80e5834ff1e98223761615c0917ff9b77b7ae057
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
+{I" Object:EF:
+class
@@ -0,0 +1,35 @@
+require File.dirname(__FILE__) + "/spec_helper"
+
+instance_eval do
+ class YARD::Serializers::YardocSerializer
+ public :dump
+ public :internal_dump
+ end
+end
+
+describe YARD::Serializers::YardocSerializer do
+ describe '#dump' do
+ before do
+ @serializer = YARD::Serializers::YardocSerializer.new('.yardoc')
+
+ Registry.clear
+ @foo = CodeObjects::ClassObject.new(:root, :Foo)
+ @bar = CodeObjects::MethodObject.new(@foo, :bar)
+ end
+
+ it "should maintain object equality when loading a dumped object" do
+ newfoo = @serializer.internal_dump(@foo)
+ newfoo.should equal(@foo)
+ newfoo.should == @foo
+ @foo.should equal(newfoo)
+ @foo.should == newfoo
+ newfoo.hash.should == @foo.hash
+ end
+
+ it "should maintain hash key equality when loading a dumped object" do
+ newfoo = @serializer.internal_dump(@foo)
+ {@foo => 1}.should have_key(newfoo)
+ {newfoo => 1}.should have_key(@foo)
+ end
+ end
+end

0 comments on commit d489b2a

Please sign in to comment.