| 1 |
# vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et : |
| 2 |
use strictures 2; |
| 3 |
use 5.020; |
| 4 |
use stable 0.031 'postderef'; |
| 5 |
use experimental 'signatures'; |
| 6 |
no autovivification warn => qw(fetch store exists delete); |
| 7 |
use if "$]" >= 5.022, experimental => 're_strict'; |
| 8 |
no if "$]" >= 5.031009, feature => 'indirect'; |
| 9 |
no if "$]" >= 5.033001, feature => 'multidimensional'; |
| 10 |
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
| 11 |
no if "$]" >= 5.041009, feature => 'smartmatch'; |
| 12 |
no feature 'switch'; |
| 13 |
use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8 |
| 14 |
|
| 15 |
use lib 't/lib'; |
| 16 |
use Helper; |
| 17 |
use JSON::Schema::Modern::Utilities qw(jsonp_set jsonp_elements); |
| 18 |
|
| 19 |
my $js = JSON::Schema::Modern->new(with_defaults => 1); |
| 20 |
|
| 21 |
subtest 'basic example' => sub { |
| 22 |
my $result = $js->evaluate( |
| 23 |
my $data = { |
| 24 |
my_object => { alpha => 1, gamma => 3 }, |
| 25 |
my_array => [ 'yellow' ], |
| 26 |
}, |
| 27 |
{ |
| 28 |
type => 'object', |
| 29 |
properties => { |
| 30 |
my_object => { |
| 31 |
type => 'object', |
| 32 |
properties => { |
| 33 |
alpha => { type => 'integer', default => 10 }, |
| 34 |
beta => { type => 'integer', default => 10 }, |
| 35 |
gamma => { type => 'integer', default => 10 }, |
| 36 |
}, |
| 37 |
}, |
| 38 |
my_array => { |
| 39 |
type => 'array', |
| 40 |
prefixItems => [ |
| 41 |
{ type => 'string', default => 'red' }, |
| 42 |
{ type => 'string', default => 'green' }, |
| 43 |
], |
| 44 |
}, |
| 45 |
}, |
| 46 |
}, |
| 47 |
); |
| 48 |
|
| 49 |
is_equal( |
| 50 |
$result->TO_JSON, |
| 51 |
{ |
| 52 |
valid => true, |
| 53 |
defaults => { |
| 54 |
'/my_object/beta' => 10, |
| 55 |
'/my_array/1' => 'green' |
| 56 |
}, |
| 57 |
}, |
| 58 |
'missing defaults are included in the result data', |
| 59 |
); |
| 60 |
|
| 61 |
my $defaults = $result->defaults; |
| 62 |
jsonp_set($data, $defaults->%{$_}) foreach keys %$defaults; |
| 63 |
|
| 64 |
is_equal( |
| 65 |
$data, |
| 66 |
{ |
| 67 |
my_object => { alpha => 1, beta => 10, gamma => 3 }, |
| 68 |
my_array => [ 'yellow', 'green' ], |
| 69 |
}, |
| 70 |
'defaults have been populated into the data instance', |
| 71 |
); |
| 72 |
}; |
| 73 |
|
| 74 |
subtest 'boolean schemas' => sub { |
| 75 |
my $result = $js->evaluate( |
| 76 |
{ |
| 77 |
my_object => { alpha => 1, gamma => 3 }, |
| 78 |
my_array => [ 'yellow' ], |
| 79 |
}, |
| 80 |
{ |
| 81 |
type => 'object', |
| 82 |
properties => { |
| 83 |
my_object => { |
| 84 |
type => 'object', |
| 85 |
properties => { |
| 86 |
alpha => true, |
| 87 |
beta => true, |
| 88 |
gamma => true, |
| 89 |
}, |
| 90 |
}, |
| 91 |
my_array => { |
| 92 |
type => 'array', |
| 93 |
prefixItems => [ true, true ], |
| 94 |
}, |
| 95 |
}, |
| 96 |
}, |
| 97 |
); |
| 98 |
|
| 99 |
is_equal( |
| 100 |
$result->TO_JSON, |
| 101 |
{ |
| 102 |
valid => true, |
| 103 |
defaults => {}, |
| 104 |
}, |
| 105 |
'boolean schemas are okay (but produce no defaults, of course)', |
| 106 |
); |
| 107 |
}; |
| 108 |
|
| 109 |
subtest 'json pointer escaping' => sub { |
| 110 |
my $result = $js->evaluate( |
| 111 |
{ |
| 112 |
'my/ob~ject' => {}, |
| 113 |
'my+arra~y' => [], |
| 114 |
}, |
| 115 |
{ |
| 116 |
type => 'object', |
| 117 |
properties => { |
| 118 |
'my/ob~ject' => { |
| 119 |
type => 'object', |
| 120 |
properties => { |
| 121 |
'al/ph~a' => { type => 'integer', default => '~ether/' }, |
| 122 |
}, |
| 123 |
}, |
| 124 |
'my+arra~y' => { |
| 125 |
type => 'array', |
| 126 |
prefixItems => [ |
| 127 |
{ type => 'string', default => '~ether/' }, |
| 128 |
], |
| 129 |
}, |
| 130 |
}, |
| 131 |
}, |
| 132 |
); |
| 133 |
|
| 134 |
is_equal( |
| 135 |
$result->TO_JSON, |
| 136 |
{ |
| 137 |
valid => true, |
| 138 |
defaults => { |
| 139 |
'/my~1ob~0ject/al~1ph~0a' => '~ether/', |
| 140 |
'/my+arra~0y/0' => '~ether/' |
| 141 |
}, |
| 142 |
}, |
| 143 |
'jsonp escaping is done', |
| 144 |
); |
| 145 |
}; |
| 146 |
|
| 147 |
subtest 'default handling in applicators' => sub { |
| 148 |
my $result = $js->evaluate( |
| 149 |
{ |
| 150 |
# in this example data, the invalid property comes before the missing default, |
| 151 |
# so we can immediately skip collecting it |
| 152 |
my_object => { alpha => 'hi', gamma => 3 }, |
| 153 |
my_array => [ 'yellow' ], |
| 154 |
}, |
| 155 |
my $schema = { |
| 156 |
type => 'object', |
| 157 |
anyOf => [ |
| 158 |
{ |
| 159 |
properties => { |
| 160 |
my_object => { |
| 161 |
type => 'object', |
| 162 |
properties => { |
| 163 |
alpha => { type => 'integer', default => 10 }, |
| 164 |
beta => { type => 'integer', default => 10 }, |
| 165 |
gamma => { type => 'integer', default => 10 }, |
| 166 |
}, |
| 167 |
}, |
| 168 |
}, |
| 169 |
}, |
| 170 |
{ |
| 171 |
properties => { |
| 172 |
my_array => { |
| 173 |
type => 'array', |
| 174 |
prefixItems => [ |
| 175 |
{ type => 'string', default => 'red' }, |
| 176 |
{ type => 'string', default => 'green' }, |
| 177 |
], |
| 178 |
}, |
| 179 |
}, |
| 180 |
}, |
| 181 |
], |
| 182 |
}, |
| 183 |
); |
| 184 |
is_equal( |
| 185 |
$result->TO_JSON, |
| 186 |
{ |
| 187 |
valid => true, |
| 188 |
defaults => { |
| 189 |
'/my_array/1' => 'green' |
| 190 |
}, |
| 191 |
}, |
| 192 |
'defaults are not included if the local subschema is already invalid', |
| 193 |
); |
| 194 |
|
| 195 |
$result = $js->evaluate( |
| 196 |
{ |
| 197 |
# in this example data, the invalid property comes after the missing default |
| 198 |
my_object => { alpha => 3, gamma => 'hi' }, |
| 199 |
my_array => [ 'yellow' ], |
| 200 |
}, |
| 201 |
$schema, |
| 202 |
); |
| 203 |
is_equal( |
| 204 |
$result->TO_JSON, |
| 205 |
{ |
| 206 |
valid => true, |
| 207 |
defaults => { |
| 208 |
'/my_array/1' => 'green' |
| 209 |
}, |
| 210 |
}, |
| 211 |
'defaults are not included from invalid properties keywords', |
| 212 |
); |
| 213 |
|
| 214 |
# now we need a schema where 'properties' keywords are still valid, but something else causes the |
| 215 |
# subschema to be invalid, so the defaults from that subschema must be discarded |
| 216 |
$result = $js->evaluate( |
| 217 |
{ |
| 218 |
my_object => {}, |
| 219 |
my_array => [], |
| 220 |
}, |
| 221 |
{ |
| 222 |
anyOf => [ # this keyword is valid (/anyOf/0 is false, /anyOf/1 is true) |
| 223 |
{ |
| 224 |
allOf => [ # this keyword is invalid (/allOf/0 is true, /allOf/1 is false) |
| 225 |
{ # this schema is valid and produces defaults |
| 226 |
type => 'object', |
| 227 |
properties => { # this keyword is valid and produces defaults |
| 228 |
my_object => { |
| 229 |
type => 'object', |
| 230 |
properties => { |
| 231 |
alpha => { type => 'integer', default => 10 }, |
| 232 |
}, |
| 233 |
}, |
| 234 |
my_array => { |
| 235 |
type => 'array', |
| 236 |
prefixItems => [ |
| 237 |
{ type => 'string', default => 'red' }, |
| 238 |
], |
| 239 |
}, |
| 240 |
}, |
| 241 |
}, |
| 242 |
false, |
| 243 |
], |
| 244 |
}, |
| 245 |
true, |
| 246 |
], |
| 247 |
}, |
| 248 |
); |
| 249 |
is_equal( |
| 250 |
$result->TO_JSON, |
| 251 |
{ |
| 252 |
valid => true, |
| 253 |
defaults => {}, |
| 254 |
}, |
| 255 |
'defaults are discarded from all valid subschemas when allOf is invalid', |
| 256 |
); |
| 257 |
|
| 258 |
$result = $js->evaluate( |
| 259 |
{ |
| 260 |
my_object => {}, |
| 261 |
my_array => [], |
| 262 |
}, |
| 263 |
{ |
| 264 |
anyOf => [ |
| 265 |
{ # this schema is invalid and should discard defaults |
| 266 |
type => 'object', |
| 267 |
minProperties => 100, # this keyword is invalid, making the containing schema invalid |
| 268 |
properties => { # this keyword is valid and produces defaults |
| 269 |
my_object => { |
| 270 |
type => 'object', |
| 271 |
properties => { |
| 272 |
alpha => { type => 'integer', default => 10 }, |
| 273 |
}, |
| 274 |
}, |
| 275 |
my_array => { |
| 276 |
type => 'array', |
| 277 |
prefixItems => [ |
| 278 |
{ type => 'string', default => 'red' }, |
| 279 |
], |
| 280 |
}, |
| 281 |
}, |
| 282 |
}, |
| 283 |
true, |
| 284 |
], |
| 285 |
}, |
| 286 |
); |
| 287 |
is_equal( |
| 288 |
$result->TO_JSON, |
| 289 |
{ |
| 290 |
valid => true, |
| 291 |
defaults => {}, |
| 292 |
}, |
| 293 |
'defaults are discarded from invalid anyOf subschemas', |
| 294 |
); |
| 295 |
|
| 296 |
$result = $js->evaluate( |
| 297 |
{ |
| 298 |
my_object => {}, |
| 299 |
my_array => [], |
| 300 |
}, |
| 301 |
{ |
| 302 |
oneOf => [ # this schema is valid as there is one valid subschema |
| 303 |
{ # this schema is invalid and should discard defaults |
| 304 |
type => 'object', |
| 305 |
minProperties => 100, # this keyword is invalid, making the containing schema invalid |
| 306 |
properties => { # this keyword is valid and produces defaults |
| 307 |
my_object => { |
| 308 |
type => 'object', |
| 309 |
properties => { |
| 310 |
alpha => { type => 'integer', default => 10 }, |
| 311 |
}, |
| 312 |
}, |
| 313 |
}, |
| 314 |
}, |
| 315 |
{ |
| 316 |
type => 'object', |
| 317 |
properties => { |
| 318 |
my_array => { |
| 319 |
type => 'array', |
| 320 |
prefixItems => [ |
| 321 |
{ type => 'string', default => 'red' }, |
| 322 |
], |
| 323 |
}, |
| 324 |
}, |
| 325 |
}, |
| 326 |
], |
| 327 |
}, |
| 328 |
); |
| 329 |
is_equal( |
| 330 |
$result->TO_JSON, |
| 331 |
{ |
| 332 |
valid => true, |
| 333 |
defaults => { |
| 334 |
'/my_array/0' => 'red' |
| 335 |
}, |
| 336 |
}, |
| 337 |
'defaults are discarded from invalid oneOf subschemas, but are kept from the valid subschema', |
| 338 |
); |
| 339 |
|
| 340 |
# same as above, but now there is a second valid schema. now we need to discard everything |
| 341 |
$result = $js->evaluate( |
| 342 |
{ |
| 343 |
my_object => {}, |
| 344 |
my_array => [], |
| 345 |
}, |
| 346 |
{ |
| 347 |
anyOf => [ |
| 348 |
{ |
| 349 |
oneOf => [ # this schema is invalid as there are two valid subschemas |
| 350 |
{ # this schema is invalid and should discard defaults |
| 351 |
type => 'object', |
| 352 |
minProperties => 100, # this keyword is invalid, making the containing schema invalid |
| 353 |
properties => { # this keyword is valid and produces defaults |
| 354 |
my_object => { |
| 355 |
type => 'object', |
| 356 |
properties => { |
| 357 |
alpha => { type => 'integer', default => 10 }, |
| 358 |
}, |
| 359 |
}, |
| 360 |
}, |
| 361 |
}, |
| 362 |
{ |
| 363 |
type => 'object', |
| 364 |
properties => { |
| 365 |
my_array => { |
| 366 |
type => 'array', |
| 367 |
prefixItems => [ |
| 368 |
{ type => 'string', default => 'red' }, |
| 369 |
], |
| 370 |
}, |
| 371 |
}, |
| 372 |
}, |
| 373 |
true, |
| 374 |
], |
| 375 |
}, |
| 376 |
true, |
| 377 |
], |
| 378 |
}, |
| 379 |
); |
| 380 |
is_equal( |
| 381 |
$result->TO_JSON, |
| 382 |
{ |
| 383 |
valid => true, |
| 384 |
defaults => {}, |
| 385 |
}, |
| 386 |
'defaults are discarded from all oneOf subschemas if there is more than one valid schema', |
| 387 |
); |
| 388 |
}; |
| 389 |
|
| 390 |
subtest 'jsonp_set permutations' => sub { |
| 391 |
foreach my $data (undef, '', 'foo', 3) { |
| 392 |
my $orig = $data; |
| 393 |
my $newdata = jsonp_set($data, '', { foo => 1 }); |
| 394 |
is_equal( |
| 395 |
$data, |
| 396 |
$orig, |
| 397 |
'cannot overwrite a non-reference with a hashref', |
| 398 |
); |
| 399 |
is_equal( |
| 400 |
$newdata, |
| 401 |
{ foo => 1 }, |
| 402 |
'...but the reference is returned in the assigned value', |
| 403 |
); |
| 404 |
} |
| 405 |
|
| 406 |
my ($data, $newdata); |
| 407 |
|
| 408 |
like( |
| 409 |
dies { jsonp_set($data = '', '/1', 2) }, |
| 410 |
qr/^cannot write into a non-reference in void context/, |
| 411 |
'when root type is a non-reference, result must be assigned', |
| 412 |
); |
| 413 |
|
| 414 |
$data = { foo => 1 }; |
| 415 |
$newdata = jsonp_set($data, '', 'a' ); |
| 416 |
is_equal( |
| 417 |
$data, |
| 418 |
{ foo => 1 }, |
| 419 |
'cannot overwrite the hashref with a non-reference', |
| 420 |
); |
| 421 |
is_equal( |
| 422 |
$newdata, |
| 423 |
'a', |
| 424 |
'...but the new primitive is returned', |
| 425 |
); |
| 426 |
like( |
| 427 |
dies { jsonp_set($data = { foo => 1 }, '', 'a') }, |
| 428 |
qr/^cannot write into a reference of a different type in void context/, |
| 429 |
'when root type of original and new data do not match, result must be assigned', |
| 430 |
); |
| 431 |
|
| 432 |
$data = [ 1, 2, 3 ]; |
| 433 |
$newdata = jsonp_set($data, '', 'a' ); |
| 434 |
is_equal( |
| 435 |
$data, |
| 436 |
[ 1, 2, 3 ], |
| 437 |
'cannot overwrite the arrayref with a non-reference', |
| 438 |
); |
| 439 |
is_equal( |
| 440 |
$newdata, |
| 441 |
'a', |
| 442 |
'...but the new primitive is returned', |
| 443 |
); |
| 444 |
like( |
| 445 |
dies { jsonp_set($data = [ 1, 2, 3 ], '', 'a') }, |
| 446 |
qr/^cannot write into a reference of a different type in void context/, |
| 447 |
'when root type of original and new data do not match, result must be assigned', |
| 448 |
); |
| 449 |
|
| 450 |
$data = [ 0, 1, 2 ]; |
| 451 |
like( |
| 452 |
dies { jsonp_set($data, '/foo', 'foo') }, |
| 453 |
qr/^cannot write a hashref into a reference to an array in void context/, |
| 454 |
'cannot use a string path in an array at the top level without mutating', |
| 455 |
); |
| 456 |
|
| 457 |
$data = { a => 1 }; |
| 458 |
jsonp_set($data, '', { foo => 1 }); |
| 459 |
is_equal( |
| 460 |
$data, |
| 461 |
{ foo => 1 }, |
| 462 |
'assigning to the root hash overwrites all data', |
| 463 |
); |
| 464 |
|
| 465 |
$data = [ 1..3 ]; |
| 466 |
jsonp_set($data, '', [ 4..6 ]); |
| 467 |
is_equal( |
| 468 |
$data, |
| 469 |
[ 4..6 ], |
| 470 |
'assigning to the root array overwrites all data', |
| 471 |
); |
| 472 |
|
| 473 |
$data = { a => 1 }; |
| 474 |
jsonp_set($data, '/', 3); |
| 475 |
is_equal( |
| 476 |
$data, |
| 477 |
{ '' => 3, a => 1 }, |
| 478 |
'empty key is legal', |
| 479 |
); |
| 480 |
|
| 481 |
$data = { a => 1 }; |
| 482 |
jsonp_set($data, '/a', 2); |
| 483 |
is_equal( |
| 484 |
$data, |
| 485 |
{ a => 2 }, |
| 486 |
'an existing key is overwritten', |
| 487 |
); |
| 488 |
|
| 489 |
$data = { a => 1 }; |
| 490 |
jsonp_set($data, '/b', 2); |
| 491 |
is_equal( |
| 492 |
$data, |
| 493 |
{ a => 1, b => 2 }, |
| 494 |
'a new key is inserted', |
| 495 |
); |
| 496 |
|
| 497 |
$data = { a => { b => 1 } }; |
| 498 |
jsonp_set($data, '/a/b', 2); |
| 499 |
is_equal( |
| 500 |
$data, |
| 501 |
{ a => { b => 2 } }, |
| 502 |
'an existing key is overwritten at the second level', |
| 503 |
); |
| 504 |
|
| 505 |
$data = { a => { b => 1 } }; |
| 506 |
jsonp_set($data, '/a/c', 2); |
| 507 |
is_equal( |
| 508 |
$data, |
| 509 |
{ a => { b => 1, c => 2 } }, |
| 510 |
'a new key is added at the second level', |
| 511 |
); |
| 512 |
|
| 513 |
$data = { a => { b => 1 } }; |
| 514 |
jsonp_set($data, '/c/d', 2); |
| 515 |
is_equal( |
| 516 |
$data, |
| 517 |
{ a => { b => 1 }, c => { d => 2 } }, |
| 518 |
'a new key is added at the first level with data added at the second', |
| 519 |
); |
| 520 |
|
| 521 |
$data = { a => 1 }; |
| 522 |
jsonp_set($data, '/a/b', { c => 1 }); |
| 523 |
is_equal( |
| 524 |
$data, |
| 525 |
{ a => { b => { c => 1 } } }, |
| 526 |
'a leaf node is overwritten with a hash', |
| 527 |
); |
| 528 |
|
| 529 |
$data = { a => 1 }; |
| 530 |
jsonp_set($data, '/a/1/0', 2); |
| 531 |
is_equal( |
| 532 |
$data, |
| 533 |
{ a => [ undef, [ 2 ] ] }, |
| 534 |
'a non-terminal node is overwritten with an array', |
| 535 |
); |
| 536 |
|
| 537 |
$data = { a => 1 }; |
| 538 |
jsonp_set($data, '/a/b/c', { d => 1 }); |
| 539 |
is_equal( |
| 540 |
$data, |
| 541 |
{ a => { b => { c => { d => 1 } } } }, |
| 542 |
'a leaf node is overwritten with a deeper hash', |
| 543 |
); |
| 544 |
|
| 545 |
$data = []; |
| 546 |
jsonp_set($data, '/0', 1); |
| 547 |
is_equal( |
| 548 |
$data, |
| 549 |
[ 1 ], |
| 550 |
'insert an array element at the first level', |
| 551 |
); |
| 552 |
|
| 553 |
$data = [ 1, 2, 3, 4 ]; |
| 554 |
jsonp_set($data, '/2', 'a'); |
| 555 |
is_equal( |
| 556 |
$data, |
| 557 |
[ 1, 2, 'a', 4 ], |
| 558 |
'overwrite an array element at the first level', |
| 559 |
); |
| 560 |
|
| 561 |
$data = [ 4, 5, [ 6, 7, [ 1, 2 ] ] ]; |
| 562 |
jsonp_set($data, '/2/1', 'a'); |
| 563 |
is_equal( |
| 564 |
$data, |
| 565 |
[ 4, 5, [ 6, 'a', [ 1, 2 ] ] ], |
| 566 |
'overwrite array entry at second level', |
| 567 |
); |
| 568 |
|
| 569 |
$data = [ 4, 5, 6 ]; |
| 570 |
jsonp_set($data, '/2/1', 'a'); |
| 571 |
is_equal( |
| 572 |
$data, |
| 573 |
[ 4, 5, [ undef, 'a' ] ], |
| 574 |
'overwrite non-ref entry at second level', |
| 575 |
); |
| 576 |
|
| 577 |
$data = { a => 1 }; |
| 578 |
jsonp_set($data, '/a/0', 3); |
| 579 |
is_equal( |
| 580 |
$data, |
| 581 |
{ a => [ 3 ] }, |
| 582 |
'overwrote non-reference terminal node with an arrayref', |
| 583 |
); |
| 584 |
|
| 585 |
$data = { a => { b => 1 } }; |
| 586 |
jsonp_set($data, '/a/0', 3); |
| 587 |
is_equal( |
| 588 |
$data, |
| 589 |
{ a => { b => 1, 0 => 3 } }, |
| 590 |
'new data added at the second level in existing hash - numeric value treated as hash key', |
| 591 |
); |
| 592 |
|
| 593 |
$data = { a => [ 1, 2, 3 ] }; |
| 594 |
jsonp_set($data, '/a/foo', 9); |
| 595 |
is_equal( |
| 596 |
$data, |
| 597 |
{ a => { foo => 9 } }, |
| 598 |
'array data at terminal node overwritten by new hash', |
| 599 |
); |
| 600 |
|
| 601 |
$data = { a => [ 1, 2, 3 ] }; |
| 602 |
jsonp_set($data, '/a/foo/0', 9); |
| 603 |
is_equal( |
| 604 |
$data, |
| 605 |
{ a => { foo => [ 9 ] } }, |
| 606 |
'array data not at terminal node overwritten by new hash', |
| 607 |
); |
| 608 |
|
| 609 |
$data = { a => 1 }; |
| 610 |
jsonp_set($data, '/a/b/0/c', 5); |
| 611 |
is_equal( |
| 612 |
$data, |
| 613 |
{ a => { b => [ { c => 5 } ] } }, |
| 614 |
'deep autovivification, with both arrays and hashes', |
| 615 |
); |
| 616 |
|
| 617 |
$data = [ 0, 1 ]; |
| 618 |
jsonp_set($data, '/0/1', 5); |
| 619 |
is_equal( |
| 620 |
$data, |
| 621 |
[ [ undef, 5 ], 1 ], |
| 622 |
'a non-ref is overwritten with an arrayref', |
| 623 |
); |
| 624 |
|
| 625 |
$data = [ 0, 1 ]; |
| 626 |
jsonp_set($data, '/3/a/b', 5); |
| 627 |
is_equal( |
| 628 |
$data, |
| 629 |
[ 0, 1, undef, { a => { b => 5 } } ], |
| 630 |
'an array is extended with a hashref', |
| 631 |
); |
| 632 |
|
| 633 |
$data = [ 0, 1 ]; |
| 634 |
jsonp_set($data, '/3/1/1', 5); |
| 635 |
is_equal( |
| 636 |
$data, |
| 637 |
[ 0, 1, undef, [ undef, [ undef, 5 ] ] ], |
| 638 |
'an array is extended with an arrayref', |
| 639 |
); |
| 640 |
|
| 641 |
$data = {}; |
| 642 |
jsonp_set($data, '/paths/~1foo~1{foo_id}/get/~0ether', { operationId => 'foo' }); |
| 643 |
is_equal( |
| 644 |
$data, |
| 645 |
{ paths => { '/foo/{foo_id}' => { get => { '~ether' => { operationId => 'foo' } } } } }, |
| 646 |
'json pointer escaping is done properly', |
| 647 |
); |
| 648 |
|
| 649 |
$data = { a => 1, b => { c => 3, d => 4 } }; |
| 650 |
my $defaults = { |
| 651 |
'/b/d' => 5, |
| 652 |
'/b/e' => 6, |
| 653 |
'/f' => 7, |
| 654 |
'/g/h/i/1' => [ 10 ], |
| 655 |
}; |
| 656 |
jsonp_set($data, $defaults->%{$_}) foreach keys %$defaults; |
| 657 |
is_equal( |
| 658 |
$data, |
| 659 |
{ a => 1, b => { c => 3, d => 5, e => 6 }, f => 7, g => { h => { i => [ undef, [ 10 ] ] } } }, |
| 660 |
'pod example', |
| 661 |
); |
| 662 |
|
| 663 |
$data = undef; |
| 664 |
$data = jsonp_set($data, $defaults->%{$_}) foreach keys %$defaults; |
| 665 |
is_equal( |
| 666 |
$data, |
| 667 |
{ b => { d => 5, e => 6 }, f => 7, g => { h => { i => [ undef, [ 10 ] ] } } }, |
| 668 |
'when an lvalue is used, can populate data even over top of an undefined value', |
| 669 |
); |
| 670 |
|
| 671 |
$data = 'root'; |
| 672 |
like( |
| 673 |
dies { jsonp_set($data, '', [ 1, 2 ]) }, |
| 674 |
qr/^cannot write into a non-reference in void context/, |
| 675 |
'when root type is a non-reference, result must be assigned', |
| 676 |
); |
| 677 |
|
| 678 |
$data = jsonp_set($data, '', [ 1, 2 ]); |
| 679 |
is_equal( |
| 680 |
$data, |
| 681 |
[ 1, 2 ], |
| 682 |
'can write a reference into a non-reference location when lvalue provided', |
| 683 |
); |
| 684 |
|
| 685 |
$data = 'root'; |
| 686 |
$data = jsonp_set($data, '', 'new value'); |
| 687 |
is_equal( |
| 688 |
$data, |
| 689 |
'new value', |
| 690 |
'can write a non-reference into a non-reference location when lvalue provided', |
| 691 |
); |
| 692 |
|
| 693 |
$data = [ 0, 1, 2 ]; |
| 694 |
jsonp_set($data, '/-', 'foo'); |
| 695 |
is_equal( |
| 696 |
$data, |
| 697 |
[ 0, 1, 2, 'foo' ], |
| 698 |
'- is accepted as an array index, referencing the item after the end of the array', |
| 699 |
); |
| 700 |
|
| 701 |
$data = ''; |
| 702 |
$data = jsonp_set($data, '/-', 'foo'); |
| 703 |
is_equal( |
| 704 |
$data, |
| 705 |
[ 'foo' ], |
| 706 |
'- is accepted as an array index, when overwriting a string', |
| 707 |
); |
| 708 |
|
| 709 |
$data = ''; |
| 710 |
$data = jsonp_set($data, '/~0~1/~0', 'fo~/o'); |
| 711 |
is_equal( |
| 712 |
$data, |
| 713 |
{ '~/' => { '~' => 'fo~/o' } }, |
| 714 |
'~ and / are properly unescaped when setting properties, but not values', |
| 715 |
); |
| 716 |
}; |
| 717 |
|
| 718 |
subtest jsonp_elements => sub { |
| 719 |
is_equal( |
| 720 |
jsonp_elements(1), |
| 721 |
{ '' => 1 }, |
| 722 |
'single primitive', |
| 723 |
); |
| 724 |
|
| 725 |
is_equal( |
| 726 |
jsonp_elements({ a => 1 }), |
| 727 |
{ '/a' => 1 }, |
| 728 |
'single level hash with one element', |
| 729 |
); |
| 730 |
|
| 731 |
is_equal( |
| 732 |
jsonp_elements({ a => 1, b => 2 }), |
| 733 |
{ '/a' => 1, '/b' => 2 }, |
| 734 |
'single level hash with two elements', |
| 735 |
); |
| 736 |
|
| 737 |
is_equal( |
| 738 |
jsonp_elements([ 0 ]), |
| 739 |
{ '/0' => 0 }, |
| 740 |
'single level array with one element', |
| 741 |
); |
| 742 |
|
| 743 |
is_equal( |
| 744 |
jsonp_elements([ 0, 1 ]), |
| 745 |
{ '/0' => 0, '/1' => 1 }, |
| 746 |
'single level array with two elements', |
| 747 |
); |
| 748 |
|
| 749 |
is_equal( |
| 750 |
jsonp_elements({ a => { b => 'c' } }), |
| 751 |
{ '/a/b' => 'c' }, |
| 752 |
'two-level hash with one element', |
| 753 |
); |
| 754 |
|
| 755 |
is_equal( |
| 756 |
jsonp_elements({ a => { b => [ 'x', 'y' ], c => { d => 'e' } } }), |
| 757 |
{ |
| 758 |
'/a/b/0' => 'x', |
| 759 |
'/a/b/1' => 'y', |
| 760 |
'/a/c/d' => 'e', |
| 761 |
}, |
| 762 |
'deep hash with multiple elements of hashes and arrays', |
| 763 |
); |
| 764 |
|
| 765 |
is_equal( |
| 766 |
jsonp_elements({ 'a~1' => 'fo~/o', 'b/2' => 'ba~/r', 'c~/3' => { 'ba~/z' => 'bo~0/op' } }), |
| 767 |
{ |
| 768 |
'/a~01' => 'fo~/o', |
| 769 |
'/b~12' => 'ba~/r', |
| 770 |
'/c~0~13/ba~0~1z' => 'bo~0/op', |
| 771 |
}, |
| 772 |
'properties with tilde and slash are correctly escaped', |
| 773 |
); |
| 774 |
}; |
| 775 |
|
| 776 |
done_testing; |