aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/marshal_test.rb
blob: 275002b8a1a9f8afc5cd2dc0858abde335ea7663 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require "abstract_unit"
require "active_support/core_ext/marshal"
require "dependencies_test_helpers"

class MarshalTest < ActiveSupport::TestCase
  include ActiveSupport::Testing::Isolation
  include DependenciesTestHelpers

  def teardown
    ActiveSupport::Dependencies.clear
    remove_constants(:EM, :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.method(:load).super_method.call(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(EM.new)
    end

    remove_constants(:EM)
    ActiveSupport::Dependencies.clear

    with_autoloading_fixtures do
      object = nil
      assert_nothing_raised do
        object = Marshal.load(dumped)
      end

      assert_kind_of EM, object
    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
      object = nil
      assert_nothing_raised do
        object = Marshal.load(dumped)
      end

      assert_kind_of ClassFolder::ClassFolderSubclass, object
    end
  end

  test "that more than one missing class is autoloaded" do
    dumped = nil
    with_autoloading_fixtures do
      dumped = Marshal.dump([EM.new, ClassFolder.new])
    end

    remove_constants(:EM, :ClassFolder)
    ActiveSupport::Dependencies.clear

    with_autoloading_fixtures do
      loaded = Marshal.load(dumped)
      assert_equal 2, loaded.size
      assert_kind_of EM, loaded[0]
      assert_kind_of ClassFolder, loaded[1]
    end
  end

  test "when one constant resolves to another" do
    class Parent; C = Class.new; end
    class Child < Parent; C = Class.new; end

    dump = Marshal.dump(Child::C.new)

    Child.send(:remove_const, :C)

    assert_raise(ArgumentError) { Marshal.load(dump) }
  end

  test "that a real missing class is causing an exception" do
    dumped = nil
    with_autoloading_fixtures do
      dumped = Marshal.dump(EM.new)
    end

    remove_constants(:EM)
    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([EM.new, SomeClass.new])
    end

    remove_constants(:EM)
    self.class.send(:remove_const, :SomeClass)
    ActiveSupport::Dependencies.clear

    with_autoloading_fixtures do
      assert_raise(NameError) do
        Marshal.load(dumped)
      end

      assert_nothing_raised do
        EM.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(EM.new, f)
      end

      f.rewind
      remove_constants(:EM)
      ActiveSupport::Dependencies.clear

      with_autoloading_fixtures do
        object = nil
        assert_nothing_raised do
          object = Marshal.load(f)
        end

        assert_kind_of EM, object
      end
    end
  end
end