oj 2.1.2 → 2.1.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of oj might be problematic. Click here for more details.

@@ -1,416 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- # Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
5
- # required. That can be set in the RUBYOPT environment variable.
6
- # export RUBYOPT=-w
7
-
8
- $VERBOSE = true
9
-
10
- $: << File.join(File.dirname(__FILE__), "../lib")
11
- $: << File.join(File.dirname(__FILE__), "../ext")
12
-
13
- require 'test/unit'
14
- require 'oj'
15
-
16
- $json1 = %{{
17
- "array": [
18
- {
19
- "num" : 3,
20
- "string": "message",
21
- "hash" : {
22
- "h2" : {
23
- "a" : [ 1, 2, 3 ]
24
- }
25
- }
26
- }
27
- ],
28
- "boolean" : true
29
- }}
30
-
31
- class DocTest < ::Test::Unit::TestCase
32
- def test_nil
33
- json = %{null}
34
- Oj::Doc.open(json) do |doc|
35
- assert_equal(NilClass, doc.type)
36
- assert_equal(nil, doc.fetch())
37
- end
38
- end
39
-
40
- def test_true
41
- json = %{true}
42
- Oj::Doc.open(json) do |doc|
43
- assert_equal(TrueClass, doc.type)
44
- assert_equal(true, doc.fetch())
45
- end
46
- end
47
-
48
- def test_false
49
- json = %{false}
50
- Oj::Doc.open(json) do |doc|
51
- assert_equal(FalseClass, doc.type)
52
- assert_equal(false, doc.fetch())
53
- end
54
- end
55
-
56
- def test_string
57
- json = %{"a string"}
58
- Oj::Doc.open(json) do |doc|
59
- assert_equal(String, doc.type)
60
- assert_equal('a string', doc.fetch())
61
- end
62
- end
63
-
64
- def test_fixnum
65
- json = %{12345}
66
- Oj::Doc.open(json) do |doc|
67
- assert_equal(Fixnum, doc.type)
68
- assert_equal(12345, doc.fetch())
69
- end
70
- end
71
-
72
- def test_float
73
- json = %{12345.6789}
74
- Oj::Doc.open(json) do |doc|
75
- assert_equal(Float, doc.type)
76
- assert_equal(12345.6789, doc.fetch())
77
- end
78
- end
79
-
80
- def test_float_exp
81
- json = %{12345.6789e7}
82
- Oj::Doc.open(json) do |doc|
83
- assert_equal(Float, doc.type)
84
- #assert_equal(12345.6789e7, doc.fetch())
85
- assert_equal(12345.6789e7.to_i, doc.fetch().to_i)
86
- end
87
- end
88
-
89
- def test_array_empty
90
- json = %{[]}
91
- Oj::Doc.open(json) do |doc|
92
- assert_equal(Array, doc.type)
93
- assert_equal([], doc.fetch())
94
- end
95
- end
96
-
97
- def test_array
98
- json = %{[true,false]}
99
- Oj::Doc.open(json) do |doc|
100
- assert_equal(Array, doc.type)
101
- assert_equal([true, false], doc.fetch())
102
- end
103
- end
104
-
105
- def test_hash_empty
106
- json = %{{}}
107
- Oj::Doc.open(json) do |doc|
108
- assert_equal(Hash, doc.type)
109
- assert_equal({}, doc.fetch())
110
- end
111
- end
112
-
113
- def test_hash
114
- json = %{{"one":true,"two":false}}
115
- Oj::Doc.open(json) do |doc|
116
- assert_equal(Hash, doc.type)
117
- assert_equal({'one' => true, 'two' => false}, doc.fetch())
118
- end
119
- end
120
-
121
- # move() and where?()
122
- def test_move_hash
123
- json = %{{"one":{"two":false}}}
124
- Oj::Doc.open(json) do |doc|
125
- doc.move('/one')
126
- assert_equal('/one', doc.where?)
127
- doc.move('/one/two')
128
- assert_equal('/one/two', doc.where?)
129
- end
130
- end
131
-
132
- def test_move_array
133
- json = %{[1,[2,true]]}
134
- Oj::Doc.open(json) do |doc|
135
- doc.move('/1')
136
- assert_equal('/1', doc.where?)
137
- doc.move('/2/1')
138
- assert_equal('/2/1', doc.where?)
139
- end
140
- end
141
-
142
- def test_move
143
- Oj::Doc.open($json1) do |doc|
144
- [ '/',
145
- '/array',
146
- '/boolean',
147
- '/array/1/hash/h2/a/3',
148
- ].each do |p|
149
- doc.move(p)
150
- assert_equal(p, doc.where?)
151
- end
152
- begin
153
- doc.move('/array/x')
154
- rescue Exception
155
- assert_equal('/', doc.where?)
156
- assert(true)
157
- end
158
- end
159
- end
160
-
161
- def test_move_relative
162
- Oj::Doc.open($json1) do |doc|
163
- [['/', 'array', '/array'],
164
- ['/array', '1/num', '/array/1/num'],
165
- ['/array/1/hash', 'h2/a', '/array/1/hash/h2/a'],
166
- ['/array/1', 'hash/h2/a/2', '/array/1/hash/h2/a/2'],
167
- ['/array/1/hash', '../string', '/array/1/string'],
168
- ['/array/1/hash', '..', '/array/1'],
169
- ].each do |start,path,where|
170
- doc.move(start)
171
- doc.move(path)
172
- assert_equal(where, doc.where?)
173
- end
174
- end
175
- end
176
-
177
- def test_type
178
- Oj::Doc.open($json1) do |doc|
179
- [['/', Hash],
180
- ['/array', Array],
181
- ['/array/1', Hash],
182
- ['/array/1/num', Fixnum],
183
- ['/array/1/string', String],
184
- ['/array/1/hash/h2/a', Array],
185
- ['/array/1/hash/../num', Fixnum],
186
- ['/array/1/hash/../..', Array],
187
- ].each do |path,type|
188
- assert_equal(type, doc.type(path))
189
- end
190
- end
191
- end
192
-
193
- def test_local_key
194
- Oj::Doc.open($json1) do |doc|
195
- [['/', nil],
196
- ['/array', 'array'],
197
- ['/array/1', 1],
198
- ['/array/1/num', 'num'],
199
- ['/array/1/string', 'string'],
200
- ['/array/1/hash/h2/a', 'a'],
201
- ['/array/1/hash/../num', 'num'],
202
- ['/array/1/hash/..', 1],
203
- ['/array/1/hash/../..', 'array'],
204
- ].each do |path,key|
205
- doc.move(path)
206
- assert_equal(key, doc.local_key())
207
- end
208
- end
209
- end
210
-
211
- def test_fetch_move
212
- Oj::Doc.open($json1) do |doc|
213
- [['/array/1/num', 3],
214
- ['/array/1/string', 'message'],
215
- ['/array/1/hash/h2/a', [1, 2, 3]],
216
- ['/array/1/hash/../num', 3],
217
- ['/array/1/hash/..', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
218
- ['/array/1/hash/../..', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
219
- ['/array/1', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
220
- ['/array', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
221
- ['/', {'array' => [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}], 'boolean' => true}],
222
- ].each do |path,val|
223
- doc.move(path)
224
- assert_equal(val, doc.fetch())
225
- end
226
- end
227
- end
228
-
229
- def test_fetch_path
230
- Oj::Doc.open($json1) do |doc|
231
- [['/array/1/num', 3],
232
- ['/array/1/string', 'message'],
233
- ['/array/1/hash/h2/a', [1, 2, 3]],
234
- ['/array/1/hash/../num', 3],
235
- ['/array/1/hash/..', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
236
- ['/array/1/hash/../..', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
237
- ['/array/1', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
238
- ['/array', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
239
- ['/', {'array' => [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}], 'boolean' => true}],
240
- ].each do |path,val|
241
- assert_equal(val, doc.fetch(path))
242
- end
243
- end
244
- end
245
-
246
- def test_home
247
- Oj::Doc.open($json1) do |doc|
248
- doc.move('/array/1/num')
249
- doc.home()
250
- assert_equal('/', doc.where?)
251
- end
252
- end
253
-
254
- def test_each_value_root
255
- Oj::Doc.open($json1) do |doc|
256
- values = []
257
- doc.each_value() { |v| values << v.to_s }
258
- assert_equal(['1', '2', '3', '3', 'message', 'true'], values.sort)
259
- end
260
- end
261
-
262
- def test_each_value_move
263
- Oj::Doc.open($json1) do |doc|
264
- doc.move('/array/1/hash')
265
- values = []
266
- doc.each_value() { |v| values << v.to_s }
267
- assert_equal(['1', '2', '3'], values.sort)
268
- end
269
- end
270
-
271
- def test_each_value_path
272
- Oj::Doc.open($json1) do |doc|
273
- values = []
274
- doc.each_value('/array/1/hash') { |v| values << v.to_s }
275
- assert_equal(['1', '2', '3'], values.sort)
276
- end
277
- end
278
-
279
- def test_each_child_move
280
- Oj::Doc.open($json1) do |doc|
281
- locations = []
282
- doc.move('/array/1/hash/h2/a')
283
- doc.each_child() { |d| locations << d.where? }
284
- assert_equal(['/array/1/hash/h2/a/1', '/array/1/hash/h2/a/2', '/array/1/hash/h2/a/3'], locations)
285
- locations = []
286
- doc.move('/array/1')
287
- doc.each_child() { |d| locations << d.where? }
288
- assert_equal(['/array/1/num', '/array/1/string', '/array/1/hash'], locations)
289
- end
290
- end
291
-
292
- def test_each_child_path
293
- Oj::Doc.open($json1) do |doc|
294
- locations = []
295
- doc.each_child('/array/1/hash/h2/a') { |d| locations << d.where? }
296
- assert_equal(['/array/1/hash/h2/a/1', '/array/1/hash/h2/a/2', '/array/1/hash/h2/a/3'], locations)
297
- locations = []
298
- doc.each_child('/array/1') { |d| locations << d.where? }
299
- assert_equal(['/array/1/num', '/array/1/string', '/array/1/hash'], locations)
300
- end
301
- end
302
-
303
- def test_size
304
- Oj::Doc.open('[1,2,3]') do |doc|
305
- assert_equal(4, doc.size)
306
- end
307
- Oj::Doc.open('{"a":[1,2,3]}') do |doc|
308
- assert_equal(5, doc.size)
309
- end
310
- end
311
-
312
- def test_open_file
313
- filename = 'open_file_test.json'
314
- File.open(filename, 'w') { |f| f.write('{"a":[1,2,3]}') }
315
- Oj::Doc.open_file(filename) do |doc|
316
- assert_equal(5, doc.size)
317
- end
318
- end
319
-
320
- def test_open_close
321
- json = %{{"a":[1,2,3]}}
322
- doc = Oj::Doc.open(json)
323
- assert_equal(Oj::Doc, doc.class)
324
- assert_equal(5, doc.size)
325
- assert_equal('/', doc.where?)
326
- doc.move('a/1')
327
- doc.home()
328
- assert_equal(2, doc.fetch('/a/2'))
329
- assert_equal(2, doc.fetch('a/2'))
330
- doc.close()
331
- begin
332
- doc.home()
333
- rescue Exception
334
- assert(true)
335
- end
336
- end
337
-
338
- def test_file_open_close
339
- filename = 'open_file_test.json'
340
- File.open(filename, 'w') { |f| f.write('{"a":[1,2,3]}') }
341
- doc = Oj::Doc.open_file(filename)
342
- assert_equal(Oj::Doc, doc.class)
343
- assert_equal(5, doc.size)
344
- assert_equal('/', doc.where?)
345
- doc.move('a/1')
346
- doc.home()
347
- assert_equal(2, doc.fetch('/a/2'))
348
- assert_equal(2, doc.fetch('a/2'))
349
- doc.close()
350
- begin
351
- doc.home()
352
- rescue Exception
353
- assert(true)
354
- end
355
- end
356
-
357
- def test_open_no_close
358
- json = %{{"a":[1,2,3]}}
359
- doc = Oj::Doc.open(json)
360
- assert_equal(Oj::Doc, doc.class)
361
- assert_equal(5, doc.size)
362
- assert_equal('/', doc.where?)
363
- doc.move('a/1')
364
- doc.home()
365
- assert_equal(2, doc.fetch('/a/2'))
366
- assert_equal(2, doc.fetch('a/2'))
367
- doc = nil
368
- GC.start
369
- # a print statement confirms close is called
370
- end
371
-
372
- def test_dump
373
- Oj::Doc.open('[1,[2,3]]') do |doc|
374
- assert_equal('[1,[2,3]]', doc.dump())
375
- end
376
- Oj::Doc.open('[1,[2,3]]') do |doc|
377
- assert_equal('[2,3]', doc.dump('/2'))
378
- end
379
- end
380
-
381
- def test_each_leaf
382
- results = Oj::Doc.open('[1,[2,3]]') do |doc|
383
- h = {}
384
- doc.each_leaf() { |d| h[d.where?] = d.fetch() }
385
- h
386
- end
387
- assert_equal({'/1' => 1, '/2/1' => 2, '/2/2' => 3}, results)
388
- end
389
-
390
- def test_each_leaf_hash
391
- results = Oj::Doc.open('{"a":{"x":2},"b":{"y":4}}') do |doc|
392
- h = {}
393
- doc.each_leaf() { |d| h[d.where?] = d.fetch() }
394
- h
395
- end
396
- assert_equal({'/a/x' => 2, '/b/y' => 4}, results)
397
- end
398
-
399
- def test_comment
400
- json = %{{
401
- "x"/*one*/:/*two*/true,//three
402
- "y":58/*four*/,
403
- "z": [1,2/*five*/,
404
- 3 // six
405
- ]
406
- }
407
- }
408
- results = Oj::Doc.open(json) do |doc|
409
- h = {}
410
- doc.each_leaf() { |d| h[d.where?] = d.fetch() }
411
- h
412
- end
413
- assert_equal({'/x' => true, '/y' => 58, '/z/1' => 1, '/z/2' => 2, '/z/3' => 3}, results)
414
- end
415
-
416
- end # DocTest
@@ -1,208 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- # Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
5
- # required. That can be set in the RUBYOPT environment variable.
6
- # export RUBYOPT=-w
7
-
8
- $VERBOSE = true
9
-
10
- $: << File.join(File.dirname(__FILE__), "../lib")
11
- $: << File.join(File.dirname(__FILE__), "../ext")
12
-
13
- require 'test/unit'
14
- require 'stringio'
15
- require 'oj'
16
-
17
- $ruby = RUBY_DESCRIPTION.split(' ')[0]
18
- $ruby = 'ree' if 'ruby' == $ruby && RUBY_DESCRIPTION.include?('Ruby Enterprise Edition')
19
-
20
- class Jam
21
- attr_accessor :x, :y
22
-
23
- def initialize(x, y)
24
- @x = x
25
- @y = y
26
- end
27
-
28
- def eql?(o)
29
- self.class == o.class && @x == o.x && @y == o.y
30
- end
31
- alias == eql?
32
-
33
- def to_json()
34
- %{{"json_class":"#{self.class}","x":#{@x},"y":#{@y}}}
35
- end
36
-
37
- def self.json_create(h)
38
- self.new(h['x'], h['y'])
39
- end
40
-
41
- end # Jam
42
-
43
- class Mimic < ::Test::Unit::TestCase
44
-
45
- def test0_mimic_json
46
- assert(defined?(JSON).nil?)
47
- Oj.mimic_JSON
48
- assert(!defined?(JSON).nil?)
49
- end
50
-
51
- # dump
52
- def test_dump_string
53
- json = JSON.dump([1, true, nil])
54
- assert_equal(%{[1,true,null]}, json)
55
- end
56
-
57
- def test_dump_io
58
- s = StringIO.new()
59
- json = JSON.dump([1, true, nil], s)
60
- assert_equal(s, json)
61
- assert_equal(%{[1,true,null]}, s.string)
62
- end
63
- # TBD options
64
-
65
- # load
66
- def test_load_string
67
- json = %{{"a":1,"b":[true,false]}}
68
- obj = JSON.load(json)
69
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
70
- end
71
-
72
- def test_load_io
73
- json = %{{"a":1,"b":[true,false]}}
74
- obj = JSON.load(StringIO.new(json))
75
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
76
- end
77
-
78
- def test_load_proc
79
- Oj.mimic_JSON # TBD
80
- children = []
81
- json = %{{"a":1,"b":[true,false]}}
82
- if 'rubinius' == $ruby || 'jruby' == $ruby || '1.8.7' == RUBY_VERSION
83
- obj = JSON.load(json) {|x| children << x }
84
- else
85
- p = Proc.new {|x| children << x }
86
- obj = JSON.load(json, p)
87
- end
88
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
89
- # JRuby 1.7.0 rb_yield() is broken and converts the [true, falser] array into true
90
- unless 'jruby' == $ruby && '1.9.3' == RUBY_VERSION
91
- assert([1, true, false, [true, false], { 'a' => 1, 'b' => [true, false]}] == children ||
92
- [true, false, [true, false], 1, { 'a' => 1, 'b' => [true, false]}] == children,
93
- "children don't match")
94
- end
95
- end
96
-
97
- # []
98
- def test_bracket_load
99
- json = %{{"a":1,"b":[true,false]}}
100
- obj = JSON[json]
101
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
102
- end
103
-
104
- def test_bracket_dump
105
- json = JSON[[1, true, nil]]
106
- assert_equal(%{[1,true,null]}, json)
107
- end
108
-
109
- # generate
110
- def test_generate
111
- json = JSON.generate({ 'a' => 1, 'b' => [true, false]})
112
- assert(%{{"a":1,"b":[true,false]}} == json ||
113
- %{{"b":[true,false],"a":1}} == json)
114
- end
115
- def test_generate_options
116
- json = JSON.generate({ 'a' => 1, 'b' => [true, false]},
117
- :indent => "--",
118
- :array_nl => "\n",
119
- :object_nl => "#\n",
120
- :space => "*",
121
- :space_before => "~")
122
- assert(%{{#
123
- --"a"~:*1,#
124
- --"b"~:*[
125
- ----true,
126
- ----false
127
- --]#
128
- }} == json ||
129
- %{{#
130
- --"b"~:*[
131
- ----true,
132
- ----false
133
- --],#
134
- --"a"~:*1#
135
- }} == json)
136
-
137
- end
138
-
139
- # fast_generate
140
- def test_fast_generate
141
- json = JSON.generate({ 'a' => 1, 'b' => [true, false]})
142
- assert(%{{"a":1,"b":[true,false]}} == json ||
143
- %{{"b":[true,false],"a":1}} == json)
144
- end
145
-
146
- # pretty_generate
147
- def test_pretty_generate
148
- json = JSON.pretty_generate({ 'a' => 1, 'b' => [true, false]})
149
- assert(%{{
150
- "a" : 1,
151
- "b" : [
152
- true,
153
- false
154
- ]
155
- }} == json ||
156
- %{{
157
- "b" : [
158
- true,
159
- false
160
- ],
161
- "a" : 1
162
- }} == json)
163
- end
164
-
165
- # parse
166
- def test_parse
167
- json = %{{"a":1,"b":[true,false]}}
168
- obj = JSON.parse(json)
169
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
170
- end
171
- def test_parse_sym_names
172
- json = %{{"a":1,"b":[true,false]}}
173
- obj = JSON.parse(json, :symbolize_names => true)
174
- assert_equal({ :a => 1, :b => [true, false]}, obj)
175
- end
176
- def test_parse_additions
177
- jam = Jam.new(true, 58)
178
- json = Oj.dump(jam, :mode => :compat)
179
- obj = JSON.parse(json)
180
- assert_equal(jam, obj)
181
- obj = JSON.parse(json, :create_additions => true)
182
- assert_equal(jam, obj)
183
- obj = JSON.parse(json, :create_additions => false)
184
- assert_equal({'json_class' => 'Jam', 'x' => true, 'y' => 58}, obj)
185
- json.gsub!('json_class', 'kson_class')
186
- JSON.create_id = 'kson_class'
187
- obj = JSON.parse(json, :create_additions => true)
188
- JSON.create_id = 'json_class'
189
- assert_equal(jam, obj)
190
- end
191
- def test_parse_bang
192
- json = %{{"a":1,"b":[true,false]}}
193
- obj = JSON.parse!(json)
194
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
195
- end
196
-
197
- # recurse_proc
198
- def test_recurse_proc
199
- children = []
200
- JSON.recurse_proc({ 'a' => 1, 'b' => [true, false]}) { |x| children << x }
201
- # JRuby 1.7.0 rb_yield() is broken and converts the [true, falser] array into true
202
- unless 'jruby' == $ruby && '1.9.3' == RUBY_VERSION
203
- assert([1, true, false, [true, false], { 'a' => 1, 'b' => [true, false]}] == children ||
204
- [true, false, [true, false], 1, { 'b' => [true, false], 'a' => 1}] == children)
205
- end
206
- end
207
-
208
- end # Mimic