Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add specs for Marshal respond_to? behaviors.

  • Loading branch information...
commit f979fb9b13372811dfc5fb4519f1886363de0a6c 1 parent d7ff2be
Charles Oliver Nutter headius authored
64 core/marshal/dump_spec.rb
View
@@ -57,6 +57,70 @@ def _dump(depth); 10; end
lambda { Marshal.dump(klass) }.should raise_error(TypeError)
lambda { Marshal.dump(mod) }.should raise_error(TypeError)
end
+
+ ruby_version_is ""..."1.9" do
+ it "invokes respond_to? for marshal_dump and _dump on user classes" do
+ obj = UserDefinedWithRespondTo.new
+
+ result = Marshal.dump(obj)
+
+ obj.collected.should == [:marshal_dump, :_dump]
+ obj.marshal_dump_called.should be_nil
+ obj._dump_called.should be_nil
+ end
+
+ it "attempts to invoke marshal_dump if respond_to? :marshal_dump is true" do
+ obj = UserDefinedWithRespondTo.new(:marshal_dump)
+
+ result = Marshal.dump(obj)
+
+ obj.collected.should == [:marshal_dump]
+ obj.marshal_dump_called.should be_true
+ obj._dump_called.should be_nil
+ end
+
+ it "attempts to invoke _dump if respond_to? :_dump is true" do
+ obj = UserDefinedWithRespondTo.new(:_dump)
+
+ result = Marshal.dump(obj)
+
+ obj.collected.should == [:marshal_dump, :_dump]
+ obj.marshal_dump_called.should be_nil
+ obj._dump_called.should be_true
+ end
+ end
+
+ ruby_version_is "1.9" do
+ it "invokes respond_to? for marshal_dump and _dump on user classes" do
+ obj = UserDefinedWithRespondTo.new
+
+ result = Marshal.dump(obj)
+
+ obj.collected.should == [:marshal_dump, :_dump]
+ obj.marshal_dump_called.should be_nil
+ obj._dump_called.should be_nil
+ end
+
+ it "attempts to invoke marshal_dump if respond_to? :marshal_dump is true" do
+ obj = UserDefinedWithRespondTo.new(:marshal_dump)
+
+ result = Marshal.dump(obj)
+
+ obj.collected.should == [:marshal_dump]
+ obj.marshal_dump_called.should be_true
+ obj._dump_called.should be_nil
+ end
+
+ it "attempts to invoke _dump if respond_to? :_dump is true" do
+ obj = UserDefinedWithRespondTo.new(:_dump)
+
+ result = Marshal.dump(obj)
+
+ obj.collected.should == [:marshal_dump, :_dump]
+ obj.marshal_dump_called.should be_nil
+ obj._dump_called.should be_true
+ end
+ end
it "raises a TypeError if _dump returns a non-string" do
lambda { Marshal.dump(UserDefinedBad.new) }.should raise_error(TypeError)
44 core/marshal/fixtures/marshal_data.rb
View
@@ -139,6 +139,50 @@ def meths_more_method() end
Struct.new "Pyramid"
Struct.new "Useful", :a, :b
+class UserDefinedWithRespondTo
+ def initialize(*names)
+ @names = names
+ end
+ def collected
+ @collected ||= []
+ end
+ attr_reader :marshal_dump_called, :_dump_called
+ def respond_to?(name)
+ collected << name
+ @names.include? name
+ end
+ def marshal_dump
+ @marshal_dump_called = true
+ "marshal_dump"
+ end
+ def _dump(depth)
+ @_dump_called = true
+ "_dump"
+ end
+end
+
+class UserDefinedWithRespondToMarshalLoad
+ def marshal_dump
+ "dump"
+ end
+
+ def respond_to?(name)
+ throw :RespondTo if name == :marshal_load
+ end
+end
+
+class UserDefinedWithRespondToLoad
+ def _dump(depth)
+ "dump"
+ end
+
+ class << self
+ def respond_to?(name)
+ throw :RespondTo if name == :_load
+ end
+ end
+end
+
module MarshalSpec
def self.random_data
randomizer = Random.new(42)
20 core/marshal/load_spec.rb
View
@@ -27,6 +27,26 @@
new_obj_metaclass_ancestors[0].should == Meths
new_obj_metaclass_ancestors[1].should == UserHashInitParams
end
+
+ # FIXME: Not proud of these next two specs, but unsure how to check for respond_to?
+ # on object and class cleanly
+ it "invokes respond_to? for marshal_load when the stream needs marshal_load" do
+ obj = UserDefinedWithRespondToMarshalLoad.new
+ data = Marshal.dump(obj)
+
+ catch(:RespondTo) do
+ Marshal.load(data)
+ end.should_not be_nil
+ end
+
+ it "invokes respond_to? for _load when the stream needs _load" do
+ obj = UserDefinedWithRespondToLoad.new
+ data = Marshal.dump(obj)
+
+ catch(:RespondTo) do
+ Marshal.load(data)
+ end.should be_nil
+ end
it "loads a user-marshaled extended object" do
obj = UserMarshal.new.extend(Meths)
Please sign in to comment.
Something went wrong with that request. Please try again.