PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/test/soap/marshal/marshaltestlib.rb

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