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 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
|
#! /usr/bin/env perl
use strict;
use warnings;
no if $] >= 5.018, warnings => 'experimental::smartmatch';
use Test::More;
use Test::Deep;
use Data::DPath 'dpath', 'dpathr';
use Data::Dumper;
# local $Data::DPath::DEBUG = 1;
BEGIN {
if ($] < 5.010) {
plan skip_all => "Perl 5.010 required for the smartmatch overloaded tests. This is ".$];
}
}
use_ok( 'Data::DPath' );
my $data = {
AAA => { BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
},
some => { where => { else => {
AAA => { BBB => { CCC => 'affe' } },
} } },
strange_keys => { 'DD DD' => { 'EE/E' => { CCC => 'zomtec' } } },
};
my @resultlist;
my $resultlist;
my $context;
# trivial matching
@resultlist = dpath('/AAA/BBB/CCC')->match($data);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'] ], "KEYs" );
@resultlist = dpathr('/AAA/BBB/CCC')->match($data);
cmp_bag(\@resultlist, [ \($data->{AAA}{BBB}{CCC}) ], "KEYs (REFERENCES)" );
@resultlist = dpath('/AAA/./BBB/./CCC')->match($data);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'] ], "KEYs + NOSTEPs" );
@resultlist = dpathr('/AAA/./BBB/./CCC')->match($data);
cmp_bag(\@resultlist, [ \($data->{AAA}{BBB}{CCC}) ], "KEYs + NOSTEPs (REFERENCES)" );
@resultlist = dpath('/AAA/BBB/CCC/..')->match($data);
cmp_bag(\@resultlist, [ { CCC => ['XXX', 'YYY', 'ZZZ'] } ], "KEYs + PARENT" );
@resultlist = dpathr('/AAA/BBB/CCC/..')->match($data);
cmp_bag(\@resultlist, [ \($data->{AAA}{BBB}) ], "KEYs + PARENT (REFERENCES)" );
@resultlist = dpath('/AAA/BBB/CCC/../.')->match($data);
cmp_bag(\@resultlist, [ { CCC => ['XXX', 'YYY', 'ZZZ'] } ], "KEYs + PARENT + NOSTEP" );
@resultlist = dpath('//../CCC')->match($data);
#print Dumper(\@resultlist);
cmp_bag(\@resultlist, [ [ qw/ XXX YYY ZZZ / ],
[ qw/ RR1 RR2 RR3 / ],
'affe', # missing due to reduction to HASH|ARRAY in _any?
'zomtec',
], "KEYs + PARENT + ANYWHERE" );
# here only CCC that is 2 levels above leafs are expected, affe/zomtec do not match for a valid reason
@resultlist = dpath('//../../CCC')->match($data);
#print Dumper(\@resultlist);
cmp_bag(\@resultlist, [ [ qw/ XXX YYY ZZZ / ],
[ qw/ RR1 RR2 RR3 / ],
], "KEYs + TOO MANY PARENT + ANYWHERE" );
@resultlist = dpath('//./.././CCC/.')->match($data);
#print Dumper(\@resultlist);
cmp_bag(\@resultlist, [ [ qw/ XXX YYY ZZZ / ],
[ qw/ RR1 RR2 RR3 / ],
'affe', # missing due to reduction to HASH|ARRAY in _any?
'zomtec',
], "KEYs + PARENT + ANYWHERE + NOSTEP" );
@resultlist = dpath('/AAA/BBB/CCC/../..')->match($data);
cmp_bag(\@resultlist, [
{
BBB => { CCC => ['XXX', 'YYY', 'ZZZ'] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
}
], "KEYs + PARENT + PARENT" );
@resultlist = dpathr('/AAA/BBB/CCC/../..')->match($data);
cmp_bag(\@resultlist, [ \($data->{AAA}) ], "KEYs + PARENT + PARENT (REFERENCES)" );
@resultlist = dpath('/AAA/././././BBB/./CCC/../././../././.')->match($data);
cmp_bag(\@resultlist, [
{
BBB => { CCC => ['XXX', 'YYY', 'ZZZ'] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
}
], "KEYs + PARENT + PARENT + NOSTEPs" );
@resultlist = dpath('/AAA/BBB/CCC/../../DDD')->match($data);
cmp_bag(\@resultlist, [ { EEE => [ qw/ uuu vvv www / ] } ], "KEYs + PARENT + KEY" );
@resultlist = dpath('/AAA/*/CCC/../../DDD')->match($data);
cmp_bag(\@resultlist, [ { EEE => [ qw/ uuu vvv www / ] } ], "KEYs + ANYSTEP + PARENT + KEY no double results" );
# -------------------- ::ancestor --------------------
@resultlist = dpath('/AAA/BBB/CCC/::ancestor')->match($data);
cmp_deeply(\@resultlist, [
# order matters!
{ CCC => [ qw/ XXX YYY ZZZ / ] },
{
BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
},
{
AAA => { BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
},
some => { where => { else => {
AAA => { BBB => { CCC => 'affe' } },
} } },
strange_keys => { 'DD DD' => { 'EE/E' => { CCC => 'zomtec' } } },
},
], "KEYs + ANCESTOR" );
@resultlist = dpath('/AAA/BBB/CCC/::ancestor[0]')->match($data);
cmp_deeply(\@resultlist, [
{ CCC => [ qw/ XXX YYY ZZZ / ] },
], "KEYs + ANCESTOR + FILTER int 0" );
@resultlist = dpath('/AAA/BBB/CCC/::ancestor[1]')->match($data);
cmp_deeply(\@resultlist, [
{
BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
},
], "KEYs + ANCESTOR + FILTER int 1" );
@resultlist = dpath('/AAA/BBB/CCC/::ancestor[2]')->match($data);
cmp_deeply(\@resultlist, [
{
AAA => { BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
},
some => { where => { else => {
AAA => { BBB => { CCC => 'affe' } },
} } },
strange_keys => { 'DD DD' => { 'EE/E' => { CCC => 'zomtec' } } },
},
], "KEYs + ANCESTOR + FILTER int 2" );
@resultlist = dpath('/AAA/BBB/CCC/::ancestor[3]')->match($data);
cmp_deeply(\@resultlist, [ ], "KEYs + ANCESTOR + FILTER int outofbound" );
# -------------------- ::ancestor-or-self --------------------
@resultlist = dpath('/AAA/BBB/CCC/::ancestor-or-self')->match($data);
cmp_deeply(\@resultlist, [
# order matters!
[ qw/ XXX YYY ZZZ / ],
{ CCC => [ qw/ XXX YYY ZZZ / ] },
{
BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
},
{
AAA => { BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
},
some => { where => { else => {
AAA => { BBB => { CCC => 'affe' } },
} } },
strange_keys => { 'DD DD' => { 'EE/E' => { CCC => 'zomtec' } } },
},
], "KEYs + ANCESTOR_OR_SELF" );
@resultlist = dpath('/AAA/BBB/CCC/::ancestor-or-self[0]')->match($data);
cmp_deeply(\@resultlist, [
[ qw/ XXX YYY ZZZ / ],
], "KEYs + ANCESTOR_OR_SELF + FILTER int 0" );
@resultlist = dpath('/AAA/BBB/CCC/::ancestor-or-self[1]')->match($data);
cmp_deeply(\@resultlist, [
{ CCC => [ qw/ XXX YYY ZZZ / ] },
], "KEYs + ANCESTOR_OR_SELF + FILTER int 1" );
@resultlist = dpath('/AAA/BBB/CCC/::ancestor-or-self[2]')->match($data);
cmp_deeply(\@resultlist, [
{
BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
},
], "KEYs + ANCESTOR_OR_SELF + FILTER int 2" );
@resultlist = dpath('/AAA/BBB/CCC/::ancestor-or-self[3]')->match($data);
cmp_deeply(\@resultlist, [
{
AAA => { BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
},
some => { where => { else => {
AAA => { BBB => { CCC => 'affe' } },
} } },
strange_keys => { 'DD DD' => { 'EE/E' => { CCC => 'zomtec' } } },
},
], "KEYs + ANCESTOR_OR_SELF + FILTER int 3" );
@resultlist = dpath('/AAA/BBB/CCC/::ancestor-or-self[4]')->match($data);
cmp_deeply(\@resultlist, [ ], "KEYs + ANCESTOR_OR_SELF + FILTER int outofbound" );
@resultlist = dpath('/AAA/BBB/CCC/"::ancestor-or-self"')->match($data);
cmp_deeply(\@resultlist, [ ], "KEYs + quoted ANCESTOR_OR_SELF" );
# -------------------- misc --------------------
@resultlist = dpath('/')->match($data);
cmp_bag(\@resultlist, [ $data ], "ROOT" );
@resultlist = dpath('/AAA/*/CCC')->match($data);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'], [ 'RR1', 'RR2', 'RR3' ] ], "KEYs + ANYSTEP" );
# --- ---
@resultlist = dpath('//AAA/*/CCC')->match($data);
cmp_bag(\@resultlist, [ 'affe', ['XXX', 'YYY', 'ZZZ'], ['RR1', 'RR2', 'RR3'] ], "ANYWHERE + KEYs + ANYSTEP" );
@resultlist = dpath('///AAA/*/CCC')->match($data);
cmp_bag(\@resultlist, [ 'affe', ['XXX', 'YYY', 'ZZZ'], ['RR1', 'RR2', 'RR3'] ], "2xANYWHERE + KEYs + ANYSTEP" );
@resultlist = Data::DPath->match($data, '//AAA/*/CCC');
cmp_bag(\@resultlist, [ 'affe', ['XXX', 'YYY', 'ZZZ'], [ 'RR1', 'RR2', 'RR3' ] ], "ANYWHERE + KEYs + ANYSTEP as function" );
@resultlist = Data::DPath->match($data, '///AAA/*/CCC');
cmp_bag(\@resultlist, [ 'affe', ['XXX', 'YYY', 'ZZZ'], [ 'RR1', 'RR2', 'RR3' ] ], "2xANYWHERE + KEYs + ANYSTEP as function" );
# from now on more via Perl 5.10 smart matching
# --------------------
@resultlist = dpath('/some//CCC')->match($data);
cmp_bag(\@resultlist, [ 'affe' ], "ROOT + ANYWHERE + KEY + KEY" );
@resultlist = dpath('//some//CCC')->match($data);
cmp_bag(\@resultlist, [ 'affe' ], "ANYWHERE + KEY + ANYWHERE + KEY" );
@resultlist = dpath('/some//else//CCC')->match($data);
cmp_bag(\@resultlist, [ 'affe' ], "ROOT + KEY + ANYWHEREs + KEY" );
@resultlist = dpath('//some//else//CCC')->match($data);
cmp_bag(\@resultlist, [ 'affe' ], "ANYWHERE + KEYs + ANYWHEREs" );
@resultlist = dpathr('//some//else//CCC')->match($data);
cmp_bag(\@resultlist, [ \($data->{some}{where}{else}{AAA}{BBB}{CCC}) ], "ANYWHERE + KEYs + ANYWHEREs (REFERENCES)" );
# --------------------
@resultlist = dpath('//AAA')->match($data);
cmp_bag(\@resultlist, [
{
BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
},
{ BBB => { CCC => 'affe' } },
], "ANYWHERE + KEY" );
@resultlist = dpath('//AAA/*')->match($data);
cmp_bag(\@resultlist, [
{ CCC => [ qw/ XXX YYY ZZZ / ] },
{ CCC => [ qw/ RR1 RR2 RR3 / ] },
{ EEE => [ qw/ uuu vvv www / ] },
{ CCC => 'affe' },
], "ANYWHERE + KEY + ANYSTEP" );
@resultlist = dpath('//AAA/*[size == 3]')->match($data);
cmp_bag(\@resultlist, [ ], "ANYWHERE + KEY + ANYSTEP + FILTER size" );
@resultlist = dpath('//AAA[size == 3]')->match($data);
cmp_bag(\@resultlist, [
{ BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
}
], "ANYWHERE + KEY + FILTER size == 3" );
@resultlist = dpath('//AAA[size != 3]')->match($data);
cmp_bag(\@resultlist, [
{ BBB => { CCC => 'affe' } }
], "ANYWHERE + KEY + FILTER size != 3" );
@resultlist = dpath('//AAA/*/*[size == 3]')->match($data);
cmp_bag(\@resultlist, [
[ qw/ XXX YYY ZZZ / ],
[ qw/ RR1 RR2 RR3 / ],
[ qw/ uuu vvv www / ],
], "ANYWHERE + KEY + ANYSTEP + FILTER size" );
@resultlist = dpath('//.[size == 3]')->match($data);
cmp_bag(\@resultlist, [
$data,
$data->{AAA},
[ qw/ XXX YYY ZZZ / ],
[ qw/ RR1 RR2 RR3 / ],
[ qw/ uuu vvv www / ],
], "ANYWHERE + FILTER size" );
@resultlist = dpath('//AAA/*[size == 1]')->match($data);
cmp_bag(\@resultlist, [
{ CCC => [ qw/ XXX YYY ZZZ / ] },
{ CCC => [ qw/ RR1 RR2 RR3 / ] },
{ EEE => [ qw/ uuu vvv www / ] },
{ CCC => 'affe' },
], "ANYWHERE + KEY + ANYSTEP + FILTER size" );
@resultlist = dpath('/AAA/*/CCC/*')->match($data);
cmp_bag(\@resultlist, [ 'XXX', 'YYY', 'ZZZ', 'RR1', 'RR2', 'RR3' ], "trailing .../* unpacks" );
@resultlist = dpath('/strange_keys/DD DD/"EE/E"/CCC')->match($data);
@resultlist = dpath('/strange_keys/"DD DD"/"EE/E"/CCC')->match($data);
cmp_bag(\@resultlist, [ 'zomtec' ], "quoted KEY containg slash" );
@resultlist = dpath('//AAA/*/CCC[size == 3]')->match($data); # array with 3 elements
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'], [ 'RR1', 'RR2', 'RR3' ] ], 'FILTER size == 3' );
@resultlist = dpath('//AAA/*/CCC[size == 1]')->match($data); # array with 1 elements
cmp_bag(\@resultlist, [ 'affe' ], 'FILTER size == 1' );
@resultlist = dpath('//AAA/*/CCC[size >= 1]')->match($data); # array with >= elements
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'], [ 'RR1', 'RR2', 'RR3' ], 'affe' ], 'FILTER size >= 1' );
@resultlist = dpath('/AAA[size == 3]')->match($data); # hash with >= elements
cmp_bag(\@resultlist, [
{
BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
}
], 'FILTER hash size == 3' );
@resultlist = dpath('/AAA[size != 3]')->match($data); # hash with keys
cmp_bag(\@resultlist, [ ], 'FILTER hash size != 3' );
@resultlist = dpath('//AAA[size >= 1]')->match($data); # hash with >= elements
cmp_bag(\@resultlist, [
{
BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
},
{ BBB => { CCC => 'affe' } },
], 'FILTER hash size >= 1' );
@resultlist = dpath('//AAA[ size >= 3 ]')->match($data); # hash with >= 3 elements
cmp_bag(\@resultlist, [
{
BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
},
], 'FILTER hash size >= 3' );
@resultlist = dpath('//AAA[size == 1]')->match($data); # hash with >= elements
cmp_bag(\@resultlist, [
{ BBB => { CCC => 'affe' } },
], 'ANYWHERE + FILTER hash size == 1' );
@resultlist = dpath('//AAA/*/CCC/*')->match($data);
cmp_bag(\@resultlist, [ 'affe', 'XXX', 'YYY', 'ZZZ', 'RR1', 'RR2', 'RR3' ] );
TODO: {
local $TODO = 'far away future spec';
@resultlist = dpath('/AAA/*/CCC/* | /some/where/else/AAA/BBB/CCC')->match($data);
# ( 'XXX', 'YYY', 'ZZZ', 'affe' )
cmp_bag(\@resultlist, [ 'XXX', 'YYY', 'ZZZ', 'RR1', 'RR2', 'RR3', 'affe' ] );
}
@resultlist = dpath('/AAA/*/CCC/*[0]')->match($data);
cmp_bag(\@resultlist, [ 'XXX', 'RR1' ], "ANYSTEP + FILTER int 0" );
@resultlist = dpath('/AAA/*/CCC/*[ 0 ]')->match($data);
cmp_bag(\@resultlist, [ 'XXX', 'RR1' ], "ANYSTEP + FILTER int 0 whitespace" );
@resultlist = dpath('/AAA/*/CCC/*[2]')->match($data);
cmp_bag(\@resultlist, [ 'ZZZ', 'RR3' ], "ANYSTEP + FILTER int 2" );
@resultlist = dpath('/AAA/*/CCC/*[ 2 ]')->match($data);
cmp_bag(\@resultlist, [ 'ZZZ', 'RR3' ], "ANYSTEP + FILTER int 2 whitespace" );
@resultlist = dpath('/AAA/*/CCC/*[-1]')->match($data);
cmp_bag(\@resultlist, [ 'ZZZ', 'RR3' ], "ANYSTEP + FILTER int -1" );
@resultlist = dpath('/AAA/*/CCC/*[ -1 ]')->match($data);
cmp_bag(\@resultlist, [ 'ZZZ', 'RR3' ], "ANYSTEP + FILTER int -1 whitespace" );
@resultlist = dpath('//AAA/*/CCC/*[0]')->match($data);
cmp_bag(\@resultlist, [ 'XXX', 'RR1', 'affe' ], "ANYWHERE + ANYSTEP + FILTER int 0" );
@resultlist = dpath('//AAA/*/CCC/*[ 0 ]')->match($data);
cmp_bag(\@resultlist, [ 'XXX', 'RR1', 'affe' ], "ANYWHERE + ANYSTEP + FILTER int 0 whitespace" );
@resultlist = dpath('//AAA/*/CCC/*[-3]')->match($data);
cmp_bag(\@resultlist, [ 'XXX', 'RR1', ], "ANYWHERE + ANYSTEP + FILTER int -3" );
@resultlist = dpath('//AAA/*/CCC/*[ -3 ]')->match($data);
cmp_bag(\@resultlist, [ 'XXX', 'RR1', ], "ANYWHERE + ANYSTEP + FILTER int -3 whitespace" );
@resultlist = dpath('//AAA/*/CCC/*[2]')->match($data);
cmp_bag(\@resultlist, [ 'ZZZ', 'RR3' ], "ANYWHERE + ANYSTEP + FILTER int 2" );
@resultlist = dpath('//AAA/*/CCC/*[ 2 ]')->match($data);
cmp_bag(\@resultlist, [ 'ZZZ', 'RR3' ], "ANYWHERE + ANYSTEP + FILTER int 2 whitespace" );
@resultlist = dpath('/AAA/*/CCC[2]')->match($data);
cmp_bag(\@resultlist, [ ], "KEY + FILTER int" );
@resultlist = dpath('//AAA/*/CCC[2]')->match($data);
cmp_bag(\@resultlist, [ ], "ANYWHERE + KEY + FILTER int" );
@resultlist = dpath('/AAA/*/CCC[0]')->match($data);
#diag Dumper(\@resultlist);
cmp_bag(\@resultlist, [ [ 'XXX', 'YYY', 'ZZZ' ], [ 'RR1', 'RR2', 'RR3' ] ], "KEY + FILTER int 0" );
@resultlist = dpath('/AAA/*/CCC[1]')->match($data);
cmp_bag(\@resultlist, [ ], "KEY + FILTER int 1" );
@resultlist = dpath('//AAA/*/CCC[0]')->match($data);
#diag Dumper(\@resultlist);
cmp_bag(\@resultlist, [ [ 'XXX', 'YYY', 'ZZZ' ], [ 'RR1', 'RR2', 'RR3' ], 'affe' ], "ANYWHERE + KEY + FILTER int 0" );
@resultlist = dpath('//AAA/*/CCC[1]')->match($data);
#diag Dumper(\@resultlist);
cmp_bag(\@resultlist, [ ], "ANYWHERE + KEY + FILTER int 1" );
# ----------------------------------------
my $data2 = [
'UUU',
'VVV',
'WWW',
{
AAA => { BBB => { CCC => [ qw/ XXX YYY ZZZ / ] } } },
];
@resultlist = dpath('/*')->match($data2); # /*
cmp_bag(\@resultlist, [ 'UUU', 'VVV', 'WWW', { AAA => { BBB => { CCC => [ qw/ XXX YYY ZZZ / ] } } } ], "ROOT + ANYSTEP" );
@resultlist = dpath('/')->match($data2);
cmp_bag(\@resultlist, [ $data2 ], "ROOT" );
@resultlist = dpath('//')->match($data2);
cmp_bag(\@resultlist, [
qw( UUU VVV WWW XXX YYY ZZZ ),
[ qw/ XXX YYY ZZZ / ],
{ CCC => [ qw/ XXX YYY ZZZ / ] },
{ BBB => { CCC => [ qw/ XXX YYY ZZZ / ] } },
{ AAA => { BBB => { CCC => [ qw/ XXX YYY ZZZ / ] } } },
$data2,
], "ANYWHERE" );
@resultlist = dpath('//*[ size == 3 ]')->match($data2);
cmp_bag(\@resultlist, [ [ qw/ XXX YYY ZZZ / ] ], "ANYWHERE + ANYSTEP + FILTER int" );
@resultlist = dpath('/*[2]')->match($data2);
cmp_bag(\@resultlist, [ 'WWW' ], "ROOT + ANYSTEP + FILTER int: plain value" );
@resultlist = dpath('/*[3]')->match($data2);
# ( { AAA => { BBB => { CCC => [ qw/ XXX YYY ZZZ / ] } } } )
cmp_bag(\@resultlist, [ { AAA => { BBB => { CCC => [ qw/ XXX YYY ZZZ / ] } } } ], "ROOT + ANYSTEP + FILTER int: ref value" );
@resultlist = dpath('//*[2]')->match($data2);
cmp_bag(\@resultlist, [ 'WWW', 'ZZZ' ], "ANYWHERE + ANYSTEP + FILTER int" );
# basic eval filters
@resultlist = dpath('/*/AAA/BBB/CCC')->match($data2);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'] ], "FILTER eval prepare" );
@resultlist = dpath('/*/AAA/BBB/CCC[17 == 17]')->match($data2);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'] ], "FILTER eval simple true" );
@resultlist = dpath('/*/AAA/BBB/CCC[0 == 0]')->match($data2);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'] ], "FILTER eval simple true with false values" );
@resultlist = dpath('/*/AAA/BBB/CCC["foo" eq "foo"]')->match($data2);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'] ], "FILTER eval simple true with strings" );
@resultlist = dpath('/*/AAA/BBB/CCC[1 == 2]')->match($data2);
cmp_bag(\@resultlist, [ ], "FILTER eval simple false" );
@resultlist = dpath('/*/AAA/BBB/CCC["foo" eq "bar"]')->match($data2);
cmp_bag(\@resultlist, [ ], "FILTER eval simple false with strings" );
# ----------------------------------------
my $data3 = {
AAA => bless( { BBB => { CCC => [ qw/ XXX YYY ZZZ / ] } }, "Foo::Bar"), # blessed BBB
some => { where => { else => {
AAA => { BBB => { CCC => 'affe' } }, # plain BBB
} } },
neighbourhoods => [
{ 'DDD' => { EEE => bless( { F1 => 'affe',
F2 => 'tiger',
F3 => 'fink',
F4 => 'star',
}, "Affe"),
FFF => 'interesting value' }
},
{ 'DDD' => { EEE => { F1 => 'bla',
F2 => 'bli',
F3 => 'blu',
F4 => 'blo',
},
FFF => 'boring value' }
},
{ 'DDD' => { EEE => { F1 => 'xbla',
F2 => 'xbli',
F3 => 'xblu',
F4 => 'xblo',
},
FFF => 'third value' }
},
{ 'DDD' => { EEE => { F1 => 'ybla',
F2 => 'ybli',
F3 => 'yblu',
F4 => 'yblo',
},
FFF => 'fourth value' }
},
],
};
# ------------------------------
@resultlist = dpath('//AAA/BBB/CCC')->match($data3);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'], 'affe' ], "ANYWHERE + KEYs in blessed structs" );
@resultlist = dpath('//AAA//BBB/CCC')->match($data3);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'], 'affe' ], "ANYWHERE + ANYWHERE + KEYs in blessed structs" );
@resultlist = dpath('//AAA//BBB//CCC')->match($data3);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'], 'affe' ], "ANYWHERE + ANYWHERE + ANYWHERE + KEYs in blessed structs" );
SKIP: {
skip "Filter functions with optional args are deprecated, use the is_XXX(args) form instead.", 1;
@resultlist = dpath('//AAA[ reftype("HASH") ]/BBB/CCC')->match($data3);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'], 'affe' ], "ANYWHERE + FILTER reftype funcall + KEYs" );
}
@resultlist = dpath('//AAA[ is_reftype("HASH") ]/BBB/CCC')->match($data3);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'], 'affe' ], "ANYWHERE + FILTER reftype funcall + KEYs" );
@resultlist = dpath('//AAA[ reftype eq "HASH" ]/BBB/CCC')->match($data3);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'], 'affe' ], "ANYWHERE + FILTER reftype eq + KEYs" );
@resultlist = dpath('//AAA[ reftype =~ /ASH/ ]/BBB/CCC')->match($data3);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'], 'affe' ], "ANYWHERE + FILTER reftype regex + KEYs" );
@resultlist = dpath('//AAA[ isa("Foo::Bar") ]/BBB/CCC')->match($data3);
cmp_bag(\@resultlist, [ ['XXX', 'YYY', 'ZZZ'] ], "ANYWHERE + FILTER isa + KEYs" );
@resultlist = dpath('//DDD/EEE/F1[ value eq "affe" ]/../../FFF')->match($data3); # the DDD/FFF where the neighbor DDD/EEE/F1 == "affe"
cmp_bag(\@resultlist, [ 'interesting value' ], "ANYWHERE + KEYs + FILTER in blessed structs" );
# ------------------------------
@resultlist = dpath('/neighbourhoods/*[0]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'interesting value' ], "ROOT + KEYs + FILTER int 0 + KEYs" );
@resultlist = dpath('/*[key =~ /neighbourhoods/]/*[0]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'interesting value' ], "ROOT + ANYSTEP + FILTER eval key matches + FILTER int 0 + KEYs" );
@resultlist = dpath('/*/.[key =~ /neighbourhoods/]/*[0]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'interesting value' ], "ROOT + ANYSTEP + NOSTEP + FILTER eval key matches + FILTER int 0 + KEYs" );
@resultlist = dpath('/*/*/../.[key =~ /neighbourhoods/]/*[0]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'interesting value' ], "ROOT + ANYSTEP + PARENT + NOSTEP + FILTER eval key matches + FILTER int 0 + KEYs" );
@resultlist = dpath('/neighbourhoods/*[1]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'boring value' ], "ROOT + KEYs + FILTER int 1 + KEYs" );
@resultlist = dpath('//neighbourhoods/*[0]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'interesting value' ], "ANYWHERE + KEYs + FILTER int 0 + KEYs" );
@resultlist = dpath('//neighbourhoods/*[1]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'boring value' ], "ANYWHERE + KEYs + FILTER int 1 + KEYs" );
@resultlist = dpath('//neighbourhoods/*[2]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'third value' ], "ANYWHERE + KEYs + FILTER int 2 + KEYs" );
@resultlist = dpath('//neighbourhoods/*[3]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'fourth value' ], "ANYWHERE + KEYs + FILTER int 3 + KEYs" );
@resultlist = dpath('//neighbourhoods/*[-1]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'fourth value' ], "ANYWHERE + KEYs + FILTER int -1 + KEYs" );
@resultlist = dpath('//neighbourhoods/*[-2]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'third value' ], "ANYWHERE + KEYs + FILTER int -2 + KEYs" );
@resultlist = dpath('//neighbourhoods/*[-3]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'boring value' ], "ANYWHERE + KEYs + FILTER int -3 + KEYs" );
@resultlist = dpath('//neighbourhoods/*[-4]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'interesting value' ], "ANYWHERE + KEYs + FILTER int -4 + KEYs" );
@resultlist = dpath('//neighbourhoods/*[-5]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ ], "ANYWHERE + KEYs + FILTER too negative int + KEYs" );
@resultlist = dpath('//neighbourhoods/*[20]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ ], "ANYWHERE + KEYs + FILTER too high int + KEYs" );
@resultlist = dpath('/*[key eq "neighbourhoods"]/*[0]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'interesting value' ], "ROOT + ANYSTEP + FILTER eval key eq + FILTER int" );
@resultlist = dpath('/*/.[key eq "neighbourhoods"]/*[0]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'interesting value' ], "ROOT + ANYSTEP + NOSTEP + FILTER eval key eq + FILTER int" );
@resultlist = dpath('/*/*/../.[key eq "neighbourhoods"]/*[0]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'interesting value' ], "ROOT + ANYSTEP + PARENT + NOSTEP + FILTER eval key eq + FILTER int" );
@resultlist = dpath('/*[key =~ /neigh.*hoods/]/*[0]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'interesting value' ], "ROOT + ANYSTEP + FILTER eval key matches + FILTER int" );
@resultlist = dpath('/*/.[key =~ /neigh.*hoods/]/*[0]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'interesting value' ], "ROOT + ANYSTEP + NOSTEP + FILTER eval key matches + FILTER int" );
@resultlist = dpath('/*/*/../.[key =~ /neigh.*hoods/]/*[0]/DDD/FFF')->match($data3);
# ( 'interesting value' )
cmp_bag(\@resultlist, [ 'interesting value' ], "ROOT + ANYSTEP + PARENT + NOSTEP + FILTER eval key matches + FILTER int" );
# ----------------------------------------
my $data4 = {
AAA => { BBB => { CCC => [ qw/
XXX
YYY
ZZZ
XXXX
YYYY
ZZZZ
/ ] } },
some => { where => { else => {
AAA => { BBB => { CCC => 'affe' } }, # plain BBB
} } },
neighbourhoods => [
{ 'DDD' => { EEE => { F1 => 'affe',
F2 => 'tiger',
F3 => 'fink',
F4 => 'star',
},
FFF => 'interesting value' }
},
{ 'DDD' => { EEE => { F1 => 'bla',
F2 => 'bli',
F3 => 'blu',
F4 => 'blo',
},
FFF => 'boring value' }
},
],
};
@resultlist = dpath('//AAA/BBB/CCC/*[ affe ]')->match($data4);
cmp_bag(\@resultlist, [ 'affe' ], "FILTER: affe" );
@resultlist = dpath('/AAA/BBB/CCC/*[ idx == 1 ]')->match($data4);
cmp_bag(\@resultlist, [ 'YYY' ], "FILTER: index" );
@resultlist = dpath('/AAA/BBB/CCC/*[ 1 ]')->match($data4);
cmp_bag(\@resultlist, [ 'YYY' ], "FILTER: index" );
@resultlist = dpath('//AAA/BBB/CCC/*[ /..../ ]')->match($data4);
cmp_bag(\@resultlist, [ 'XXXX', 'YYYY', 'ZZZZ', 'affe' ], "FILTER eval regex five chars" );
@resultlist = dpath('//AAA/BBB/CCC/*[ /[A-Z]+/ ]')->match($data4);
cmp_bag(\@resultlist, [ 'XXX', 'YYY', 'ZZZ', 'XXXX', 'YYYY', 'ZZZZ', ], "FILTER eval regex just capitalizes" );
SKIP:
{
skip "quote semantics changed", 1;
@resultlist = dpath('//AAA/BBB/CCC/"*"[ m/[A-Z]+/ ]')->match($data4);
cmp_bag(\@resultlist, [ 'XXX', 'YYY', 'ZZZ', 'XXXX', 'YYYY', 'ZZZZ', ], "FILTER eval regex with quotes and slashes" );
}
@resultlist = dpath('//AAA/BBB/*[key eq "CCC"]')->match($data);
cmp_bag(\@resultlist, [
[ qw/ XXX YYY ZZZ / ],
'affe',
], "ANYWHERE + STEP + ANYSTEP + FILTER eval key eq string" );
@resultlist = dpath('//AAA/BBB/.[key eq "CCC"]')->match($data);
cmp_bag(\@resultlist, [ ], "ANYWHERE + STEP + NOSTEP + FILTER eval key eq string" );
@resultlist = dpath('//AAA/*[ key eq "CCC" ]')->match($data);
cmp_bag(\@resultlist, [
], "ANYWHERE + ANYSTEP + FILTER eval key eq string" );
@resultlist = dpath('//AAA/*/*[ key eq "CCC" ]')->match($data);
cmp_bag(\@resultlist, [
[ qw/ XXX YYY ZZZ / ],
[ qw/ RR1 RR2 RR3 / ],
'affe',
], "ANYWHERE + ANYSTEP + ANYSTEP + FILTER eval key eq string" );
@resultlist = dpath('//AAA/*/CCC')->match($data);
cmp_bag(\@resultlist, [
[ qw/ XXX YYY ZZZ / ],
[ qw/ RR1 RR2 RR3 / ],
'affe',
], "ANYWHERE + STEP + ANYSTEP + STEP" );
@resultlist = dpath('//AAA/*/CCC/.[ key eq "CCC" ]')->match($data);
cmp_bag(\@resultlist, [
[ qw/ XXX YYY ZZZ / ],
[ qw/ RR1 RR2 RR3 / ],
'affe',
], "ANYWHERE + STEP + ANYSTEP + STEP + FILTER eval key eq last STEP" );
@resultlist = dpath('//.[ key eq "DD DD" ]')->match($data);
cmp_bag(\@resultlist, [
{ 'EE/E' => { CCC => 'zomtec' } }
], "ANYWHERE + NOSTEP + FILTER eval key" );
@resultlist = dpath('//.[ key eq "EE/E" ]')->match($data);
cmp_bag(\@resultlist, [
{ CCC => 'zomtec' }
], "ANYWHERE + NOSTEP + FILTER eval key + slash in eval" );
@resultlist = dpath('//AAA/*/CCC/.[ key eq "CCC" ]')->match($data);
cmp_bag(\@resultlist, [
[ qw/ XXX YYY ZZZ / ],
[ qw/ RR1 RR2 RR3 / ],
'affe',
], "ANYWHERE + STEP + ANYSTEP + STEP + FILTER eval key eq last STEP" );
@resultlist = dpath('//AAA/*[ key =~ /.../ ]')->match($data);
cmp_bag(\@resultlist, [
{ CCC => [ qw/ XXX YYY ZZZ / ] },
{ CCC => [ qw/ RR1 RR2 RR3 / ] },
{ EEE => [ qw/ uuu vvv www / ] },
{ CCC => 'affe' },
], "ANYWHERE + STEP + ANYSTEP + FILTER eval key matches" );
@resultlist = dpath('//AAA/*[ key =~ qr(...) ]')->match($data);
cmp_bag(\@resultlist, [
{ CCC => [ qw/ XXX YYY ZZZ / ] },
{ CCC => [ qw/ RR1 RR2 RR3 / ] },
{ EEE => [ qw/ uuu vvv www / ] },
{ CCC => 'affe' },
], "ROOT + ANYSTEP + FILTER eval key matches qr()" );
@resultlist = dpath('//AAA/*[ key =~ m(...) ]')->match($data);
cmp_bag(\@resultlist, [
{ CCC => [ qw/ XXX YYY ZZZ / ] },
{ CCC => [ qw/ RR1 RR2 RR3 / ] },
{ EEE => [ qw/ uuu vvv www / ] },
{ CCC => 'affe' },
], "ROOT + ANYSTEP + FILTER eval with key matches m(...)" );
@resultlist = dpath('//AAA/*[ key =~ /CC/ ]')->match($data);
cmp_bag(\@resultlist, [ ], "ROOT + ANYSTEP + FILTER eval with key matches /CC/" );
@resultlist = dpath('//AAA/*/*[ key =~ /CC/ ]')->match($data);
cmp_bag(\@resultlist, [
[ qw/ XXX YYY ZZZ / ],
[ qw/ RR1 RR2 RR3 / ],
'affe',
], "ROOT + ANYSTEP + ANYSTEP + FILTER eval with key matches /CC/" );
@resultlist = dpath('//CCC/*[value eq "RR2"]')->match($data);
#print STDERR "resultlist = ", Dumper(\@resultlist);
cmp_bag(\@resultlist, [ 'RR2' ], "ANYWHERE + ANYSTEP + FILTER eval value" );
# print STDERR "**************************************************\n";
# print STDERR "resultlist = ", Dumper($data ~~ dpath('//CCC/*[value eq "RR2"]')); # /..
@resultlist = dpath('//CCC/*[value eq "RR2"]/..')->match($data);
#print STDERR "resultlist = ", Dumper(\@resultlist);
cmp_bag(\@resultlist, [ [ 'RR1', 'RR2', 'RR3' ] ], "ANYWHERE + ANYSTEP + FILTER eval value + PARENT" );
# print STDERR "**************************************************\n";
@resultlist = dpath('//CCC/*[value eq "RR2"]/../..')->match($data);
#print STDERR "resultlist = ", Dumper(\@resultlist);
cmp_bag(\@resultlist, [ { CCC => [ 'RR1', 'RR2', 'RR3' ] } ], "ANYWHERE + ANYSTEP + FILTER eval value + 2xPARENT" );
# ----------------------------------------
my $data5 = {
AAA => { BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => [ qw/ RR1 RR2 RR3 / ] },
DDD => { EEE => [ qw/ uuu vvv www / ] },
"*" => { CCC => [ qw/ ASTAR BSTAR CSTAR / ] },
"//" => { CCC => [ qw/ ASLASH BSLASH CSLASH / ] },
".." => { CCC => [ qw/ ADOTDOT BDOTDOT CDOTDOT / ] },
"." => { CCC => [ qw/ ADOT BDOT CDOT / ] },
},
};
@resultlist = dpath('/AAA/*/CCC')->match($data5);
cmp_bag(\@resultlist, [ [ qw/ XXX YYY ZZZ / ],
[ qw/ RR1 RR2 RR3 / ],
[ qw/ ASTAR BSTAR CSTAR / ],
[ qw/ ASLASH BSLASH CSLASH / ],
[ qw/ ADOTDOT BDOTDOT CDOTDOT / ],
[ qw/ ADOT BDOT CDOT / ],
], "KEYs + ANYSTEP again" );
@resultlist = dpath('/AAA/"*"/CCC')->match($data5);
cmp_bag(\@resultlist, [ [ qw/ ASTAR BSTAR CSTAR / ] ], "KEYs + (*)" );
@resultlist = dpath('/AAA/"//"/CCC')->match($data5);
cmp_bag(\@resultlist, [ [ qw/ ASLASH BSLASH CSLASH / ] ], "KEYs + (//)" );
@resultlist = dpath('/AAA/".."/CCC')->match($data5);
cmp_bag(\@resultlist, [ [ qw/ ADOTDOT BDOTDOT CDOTDOT / ] ], "KEYs + (..)" );
@resultlist = dpath('/AAA/"."/CCC')->match($data5);
cmp_bag(\@resultlist, [ [ qw/ ADOT BDOT CDOT / ] ], "KEYs + (.)" );
# ----------------------------------------
my $data6 = bless [
[ 2, 3, 5, 7, 11, 13, 17, 19, 23 ],
[ 1, 2, 3, 4 ],
[ qw( AAA BBB CCC DDD ) ],
[ 11, 22, 33 ],
{
hot => {
stuff => {
ahead => [ qw( affe tiger fink star ) ],
"" => "some value on empty key",
}
}
},
], "Some::Funky::Stuff";
@resultlist = dpath('/.[ isa("Foo::Bar") ]')->match($data6);
cmp_bag(\@resultlist, [ ], "ROOT + NOSTEP + FILTER isa (with no match)" );
@resultlist = dpath('/.[ isa("Some::Funky::Stuff") ]')->match($data6);
cmp_bag(\@resultlist, [ $data6 ], "ROOT + NOSTEP + FILTER isa" );
# chaining filters by using NOSTEP
@resultlist = dpath('/.[ isa("Some::Funky::Stuff") ]/.[ size == 5 ]')->match($data6);
cmp_bag(\@resultlist, [ $data6 ], "ROOT + NOSTEP + FILTER isa + FILTER size" );
@resultlist = dpath('/.[ isa("Some::Funky::Stuff") ]/.[ size == 5 ]/.[ reftype eq "ARRAY" ]')->match($data6);
cmp_bag(\@resultlist, [ $data6 ], "ROOT + NOSTEP + FILTER isa + FILTER size + FILTER reftype" );
@resultlist = dpath('//.[ size == 4 ]')->match($data6);
cmp_bag(\@resultlist, [
[ 1, 2, 3, 4 ],
[ qw( AAA BBB CCC DDD ) ],
[ qw( affe tiger fink star ) ],
], "ANYWHERE + NOSTEP + FILTER int" );
@resultlist = dpath('//""/')->match($data6);
cmp_bag(\@resultlist, [ "some value on empty key" ], "empty key");
my $data7 = [
[ 2, 3, 5, 7, 11, 13, 17, 19, 23 ],
[ 1, 2, 3, 4 ],
[ qw( AAA BBB CCC DDD ) ],
[ 11, 22, 33 ],
{
hot => {
stuff => {
ahead => [ qw( affe tiger fink star ) ],
"" => "some value on empty key",
}
}
},
];
@resultlist = dpathr('//.[ size == 4 ]')->match($data7);
cmp_bag(\@resultlist, [
\($data7->[1]),
\($data7->[2]),
\($data7->[4]{hot}{stuff}{ahead}),
], "ANYWHERE + NOSTEP + FILTER int (REFERENCES)" );
${$resultlist[0]} = [ qw(one two three four) ];
${$resultlist[1]} = "there once was an array in LA";
${$resultlist[2]} = { affe => "tiger",
fink => "star",
};
my $data7_expected_change = [
[ 2, 3, 5, 7, 11, 13, 17, 19, 23 ],
[ 'one', 'two', 'three', 'four' ],
"there once was an array in LA",
[ 11, 22, 33 ],
{
hot => {
stuff => {
ahead => { affe => "tiger",
fink => "star" },
"" => "some value on empty key",
}
}
},
];
# diag Dumper(\@resultlist);
# diag Dumper($data7_expected_change);
# diag Dumper($data7);
cmp_bag($data7, $data7_expected_change, "ANYWHERE + NOSTEP + FILTER int (REFERENCES CHANGED)" );
my $data8 = {
AAA => { BBB => { CCC => [ qw/ XXX YYY ZZZ / ] },
RRR => { CCC => bless([
[ qw/ RR1 RR2 RR3 /],
[ 11, 22, 33 ],
], "Zomtec") },
DDD => { EEE => [ qw/ uuu vvv www / ] },
},
some => { where => { else => {
AAA => { BBB => { CCC => 'affe' } },
} } },
strange_keys => { 'DD DD' => { 'EE/E' => { CCC => 'zomtec' } } },
};
TODO: {
local $TODO = "REAL TODO FIX ME SOON!";
@resultlist = dpath('//CCC//*[ value eq "RR3" ]/..')->match($data8); # /../*[1]');
# print STDERR "resultlist = ", Dumper(\@resultlist);
cmp_bag(\@resultlist, [ [ 11, 22, 33 ] ], "ANYWHERE + ANYSTEP + FILTER eval value + PARENT + bless" );
}
TODO: {
local $TODO = "REAL TODO FIX ME SOON! (but depends on test before)";
@resultlist = dpath('//CCC//*[ value eq "RR3" ]/../../*[1]')->match($data8);
# print STDERR "resultlist = ", Dumper(\@resultlist);
cmp_bag(\@resultlist, [ [ 11, 22, 33 ] ], "ANYWHERE + ANYSTEP + FILTER eval value + 2xPARENT + FILTER int + bless" );
}
done_testing();
|