| 1 |
require 'test/unit' |
|---|
| 2 |
require 'cmarshal' |
|---|
| 3 |
CMarshal.soap4r |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
module MarshalTestLib |
|---|
| 7 |
def encode(o) |
|---|
| 8 |
#self.class::MarshalClass.dump(o) |
|---|
| 9 |
CMarshal.dump(o) |
|---|
| 10 |
end |
|---|
| 11 |
|
|---|
| 12 |
def decode(s) |
|---|
| 13 |
#self.class::MarshalClass.load(s) |
|---|
| 14 |
CMarshal.load(s) |
|---|
| 15 |
end |
|---|
| 16 |
|
|---|
| 17 |
def marshaltest(o1) |
|---|
| 18 |
#o1.instance_eval { remove_instance_variable '@v' if defined? @v } |
|---|
| 19 |
str = encode(o1) |
|---|
| 20 |
print str, "\n" if $DEBUG |
|---|
| 21 |
o2 = decode(str) |
|---|
| 22 |
o2 |
|---|
| 23 |
end |
|---|
| 24 |
|
|---|
| 25 |
def marshal_equal(o1) |
|---|
| 26 |
o2 = marshaltest(o1) |
|---|
| 27 |
assert_equal(o1.class, o2.class, caller[0]) |
|---|
| 28 |
iv1 = o1.instance_variables.sort |
|---|
| 29 |
iv2 = o2.instance_variables.sort |
|---|
| 30 |
assert_equal(iv1, iv2) |
|---|
| 31 |
val1 = iv1.map {|var| o1.instance_eval {eval var}} |
|---|
| 32 |
val2 = iv1.map {|var| o2.instance_eval {eval var}} |
|---|
| 33 |
assert_equal(val1, val2, caller[0]) |
|---|
| 34 |
if block_given? |
|---|
| 35 |
assert_equal(yield(o1), yield(o2), caller[0]) |
|---|
| 36 |
else |
|---|
| 37 |
assert_equal(o1, o2, caller[0]) |
|---|
| 38 |
end |
|---|
| 39 |
end |
|---|
| 40 |
|
|---|
| 41 |
class MyObject; def initialize(v) @v = v end; attr_reader :v; end |
|---|
| 42 |
def test_object |
|---|
| 43 |
o1 = Object.new |
|---|
| 44 |
o1.instance_eval { @iv = 1 } |
|---|
| 45 |
marshal_equal(o1) {|o| o.instance_eval { @iv }} |
|---|
| 46 |
end |
|---|
| 47 |
|
|---|
| 48 |
def test_object_subclass |
|---|
| 49 |
marshal_equal(MyObject.new(2)) {|o| o.v} |
|---|
| 50 |
end |
|---|
| 51 |
|
|---|
| 52 |
class MyArray < Array; def initialize(v, *args) super args; @v = v; end end |
|---|
| 53 |
def test_array |
|---|
| 54 |
marshal_equal([1,2,3]) |
|---|
| 55 |
end |
|---|
| 56 |
|
|---|
| 57 |
def test_array_subclass |
|---|
| 58 |
marshal_equal(MyArray.new(0, 1,2,3)) |
|---|
| 59 |
end |
|---|
| 60 |
|
|---|
| 61 |
class MyException < Exception; def initialize(v, *args) super(*args); @v = v; end; attr_reader :v; end |
|---|
| 62 |
def test_exception |
|---|
| 63 |
marshal_equal(Exception.new('foo')) {|o| o.message} |
|---|
| 64 |
end |
|---|
| 65 |
|
|---|
| 66 |
def test_exception_subclass |
|---|
| 67 |
marshal_equal(MyException.new(20, "bar")) {|o| [o.message, o.v]} |
|---|
| 68 |
end |
|---|
| 69 |
|
|---|
| 70 |
def test_false |
|---|
| 71 |
marshal_equal(false) |
|---|
| 72 |
end |
|---|
| 73 |
|
|---|
| 74 |
class MyHash < Hash; def initialize(v, *args) super(*args); @v = v; end end |
|---|
| 75 |
def test_hash |
|---|
| 76 |
marshal_equal({1=>2, 3=>4}) |
|---|
| 77 |
end |
|---|
| 78 |
|
|---|
| 79 |
def test_hash_default |
|---|
| 80 |
h = Hash.new(:default) |
|---|
| 81 |
h[5] = 6 |
|---|
| 82 |
marshal_equal(h) |
|---|
| 83 |
end |
|---|
| 84 |
|
|---|
| 85 |
def test_hash_subclass |
|---|
| 86 |
h = MyHash.new(7, 8) |
|---|
| 87 |
h[4] = 5 |
|---|
| 88 |
marshal_equal(h) |
|---|
| 89 |
end |
|---|
| 90 |
|
|---|
| 91 |
def test_hash_default_proc |
|---|
| 92 |
h = Hash.new {} |
|---|
| 93 |
assert_raises(TypeError) { marshaltest(h) } |
|---|
| 94 |
end |
|---|
| 95 |
|
|---|
| 96 |
def test_bignum |
|---|
| 97 |
marshal_equal(-0x4000_0000_0000_0001) |
|---|
| 98 |
marshal_equal(-0x4000_0001) |
|---|
| 99 |
marshal_equal(0x4000_0000) |
|---|
| 100 |
marshal_equal(0x4000_0000_0000_0000) |
|---|
| 101 |
end |
|---|
| 102 |
|
|---|
| 103 |
def test_fixnum |
|---|
| 104 |
marshal_equal(-0x4000_0000) |
|---|
| 105 |
marshal_equal(-1) |
|---|
| 106 |
marshal_equal(0) |
|---|
| 107 |
marshal_equal(1) |
|---|
| 108 |
marshal_equal(0x3fff_ffff) |
|---|
| 109 |
end |
|---|
| 110 |
|
|---|
| 111 |
def test_float |
|---|
| 112 |
marshal_equal(-1.0) |
|---|
| 113 |
marshal_equal(0.0) |
|---|
| 114 |
marshal_equal(1.0) |
|---|
| 115 |
end |
|---|
| 116 |
|
|---|
| 117 |
def test_float_inf_nan |
|---|
| 118 |
marshal_equal(1.0/0.0) |
|---|
| 119 |
marshal_equal(-1.0/0.0) |
|---|
| 120 |
marshal_equal(0.0/0.0) {|o| o.nan?} |
|---|
| 121 |
marshal_equal(-0.0) {|o| 1.0/o} |
|---|
| 122 |
end |
|---|
| 123 |
|
|---|
| 124 |
class MyRange < Range; def initialize(v, *args) super(*args); @v = v; end end |
|---|
| 125 |
def test_range |
|---|
| 126 |
marshal_equal(1..2) |
|---|
| 127 |
marshal_equal(1...3) |
|---|
| 128 |
end |
|---|
| 129 |
|
|---|
| 130 |
def test_range_subclass |
|---|
| 131 |
STDERR.puts("test_range_subclass: known bug should be fixed.") |
|---|
| 132 |
return |
|---|
| 133 |
marshal_equal(MyRange.new(4,5,8, false)) |
|---|
| 134 |
end |
|---|
| 135 |
|
|---|
| 136 |
class MyRegexp < Regexp; def initialize(v, *args) super(*args); @v = v; end end |
|---|
| 137 |
def test_regexp |
|---|
| 138 |
marshal_equal(/a/) |
|---|
| 139 |
end |
|---|
| 140 |
|
|---|
| 141 |
def test_regexp_subclass |
|---|
| 142 |
STDERR.puts("test_regexp_subclass: known bug should be fixed.") |
|---|
| 143 |
return |
|---|
| 144 |
marshal_equal(MyRegexp.new(10, "a")) |
|---|
| 145 |
end |
|---|
| 146 |
|
|---|
| 147 |
class MyString < String; def initialize(v, *args) super(*args); @v = v; end end |
|---|
| 148 |
def test_string |
|---|
| 149 |
marshal_equal("abc") |
|---|
| 150 |
end |
|---|
| 151 |
|
|---|
| 152 |
def test_string_subclass |
|---|
| 153 |
marshal_equal(MyString.new(10, "a")) |
|---|
| 154 |
end |
|---|
| 155 |
|
|---|
| 156 |
MyStruct = Struct.new("MyStruct", :a, :b) |
|---|
| 157 |
class MySubStruct < MyStruct; def initialize(v, *args) super(*args); @v = v; end end |
|---|
| 158 |
def test_struct |
|---|
| 159 |
marshal_equal(MyStruct.new(1,2)) |
|---|
| 160 |
end |
|---|
| 161 |
|
|---|
| 162 |
def test_struct_subclass |
|---|
| 163 |
marshal_equal(MySubStruct.new(10,1,2)) |
|---|
| 164 |
end |
|---|
| 165 |
|
|---|
| 166 |
def test_symbol |
|---|
| 167 |
marshal_equal(:a) |
|---|
| 168 |
marshal_equal(:a?) |
|---|
| 169 |
marshal_equal(:a!) |
|---|
| 170 |
marshal_equal(:a=) |
|---|
| 171 |
marshal_equal(:|) |
|---|
| 172 |
marshal_equal(:^) |
|---|
| 173 |
marshal_equal(:&) |
|---|
| 174 |
marshal_equal(:<=>) |
|---|
| 175 |
marshal_equal(:==) |
|---|
| 176 |
marshal_equal(:===) |
|---|
| 177 |
marshal_equal(:=~) |
|---|
| 178 |
marshal_equal(:>) |
|---|
| 179 |
marshal_equal(:>=) |
|---|
| 180 |
marshal_equal(:<) |
|---|
| 181 |
marshal_equal(:<=) |
|---|
| 182 |
marshal_equal(:<<) |
|---|
| 183 |
marshal_equal(:>>) |
|---|
| 184 |
marshal_equal(:+) |
|---|
| 185 |
marshal_equal(:-) |
|---|
| 186 |
marshal_equal(:*) |
|---|
| 187 |
marshal_equal(:/) |
|---|
| 188 |
marshal_equal(:%) |
|---|
| 189 |
marshal_equal(:**) |
|---|
| 190 |
marshal_equal(:~) |
|---|
| 191 |
marshal_equal(:+@) |
|---|
| 192 |
marshal_equal(:-@) |
|---|
| 193 |
marshal_equal(:[]) |
|---|
| 194 |
marshal_equal(:[]=) |
|---|
| 195 |
marshal_equal(:`) #` |
|---|
| 196 |
marshal_equal("a b".intern) |
|---|
| 197 |
end |
|---|
| 198 |
|
|---|
| 199 |
class MyTime < Time; def initialize(v, *args) super(*args); @v = v; end end |
|---|
| 200 |
def test_time |
|---|
| 201 |
marshal_equal(Time.now) |
|---|
| 202 |
end |
|---|
| 203 |
|
|---|
| 204 |
def test_time_subclass |
|---|
| 205 |
STDERR.puts("test_time_subclass: known bug should be fixed.") |
|---|
| 206 |
return |
|---|
| 207 |
marshal_equal(MyTime.new(10)) |
|---|
| 208 |
end |
|---|
| 209 |
|
|---|
| 210 |
def test_true |
|---|
| 211 |
marshal_equal(true) |
|---|
| 212 |
end |
|---|
| 213 |
|
|---|
| 214 |
def test_nil |
|---|
| 215 |
marshal_equal(nil) |
|---|
| 216 |
end |
|---|
| 217 |
|
|---|
| 218 |
def test_share |
|---|
| 219 |
o = [:share] |
|---|
| 220 |
o1 = [o, o] |
|---|
| 221 |
o2 = marshaltest(o1) |
|---|
| 222 |
assert_same(o2.first, o2.last) |
|---|
| 223 |
end |
|---|
| 224 |
|
|---|
| 225 |
class CyclicRange < Range |
|---|
| 226 |
def <=>(other); true; end |
|---|
| 227 |
end |
|---|
| 228 |
def test_range_cyclic |
|---|
| 229 |
o1 = CyclicRange.allocate |
|---|
| 230 |
o1.instance_eval { initialize(o1, o1) } |
|---|
| 231 |
o2 = marshaltest(o1) |
|---|
| 232 |
assert_same(o2, o2.begin) |
|---|
| 233 |
assert_same(o2, o2.end) |
|---|
| 234 |
end |
|---|
| 235 |
|
|---|
| 236 |
def test_singleton |
|---|
| 237 |
o = Object.new |
|---|
| 238 |
def o.m() end |
|---|
| 239 |
assert_raises(TypeError) { marshaltest(o) } |
|---|
| 240 |
o = Object.new |
|---|
| 241 |
class << o |
|---|
| 242 |
@v = 1 |
|---|
| 243 |
end |
|---|
| 244 |
assert_raises(TypeError) { marshaltest(o) } |
|---|
| 245 |
assert_raises(TypeError) { marshaltest(ARGF) } |
|---|
| 246 |
assert_raises(TypeError) { marshaltest(ENV) } |
|---|
| 247 |
end |
|---|
| 248 |
|
|---|
| 249 |
module Mod1 end |
|---|
| 250 |
module Mod2 end |
|---|
| 251 |
def test_extend |
|---|
| 252 |
o = Object.new |
|---|
| 253 |
o.extend Module.new |
|---|
| 254 |
assert_raises(TypeError) { marshaltest(o) } |
|---|
| 255 |
|
|---|
| 256 |
STDERR.puts("test_range_subclass: known bug should be fixed.") |
|---|
| 257 |
return |
|---|
| 258 |
o = Object.new |
|---|
| 259 |
o.extend Mod1 |
|---|
| 260 |
marshal_equal(o) { |obj| obj.kind_of? Mod1 } |
|---|
| 261 |
o = Object.new |
|---|
| 262 |
o.extend Mod1 |
|---|
| 263 |
o.extend Mod2 |
|---|
| 264 |
marshal_equal(o) {|obj| class << obj; ancestors end} |
|---|
| 265 |
end |
|---|
| 266 |
|
|---|
| 267 |
def test_anonymous |
|---|
| 268 |
c = Class.new |
|---|
| 269 |
assert_raises(TypeError) { marshaltest(c) } |
|---|
| 270 |
o = c.new |
|---|
| 271 |
assert_raises(TypeError) { marshaltest(o) } |
|---|
| 272 |
m = Module.new |
|---|
| 273 |
assert_raises(TypeError) { marshaltest(m) } |
|---|
| 274 |
end |
|---|
| 275 |
|
|---|
| 276 |
def test_string_empty |
|---|
| 277 |
marshal_equal("") |
|---|
| 278 |
end |
|---|
| 279 |
|
|---|
| 280 |
def test_string_crlf |
|---|
| 281 |
marshal_equal("\r\n") |
|---|
| 282 |
end |
|---|
| 283 |
|
|---|
| 284 |
def test_string_escape |
|---|
| 285 |
marshal_equal("\0<;;>\1;;") |
|---|
| 286 |
end |
|---|
| 287 |
|
|---|
| 288 |
MyStruct2 = Struct.new(:a, :b) |
|---|
| 289 |
def test_struct_toplevel |
|---|
| 290 |
marshal_equal(MyStruct2.new(1,2)) |
|---|
| 291 |
end |
|---|
| 292 |
end |
|---|
| 293 |
|
|---|
| 294 |
class TestMarshal < Test::Unit::TestCase |
|---|
| 295 |
include MarshalTestLib |
|---|
| 296 |
end |
|---|