File: references.t

package info (click to toggle)
libdata-dpath-perl 0.60-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,328 kB
  • sloc: perl: 3,737; makefile: 2
file content (130 lines) | stat: -rw-r--r-- 5,430 bytes parent folder | download | duplicates (5)
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
#! /usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Test::Deep;
use Data::DPath qw'dpathr dpath';
use Data::Dumper;

# local $Data::DPath::DEBUG = 1;

use_ok( 'Data::DPath' );

my $data = {
            'goal' => 15,
           };

# ==================================================

my $res;

# --------------------------------------------------

$res = [ dpath('/goal')->match($data) ];
isnt("".\($data->{goal}), "".\($res->[0]), "ROOT/KEY - references are to copies");
# diag "orig:    " . $data->{goal} . " -- " . \($data->{goal});
# diag "dpath:   " . $res->[0]     . " -- " . \($res->[0]);

$res = [ dpathr('/goal')->match($data) ];
is("".\($data->{goal}), "".$res->[0], "ROOT/KEY - references are the same");
# diag "orig:   \\" . $data->{goal} . " -- " . \($data->{goal});
# diag "dpathr: \\" . ${$res->[0]}  . " -- " . $res->[0];

# --------------------------------------------------

$res = [ dpath('//goal')->match($data) ];
isnt("".\($data->{goal}), "".\($res->[0]), "ANYWHERE/KEY - references are to copies");
# diag "orig:    " . $data->{goal} . " -- " . \($data->{goal});
# diag "dpath:   " . $res->[0]     . " -- " . \($res->[0]);

$res = [ dpathr('//goal')->match($data) ];
is("".\($data->{goal}), "".$res->[0], "ANYWHERE/KEY - references are the same");
# diag "orig:   \\" . $data->{goal} . " -- " . \($data->{goal});
# diag "dpathr: \\" . ${$res->[0]}  . " -- " . $res->[0];

# --------------------------------------------------

$res = [ dpath('/*')->match($data) ];
isnt("".\($data->{goal}), "".\($res->[0]), "ROOT/ANYSTEP - references are to copies");
# diag "orig:    " . $data->{goal} . " -- " . \($data->{goal});
# diag "dpath:   " . $res->[0]     . " -- " . \($res->[0]);

$res = [ dpathr('/*')->match($data) ];
is("".\($data->{goal}), "".$res->[0], "ROOT/ANYSTEP - references are the same");
# diag "orig:   \\" . $data->{goal} . " -- " . \($data->{goal});
# diag "dpathr: \\" . ${$res->[0]}  . " -- " . $res->[0];

# --------------------------------------------------

$res = [ dpath('//*')->match($data) ];
isnt("".\($data->{goal}), "".\($res->[0]), "ANYWHERE/ANYSTEP - references are to copies");
# diag "orig:    " . $data->{goal} . " -- " . \($data->{goal});
# diag "dpath:   " . $res->[0]     . " -- " . \($res->[0]);

$res = [ dpathr('//*')->match($data) ];
is("".\($data->{goal}), "".$res->[0], "ANYWHERE/ANYSTEP - references are the same");
# diag "orig:   \\" . $data->{goal} . " -- " . \($data->{goal});
# diag "dpathr: \\" . ${$res->[0]}  . " -- " . $res->[0];

# --------------------------------------------------

my $old = $data->{goal};
my $new = 17;

# --------------------------------------------------

is($data->{goal}, $old, "ANYWHERE/KEY -- value before change");
$res = [ dpathr('//goal')->match($data) ];
${$res->[0]} = $new;
is($data->{goal}, $new, "ANYWHERE/KEY -- value after change");
$res = [ dpathr("//goal[ value eq $new]")->match($data) ];
is(${$res->[0]}, $new, "ANYWHERE/KEY[FILTER] -- found again with new value");
${$res->[0]} = $old;
is($data->{goal}, $old, "ANYWHERE/KEY[FILTER] -- value changed back to orig");
$res = [ dpathr("//goal[ value eq $old]")->match($data) ];
is(${$res->[0]}, $old, "ANYWHERE/KEY[FILTER] -- found again with orig value");

# --------------------------------------------------

is($data->{goal}, $old, "modify -- ANYWHERE/KEY -- value before change");
$res = [ dpathr("//goal")->match($data) ];
${$res->[0]} = $new;
is($data->{goal}, $new, "modify -- ANYWHERE/KEY -- value after change");
$res = [ dpathr("//goal[ value eq $new]")->match($data) ];
is(${$res->[0]}, $new, "modify -- ANYWHERE/KEY[FILTER] -- found again with new value");
${$res->[0]} = $old;
is($data->{goal}, $old, "modify -- ANYWHERE/KEY[FILTER] -- value changed back to orig");
$res = [ dpathr("//goal[ value eq $old]")->match($data) ];
is(${$res->[0]}, $old, "modify -- ANYWHERE/KEY[FILTER] -- found again with orig value");

# --------------------------------------------------

is($data->{goal}, $old, "modify -- ANYWHERE/ANYSTEP -- value before change");
$res = [ dpathr("//*")->match($data) ];
${$res->[0]} = $new;
is($data->{goal}, $new, "modify -- ANYWHERE/ANYSTEP -- value after change");
$res = [ dpathr("//*[ value eq $new]")->match($data) ];
is(${$res->[0]}, $new, "modify -- ANYWHERE/ANYSTEP[FILTER] -- found again with new value");
${$res->[0]} = $old;
is($data->{goal}, $old, "modify -- ANYWHERE/ANYSTEP[FILTER] -- value changed back to orig");
$res = [ dpathr("//*[ value eq $old]")->match($data) ];
is(${$res->[0]}, $old, "modify -- ANYWHERE/ANYSTEP[FILTER] -- found again with orig value");

# --------------------------------------------------

is($data->{goal}, $old, "modify -- ANYWHERE/ANYSTEP/PARENT/ANYSTEP -- value before change");
$res = [ dpathr("//*/../*")->match($data) ];
${$res->[0]} = $new;
is($data->{goal}, $new, "modify -- ANYWHERE/ANYSTEP/PARENT/ANYSTEP -- value after change");
$res = [ dpathr("//*/../*[ value eq $new]")->match($data) ];
is(${$res->[0]}, $new, "modify -- ANYWHERE/ANYSTEP/PARENT/ANYSTEP[FILTER] -- found again with new value");
${$res->[0]} = $old;
is($data->{goal}, $old, "modify -- ANYWHERE/ANYSTEP/PARENT/ANYSTEP[FILTER] -- value changed back to orig");
$res = [ dpathr("//*/../*[ value eq $old]")->match($data) ];
is(${$res->[0]}, $old, "modify -- ANYWHERE/ANYSTEP/PARENT/ANYSTEP[FILTER] -- found again with orig value");

# --------------------------------------------------

done_testing();