-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
test_various.rb
executable file
·720 lines (629 loc) · 17.5 KB
/
test_various.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#!/usr/bin/env ruby
# encoding: utf-8
$: << File.dirname(__FILE__)
require 'helper'
class Juice < Minitest::Test
def gen_whitespaced_string(length = Random.new.rand(100))
whitespace_chars = [" ", "\t", "\f", "\n", "\r"]
result = ""
length.times { result << whitespace_chars.sample }
result
end
module TestModule
end
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
# contributed by sauliusg to fix as_json
class Orange < Jam
def initialize(x, y)
super
end
def as_json()
{ :json_class => self.class,
:x => @x,
:y => @y }
end
def self.json_create(h)
self.new(h['x'], h['y'])
end
end
class Melon < Jam
def initialize(x, y)
super
end
def as_json(options)
"#{x} #{y}"
end
def self.json_create(h)
self.new(h['x'], h['y'])
end
end
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
def setup
@default_options = Oj.default_options
end
def teardown
Oj.default_options = @default_options
end
def test_set_options
orig = Oj.default_options()
alt ={
indent: " - ",
second_precision: 5,
circular: true,
class_cache: false,
auto_define: true,
symbol_keys: true,
bigdecimal_as_decimal: false,
use_to_json: false,
use_to_hash: false,
use_as_json: false,
use_raw_json: false,
nilnil: true,
empty_string: true,
allow_gc: false,
quirks_mode: false,
allow_invalid_unicode: true,
float_precision: 13,
mode: :strict,
escape_mode: :ascii,
time_format: :unix_zone,
bigdecimal_load: :float,
compat_bigdecimal: true,
create_id: 'classy',
create_additions: true,
space: 'z',
array_nl: 'a',
object_nl: 'o',
space_before: 'b',
nan: :huge,
hash_class: Hash,
omit_nil: false,
allow_nan: true,
integer_range: nil,
array_class: Array,
ignore: nil,
ignore_under: true,
trace: true,
safe: true,
}
Oj.default_options = alt
#keys = alt.keys
#Oj.default_options.keys.each { |k| puts k unless keys.include? k}
opts = Oj.default_options()
assert_equal(alt, opts);
Oj.default_options = orig # return to original
# verify back to original
opts = Oj.default_options()
assert_equal(orig, opts);
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_float_parse
Oj.default_options = { :float_precision => 16, :bigdecimal_load => :auto }
n = Oj.load('0.00001234567890123456')
assert_equal(Float, n.class)
assert_equal('1.234567890123456e-05', "%0.15e" % [n])
n = Oj.load('-0.00001234567890123456')
assert_equal(Float, n.class)
assert_equal('-1.234567890123456e-05', "%0.15e" % [n])
n = Oj.load('1000.0000123456789')
assert_equal(BigDecimal, n.class)
assert_equal('0.10000000123456789E4', n.to_s.upcase)
n = Oj.load('-0.000012345678901234567')
assert_equal(BigDecimal, n.class)
assert_equal('-0.12345678901234567E-4', n.to_s.upcase)
end
=begin
# TBD move to custom
def test_float_dump
Oj.default_options = { :float_precision => 16 }
assert_equal('1405460727.723866', Oj.dump(1405460727.723866))
Oj.default_options = { :float_precision => 5 }
assert_equal('1.4055', Oj.dump(1.405460727))
Oj.default_options = { :float_precision => 0 }
assert_equal('1405460727.723866', Oj.dump(1405460727.723866))
Oj.default_options = { :float_precision => 15 }
assert_equal('0.56', Oj.dump(0.56))
assert_equal('0.5773', Oj.dump(0.5773))
assert_equal('0.6768', Oj.dump(0.6768))
assert_equal('0.685', Oj.dump(0.685))
assert_equal('0.7032', Oj.dump(0.7032))
assert_equal('0.7051', Oj.dump(0.7051))
assert_equal('0.8274', Oj.dump(0.8274))
assert_equal('0.9149', Oj.dump(0.9149))
assert_equal('64.4', Oj.dump(64.4))
assert_equal('71.6', Oj.dump(71.6))
assert_equal('73.4', Oj.dump(73.4))
assert_equal('80.6', Oj.dump(80.6))
assert_equal('-95.640172', Oj.dump(-95.640172))
end
=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)
assert_equal("a\u0000a", dump_and_load("a\u0000a", false))
end
def test_encode
opts = Oj.default_options
Oj.default_options = { :ascii_only => false }
dump_and_load("ぴーたー", false)
Oj.default_options = { :ascii_only => true }
json = Oj.dump("ぴーたー")
assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
dump_and_load("ぴーたー", false)
Oj.default_options = opts
end
def test_unicode
# hits the 3 normal ranges and one extended surrogate pair
json = %{"\\u019f\\u05e9\\u3074\\ud834\\udd1e"}
obj = Oj.load(json)
json2 = Oj.dump(obj, :ascii_only => true)
assert_equal(json, json2)
end
def test_invalid_unicode_raise
# validate that an invalid unicode raises unless the :allow_invalid_unicode is true
json = %{"x\\ud83dy"}
begin
Oj.load(json)
rescue Exception
assert(true)
return
end
assert(false, "*** expected an exception")
end
def test_invalid_unicode_ok
# validate that an invalid unicode raises unless the :allow_invalid_unicode is true
json = %{"x\\ud83dy"}
obj = Oj.load(json, :allow_invalid_unicode => true)
# The same as what ruby would do with the invalid encoding.
assert_equal("x\xED\xA0\xBDy", obj.to_s)
end
def test_dump_options
json = Oj.dump({ 'a' => 1, 'b' => [true, false]},
:mode => :compat,
:indent => "--",
:array_nl => "\n",
:object_nl => "#\n",
:space => "*",
:space_before => "~")
assert(%{{#
--"a"~:*1,#
--"b"~:*[
----true,
----false
--]#
}} == json ||
%{{#
--"b"~:*[
----true,
----false
--],#
--"a"~:*1#
}} == json)
end
def test_null_char
assert_raises(Oj::ParseError) { Oj.load("\"\0\"") }
assert_raises(Oj::ParseError) { Oj.load("\"\\\0\"") }
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
def test_array_not_closed
begin
Oj.load('[')
rescue Exception
assert(true)
return
end
assert(false, "*** expected an exception")
end
# multiple JSON in one string
def test_multiple_json_callback
json = %{{"a":1}
[1,2][3,4]
{"b":2}
}
results = []
Oj.load(json, :mode => :strict) { |x, start, len| results << [x, start, len] }
assert_equal([[{"a"=>1}, 0, 7], [[1,2], 7, 6], [[3,4], 13, 5], [{"b"=>2}, 18, 8]], results)
end
def test_multiple_json_no_callback
json = %{{"a":1}
[1,2][3,4]
{"b":2}
}
assert_raises(Oj::ParseError) { Oj.load(json) }
end
# encoding tests
def test_does_not_escape_entities_by_default
Oj.default_options = { :escape_mode => :ascii } # set in mimic mode
# use Oj to create the hash since some Rubies don't deal nicely with unicode.
json = %{{"key":"I <3 this\\u2028space"}}
hash = Oj.load(json)
out = Oj.dump(hash)
assert_equal(json, out)
end
def test_escapes_entities_by_default_when_configured_to_do_so
hash = {'key' => "I <3 this"}
Oj.default_options = {:escape_mode => :xss_safe}
out = Oj.dump hash
assert_equal(%{{"key":"I \\u003c3 this"}}, out)
end
def test_escapes_entities_when_asked_to
hash = {'key' => "I <3 this"}
out = Oj.dump(hash, :escape_mode => :xss_safe)
assert_equal(%{{"key":"I \\u003c3 this"}}, out)
end
def test_does_not_escape_entities_when_not_asked_to
hash = {'key' => "I <3 this"}
out = Oj.dump(hash, :escape_mode => :json)
assert_equal(%{{"key":"I <3 this"}}, out)
end
def test_escapes_common_xss_vectors
hash = {'key' => "<script>alert(123) && formatHD()</script>"}
out = Oj.dump(hash, :escape_mode => :xss_safe)
assert_equal(%{{"key":"\\u003cscript\\u003ealert(123) \\u0026\\u0026 formatHD()\\u003c\\/script\\u003e"}}, out)
end
def test_escape_newline_by_default
Oj.default_options = { :escape_mode => :json }
json = %{["one","two\\n2"]}
x = Oj.load(json)
out = Oj.dump(x)
assert_equal(json, out)
end
def test_does_not_escape_newline
Oj.default_options = { :escape_mode => :newline }
json = %{["one","two\n2"]}
x = Oj.load(json)
out = Oj.dump(x)
assert_equal(json, out)
end
def test_dump_invalid_utf8
Oj.default_options = { :escape_mode => :ascii }
assert_raises(EncodingError) {
Oj.dump(["abc\xbe\x1f\x11"], mode: :strict)
}
end
# Symbol
def test_symbol_null
json = Oj.dump(:abc, :mode => :null)
assert_equal('"abc"', json)
end
# Time
def test_time_null
t = Time.local(2012, 1, 5, 23, 58, 7)
json = Oj.dump(t, :mode => :null)
assert_equal('null', json)
end
def test_time_neg
t = Time.parse("1900-01-01 00:18:59 UTC")
json = Oj.dump(t, :mode => :custom, :time_format => :unix)
assert_equal('-2208987661.000000000', json)
end
def test_time_years
(-2020..2020).each { |year|
s = "%04d-03-01T15:14:13Z" % [year]
json = Oj.dump(Time.parse(s), mode: :custom, time_format: :xmlschema, second_precision: -1)
assert_equal(s, json[1..-2])
json = Oj.dump(Time.parse(s), mode: :custom, time_format: :xmlschema, second_precision: 3)
assert_equal(s[0..-2] + '.000Z', json[1..-2])
}
end
# Class
def test_class_null
json = Oj.dump(Juice, :mode => :null)
assert_equal('null', json)
end
# Module
def test_module_null
json = Oj.dump(TestModule, :mode => :null)
assert_equal('null', json)
end
# Hash
def test_non_str_hash_null
begin
Oj.dump({ 1 => true, 0 => false }, :mode => :null)
rescue Exception
assert(true)
return
end
assert(false, "*** expected an exception")
end
def test_hash_not_closed
begin
Oj.load('{')
rescue Exception
assert(true)
return
end
assert(false, "*** expected an exception")
end
# Object with to_json()
def test_json_object_null
obj = Jeez.new(true, 58)
json = Oj.dump(obj, :mode => :null)
assert_equal('null', json)
end
# Object with to_hash()
def test_to_hash_object_null
obj = Jazz.new(true, 58)
json = Oj.dump(obj, :mode => :null)
assert_equal('null', json)
end
# Object with as_json() # contributed by sauliusg
def test_as_json_object_null
obj = Orange.new(true, 58)
json = Oj.dump(obj, :mode => :null)
assert_equal('null', json)
end
# Object without to_json() or to_hash()
def test_object_null
obj = Jam.new(true, 58)
json = Oj.dump(obj, :mode => :null)
assert_equal('null', json)
end
# Range
def test_range_null
json = Oj.dump(1..7, :mode => :null)
assert_equal('null', json)
end
# BigNum
def test_bignum_null
json = Oj.dump(7 ** 55, :mode => :null)
assert_equal('30226801971775055948247051683954096612865741943', json)
end
def test_bignum_object
dump_and_load(7 ** 55, false)
dump_and_load(10 ** 19, false)
end
# BigDecimal
def test_bigdecimal_null
mode = Oj.default_options[:mode]
Oj.default_options = {:mode => :null}
dump_and_load(BigDecimal('3.14159265358979323846'), false)
Oj.default_options = {:mode => mode}
end
def test_infinity
n = Oj.load('Infinity', :mode => :object)
assert_equal(BigDecimal('Infinity').to_f, n);
x = Oj.load('Infinity', :mode => :compat)
assert_equal('Infinity', x.to_s)
end
# Date
def test_date_null
json = Oj.dump(Date.new(2012, 6, 19), :mode => :null)
assert_equal('null', json)
end
# DateTime
def test_datetime_null
json = Oj.dump(DateTime.new(2012, 6, 19, 20, 19, 27), :mode => :null)
assert_equal('null', json)
end
# autodefine Oj::Bag
def test_bag
json = %{{
"^o":"Juice::Jem",
"x":true,
"y":58 }}
obj = Oj.load(json, :mode => :object, :auto_define => true)
assert_equal('Juice::Jem', obj.class.name)
assert_equal(true, obj.x)
assert_equal(58, obj.y)
end
# Stream Deeply Nested
def test_deep_nest_dump
begin
a = []
10000.times { a << [a] }
Oj.dump(a)
rescue Exception
assert(true)
return
end
assert(false, "*** expected an exception")
end
# Stream IO
def test_io_string
src = { 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}
output = StringIO.open("", "w+")
Oj.to_stream(output, src)
input = StringIO.new(output.string())
obj = Oj.load(input, :mode => :strict)
assert_equal(src, obj)
end
def test_io_file
src = { 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}
filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
File.open(filename, "w") { |f|
Oj.to_stream(f, src)
}
f = File.new(filename)
obj = Oj.load(f, :mode => :strict)
f.close()
assert_equal(src, obj)
end
# comments
def test_comment_slash
json = %{{
"x":true,//three
"y":58,
"z": [1,2,
3 // six
]}
}
obj = Oj.load(json, :mode => :strict)
assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
end
def test_comment_c
json = %{/*before*/
{
"x"/*one*/:/*two*/true,
"y":58,
"z": [1,2,3]}
}
obj = Oj.load(json, :mode => :strict)
assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
end
def test_comment
json = %{{
"x"/*one*/:/*two*/true,//three
"y":58/*four*/,
"z": [1,2/*five*/,
3 // six
]
}
}
obj = Oj.load(json, :mode => :strict)
assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
end
def test_nilnil_false
begin
Oj.load(nil, :nilnil => false)
rescue Exception
assert(true)
return
end
assert(false, "*** expected an exception")
end
def test_nilnil_true
obj = Oj.load(nil, :nilnil => true)
assert_nil(obj)
end
def test_empty_string_true
result = Oj.load(gen_whitespaced_string, :empty_string => true, mode: :strict)
assert_nil(result)
end
def test_empty_string_false
# Could be either a Oj::ParseError or an EncodingError depending on
# whether mimic_JSON has been called. Since we don't know when the test
# will be called either is okay.
begin
Oj.load(gen_whitespaced_string, :empty_string => false)
rescue Exception => e
assert(Oj::ParseError == e.class || EncodingError == e.class)
return
end
assert(false, "*** expected an exception")
end
def test_quirks_null_mode
assert_raises(Oj::ParseError) { Oj.load("null", :quirks_mode => false) }
assert_nil(Oj.load("null", :quirks_mode => true))
end
def test_quirks_bool_mode
assert_raises(Oj::ParseError) { Oj.load("true", :quirks_mode => false) }
assert_equal(true, Oj.load("true", :quirks_mode => true))
end
def test_quirks_number_mode
assert_raises(Oj::ParseError) { Oj.load("123", :quirks_mode => false) }
assert_equal(123, Oj.load("123", :quirks_mode => true))
end
def test_quirks_decimal_mode
assert_raises(Oj::ParseError) { Oj.load("123.45", :quirks_mode => false) }
assert_equal(123.45, Oj.load("123.45", :quirks_mode => true))
end
def test_quirks_string_mode
assert_raises(Oj::ParseError) { Oj.load('"string"', :quirks_mode => false) }
assert_equal('string', Oj.load('"string"', :quirks_mode => true))
end
def test_error_path
msg = ''
assert_raises(Oj::ParseError) {
begin
Oj.load(%|{
"first": [
1, 2, { "third": 123x }
]
}|)
rescue Oj::ParseError => e
msg = e.message
raise e
end
}
assert_equal('after first[2].third', msg.split('(')[1].split(')')[0])
end
def test_bad_bignum
if '2.4.0' < RUBY_VERSION
assert_raises Oj::ParseError do
Oj.load(%|{ "big": -e123456789 }|, mode: :strict)
end
end
end
def test_quirks_array_mode
assert_equal([], Oj.load("[]", :quirks_mode => false))
assert_equal([], Oj.load("[]", :quirks_mode => true))
end
def test_quirks_object_mode
assert_equal({}, Oj.load("{}", :quirks_mode => false))
assert_equal({}, Oj.load("{}", :quirks_mode => true))
end
def test_omit_nil
jam = Jam.new({'a' => 1, 'b' => nil }, nil)
json = Oj.dump(jam, :omit_nil => true, :mode => :object)
assert_equal(%|{"^o":"Juice::Jam","x":{"a":1}}|, json)
json = Oj.dump({'x' => {'a' => 1, 'b' => nil }, 'y' => nil}, :omit_nil => true, :mode => :strict)
assert_equal(%|{"x":{"a":1}}|, json)
json = Oj.dump({'x' => {'a' => 1, 'b' => nil }, 'y' => nil}, :omit_nil => true, :mode => :null)
assert_equal(%|{"x":{"a":1}}|, json)
end
def dump_and_load(obj, trace=false)
json = Oj.dump(obj, :indent => 2)
puts json if trace
loaded = Oj.load(json)
if obj.nil?
assert_nil(loaded)
else
assert_equal(obj, loaded)
end
loaded
end
end