diff options
author | Uriel Katz <uriel.katz@gmail.com> | 2012-11-17 02:05:53 +0200 |
---|---|---|
committer | Uriel Katz <uriel.katz@gmail.com> | 2012-12-01 14:00:16 +0200 |
commit | 9ee0ffb3602720fceaecc2fbae1c932341482e31 (patch) | |
tree | 9a3f7c9fab2bfce6c1ceb136e72be133f58fc8df /activesupport/test/core_ext | |
parent | 005d910624bbfa724b638426a000c8074d4201a2 (diff) | |
download | rails-9ee0ffb3602720fceaecc2fbae1c932341482e31.tar.gz rails-9ee0ffb3602720fceaecc2fbae1c932341482e31.tar.bz2 rails-9ee0ffb3602720fceaecc2fbae1c932341482e31.zip |
Patched Marshal#load to work with constant autoloading (active_support/dependecies.rb) (issue #8167)
Diffstat (limited to 'activesupport/test/core_ext')
-rw-r--r-- | activesupport/test/core_ext/marshal_test.rb | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/marshal_test.rb b/activesupport/test/core_ext/marshal_test.rb new file mode 100644 index 0000000000..ac79b15fa8 --- /dev/null +++ b/activesupport/test/core_ext/marshal_test.rb @@ -0,0 +1,124 @@ +require 'abstract_unit' +require 'active_support/core_ext/marshal' +require 'dependecies_test_helpers' + +class MarshalTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + include DependeciesTestHelpers + + def teardown + ActiveSupport::Dependencies.clear + remove_constants(:E, :ClassFolder) + end + + test "that Marshal#load still works" do + sanity_data = ["test", [1, 2, 3], {a: [1, 2, 3]}, ActiveSupport::TestCase] + sanity_data.each do |obj| + dumped = Marshal.dump(obj) + assert_equal Marshal.load_without_autoloading(dumped), Marshal.load(dumped) + end + end + + test "that a missing class is autoloaded from string" do + dumped = nil + with_autoloading_fixtures do + dumped = Marshal.dump(E.new) + end + + remove_constants(:E) + ActiveSupport::Dependencies.clear + + with_autoloading_fixtures do + assert_kind_of E, Marshal.load(dumped) + end + end + + test "that classes in sub modules work" do + dumped = nil + with_autoloading_fixtures do + dumped = Marshal.dump(ClassFolder::ClassFolderSubclass.new) + end + + remove_constants(:ClassFolder) + ActiveSupport::Dependencies.clear + + with_autoloading_fixtures do + assert_kind_of ClassFolder::ClassFolderSubclass, Marshal.load(dumped) + end + end + + test "that more than one missing class is autoloaded" do + dumped = nil + with_autoloading_fixtures do + dumped = Marshal.dump([E.new, ClassFolder.new]) + end + + remove_constants(:E, :ClassFolder) + ActiveSupport::Dependencies.clear + + with_autoloading_fixtures do + loaded = Marshal.load(dumped) + assert_equal 2, loaded.size + assert_kind_of E, loaded[0] + assert_kind_of ClassFolder, loaded[1] + end + end + + test "that a real missing class is causing an exception" do + dumped = nil + with_autoloading_fixtures do + dumped = Marshal.dump(E.new) + end + + remove_constants(:E) + ActiveSupport::Dependencies.clear + + assert_raise(NameError) do + Marshal.load(dumped) + end + end + + test "when first class is autoloaded and second not" do + dumped = nil + class SomeClass + end + + with_autoloading_fixtures do + dumped = Marshal.dump([E.new, SomeClass.new]) + end + + remove_constants(:E) + self.class.send(:remove_const, :SomeClass) + ActiveSupport::Dependencies.clear + + with_autoloading_fixtures do + assert_raise(NameError) do + Marshal.load(dumped) + end + + assert_nothing_raised("E failed to load while we expect only SomeClass to fail loading") do + E.new + end + + assert_raise(NameError, "We expected SomeClass to not be loaded but it is!") do + SomeClass.new + end + end + end + + test "loading classes from files trigger autoloading" do + Tempfile.open("object_serializer_test") do |f| + with_autoloading_fixtures do + Marshal.dump(E.new, f) + end + + f.rewind + remove_constants(:E) + ActiveSupport::Dependencies.clear + + with_autoloading_fixtures do + assert_kind_of E, Marshal.load(f) + end + end + end +end
\ No newline at end of file |