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
|
#!/usr/bin/env ruby
# frozen_string_literal: true
$LOAD_PATH << __dir__
require 'helper'
class DocTest < Minitest::Test
def setup
@default_options = Oj.default_options
@json1 = %|{
"array": [
{
"num" : 3,
"string": "message",
"hash" : {
"h2" : {
"a" : [ 1, 2, 3 ]
}
}
}
],
"boolean" : true
}|
end
def teardown
Oj.default_options = @default_options
end
def test_nil
json = %{null}
Oj::Doc.open(json) do |doc|
assert_equal(NilClass, doc.type)
assert_nil(doc.fetch())
end
end
def test_leaf_of_existing_path
json = %{{"foo": 1, "fizz": true}}
Oj::Doc.open(json) do |doc|
%w(/foo/bar /fizz/bar).each do |path|
assert_nil(doc.fetch(path))
assert_equal(:default, doc.fetch(path, :default))
refute(doc.exists?(path))
end
end
end
def test_true
json = %{true}
Oj::Doc.open(json) do |doc|
assert_equal(TrueClass, doc.type)
assert(doc.fetch())
end
end
def test_false
json = %{false}
Oj::Doc.open(json) do |doc|
assert_equal(FalseClass, doc.type)
refute(doc.fetch())
end
end
def test_string
json = %{"a string"}
Oj::Doc.open(json) do |doc|
assert_equal(String, doc.type)
assert_equal('a string', doc.fetch())
end
end
def test_encoding
json = %{"ぴーたー"}
Oj::Doc.open(json) do |doc|
assert_equal(String, doc.type)
assert_equal('ぴーたー', doc.fetch())
end
end
def test_encoding_escaped
json = %{"\\u3074\\u30fc\\u305f\\u30fc"}
Oj::Doc.open(json) do |doc|
assert_equal(String, doc.type)
assert_equal('ぴーたー', doc.fetch())
end
end
def test_fixnum
json = %{12345}
Oj::Doc.open(json) do |doc|
assert_equal(Integer, doc.type)
assert_equal(12_345, doc.fetch())
end
end
def test_float
json = %{12345.6789}
Oj::Doc.open(json) do |doc|
assert_equal(Float, doc.type)
assert_in_delta(12_345.6789, doc.fetch())
end
end
def test_float_exp
json = %{12345.6789e7}
Oj::Doc.open(json) do |doc|
assert_equal(Float, doc.type)
# assert_equal(12345.6789e7, doc.fetch())
assert_equal(12_345.6789e7.to_i, doc.fetch().to_i)
end
end
def test_array_empty
json = %{[]}
Oj::Doc.open(json) do |doc|
assert_equal(Array, doc.type)
assert_empty(doc.fetch())
end
end
def test_array
json = %{[true,false]}
Oj::Doc.open(json) do |doc|
assert_equal(Array, doc.type)
assert_equal([true, false], doc.fetch())
end
end
def test_hash_empty
json = %{{}}
Oj::Doc.open(json) do |doc|
assert_equal(Hash, doc.type)
assert_empty(doc.fetch())
end
end
def test_hash
json = %{{"one":true,"two":false}}
Oj::Doc.open(json) do |doc|
assert_equal(Hash, doc.type)
assert_equal({'one' => true, 'two' => false}, doc.fetch())
end
end
# move() and where?()
def test_move_hash
json = %{{"one":{"two":false}}}
Oj::Doc.open(json) do |doc|
doc.move('/one')
assert_equal('/one', doc.where?)
doc.move('/one/two')
assert_equal('/one/two', doc.where?)
end
end
def test_move_array
json = %{[1,[2,true]]}
Oj::Doc.open(json) do |doc|
doc.move('/1')
assert_equal('/1', doc.where?)
doc.move('/2/1')
assert_equal('/2/1', doc.where?)
end
end
def test_move
Oj::Doc.open(@json1) do |doc|
[
'/',
'/array',
'/boolean',
'/array/1/hash/h2/a/3',
].each do |p|
doc.move(p)
assert_equal(p, doc.where?)
end
begin
doc.move('/array/x')
rescue Exception
assert_equal('/', doc.where?)
assert(true)
end
end
end
def test_move_slash
Oj::Doc.open(%|{"top":{"a/b":3}}|) do |doc|
doc.move('top/a\/b')
assert_equal('/top/a\/b', doc.where?)
end
end
def test_fetch_slash
Oj::Doc.open(%|{"a/b":3}|) do |doc|
x = doc.fetch('a\/b')
assert_equal(3, x)
end
end
def test_move_relative
Oj::Doc.open(@json1) do |doc|
[
['/', 'array', '/array'],
['/array', '1/num', '/array/1/num'],
['/array/1/hash', 'h2/a', '/array/1/hash/h2/a'],
['/array/1', 'hash/h2/a/2', '/array/1/hash/h2/a/2'],
['/array/1/hash', '../string', '/array/1/string'],
['/array/1/hash', '..', '/array/1'],
].each do |start, path, where|
doc.move(start)
doc.move(path)
assert_equal(where, doc.where?)
end
end
end
def test_type
Oj::Doc.open(@json1) do |doc|
[
['/', Hash],
['/array', Array],
['/array/1', Hash],
['/array/1/num', Integer],
['/array/1/string', String],
['/array/1/hash/h2/a', Array],
['/array/1/hash/../num', Integer],
['/array/1/hash/../..', Array],
].each do |path, type|
assert_equal(type, doc.type(path))
end
end
end
def test_local_key
Oj::Doc.open(@json1) do |doc|
[
['/', nil],
['/array', 'array'],
['/array/1', 1],
['/array/1/num', 'num'],
['/array/1/string', 'string'],
['/array/1/hash/h2/a', 'a'],
['/array/1/hash/../num', 'num'],
['/array/1/hash/..', 1],
['/array/1/hash/../..', 'array'],
].each do |path, key|
doc.move(path)
if key.nil?
assert_nil(doc.local_key())
else
assert_equal(key, doc.local_key())
end
end
end
end
def test_fetch_move
Oj::Doc.open(@json1) do |doc|
[
['/array/1/num', 3],
['/array/1/string', 'message'],
['/array/1/hash/h2/a', [1, 2, 3]],
['/array/1/hash/../num', 3],
['/array/1/hash/..', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
['/array/1/hash/../..', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
['/array/1', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
['/array', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
['/', {'array' => [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}], 'boolean' => true}],
].each do |path, val|
doc.move(path)
assert_equal(val, doc.fetch())
end
end
end
def test_fetch_path
Oj::Doc.open(@json1) do |doc|
[
['/array/1/num', 3],
['/array/1/string', 'message'],
['/array/1/hash/h2/a', [1, 2, 3]],
['/array/1/hash/../num', 3],
['/array/1/hash/..', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
['/array/1/hash/../..', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
['/array/1', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
['/array', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
['/', {'array' => [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}], 'boolean' => true}],
['/nothing', nil],
['/array/10', nil],
].each do |path, val|
if val.nil?
assert_nil(doc.fetch(path))
else
assert_equal(val, doc.fetch(path))
end
end
end
# verify empty hash and arrays return nil when a member is requested
Oj::Doc.open('{}') do |doc|
assert_nil(doc.fetch('/x'))
assert_nil(doc.fetch('/0'))
end
Oj::Doc.open('[]') do |doc|
assert_nil(doc.fetch('/x'))
assert_nil(doc.fetch('/0'))
end
end
def test_move_fetch_path
Oj::Doc.open(@json1) do |doc|
[
['/array/1', 'num', 3],
['/array/1', 'string', 'message'],
['/array/1/hash', 'h2/a', [1, 2, 3]],
].each do |path, fetch_path, val|
doc.move(path)
assert_equal(val, doc.fetch(fetch_path))
end
end
end
def test_exists
Oj::Doc.open(@json1) do |doc|
[
['/array/1', true],
['/array/1', true],
['/array/1/hash', true],
['/array/1/dash', false],
['/array/3', false],
['/nothing', false],
].each do |path, val|
assert_equal(val, doc.exists?(path), "failed for #{path.inspect}")
end
end
end
def test_home
Oj::Doc.open(@json1) do |doc|
doc.move('/array/1/num')
doc.home()
assert_equal('/', doc.where?)
end
end
def test_each_value_root
Oj::Doc.open(@json1) do |doc|
values = []
doc.each_value() { |v| values << v.to_s }
assert_equal(['1', '2', '3', '3', 'message', 'true'], values.sort)
end
end
def test_each_value_move
Oj::Doc.open(@json1) do |doc|
doc.move('/array/1/hash')
values = []
doc.each_value() { |v| values << v.to_s }
assert_equal(['1', '2', '3'], values.sort)
end
end
def test_each_value_path
Oj::Doc.open(@json1) do |doc|
values = []
doc.each_value('/array/1/hash') { |v| values << v.to_s }
assert_equal(['1', '2', '3'], values.sort)
end
end
def test_each_child_move
Oj::Doc.open(@json1) do |doc|
locations = []
doc.move('/array/1/hash/h2/a')
doc.each_child() { |d| locations << d.where? }
assert_equal(['/array/1/hash/h2/a/1', '/array/1/hash/h2/a/2', '/array/1/hash/h2/a/3'], locations)
locations = []
doc.move('/array/1')
doc.each_child() { |d| locations << d.where? }
assert_equal(['/array/1/num', '/array/1/string', '/array/1/hash'], locations)
end
end
def test_each_child_path
Oj::Doc.open(@json1) do |doc|
locations = []
doc.each_child('/array/1/hash/h2/a') { |d| locations << d.where? }
assert_equal(['/array/1/hash/h2/a/1', '/array/1/hash/h2/a/2', '/array/1/hash/h2/a/3'], locations)
locations = []
doc.each_child('/array/1') { |d| locations << d.where? }
assert_equal(['/array/1/num', '/array/1/string', '/array/1/hash'], locations)
end
end
def test_nested_each_child
h = {}
Oj::Doc.open('{"a":1,"c":[2],"d":3}') do |doc|
doc.each_child('/') do |child|
h[child.path] = child.fetch
child.each_child do |grandchild|
h[grandchild.path] = grandchild.fetch
end
end
end
assert_equal({'/a'=>1, '/c'=>[2], '/c/1'=>2, '/d'=>3}, h)
end
def test_size
Oj::Doc.open('[1,2,3]') do |doc|
assert_equal(4, doc.size)
end
Oj::Doc.open('{"a":[1,2,3]}') do |doc|
assert_equal(5, doc.size)
end
end
def test_open_file
filename = File.join(__dir__, 'open_file_test.json')
File.write(filename, '{"a":[1,2,3]}')
Oj::Doc.open_file(filename) do |doc|
assert_equal(5, doc.size)
end
end
def test_open_close
json = %{{"a":[1,2,3]}}
doc = Oj::Doc.open(json)
assert_equal(Oj::Doc, doc.class)
assert_equal(5, doc.size)
assert_equal('/', doc.where?)
doc.move('a/1')
doc.home()
assert_equal(2, doc.fetch('/a/2'))
assert_equal(2, doc.fetch('a/2'))
doc.close()
begin
doc.home()
rescue Exception
assert(true)
end
end
def test_file_open_close
filename = File.join(__dir__, 'open_file_test.json')
File.write(filename, '{"a":[1,2,3]}')
doc = Oj::Doc.open_file(filename)
assert_equal(Oj::Doc, doc.class)
assert_equal(5, doc.size)
assert_equal('/', doc.where?)
doc.move('a/1')
doc.home()
assert_equal(2, doc.fetch('/a/2'))
assert_equal(2, doc.fetch('a/2'))
doc.close()
begin
doc.home()
rescue Exception
assert(true)
end
end
def test_open_no_close
json = %{{"a":[1,2,3]}}
doc = Oj::Doc.open(json)
assert_equal(Oj::Doc, doc.class)
assert_equal(5, doc.size)
assert_equal('/', doc.where?)
doc.move('a/1')
doc.home()
assert_equal(2, doc.fetch('/a/2'))
assert_equal(2, doc.fetch('a/2'))
GC.start
# a print statement confirms close is called
end
def test_dump
Oj::Doc.open('[1,[2,3]]') do |doc|
assert_equal('[1,[2,3]]', doc.dump())
end
Oj::Doc.open('[1,[2,3]]') do |doc|
assert_equal('[2,3]', doc.dump('/2'))
end
end
def test_each_leaf
results = Oj::Doc.open('[1,[2,3]]') do |doc|
h = {}
doc.each_leaf() { |d| h[d.where?] = d.fetch() }
h
end
assert_equal({'/1' => 1, '/2/1' => 2, '/2/2' => 3}, results)
end
def test_each_leaf_hash
results = Oj::Doc.open('{"a":{"x":2},"b":{"y":4}}') do |doc|
h = {}
doc.each_leaf() { |d| h[d.where?] = d.fetch() }
h
end
assert_equal({'/a/x' => 2, '/b/y' => 4}, results)
end
def test_doc_empty
result = Oj::Doc.open('') { |doc| doc.each_child {} }
assert_nil(result)
end
def test_comment
json = %{{
"x"/*one*/:/*two*/true,//three
"y":58/*four*/,
"z": [1,2/*five*/,
3 // six
]
}
}
results = Oj::Doc.open(json) do |doc|
h = {}
doc.each_leaf() { |d| h[d.where?] = d.fetch() }
h
end
assert_equal({'/x' => true, '/y' => 58, '/z/1' => 1, '/z/2' => 2, '/z/3' => 3}, results)
end
end # DocTest
|