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
|
#! /usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Deep;
use Data::DPath 'dpath';
use Data::Dumper;
# local $Data::DPath::DEBUG = 1;
use_ok( 'Data::DPath' );
my $data = {
'zero' => {
'goal' => 0,
'count' => "zero",
},
'undef' => {
'goal' => undef,
'count' => "UNDEF",
},
'normal' => {
'normal' => {
'goal' => 15,
'data_size' => 254242,
}
}
};
# ==================================================
my $res;
$res = [ dpath('//goal[ value == 15]')->match($data) ];
cmp_bag($res, [ 15 ], "leaf with value");
$res = [ dpath('//goal')->match($data) ];
cmp_bag($res, [ 15, 0, undef ], "many leafs with value");
$res = [ dpath('//goal[value == 15]/../data_size')->match($data) ];
cmp_bag($res, [ 254242 ], "data_size via leaf");
$res = [ dpath('//goal[ value eq 0 ]')->match($data) ];
cmp_bag($res, [ 0 ], "leaf of value 0");
$res = [ dpath('//goal[value eq 0]/../count')->match($data) ];
cmp_bag($res, [ "zero" ], "data_size via leaf of value 0");
$res = [ dpath('//goal[ value eq undef ]')->match($data) ];
cmp_bag($res, [ undef ], "leaf of value undef");
$res = [ dpath('//goal[ value eq undef ]/../count')->match($data) ];
cmp_bag($res, [ "UNDEF" ], "data_size via leaf of value undef");
$res = [ dpath('/normal/normal/goal')->match($data) ];
cmp_bag($res, [ 15 ], "absolute path - leaf with value");
$res = [ dpath('/zero/goal')->match($data) ];
cmp_bag($res, [ 0 ], "absolute path - leaf of value 0");
done_testing();
|