PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/test/ruby/marshaltestlib.rb

http://github.com/ruby/ruby
Ruby | 439 lines | 365 code | 60 blank | 14 comment | 2 complexity | af1237549e4397be5c799e23eeb8b26f MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # coding: utf-8
  2. # frozen_string_literal: false
  3. module MarshalTestLib
  4. # include this module to a Test::Unit::TestCase and define encode(o) and
  5. # decode(s) methods. e.g.
  6. #
  7. # def encode(o)
  8. # SOAPMarshal.dump(o)
  9. # end
  10. #
  11. # def decode(s)
  12. # SOAPMarshal.load(s)
  13. # end
  14. NegativeZero = (-1.0 / (1.0 / 0.0))
  15. module Mod1; end
  16. module Mod2; end
  17. def marshaltest(o1)
  18. str = encode(o1)
  19. print str.dump, "\n" if $DEBUG
  20. o2 = decode(str)
  21. o2
  22. end
  23. def marshal_equal(o1, msg = nil)
  24. msg = msg ? msg + "(#{ caller[0] })" : caller[0]
  25. o2 = marshaltest(o1)
  26. assert_equal(o1.class, o2.class, msg)
  27. iv1 = o1.instance_variables.sort
  28. iv2 = o2.instance_variables.sort
  29. assert_equal(iv1, iv2)
  30. val1 = iv1.map {|var| o1.instance_eval {eval var.to_s}}
  31. val2 = iv1.map {|var| o2.instance_eval {eval var.to_s}}
  32. assert_equal(val1, val2, msg)
  33. if block_given?
  34. assert_equal(yield(o1), yield(o2), msg)
  35. else
  36. assert_equal(o1, o2, msg)
  37. end
  38. end
  39. def marshal_equal_with_ancestry(o1, msg = nil)
  40. marshal_equal(o1, msg) do |o|
  41. ancestry = o.singleton_class.ancestors
  42. ancestry[ancestry.index(o.singleton_class)] = :singleton_class
  43. ancestry
  44. end
  45. end
  46. class MyObject; def initialize(v) @v = v end; attr_reader :v; end
  47. def test_object
  48. o1 = Object.new
  49. o1.instance_eval { @iv = 1 }
  50. marshal_equal(o1) {|o| o.instance_eval { @iv }}
  51. end
  52. def test_object_subclass
  53. marshal_equal(MyObject.new(2)) {|o| o.v}
  54. end
  55. def test_object_extend
  56. o1 = Object.new
  57. o1.extend(Mod1)
  58. marshal_equal_with_ancestry(o1)
  59. o1.extend(Mod2)
  60. marshal_equal_with_ancestry(o1)
  61. end
  62. def test_object_subclass_extend
  63. o1 = MyObject.new(2)
  64. o1.extend(Mod1)
  65. marshal_equal_with_ancestry(o1)
  66. o1.extend(Mod2)
  67. marshal_equal_with_ancestry(o1)
  68. end
  69. def test_object_prepend
  70. bug8041 = '[ruby-core:53202] [Bug #8041]'
  71. o1 = MyObject.new(42)
  72. o1.singleton_class.class_eval {prepend Mod1}
  73. assert_nothing_raised(ArgumentError, bug8041) {
  74. marshal_equal_with_ancestry(o1, bug8041)
  75. }
  76. end
  77. class MyArray < Array
  78. def initialize(v, *args)
  79. super(args)
  80. @v = v
  81. end
  82. end
  83. def test_array
  84. marshal_equal(5)
  85. marshal_equal([1,2,3])
  86. end
  87. def test_array_subclass
  88. marshal_equal(MyArray.new(0, 1, 2, 3))
  89. end
  90. def test_array_ivar
  91. o1 = Array.new
  92. o1.instance_eval { @iv = 1 }
  93. marshal_equal(o1) {|o| o.instance_eval { @iv }}
  94. end
  95. class MyException < Exception; def initialize(v, *args) super(*args); @v = v; end; attr_reader :v; end
  96. def test_exception
  97. marshal_equal(Exception.new('foo')) {|o| o.message}
  98. obj = Object.new
  99. e = assert_raise(NoMethodError) {obj.no_such_method()}
  100. marshal_equal(e) {|o| o.message}
  101. end
  102. def test_exception_subclass
  103. marshal_equal(MyException.new(20, "bar")) {|o| [o.message, o.v]}
  104. end
  105. def test_false
  106. marshal_equal(false)
  107. end
  108. class MyHash < Hash; def initialize(v, *args) super(*args); @v = v; end end
  109. def test_hash
  110. marshal_equal({1=>2, 3=>4})
  111. end
  112. def test_hash_default
  113. h = Hash.new(:default)
  114. h[5] = 6
  115. marshal_equal(h)
  116. end
  117. def test_hash_subclass
  118. h = MyHash.new(7, 8)
  119. h[4] = 5
  120. marshal_equal(h)
  121. end
  122. def test_hash_default_proc
  123. h = Hash.new {}
  124. assert_raise(TypeError) { marshaltest(h) }
  125. end
  126. def test_hash_ivar
  127. o1 = Hash.new
  128. o1.instance_eval { @iv = 1 }
  129. marshal_equal(o1) {|o| o.instance_eval { @iv }}
  130. end
  131. def test_hash_extend
  132. o1 = Hash.new
  133. o1.extend(Mod1)
  134. marshal_equal_with_ancestry(o1)
  135. o1.extend(Mod2)
  136. marshal_equal_with_ancestry(o1)
  137. end
  138. def test_hash_subclass_extend
  139. o1 = MyHash.new(2)
  140. o1.extend(Mod1)
  141. marshal_equal_with_ancestry(o1)
  142. o1.extend(Mod2)
  143. marshal_equal_with_ancestry(o1)
  144. end
  145. def test_bignum
  146. marshal_equal(-0x4000_0000_0000_0001)
  147. marshal_equal(-0x4000_0001)
  148. marshal_equal(0x4000_0000)
  149. marshal_equal(0x4000_0000_0000_0000)
  150. end
  151. def test_fixnum
  152. marshal_equal(-0x4000_0000)
  153. marshal_equal(-0x3fff_ffff)
  154. marshal_equal(-1)
  155. marshal_equal(0)
  156. marshal_equal(1)
  157. marshal_equal(0x3fff_ffff)
  158. end
  159. def test_float
  160. marshal_equal(-1.0)
  161. marshal_equal(0.0)
  162. marshal_equal(1.0)
  163. end
  164. def test_float_inf_nan
  165. marshal_equal(1.0/0.0)
  166. marshal_equal(-1.0/0.0)
  167. marshal_equal(0.0/0.0) {|o| o.nan?}
  168. marshal_equal(NegativeZero) {|o| 1.0/o}
  169. end
  170. class MyRange < Range; def initialize(v, *args) super(*args); @v = v; end end
  171. def test_range
  172. marshal_equal(1..2)
  173. marshal_equal(1...3)
  174. end
  175. def test_range_subclass
  176. marshal_equal(MyRange.new(4,5,8, false))
  177. end
  178. class MyRegexp < Regexp; def initialize(v, *args) super(*args); @v = v; end end
  179. def test_regexp
  180. marshal_equal(/a/)
  181. marshal_equal(/A/i)
  182. marshal_equal(/A/mx)
  183. marshal_equal(/a\u3042/)
  184. marshal_equal(/a恂/)
  185. assert_equal(Regexp.new("恂".force_encoding("ASCII-8BIT")),
  186. Marshal.load("\004\b/\b\343\201\202\000"))
  187. assert_equal(/au3042/, Marshal.load("\004\b/\fa\\u3042\000"))
  188. #assert_equal(/au3042/u, Marshal.load("\004\b/\fa\\u3042@")) # spec
  189. end
  190. def test_regexp_subclass
  191. marshal_equal(MyRegexp.new(10, "a"))
  192. end
  193. class MyString < String; def initialize(v, *args) super(*args); @v = v; end end
  194. def test_string
  195. marshal_equal("abc")
  196. end
  197. def test_string_ivar
  198. o1 = ""
  199. o1.instance_eval { @iv = 1 }
  200. marshal_equal(o1) {|o| o.instance_eval { @iv }}
  201. end
  202. def test_string_subclass
  203. marshal_equal(MyString.new(10, "a"))
  204. end
  205. def test_string_subclass_cycle
  206. str = MyString.new(10, "b")
  207. str.instance_eval { @v = str }
  208. marshal_equal(str) { |o|
  209. assert_same(o, o.instance_eval { @v })
  210. o.instance_eval { @v }
  211. }
  212. end
  213. def test_string_subclass_extend
  214. o = "abc"
  215. o.extend(Mod1)
  216. str = MyString.new(o, "c")
  217. marshal_equal(str) { |v|
  218. assert_kind_of(Mod1, v.instance_eval { @v })
  219. }
  220. end
  221. MyStruct = Struct.new("MyStruct", :a, :b)
  222. class MySubStruct < MyStruct; def initialize(v, *args) super(*args); @v = v; end end
  223. def test_struct
  224. marshal_equal(MyStruct.new(1,2))
  225. end
  226. def test_struct_subclass
  227. marshal_equal(MySubStruct.new(10,1,2))
  228. end
  229. def test_struct_ivar
  230. o1 = MyStruct.new
  231. o1.instance_eval { @iv = 1 }
  232. marshal_equal(o1) {|o| o.instance_eval { @iv }}
  233. end
  234. def test_struct_subclass_extend
  235. o1 = MyStruct.new
  236. o1.extend(Mod1)
  237. marshal_equal_with_ancestry(o1)
  238. o1.extend(Mod2)
  239. marshal_equal_with_ancestry(o1)
  240. end
  241. def test_symbol
  242. marshal_equal(:a)
  243. marshal_equal(:a?)
  244. marshal_equal(:a!)
  245. marshal_equal(:a=)
  246. marshal_equal(:|)
  247. marshal_equal(:^)
  248. marshal_equal(:&)
  249. marshal_equal(:<=>)
  250. marshal_equal(:==)
  251. marshal_equal(:===)
  252. marshal_equal(:=~)
  253. marshal_equal(:>)
  254. marshal_equal(:>=)
  255. marshal_equal(:<)
  256. marshal_equal(:<=)
  257. marshal_equal(:<<)
  258. marshal_equal(:>>)
  259. marshal_equal(:+)
  260. marshal_equal(:-)
  261. marshal_equal(:*)
  262. marshal_equal(:/)
  263. marshal_equal(:%)
  264. marshal_equal(:**)
  265. marshal_equal(:~)
  266. marshal_equal(:+@)
  267. marshal_equal(:-@)
  268. marshal_equal(:[])
  269. marshal_equal(:[]=)
  270. marshal_equal(:`) #`
  271. marshal_equal("a b".intern)
  272. end
  273. class MyTime < Time; def initialize(v, *args) super(*args); @v = v; end end
  274. def test_time
  275. # once there was a bug caused by usec overflow. try a little harder.
  276. 10.times do
  277. t = Time.now
  278. marshal_equal(t, t.usec.to_s)
  279. end
  280. end
  281. def test_time_subclass
  282. marshal_equal(MyTime.new(10))
  283. end
  284. def test_time_ivar
  285. o1 = Time.now
  286. o1.instance_eval { @iv = 1 }
  287. marshal_equal(o1) {|o| o.instance_eval { @iv }}
  288. end
  289. def test_time_in_array
  290. t = Time.now
  291. assert_equal([t,t], Marshal.load(Marshal.dump([t, t])), "[ruby-dev:34159]")
  292. end
  293. def test_true
  294. marshal_equal(true)
  295. end
  296. def test_nil
  297. marshal_equal(nil)
  298. end
  299. def test_share
  300. o = [:share]
  301. o1 = [o, o]
  302. o2 = marshaltest(o1)
  303. assert_same(o2.first, o2.last)
  304. end
  305. class CyclicRange < Range
  306. def <=>(other); true; end
  307. end
  308. def test_range_cyclic
  309. return unless CyclicRange.respond_to?(:allocate) # test for 1.8
  310. o1 = CyclicRange.allocate
  311. o1.instance_eval { initialize(o1, o1) }
  312. o2 = marshaltest(o1)
  313. assert_same(o2, o2.begin)
  314. assert_same(o2, o2.end)
  315. end
  316. def test_singleton
  317. o = Object.new
  318. def o.m() end
  319. assert_raise(TypeError) { marshaltest(o) }
  320. bug8043 = '[ruby-core:53206] [Bug #8043]'
  321. class << o; prepend Mod1; end
  322. assert_raise(TypeError, bug8043) {marshaltest(o)}
  323. o = Object.new
  324. c = class << o
  325. @v = 1
  326. class C; self; end
  327. end
  328. assert_raise(TypeError) { marshaltest(o) }
  329. assert_raise(TypeError) { marshaltest(c) }
  330. assert_raise(TypeError) { marshaltest(ARGF) }
  331. assert_raise(TypeError) { marshaltest(ENV) }
  332. end
  333. def test_extend
  334. o = Object.new
  335. o.extend Mod1
  336. marshal_equal(o) { |obj| obj.kind_of? Mod1 }
  337. o = Object.new
  338. o.extend Mod1
  339. o.extend Mod2
  340. marshal_equal_with_ancestry(o)
  341. o = Object.new
  342. o.extend Module.new
  343. assert_raise(TypeError) { marshaltest(o) }
  344. end
  345. def test_extend_string
  346. o = ""
  347. o.extend Mod1
  348. marshal_equal(o) { |obj| obj.kind_of? Mod1 }
  349. o = ""
  350. o.extend Mod1
  351. o.extend Mod2
  352. marshal_equal_with_ancestry(o)
  353. o = ""
  354. o.extend Module.new
  355. assert_raise(TypeError) { marshaltest(o) }
  356. end
  357. def test_anonymous
  358. c = Class.new
  359. assert_raise(TypeError) { marshaltest(c) }
  360. o = c.new
  361. assert_raise(TypeError) { marshaltest(o) }
  362. m = Module.new
  363. assert_raise(TypeError) { marshaltest(m) }
  364. end
  365. def test_string_empty
  366. marshal_equal("")
  367. end
  368. def test_string_crlf
  369. marshal_equal("\r\n")
  370. end
  371. def test_string_escape
  372. marshal_equal("\0<;;>\1;;")
  373. end
  374. MyStruct2 = Struct.new(:a, :b)
  375. def test_struct_toplevel
  376. o = MyStruct2.new(1,2)
  377. marshal_equal(o)
  378. end
  379. end