-
-
Notifications
You must be signed in to change notification settings - Fork 251
/
tests.rb
executable file
·480 lines (440 loc) · 11.2 KB
/
tests.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/env ruby -wW1
# encoding: UTF-8
$: << File.join(File.dirname(__FILE__), "../lib")
$: << File.join(File.dirname(__FILE__), "../ext")
require 'test/unit'
require 'oj'
class Jam
attr_accessor :x, :y
def initialize(x, y)
@x = x
@y = y
end
def eql?(o)
self.class == o.class && @x == o.x && @y == o.y
end
alias == eql?
end # Jam
class Jeez < Jam
def initialize(x, y)
super
end
def to_json()
%{{"json_class":"#{self.class}","x":#{@x},"y":#{@y}}}
end
def self.json_create(h)
self.new(h['x'], h['y'])
end
end # Jeez
class Jazz < Jam
def initialize(x, y)
super
end
def to_hash()
{ 'json_class' => self.class.to_s, 'x' => @x, 'y' => @y }
end
def self.json_create(h)
self.new(h['x'], h['y'])
end
end # Jazz
class Range
def to_hash()
{ 'begin' => self.begin, 'end' => self.end, 'exclude_end' => self.exclude_end? }
end
end # Range
class Juice < ::Test::Unit::TestCase
def test_get_options
opts = Oj.default_options()
assert_equal(opts, {
:encoding=>nil,
:indent=>0,
:circular=>false,
:auto_define=>true,
:mode=>:object})
end
def test_set_options
orig = {
:encoding=>nil,
:indent=>0,
:circular=>false,
:auto_define=>true,
:mode=>:object}
o2 = {
:encoding=>"UTF-8",
:indent=>4,
:circular=>true,
:auto_define=>false,
:mode=>:compat}
o3 = { :indent => 4 }
Oj.default_options = o2
opts = Oj.default_options()
assert_equal(opts, o2);
Oj.default_options = o3 # see if it throws an exception
Oj.default_options = orig # return to original
end
def test_nil
dump_and_load(nil, false)
end
def test_true
dump_and_load(true, false)
end
def test_false
dump_and_load(false, false)
end
def test_fixnum
dump_and_load(0, false)
dump_and_load(12345, false)
dump_and_load(-54321, false)
dump_and_load(1, false)
end
def test_bignum
dump_and_load(12345678901234567890123456789, false)
end
def test_float
dump_and_load(0.0, false)
dump_and_load(12345.6789, false)
dump_and_load(-54321.012, false)
dump_and_load(2.48e16, false)
end
def test_string
dump_and_load('', false)
dump_and_load('abc', false)
dump_and_load("abc\ndef", false)
dump_and_load("a\u0041", false)
end
def test_string_object
dump_and_load('abc', false)
dump_and_load(':abc', false)
end
def test_encode
Oj.default_options = { :encoding => 'UTF-8' }
dump_and_load("ぴーたー", false)
Oj.default_options = { :encoding => nil }
end
def test_array
dump_and_load([], false)
dump_and_load([true, false], false)
dump_and_load(['a', 1, nil], false)
dump_and_load([[nil]], false)
dump_and_load([[nil], 58], false)
end
# Symbol
def test_symbol_strict
begin
json = Oj.dump(:abc, :mode => :strict)
rescue Exception => e
assert(true)
end
end
def test_symbol_null
json = Oj.dump(:abc, :mode => :null)
assert_equal('null', json)
end
def test_symbol_compat
json = Oj.dump(:abc, :mode => :compat)
assert_equal('"abc"', json)
end
def test_symbol_object
Oj.default_options = { :mode => :object }
dump_and_load(''.to_sym, false)
dump_and_load(:abc, false)
dump_and_load(':xyz'.to_sym, false)
end
# Time
def test_time_strict
t = Time.new(2012, 1, 5, 23, 58, 7)
begin
json = Oj.dump(t, :mode => :strict)
rescue Exception => e
assert(true)
end
end
def test_time_null
t = Time.new(2012, 1, 5, 23, 58, 7)
json = Oj.dump(t, :mode => :null)
assert_equal('null', json)
end
def test_time_compat
t = Time.new(2012, 1, 5, 23, 58, 7)
json = Oj.dump(t, :mode => :compat)
assert_equal(%{1325775487.000000}, json)
end
def test_time_object
t = Time.now()
Oj.default_options = { :mode => :object }
dump_and_load(t, false)
end
# Class
def test_class_strict
begin
json = Oj.dump(Juice, :mode => :strict)
rescue Exception => e
assert(true)
end
end
def test_class_null
json = Oj.dump(Juice, :mode => :null)
assert_equal('null', json)
end
def test_class_compat
json = Oj.dump(Juice, :mode => :compat)
assert_equal(%{"Juice"}, json)
end
def test_class_object
Oj.default_options = { :mode => :object }
dump_and_load(Juice, false)
end
# Hash
def test_hash
Oj.default_options = { :mode => :strict }
dump_and_load({}, false)
dump_and_load({ 'true' => true, 'false' => false}, false)
dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
end
def test_non_str_hash_strict
begin
json = Oj.dump({ 1 => true, 0 => false }, :mode => :strict)
rescue Exception => e
assert(true)
end
end
def test_non_str_hash_null
begin
json = Oj.dump({ 1 => true, 0 => false }, :mode => :null)
rescue Exception => e
assert(true)
end
end
def test_non_str_hash_compat
begin
json = Oj.dump({ 1 => true, 0 => false }, :mode => :compat)
rescue Exception => e
assert(true)
end
end
def test_non_str_hash_object
Oj.default_options = { :mode => :object }
json = Oj.dump({ 1 => true, 0 => false, :sim => nil })
h = Oj.load(json, :mode => :strict)
assert_equal({"^#1" => [1, true], "^#2" => [0, false], ":sim" => nil}, h)
h = Oj.load(json)
assert_equal({ 1 => true, 0 => false, :sim => nil }, h)
end
def test_mixed_hash_object
Oj.default_options = { :mode => :object }
json = Oj.dump({ 1 => true, 0 => false, 'nil' => nil, :sim => 4 })
h = Oj.load(json, :mode => :strict)
assert_equal({"^#1" => [1, true], "^#2" => [0, false], "nil" => nil, ":sim" => 4}, h)
h = Oj.load(json)
assert_equal({ 1 => true, 0 => false, 'nil' => nil, :sim => 4 }, h)
end
# Object with to_json()
def test_json_object_strict
obj = Jeez.new(true, 58)
begin
json = Oj.dump(obj, :mode => :strict)
rescue Exception => e
assert(true)
end
end
def test_json_object_null
obj = Jeez.new(true, 58)
json = Oj.dump(obj, :mode => :null)
assert_equal('null', json)
end
def test_json_object_compat
Oj.default_options = { :mode => :compat }
obj = Jeez.new(true, 58)
json = Oj.dump(obj, :indent => 2)
assert_equal(%{{"json_class":"Jeez","x":true,"y":58}}, json)
dump_and_load(obj, false)
end
def test_json_object_object
obj = Jeez.new(true, 58)
json = Oj.dump(obj, :mode => :object, :indent => 2)
assert_equal(%{{
"^o":"Jeez",
"x":true,
"y":58}}, json)
obj2 = Oj.load(json, :mode => :object)
assert_equal(obj, obj2)
end
# Object with to_hash()
def test_to_hash_object_strict
obj = Jazz.new(true, 58)
begin
json = Oj.dump(obj, :mode => :strict)
rescue Exception => e
assert(true)
end
end
def test_to_hash_object_null
obj = Jazz.new(true, 58)
json = Oj.dump(obj, :mode => :null)
assert_equal('null', json)
end
def test_to_hash_object_compat
obj = Jazz.new(true, 58)
json = Oj.dump(obj, :mode => :compat, :indent => 2)
assert_equal(%{{
"json_class":"Jazz",
"x":true,
"y":58}}, json)
end
def test_to_hash_object_object
obj = Jazz.new(true, 58)
json = Oj.dump(obj, :mode => :object, :indent => 2)
assert_equal(%{{
"^o":"Jazz",
"x":true,
"y":58}}, json)
obj2 = Oj.load(json, :mode => :object)
assert_equal(obj, obj2)
end
# Object without to_json() or to_hash()
def test_object_strict
obj = Jam.new(true, 58)
begin
json = Oj.dump(obj, :mode => :strict)
rescue Exception => e
assert(true)
end
end
def test_object_null
obj = Jam.new(true, 58)
json = Oj.dump(obj, :mode => :null)
assert_equal('null', json)
end
def test_object_compat
obj = Jam.new(true, 58)
json = Oj.dump(obj, :mode => :compat, :indent => 2)
assert_equal(%{{
"x":true,
"y":58}}, json)
end
def test_object_object
obj = Jam.new(true, 58)
json = Oj.dump(obj, :mode => :object, :indent => 2)
assert_equal(%{{
"^o":"Jam",
"x":true,
"y":58}}, json)
obj2 = Oj.load(json, :mode => :object)
assert_equal(obj, obj2)
end
# Exception
def test_exception
err = nil
begin
raise StandardError.new('A Message')
rescue Exception => e
err = e
end
json = Oj.dump(err, :mode => :object, :indent => 2)
#puts "*** #{json}"
e2 = Oj.load(json, :mode => :strict)
assert_equal(err.class.to_s, e2['^o'])
assert_equal(err.message, e2['~mesg'])
assert_equal(err.backtrace, e2['~bt'])
e2 = Oj.load(json, :mode => :object)
assert_equal(e, e2);
end
# Range
def test_range_strict
begin
json = Oj.dump(1..7, :mode => :strict)
rescue Exception => e
assert(true)
end
end
def test_range_null
json = Oj.dump(1..7, :mode => :null)
assert_equal('null', json)
end
def test_range_compat
json = Oj.dump(1..7, :mode => :compat)
assert_equal(%{{"begin":1,"end":7,"exclude_end":false}}, json)
json = Oj.dump(1...7, :mode => :compat)
assert_equal(%{{"begin":1,"end":7,"exclude_end":true}}, json)
end
def test_range_object
Oj.default_options = { :mode => :object }
json = Oj.dump(1..7, :mode => :object, :indent => 0)
assert_equal(%{{"^u":["Range",1,7,false]}}, json)
dump_and_load(1..7, false)
dump_and_load(1..1, false)
dump_and_load(1...7, false)
end
# autodefine Oj::Bag
def test_bag
json = %{{
"^o":"Jem",
"x":true,
"y":58 }}
obj = Oj.load(json, :mode => :object)
assert_equal('Jem', obj.class.name)
assert_equal(true, obj.x)
assert_equal(58, obj.y)
end
# Circular
def test_circular_object
obj = Jam.new(nil, 58)
obj.x = obj
json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
assert_equal(%{{
"^o":"Jam",
"^i":1,
"x":{"^r":1},
"y":58}}, json)
obj2 = Oj.load(json, :mode => :object, :circular => true)
assert_equal(obj2.x.__id__, obj2.__id__)
end
def test_circular_hash
h = { 'a' => 7 }
h['b'] = h
json = Oj.dump(h, :mode => :object, :indent => 2, :circular => true)
assert_equal(%{{
"^i":1,
"a":7,
"b":{"^r":1}}}, json)
h2 = Oj.load(json, :mode => :object, :circular => true)
assert_equal(h['b'].__id__, h.__id__)
end
def xtest_circular_array
a = [7]
a << a
json = Oj.dump(a, :mode => :object, :indent => 2, :circular => true)
puts json
assert_equal(%{[
"^i1",
7,
{"^r":1}]}, json)
h2 = Oj.load(json, :mode => :object, :circular => true)
assert_equal(h['b'].__id__, h.__id__)
end
def test_circular
h = { 'a' => 7 }
obj = Jam.new(h, 58)
obj.x['b'] = obj
json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
assert_equal(%{{
"^o":"Jam",
"^i":1,
"x":{
"^i":2,
"a":7,
"b":{"^r":1}
},
"y":58}}, json)
obj2 = Oj.load(json, :mode => :object, :circular => true)
assert_equal(obj.x.__id__, h.__id__)
assert_equal(h['b'].__id__, obj.__id__)
end
def dump_and_load(obj, trace=false)
json = Oj.dump(obj, :indent => 2)
puts json if trace
loaded = Oj.load(json);
assert_equal(obj, loaded)
loaded
end
end