-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtst-marshal.rb
232 lines (205 loc) · 5.41 KB
/
tst-marshal.rb
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
require 'test/unit'
module MarshalTestLib
DebugPrint = false
MarshalClass = Marshal
def encode(o)
self.class::MarshalClass.dump(o)
end
def decode(s)
self.class::MarshalClass.load(s)
end
def marshaltest(o1)
#o1.instance_eval { remove_instance_variable '@v' if defined? @v }
str = encode(o1)
print str, "\n" if self.class::DebugPrint
o2 = decode(str)
o2
end
def marshal_equal(o1)
o2 = marshaltest(o1)
assert_equal(o1.class, o2.class)
iv1 = o1.instance_variables.sort
iv2 = o2.instance_variables.sort
assert_equal(iv1, iv2)
val1 = iv1.map {|var| o1.instance_eval {eval var}}
val2 = iv1.map {|var| o2.instance_eval {eval var}}
assert_equal(val1, val2)
if block_given?
assert_equal(yield(o1), yield(o2))
else
assert_equal(o1, o2)
end
end
class MyObject; def initialize(v) @v = v end; attr_reader :v; end
def test_object
o1 = Object.new
o1.instance_eval { @iv = 1 }
marshal_equal(o1) {|o| o.instance_eval { @iv }}
marshal_equal(MyObject.new(2)) {|o| o.v}
end
class MyArray < Array; def initialize(v, *args) super args; @v = v; end end
def test_array
marshal_equal([1,2,3])
marshal_equal(MyArray.new(0, 1,2,3))
end
class MyException < Exception; def initialize(v, *args) super(*args); @v = v; end; attr_reader :v; end
def test_exception
marshal_equal(Exception.new('foo')) {|o| o.message}
marshal_equal(MyException.new(20, "bar")) {|o| [o.message, o.v]}
end
def test_false
marshal_equal(false)
end
class MyHash < Hash; def initialize(v, *args) super(*args); @v = v; end end
def test_hash
marshal_equal({1=>2, 3=>4})
h = Hash.new(:default)
h[5] = 6
marshal_equal(h)
h = MyHash.new(7, 8)
h[4] = 5
marshal_equal(h)
h = Hash.new {}
assert_raises(TypeError) { marshaltest(h) }
end
def test_bignum
marshal_equal(-0x4000_0000_0000_0001)
marshal_equal(-0x4000_0001)
marshal_equal(0x4000_0000)
marshal_equal(0x4000_0000_0000_0000)
end
def test_fixnum
marshal_equal(-0x4000_0000)
marshal_equal(-1)
marshal_equal(0)
marshal_equal(1)
marshal_equal(0x3fff_ffff)
end
def test_float
marshal_equal(-1.0)
marshal_equal(0.0)
marshal_equal(1.0)
marshal_equal(1.0/0.0)
marshal_equal(-1.0/0.0)
marshal_equal(0.0/0.0) {|o| o.nan?}
marshal_equal(-0.0) {|o| 1.0/o}
end
class MyRange < Range; def initialize(v, *args) super(*args); @v = v; end end
def test_range
marshal_equal(1..2)
marshal_equal(1...3)
marshal_equal(MyRange.new(4,5,8, false))
end
class MyRegexp < Regexp; def initialize(v, *args) super(*args); @v = v; end end
def test_regexp
marshal_equal(/a/)
marshal_equal(MyRegexp.new(10, "a"))
end
class MyString < String; def initialize(v, *args) super(*args); @v = v; end end
def test_string
marshal_equal("abc")
marshal_equal(MyString.new(10, "a"))
end
MyStruct = Struct.new("MyStruct", :a, :b)
class MySubStruct < MyStruct; def initialize(v, *args) super(*args); @v = v; end end
def test_struct
marshal_equal(MyStruct.new(1,2))
marshal_equal(MySubStruct.new(10,1,2))
end
def test_symbol
marshal_equal(:a)
marshal_equal(:a?)
marshal_equal(:a!)
marshal_equal(:a=)
marshal_equal(:|)
marshal_equal(:^)
marshal_equal(:&)
marshal_equal(:<=>)
marshal_equal(:==)
marshal_equal(:===)
marshal_equal(:=~)
marshal_equal(:>)
marshal_equal(:>=)
marshal_equal(:<)
marshal_equal(:<=)
marshal_equal(:<<)
marshal_equal(:>>)
marshal_equal(:+)
marshal_equal(:-)
marshal_equal(:*)
marshal_equal(:/)
marshal_equal(:%)
marshal_equal(:**)
marshal_equal(:~)
marshal_equal(:+@)
marshal_equal(:-@)
marshal_equal(:[])
marshal_equal(:[]=)
marshal_equal(:`) # `
marshal_equal("a b".intern)
end
class MyTime < Time; def initialize(v, *args) super(*args); @v = v; end end
def test_time
marshal_equal(Time.now)
marshal_equal(MyTime.new(10))
end
def test_true
marshal_equal(true)
end
def test_nil
marshal_equal(nil)
end
def test_share
o = [:share]
o1 = [o, o]
o2 = marshaltest(o1)
assert_same(o2.first, o2.last)
end
class CyclicRange < Range
def <=>(other) self.object_id <=> other.object_id end
end
def test_cyclic_range
o1 = CyclicRange.allocate
o1.instance_eval { initialize o1, o1 }
o2 = marshaltest(o1)
assert_same(o2, o2.begin)
assert_same(o2, o2.end)
end
def test_singleton
o = Object.new
def o.m() end
assert_raises(TypeError) { marshaltest(o) }
o = Object.new
class << o
@v = 1
end
assert_raises(TypeError) { marshaltest(o) }
assert_raises(TypeError) { marshaltest(ARGF) }
assert_raises(TypeError) { marshaltest(ENV) }
end
module Mod1 end
module Mod2 end
def test_extend
o = Object.new
o.extend Mod1
marshal_equal(o) {|obj| obj.kind_of? Mod1}
o = Object.new
o.extend Module.new
assert_raises(TypeError) { marshaltest(o) }
o = Object.new
o.extend Mod1
o.extend Mod2
marshal_equal(o) {|obj| class << obj; ancestors end}
end
def test_anonymous
c = Class.new
assert_raises(TypeError) { marshaltest(c) }
o = c.new
assert_raises(TypeError) { marshaltest(o) }
m = Module.new
assert_raises(TypeError) { marshaltest(m) }
end
end
class MarshalTest < Test::Unit::TestCase
include MarshalTestLib
end