Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 404 lines (364 sloc) 10.372 kB
781f6d5 started on value_at() method
Peter Ohler authored
1 #!/usr/bin/env ruby -wW1
2 # encoding: UTF-8
3
4 $: << File.join(File.dirname(__FILE__), "../lib")
5 $: << File.join(File.dirname(__FILE__), "../ext")
6
7 require 'test/unit'
8 require 'oj'
9
c866d0f changed name from fast to doc
Peter Ohler authored
10 $json1 = %{{
11 "array": [
12 {
13 "num" : 3,
14 "string": "message",
15 "hash" : {
16 "h2" : {
17 "a" : [ 1, 2, 3 ]
18 }
19 }
20 }
21 ],
22 "boolean" : true
23 }}
24
25 class DocTest < ::Test::Unit::TestCase
781f6d5 started on value_at() method
Peter Ohler authored
26 def test_nil
27 json = %{null}
a39194c working on completing tests and navigation functionality
Peter Ohler authored
28 Oj::Doc.open(json) do |doc|
29 assert_equal(NilClass, doc.type)
30 assert_equal(nil, doc.fetch())
781f6d5 started on value_at() method
Peter Ohler authored
31 end
32 end
33
34 def test_true
35 json = %{true}
a39194c working on completing tests and navigation functionality
Peter Ohler authored
36 Oj::Doc.open(json) do |doc|
37 assert_equal(TrueClass, doc.type)
38 assert_equal(true, doc.fetch())
781f6d5 started on value_at() method
Peter Ohler authored
39 end
40 end
41
42 def test_false
43 json = %{false}
a39194c working on completing tests and navigation functionality
Peter Ohler authored
44 Oj::Doc.open(json) do |doc|
45 assert_equal(FalseClass, doc.type)
46 assert_equal(false, doc.fetch())
781f6d5 started on value_at() method
Peter Ohler authored
47 end
48 end
49
50 def test_string
51 json = %{"a string"}
a39194c working on completing tests and navigation functionality
Peter Ohler authored
52 Oj::Doc.open(json) do |doc|
53 assert_equal(String, doc.type)
54 assert_equal('a string', doc.fetch())
781f6d5 started on value_at() method
Peter Ohler authored
55 end
56 end
57
58 def test_fixnum
59 json = %{12345}
a39194c working on completing tests and navigation functionality
Peter Ohler authored
60 Oj::Doc.open(json) do |doc|
61 assert_equal(Fixnum, doc.type)
62 assert_equal(12345, doc.fetch())
781f6d5 started on value_at() method
Peter Ohler authored
63 end
64 end
65
66 def test_float
67 json = %{12345.6789}
a39194c working on completing tests and navigation functionality
Peter Ohler authored
68 Oj::Doc.open(json) do |doc|
69 assert_equal(Float, doc.type)
70 assert_equal(12345.6789, doc.fetch())
781f6d5 started on value_at() method
Peter Ohler authored
71 end
72 end
73
74 def test_float_exp
75 json = %{12345.6789e7}
a39194c working on completing tests and navigation functionality
Peter Ohler authored
76 Oj::Doc.open(json) do |doc|
77 assert_equal(Float, doc.type)
a963a7f fixed bug for Oj::Doc and jruby
Peter Ohler authored
78 #assert_equal(12345.6789e7, doc.fetch())
79 assert_equal(12345.6789e7.to_i, doc.fetch().to_i)
781f6d5 started on value_at() method
Peter Ohler authored
80 end
81 end
82
83 def test_array_empty
84 json = %{[]}
a39194c working on completing tests and navigation functionality
Peter Ohler authored
85 Oj::Doc.open(json) do |doc|
86 assert_equal(Array, doc.type)
87 assert_equal([], doc.fetch())
781f6d5 started on value_at() method
Peter Ohler authored
88 end
89 end
90
91 def test_array
92 json = %{[true,false]}
a39194c working on completing tests and navigation functionality
Peter Ohler authored
93 Oj::Doc.open(json) do |doc|
94 assert_equal(Array, doc.type)
95 assert_equal([true, false], doc.fetch())
781f6d5 started on value_at() method
Peter Ohler authored
96 end
97 end
98
99 def test_hash_empty
100 json = %{{}}
a39194c working on completing tests and navigation functionality
Peter Ohler authored
101 Oj::Doc.open(json) do |doc|
102 assert_equal(Hash, doc.type)
103 assert_equal({}, doc.fetch())
781f6d5 started on value_at() method
Peter Ohler authored
104 end
105 end
106
107 def test_hash
108 json = %{{"one":true,"two":false}}
a39194c working on completing tests and navigation functionality
Peter Ohler authored
109 Oj::Doc.open(json) do |doc|
110 assert_equal(Hash, doc.type)
111 assert_equal({'one' => true, 'two' => false}, doc.fetch())
781f6d5 started on value_at() method
Peter Ohler authored
112 end
113 end
114
60a780b fleshing out the access methods
Peter Ohler authored
115 # move() and where?()
116 def test_move_hash
117 json = %{{"one":{"two":false}}}
a39194c working on completing tests and navigation functionality
Peter Ohler authored
118 Oj::Doc.open(json) do |doc|
119 doc.move('/one')
120 assert_equal('/one', doc.where?)
121 doc.move('/one/two')
122 assert_equal('/one/two', doc.where?)
60a780b fleshing out the access methods
Peter Ohler authored
123 end
124 end
125
126 def test_move_array
127 json = %{[1,[2,true]]}
a39194c working on completing tests and navigation functionality
Peter Ohler authored
128 Oj::Doc.open(json) do |doc|
129 doc.move('/1')
130 assert_equal('/1', doc.where?)
131 doc.move('/2/1')
132 assert_equal('/2/1', doc.where?)
60a780b fleshing out the access methods
Peter Ohler authored
133 end
134 end
135
136 def test_move
a39194c working on completing tests and navigation functionality
Peter Ohler authored
137 Oj::Doc.open($json1) do |doc|
60a780b fleshing out the access methods
Peter Ohler authored
138 [ '/',
139 '/array',
140 '/boolean',
141 '/array/1/hash/h2/a/3',
142 ].each do |p|
a39194c working on completing tests and navigation functionality
Peter Ohler authored
143 doc.move(p)
144 assert_equal(p, doc.where?)
60a780b fleshing out the access methods
Peter Ohler authored
145 end
146 begin
a39194c working on completing tests and navigation functionality
Peter Ohler authored
147 doc.move('/array/x')
60a780b fleshing out the access methods
Peter Ohler authored
148 rescue Exception => e
a39194c working on completing tests and navigation functionality
Peter Ohler authored
149 assert_equal('/', doc.where?)
60a780b fleshing out the access methods
Peter Ohler authored
150 assert(true)
151 end
152 end
153 end
154
c866d0f changed name from fast to doc
Peter Ohler authored
155 def test_move_relative
a39194c working on completing tests and navigation functionality
Peter Ohler authored
156 Oj::Doc.open($json1) do |doc|
c866d0f changed name from fast to doc
Peter Ohler authored
157 [['/', 'array', '/array'],
158 ['/array', '1/num', '/array/1/num'],
159 ['/array/1/hash', 'h2/a', '/array/1/hash/h2/a'],
160 ['/array/1', 'hash/h2/a/2', '/array/1/hash/h2/a/2'],
161 ['/array/1/hash', '../string', '/array/1/string'],
162 ['/array/1/hash', '..', '/array/1'],
163 ].each do |start,path,where|
a39194c working on completing tests and navigation functionality
Peter Ohler authored
164 doc.move(start)
165 doc.move(path)
166 assert_equal(where, doc.where?)
c866d0f changed name from fast to doc
Peter Ohler authored
167 end
168 end
169 end
60a780b fleshing out the access methods
Peter Ohler authored
170
c866d0f changed name from fast to doc
Peter Ohler authored
171 def test_type
a39194c working on completing tests and navigation functionality
Peter Ohler authored
172 Oj::Doc.open($json1) do |doc|
c866d0f changed name from fast to doc
Peter Ohler authored
173 [['/', Hash],
174 ['/array', Array],
175 ['/array/1', Hash],
176 ['/array/1/num', Fixnum],
177 ['/array/1/string', String],
178 ['/array/1/hash/h2/a', Array],
179 ['/array/1/hash/../num', Fixnum],
a39194c working on completing tests and navigation functionality
Peter Ohler authored
180 ['/array/1/hash/../..', Array],
c866d0f changed name from fast to doc
Peter Ohler authored
181 ].each do |path,type|
a39194c working on completing tests and navigation functionality
Peter Ohler authored
182 assert_equal(type, doc.type(path))
c866d0f changed name from fast to doc
Peter Ohler authored
183 end
184 end
185 end
60a780b fleshing out the access methods
Peter Ohler authored
186
a39194c working on completing tests and navigation functionality
Peter Ohler authored
187 def test_local_key
188 Oj::Doc.open($json1) do |doc|
189 [['/', nil],
190 ['/array', 'array'],
191 ['/array/1', 1],
192 ['/array/1/num', 'num'],
193 ['/array/1/string', 'string'],
194 ['/array/1/hash/h2/a', 'a'],
195 ['/array/1/hash/../num', 'num'],
196 ['/array/1/hash/..', 1],
197 ['/array/1/hash/../..', 'array'],
198 ].each do |path,key|
199 doc.move(path)
200 assert_equal(key, doc.local_key())
201 end
202 end
203 end
204
205 def test_fetch_move
206 Oj::Doc.open($json1) do |doc|
207 [['/array/1/num', 3],
208 ['/array/1/string', 'message'],
209 ['/array/1/hash/h2/a', [1, 2, 3]],
210 ['/array/1/hash/../num', 3],
211 ['/array/1/hash/..', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
212 ['/array/1/hash/../..', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
213 ['/array/1', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
214 ['/array', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
215 ['/', {'array' => [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}], 'boolean' => true}],
216 ].each do |path,val|
217 doc.move(path)
218 assert_equal(val, doc.fetch())
219 end
220 end
221 end
222
223 def test_fetch_path
224 Oj::Doc.open($json1) do |doc|
225 [['/array/1/num', 3],
226 ['/array/1/string', 'message'],
227 ['/array/1/hash/h2/a', [1, 2, 3]],
228 ['/array/1/hash/../num', 3],
229 ['/array/1/hash/..', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
230 ['/array/1/hash/../..', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
231 ['/array/1', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
232 ['/array', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
233 ['/', {'array' => [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}], 'boolean' => true}],
234 ].each do |path,val|
235 assert_equal(val, doc.fetch(path))
236 end
237 end
238 end
239
240 def test_home
241 Oj::Doc.open($json1) do |doc|
242 doc.move('/array/1/num')
243 doc.home()
244 assert_equal('/', doc.where?)
245 end
246 end
247
248 def test_each_value_root
249 Oj::Doc.open($json1) do |doc|
250 values = []
251 doc.each_value() { |v| values << v.to_s }
252 assert_equal(['1', '2', '3', '3', 'message', 'true'], values.sort)
253 end
254 end
255
256 def test_each_value_move
257 Oj::Doc.open($json1) do |doc|
258 doc.move('/array/1/hash')
259 values = []
260 doc.each_value() { |v| values << v.to_s }
261 assert_equal(['1', '2', '3'], values.sort)
262 end
263 end
264
265 def test_each_value_path
266 Oj::Doc.open($json1) do |doc|
267 values = []
268 doc.each_value('/array/1/hash') { |v| values << v.to_s }
269 assert_equal(['1', '2', '3'], values.sort)
270 end
271 end
272
f5aae61 docs completed, ready for a release
Peter Ohler authored
273 def test_each_child_move
a39194c working on completing tests and navigation functionality
Peter Ohler authored
274 Oj::Doc.open($json1) do |doc|
275 locations = []
276 doc.move('/array/1/hash/h2/a')
f5aae61 docs completed, ready for a release
Peter Ohler authored
277 doc.each_child() { |d| locations << d.where? }
a39194c working on completing tests and navigation functionality
Peter Ohler authored
278 assert_equal(['/array/1/hash/h2/a/1', '/array/1/hash/h2/a/2', '/array/1/hash/h2/a/3'], locations)
279 locations = []
280 doc.move('/array/1')
f5aae61 docs completed, ready for a release
Peter Ohler authored
281 doc.each_child() { |d| locations << d.where? }
a39194c working on completing tests and navigation functionality
Peter Ohler authored
282 assert_equal(['/array/1/num', '/array/1/string', '/array/1/hash'], locations)
283 end
284 end
285
f5aae61 docs completed, ready for a release
Peter Ohler authored
286 def test_each_child_path
a39194c working on completing tests and navigation functionality
Peter Ohler authored
287 Oj::Doc.open($json1) do |doc|
288 locations = []
f5aae61 docs completed, ready for a release
Peter Ohler authored
289 doc.each_child('/array/1/hash/h2/a') { |d| locations << d.where? }
a39194c working on completing tests and navigation functionality
Peter Ohler authored
290 assert_equal(['/array/1/hash/h2/a/1', '/array/1/hash/h2/a/2', '/array/1/hash/h2/a/3'], locations)
291 locations = []
f5aae61 docs completed, ready for a release
Peter Ohler authored
292 doc.each_child('/array/1') { |d| locations << d.where? }
a39194c working on completing tests and navigation functionality
Peter Ohler authored
293 assert_equal(['/array/1/num', '/array/1/string', '/array/1/hash'], locations)
294 end
295 end
296
f5aae61 docs completed, ready for a release
Peter Ohler authored
297 def test_size
298 Oj::Doc.open('[1,2,3]') do |doc|
299 assert_equal(4, doc.size)
300 end
301 Oj::Doc.open('{"a":[1,2,3]}') do |doc|
302 assert_equal(5, doc.size)
303 end
304 end
305
306 def test_open_file
307 filename = 'open_file_test.json'
308 File.open(filename, 'w') { |f| f.write('{"a":[1,2,3]}') }
309 Oj::Doc.open_file(filename) do |doc|
310 assert_equal(5, doc.size)
311 end
312 end
313
4023697 open and close option fo Oj::Doc implemented but not documented yet.
Peter Ohler authored
314 def test_open_close
315 json = %{{"a":[1,2,3]}}
316 doc = Oj::Doc.open(json)
317 assert_equal(Oj::Doc, doc.class)
318 assert_equal(5, doc.size)
319 assert_equal('/', doc.where?)
12ea8e6 ready for 1.1.0 release
Peter Ohler authored
320 doc.move('a/1')
321 doc.home()
322 assert_equal(2, doc.fetch('/a/2'))
323 assert_equal(2, doc.fetch('a/2'))
324 doc.close()
325 begin
326 doc.home()
327 rescue Exception => e
328 assert(true)
329 end
330 end
331
332 def test_file_open_close
333 filename = 'open_file_test.json'
334 File.open(filename, 'w') { |f| f.write('{"a":[1,2,3]}') }
335 doc = Oj::Doc.open_file(filename)
336 assert_equal(Oj::Doc, doc.class)
337 assert_equal(5, doc.size)
338 assert_equal('/', doc.where?)
4023697 open and close option fo Oj::Doc implemented but not documented yet.
Peter Ohler authored
339 doc.move('a/1')
340 doc.home()
341 assert_equal(2, doc.fetch('/a/2'))
342 assert_equal(2, doc.fetch('a/2'))
343 doc.close()
344 begin
345 doc.home()
346 rescue Exception => e
347 assert(true)
348 end
349 end
350
4344f4b Oj::Doc will close on GC
Peter Ohler authored
351
352 def test_open_no_close
353 json = %{{"a":[1,2,3]}}
354 doc = Oj::Doc.open(json)
355 assert_equal(Oj::Doc, doc.class)
356 assert_equal(5, doc.size)
357 assert_equal('/', doc.where?)
358 doc.move('a/1')
359 doc.home()
360 assert_equal(2, doc.fetch('/a/2'))
361 assert_equal(2, doc.fetch('a/2'))
362 doc = nil
363 GC.start
0d34554 Oj::Doc will close on GC
Peter Ohler authored
364 # a print statement confirms close is called
4344f4b Oj::Doc will close on GC
Peter Ohler authored
365 end
366
367
f5aae61 docs completed, ready for a release
Peter Ohler authored
368 def test_dump
369 Oj::Doc.open('[1,[2,3]]') do |doc|
370 assert_equal('[1,[2,3]]', doc.dump())
371 end
372 Oj::Doc.open('[1,[2,3]]') do |doc|
373 assert_equal('[2,3]', doc.dump('/2'))
374 end
375 end
376
377 def test_each_leaf
378 results = Oj::Doc.open('[1,[2,3]]') do |doc|
379 h = {}
380 doc.each_leaf() { |d| h[d.where?] = d.fetch() }
381 h
382 end
383 assert_equal({'/1' => 1, '/2/1' => 2, '/2/2' => 3}, results)
384 end
c866d0f changed name from fast to doc
Peter Ohler authored
385
28ab9ce mimic is completed, ready for merge
Peter Ohler authored
386 def test_comment
387 json = %{{
388 "x"/*one*/:/*two*/true,//three
389 "y":58/*four*/,
390 "z": [1,2/*five*/,
391 3 // six
392 ]
393 }
394 }
395 results = Oj::Doc.open(json) do |doc|
396 h = {}
397 doc.each_leaf() { |d| h[d.where?] = d.fetch() }
398 h
399 end
400 assert_equal({'/x' => true, '/y' => 58, '/z/1' => 1, '/z/2' => 2, '/z/3' => 3}, results)
401 end
402
c866d0f changed name from fast to doc
Peter Ohler authored
403 end # DocTest
Something went wrong with that request. Please try again.