File: test_compat.rb

package info (click to toggle)
ruby-oj 2.17.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 968 kB
  • ctags: 1,672
  • sloc: ansic: 10,658; ruby: 6,524; makefile: 2
file content (393 lines) | stat: -rwxr-xr-x 10,347 bytes parent folder | download
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
#!/usr/bin/env ruby
# encoding: UTF-8

$: << File.dirname(__FILE__)

require 'helper'

class CompatJuice < Minitest::Test

  class Jeez
    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?

    def as_json()
      {"json_class" => self.class.to_s,"x" => @x,"y" => @y}
    end

    def self.json_create(h)
      self.new(h['x'], h['y'])
    end
  end # Jeez

  module One
    module Two
      module Three
        class Deep

          def initialize()
          end

          def eql?(o)
            self.class == o.class
          end
          alias == eql?

          def to_hash()
            {'json_class' => "#{self.class.name}"}
          end

          def to_json(*a)
            %{{"json_class":"#{self.class.name}"}}
          end

          def self.json_create(h)
            self.new()
          end
        end # Deep
      end # Three
    end # Two
  end # One

  def setup
    @default_options = Oj.default_options
  end

  def teardown
    Oj.default_options = @default_options
  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
    dump_and_load(0.0, false)
    dump_and_load(12345.6789, false)
    dump_and_load(70.35, false)
    dump_and_load(-54321.012, false)
    dump_and_load(1.7775, false)
    dump_and_load(2.5024, false)
    dump_and_load(2.48e16, false)
    dump_and_load(2.48e100 * 1.0e10, false)
    dump_and_load(-2.48e100 * 1.0e10, 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_encode
    opts = Oj.default_options
    Oj.default_options = { :ascii_only => false }
    unless 'jruby' == $ruby
      dump_and_load("ぴーたー", false)
    end
    Oj.default_options = { :ascii_only => true }
    json = Oj.dump("ぴーたー")
    assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
    unless 'jruby' == $ruby
      dump_and_load("ぴーたー", false)
    end
    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_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_deep
    dump_and_load([1,[2,[3,[4,[5,[6,[7,[8,[9,[10,[11,[12,[13,[14,[15,[16,[17,[18,[19,[20]]]]]]]]]]]]]]]]]]]], false)
  end

  # Hash
  def test_hash
    dump_and_load({}, false)
    dump_and_load({ 'true' => true, 'false' => false}, false)
    dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
  end

  def test_hash_deep
    dump_and_load({'1' => {
                      '2' => {
                        '3' => {
                          '4' => {
                            '5' => {
                              '6' => {
                                '7' => {
                                  '8' => {
                                    '9' => {
                                      '10' => {
                                        '11' => {
                                          '12' => {
                                            '13' => {
                                              '14' => {
                                                '15' => {
                                                  '16' => {
                                                    '17' => {
                                                      '18' => {
                                                        '19' => {
                                                          '20' => {}}}}}}}}}}}}}}}}}}}}}, false)
  end

  def test_hash_escaped_key
    json = %{{"a\nb":true,"c\td":false}}
    obj = Oj.compat_load(json)
    assert_equal({"a\nb" => true, "c\td" => false}, obj)
  end

  def test_bignum_object
    dump_and_load(7 ** 55, false)
  end

  # BigDecimal
  def test_bigdecimal_compat
    dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
  end

  def test_bigdecimal_load
    orig = BigDecimal.new('80.51')
    json = Oj.dump(orig, :mode => :compat, :bigdecimal_as_decimal => true)
    bg = Oj.load(json, :mode => :compat, :bigdecimal_load => true)
    assert_equal(BigDecimal, bg.class)
    assert_equal(orig, bg)
  end

  # Time
  def test_time_ruby
    if RUBY_VERSION.start_with?('1.8')
      t = Time.parse('2015-01-05T21:37:07.123456-08:00')
    else
      t = Time.new(2015, 1, 5, 21, 37, 7.123456, -8 * 3600)
    end
    expect = '"' + t.to_s + '"'
    json = Oj.dump(t, :mode => :compat, :time_format => :ruby)
    assert_equal(expect, json)
  end
  def test_time_xml
    if RUBY_VERSION.start_with?('1.8')
      t = Time.parse('2015-01-05T21:37:07.123456-08:00')
    else
      t = Time.new(2015, 1, 5, 21, 37, 7.123456, -8 * 3600)
    end
    json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema, :second_precision => 6)
    assert_equal('"2015-01-05T21:37:07.123456-08:00"', json)
  end
  unless RUBY_VERSION.start_with?('1.8')
    def test_time_xml_12345
      t = Time.new(2015, 1, 5, 21, 37, 7.123456, 12345)
      json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema, :second_precision => 6)
      assert_equal('"2015-01-05T21:37:07.123456+03:25"', json)
    end
  end
  def test_time_unix
    if RUBY_VERSION.start_with?('1.8')
      t = Time.parse('2015-01-05T21:37:07.123456-08:00')
    else
      t = Time.new(2015, 1, 5, 21, 37, 7.123456, -8 * 3600)
    end
    json = Oj.dump(t, :mode => :compat, :time_format => :unix, :second_precision => 6)
    assert_equal('1420522627.123456', json)
  end
  def test_time_unix_zone
    if RUBY_VERSION.start_with?('1.8')
      t = Time.parse('2015-01-05T21:37:07.123456-08:00')
    else
      t = Time.new(2015, 1, 5, 21, 37, 7.123456, -8 * 3600)
    end
    json = Oj.dump(t, :mode => :compat, :time_format => :unix_zone, :second_precision => 6)
    assert_equal('1420522627.123456e-28800', json)
  end
  unless RUBY_VERSION.start_with?('1.8')
    def test_time_unix_zone_12345
      t = Time.new(2015, 1, 5, 21, 37, 7.123456, 12345)
      json = Oj.dump(t, :mode => :compat, :time_format => :unix_zone, :second_precision => 6)
      assert_equal('1420481482.123456e12345', json)
    end
  end
  def test_time_unix_zone_early
    if RUBY_VERSION.start_with?('1.8')
      t = Time.parse('1954-01-05T21:37:07.123456-08:00')
    else
      t = Time.new(1954, 1, 5, 21, 37, 7.123456, -8 * 3600)
    end
    json = Oj.dump(t, :mode => :compat, :time_format => :unix_zone, :second_precision => 6)
    assert_equal('-504469372.876544e-28800', json)
  end

  # Stream IO
  def test_io_string
    json = %{{
  "x":true,
  "y":58,
  "z": [1,2,3]
}
}
    input = StringIO.new(json)
    obj = Oj.compat_load(input)
    assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
  end

  def test_io_file
    filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
    File.open(filename, 'w') { |f| f.write(%{{
  "x":true,
  "y":58,
  "z": [1,2,3]
}
}) }
    f = File.new(filename)
    obj = Oj.compat_load(f)
    f.close()
    assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
  end

  # symbol_keys option
  def test_symbol_keys
    json = %{{
  "x":true,
  "y":58,
  "z": [1,2,3]
}
}
    obj = Oj.compat_load(json, :symbol_keys => true)
    assert_equal({ :x => true, :y => 58, :z => [1, 2, 3]}, obj)
  end

  # comments
  def test_comment_slash
    json = %{{
  "x":true,//three
  "y":58,
  "z": [1,2,
3 // six
]}
}
    obj = Oj.compat_load(json)
    assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
  end

  def test_comment_c
    json = %{{
  "x"/*one*/:/*two*/true,
  "y":58,
  "z": [1,2,3]}
}
    obj = Oj.compat_load(json)
    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.compat_load(json)
    assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
  end

  def test_json_object_compat
    obj = Jeez.new(true, 58)
    Oj.default_options = { :mode => :compat, :use_to_json => true }
    dump_and_load(obj, false)
  end

  def test_json_module_object
    obj = One::Two::Three::Deep.new()
    dump_and_load(obj, false)
  end

  def test_json_object_create_id
    expected = Jeez.new(true, 58)
    json = Oj.dump(expected, :indent => 2, :mode => :compat, :use_to_json => true)
    obj = Oj.compat_load(json)
    assert_equal(expected, obj)
  end

  def test_json_object_bad
    json = %{{"json_class":"CompatJuice::Junk","x":true}}
    begin
      Oj.compat_load(json)
    rescue Exception => e
      assert_equal("Oj::ParseError", e.class().name)
      return
    end
    assert(false, "*** expected an exception")
  end

  def test_json_object_create_cache
    expected = Jeez.new(true, 58)
    json = Oj.dump(expected, :indent => 2, :mode => :compat, :use_to_json => true)
    obj = Oj.compat_load(json, :class_cache => true)
    assert_equal(expected, obj)
    obj = Oj.compat_load(json, :class_cache => false)
    assert_equal(expected, obj)
  end

  def test_json_object_create_id_other
    expected = Jeez.new(true, 58)
    json = Oj.dump(expected, :indent => 2, :mode => :compat, :use_to_json => true)
    json.gsub!('json_class', '_class_')
    obj = Oj.compat_load(json, :create_id => "_class_")
    assert_equal(expected, obj)
  end

  def test_json_object_create_deep
    expected = One::Two::Three::Deep.new()
    json = Oj.dump(expected, :indent => 2, :mode => :compat)
    obj = Oj.compat_load(json)
    assert_equal(expected, obj)
  end

  def dump_and_load(obj, trace=false)
    json = Oj.dump(obj, :indent => 2, :mode => :compat)
    puts json if trace
    loaded = Oj.compat_load(json);
    assert_equal(obj, loaded)
    loaded
  end

end