line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package XML::Parser::Lite::Tree::XPath::Test;
|
2
|
|
|
|
|
|
|
|
3
|
28
|
|
|
28
|
|
922128
|
use strict;
|
|
28
|
|
|
|
|
75
|
|
|
28
|
|
|
|
|
1107
|
|
4
|
28
|
|
|
28
|
|
149
|
use vars qw(@ISA @EXPORT);
|
|
28
|
|
|
|
|
55
|
|
|
28
|
|
|
|
|
1984
|
|
5
|
28
|
|
|
28
|
|
158
|
use Test::More;
|
|
28
|
|
|
|
|
48
|
|
|
28
|
|
|
|
|
205
|
|
6
|
|
|
|
|
|
|
|
7
|
28
|
|
|
28
|
|
52107
|
use XML::Parser::Lite::Tree;
|
|
28
|
|
|
|
|
213971
|
|
|
28
|
|
|
|
|
1527
|
|
8
|
28
|
|
|
28
|
|
19760
|
use XML::Parser::Lite::Tree::XPath;
|
|
28
|
|
|
|
|
95
|
|
|
28
|
|
|
|
|
1038
|
|
9
|
28
|
|
|
28
|
|
190
|
use Data::Dumper;
|
|
28
|
|
|
|
|
56
|
|
|
28
|
|
|
|
|
69120
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
require Exporter;
|
12
|
|
|
|
|
|
|
@ISA = qw(Exporter);
|
13
|
|
|
|
|
|
|
@EXPORT = qw(
|
14
|
|
|
|
|
|
|
set_xml
|
15
|
|
|
|
|
|
|
test_tree
|
16
|
|
|
|
|
|
|
test_nodeset
|
17
|
|
|
|
|
|
|
test_number
|
18
|
|
|
|
|
|
|
test_string
|
19
|
|
|
|
|
|
|
test_error
|
20
|
|
|
|
|
|
|
test_boolean
|
21
|
|
|
|
|
|
|
);
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our $xpath;
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub set_xml {
|
26
|
29
|
|
|
29
|
0
|
237
|
my ($xml) = @_;
|
27
|
|
|
|
|
|
|
|
28
|
29
|
|
|
|
|
553
|
$xml =~ s/>\s+>
|
29
|
29
|
|
|
|
|
649
|
$xml =~ s/^\s*(.*?)\s*$/$1/;
|
30
|
|
|
|
|
|
|
|
31
|
29
|
|
|
|
|
452
|
my $parser = new XML::Parser::Lite::Tree(process_ns => 1);
|
32
|
29
|
|
|
|
|
4936
|
my $tree = $parser->parse($xml);
|
33
|
29
|
|
|
|
|
50188
|
$xpath = new XML::Parser::Lite::Tree::XPath($tree);
|
34
|
|
|
|
|
|
|
}
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub test_tree {
|
37
|
41
|
|
|
41
|
0
|
226
|
my ($path, $dump) = @_;
|
38
|
|
|
|
|
|
|
|
39
|
41
|
|
|
|
|
250
|
my $tokener = XML::Parser::Lite::Tree::XPath::Tokener->new();
|
40
|
41
|
50
|
|
|
|
159
|
if (!$tokener->parse($path)){
|
41
|
0
|
|
|
|
|
0
|
print "Path: $path\n";
|
42
|
0
|
|
|
|
|
0
|
print "Failed toke: ($tokener->{error})\n";
|
43
|
0
|
|
|
|
|
0
|
ok(0);
|
44
|
0
|
|
|
|
|
0
|
return;
|
45
|
|
|
|
|
|
|
}
|
46
|
|
|
|
|
|
|
|
47
|
41
|
|
|
|
|
184
|
my $tree = XML::Parser::Lite::Tree::XPath::Tree->new();
|
48
|
41
|
50
|
|
|
|
163
|
if (!$tree->build_tree($tokener->{tokens})){
|
49
|
0
|
|
|
|
|
0
|
print "Path: $path\n";
|
50
|
0
|
|
|
|
|
0
|
print "Failed tree: ($tree->{error})\n";
|
51
|
|
|
|
|
|
|
#print Dumper $tree;
|
52
|
0
|
|
|
|
|
0
|
ok(0);
|
53
|
0
|
|
|
|
|
0
|
return;
|
54
|
|
|
|
|
|
|
}
|
55
|
|
|
|
|
|
|
|
56
|
41
|
|
|
|
|
174
|
my $dump_got = $tree->dump_flat();
|
57
|
|
|
|
|
|
|
|
58
|
41
|
|
|
|
|
216
|
ok($dump_got eq $dump);
|
59
|
|
|
|
|
|
|
|
60
|
41
|
50
|
|
|
|
28026
|
unless ($dump_got eq $dump){
|
61
|
0
|
|
|
|
|
0
|
print "Path: $path\n";
|
62
|
0
|
|
|
|
|
0
|
print "Expected: $dump\n";
|
63
|
0
|
|
|
|
|
0
|
print "Dump: $dump_got\n";
|
64
|
0
|
|
|
|
|
0
|
print $tree->dump_tree();
|
65
|
|
|
|
|
|
|
}
|
66
|
|
|
|
|
|
|
|
67
|
41
|
|
|
|
|
768
|
return $dump_got;
|
68
|
|
|
|
|
|
|
}
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub test_nodeset {
|
71
|
73
|
|
|
73
|
0
|
1023
|
my ($path, $expected) = @_;
|
72
|
|
|
|
|
|
|
|
73
|
73
|
|
|
|
|
356
|
my $nodes = $xpath->select_nodes($path);
|
74
|
|
|
|
|
|
|
|
75
|
73
|
50
|
|
|
|
322
|
unless ('ARRAY' eq ref $nodes){
|
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
0
|
print "Error: $xpath->{error}\n";
|
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
0
|
ok(0);
|
80
|
0
|
|
|
|
|
0
|
ok(0) for @{$expected};
|
|
0
|
|
|
|
|
0
|
|
81
|
0
|
|
|
|
|
0
|
return;
|
82
|
|
|
|
|
|
|
}
|
83
|
|
|
|
|
|
|
|
84
|
73
|
|
|
|
|
132
|
my $bad = 0;
|
85
|
|
|
|
|
|
|
|
86
|
73
|
|
|
|
|
105
|
my $ok = scalar(@{$nodes}) == scalar(@{$expected});
|
|
73
|
|
|
|
|
137
|
|
|
73
|
|
|
|
|
192
|
|
87
|
73
|
50
|
|
|
|
209
|
$bad++ unless $ok;
|
88
|
73
|
|
|
|
|
387
|
ok($ok);
|
89
|
|
|
|
|
|
|
|
90
|
73
|
50
|
|
|
|
70008
|
if (!$ok){
|
91
|
0
|
|
|
|
|
0
|
print "# wrong node count. got ".scalar(@{$nodes}).", expected ".scalar(@{$expected})."\n";
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
92
|
|
|
|
|
|
|
}
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
73
|
|
|
|
|
143
|
my $i = 0;
|
96
|
73
|
|
|
|
|
127
|
for my $xnode(@{$expected}){
|
|
73
|
|
|
|
|
200
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# $xnode is a hash ref which should match stuff in $nodes[$i]
|
99
|
|
|
|
|
|
|
|
100
|
247
|
|
|
|
|
339
|
for my $key(keys %{$xnode}){
|
|
247
|
|
|
|
|
858
|
|
101
|
|
|
|
|
|
|
|
102
|
473
|
100
|
|
|
|
81144
|
if ($key eq 'nodename'){
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
241
|
|
|
|
|
690
|
$ok = $nodes->[$i]->{name} eq $xnode->{$key};
|
105
|
|
|
|
|
|
|
|
106
|
241
|
50
|
|
|
|
569
|
print "# node name - expected: $xnode->{$key}, got: $nodes->[$i]->{name}\n" unless $ok;
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
}elsif ($key eq 'attributecount'){
|
109
|
|
|
|
|
|
|
|
110
|
1
|
|
|
|
|
4
|
$ok = scalar(keys %{$nodes->[$i]->{attributes}}) == $xnode->{$key};
|
|
1
|
|
|
|
|
4
|
|
111
|
|
|
|
|
|
|
|
112
|
1
|
50
|
|
|
|
6
|
print "# attribute count - expected: $xnode->{$key}, got: ".scalar(keys %{$nodes->[$i]->{attributes}})."\n" unless $ok;
|
|
0
|
|
|
|
|
0
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
}elsif ($key eq 'type'){
|
115
|
|
|
|
|
|
|
|
116
|
8
|
|
|
|
|
26
|
$ok = $nodes->[$i]->{type} eq $xnode->{$key};
|
117
|
|
|
|
|
|
|
|
118
|
8
|
50
|
|
|
|
34
|
print "# node type - expected: $xnode->{$key}, got: $nodes->[$i]->{type}\n" unless $ok;
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
}elsif ($key eq 'value'){
|
121
|
|
|
|
|
|
|
|
122
|
2
|
|
|
|
|
7
|
$ok = $nodes->[$i]->{value} eq $xnode->{$key};
|
123
|
|
|
|
|
|
|
|
124
|
2
|
50
|
|
|
|
7
|
print "# value - expected: $xnode->{$key}, got: $nodes->[$i]->{value}\n" unless $ok;
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
}else{
|
127
|
221
|
|
|
|
|
756
|
$ok = $nodes->[$i]->{attributes}->{$key} eq $xnode->{$key};
|
128
|
|
|
|
|
|
|
|
129
|
221
|
50
|
|
|
|
518
|
print "# attribute $key - expected: $xnode->{$key}, got: $nodes->[$i]->{attributes}->{$key}\n" unless $ok;
|
130
|
|
|
|
|
|
|
}
|
131
|
|
|
|
|
|
|
|
132
|
473
|
50
|
|
|
|
843
|
$bad++ unless $ok;
|
133
|
473
|
|
|
|
|
1131
|
ok($ok);
|
134
|
|
|
|
|
|
|
}
|
135
|
|
|
|
|
|
|
|
136
|
247
|
|
|
|
|
88450
|
$i++;
|
137
|
|
|
|
|
|
|
}
|
138
|
|
|
|
|
|
|
|
139
|
73
|
50
|
|
|
|
4800
|
if ($bad){
|
140
|
0
|
|
|
|
|
0
|
print "# codes don't match. got:\n";
|
141
|
0
|
|
|
|
|
0
|
for my $node(@{$nodes}){
|
|
0
|
|
|
|
|
0
|
|
142
|
0
|
|
|
|
|
0
|
print "# \t";
|
143
|
0
|
|
|
|
|
0
|
print "($node->{type} : $node->{order}) ";
|
144
|
0
|
|
|
|
|
0
|
print "$node->{name}";
|
145
|
0
|
|
|
|
|
0
|
for my $key(keys %{$node->{attributes}}){
|
|
0
|
|
|
|
|
0
|
|
146
|
0
|
|
|
|
|
0
|
print ", $key=$node->{attributes}->{$key}";
|
147
|
|
|
|
|
|
|
}
|
148
|
0
|
|
|
|
|
0
|
print "\n";
|
149
|
|
|
|
|
|
|
}
|
150
|
0
|
|
|
|
|
0
|
print "# expected:\n";
|
151
|
0
|
|
|
|
|
0
|
my $i = 1;
|
152
|
0
|
|
|
|
|
0
|
for my $node(@{$expected}){
|
|
0
|
|
|
|
|
0
|
|
153
|
0
|
|
|
|
|
0
|
print "# \t$i";
|
154
|
0
|
|
|
|
|
0
|
for my $key(keys %{$node}){
|
|
0
|
|
|
|
|
0
|
|
155
|
0
|
|
|
|
|
0
|
print ", $key={$node->{$key}}";
|
156
|
|
|
|
|
|
|
}
|
157
|
0
|
|
|
|
|
0
|
print "\n";
|
158
|
0
|
|
|
|
|
0
|
$i++;
|
159
|
|
|
|
|
|
|
}
|
160
|
0
|
|
|
|
|
0
|
print Dumper $nodes;
|
161
|
|
|
|
|
|
|
}
|
162
|
|
|
|
|
|
|
}
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub test_number {
|
165
|
15
|
|
|
15
|
0
|
65
|
my ($path, $expected) = @_;
|
166
|
|
|
|
|
|
|
|
167
|
15
|
|
|
|
|
59
|
my $ret = $xpath->query($path);
|
168
|
|
|
|
|
|
|
|
169
|
15
|
50
|
|
|
|
39
|
if (!$ret){
|
170
|
0
|
|
|
|
|
0
|
print "Error: $xpath->{error}\n";
|
171
|
0
|
|
|
|
|
0
|
ok(0);
|
172
|
0
|
|
|
|
|
0
|
ok(0);
|
173
|
0
|
|
|
|
|
0
|
return;
|
174
|
|
|
|
|
|
|
}
|
175
|
|
|
|
|
|
|
|
176
|
15
|
|
|
|
|
59
|
ok($ret->{type} eq 'number');
|
177
|
|
|
|
|
|
|
|
178
|
15
|
50
|
|
|
|
15437
|
if ($ret->{type} eq 'number'){
|
179
|
15
|
|
|
|
|
50
|
ok($ret->{value} == $expected);
|
180
|
|
|
|
|
|
|
|
181
|
15
|
50
|
|
|
|
5855
|
if ($ret->{value} != $expected){
|
182
|
0
|
|
|
|
|
0
|
print "expected $expected, got $ret->{value}\n";
|
183
|
|
|
|
|
|
|
}
|
184
|
|
|
|
|
|
|
}else{
|
185
|
0
|
|
|
|
|
0
|
print "got a $ret->{type} result\n";
|
186
|
0
|
|
|
|
|
0
|
ok(0);
|
187
|
|
|
|
|
|
|
}
|
188
|
|
|
|
|
|
|
}
|
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub test_string {
|
191
|
31
|
|
|
31
|
0
|
138
|
my ($path, $expected) = @_;
|
192
|
|
|
|
|
|
|
|
193
|
31
|
|
|
|
|
102
|
my $ret = $xpath->query($path);
|
194
|
|
|
|
|
|
|
|
195
|
31
|
50
|
|
|
|
98
|
if (!$ret){
|
196
|
0
|
|
|
|
|
0
|
print "Error: $xpath->{error}\n";
|
197
|
0
|
|
|
|
|
0
|
ok(0);
|
198
|
0
|
|
|
|
|
0
|
ok(0);
|
199
|
0
|
|
|
|
|
0
|
return;
|
200
|
|
|
|
|
|
|
}
|
201
|
|
|
|
|
|
|
|
202
|
31
|
|
|
|
|
137
|
ok($ret->{type} eq 'string');
|
203
|
|
|
|
|
|
|
|
204
|
31
|
50
|
|
|
|
12704
|
if ($ret->{type} eq 'string'){
|
205
|
31
|
|
|
|
|
102
|
ok($ret->{value} eq $expected);
|
206
|
|
|
|
|
|
|
|
207
|
31
|
50
|
|
|
|
8725
|
if ($ret->{value} ne $expected){
|
208
|
0
|
|
|
|
|
0
|
print "# expected $expected, got $ret->{value}\n";
|
209
|
|
|
|
|
|
|
}
|
210
|
|
|
|
|
|
|
}else{
|
211
|
0
|
|
|
|
|
0
|
print "# got a $ret->{type} result\n";
|
212
|
0
|
|
|
|
|
0
|
ok(0);
|
213
|
|
|
|
|
|
|
}
|
214
|
|
|
|
|
|
|
}
|
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
sub test_error {
|
217
|
1
|
|
|
1
|
0
|
10
|
my ($path, $expected) = @_;
|
218
|
|
|
|
|
|
|
|
219
|
1
|
|
|
|
|
5
|
my $ret = $xpath->query($path);
|
220
|
|
|
|
|
|
|
|
221
|
1
|
50
|
|
|
|
4
|
if ($ret){
|
222
|
0
|
|
|
|
|
0
|
print "# no error - but we expected one!\n";
|
223
|
0
|
|
|
|
|
0
|
ok(0);
|
224
|
|
|
|
|
|
|
}else{
|
225
|
1
|
50
|
|
|
|
9
|
if ($xpath->{error} =~ $expected){
|
226
|
|
|
|
|
|
|
|
227
|
1
|
|
|
|
|
3
|
ok(1);
|
228
|
|
|
|
|
|
|
}else{
|
229
|
0
|
|
|
|
|
0
|
print "# wrong error\n";
|
230
|
0
|
|
|
|
|
0
|
print "# expected: $expected\n";
|
231
|
0
|
|
|
|
|
0
|
print "# got: $xpath->{error}\n";
|
232
|
0
|
|
|
|
|
0
|
ok(0);
|
233
|
|
|
|
|
|
|
}
|
234
|
|
|
|
|
|
|
}
|
235
|
|
|
|
|
|
|
}
|
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
sub test_boolean {
|
238
|
10
|
|
|
10
|
0
|
280
|
my ($path, $expected) = @_;
|
239
|
|
|
|
|
|
|
|
240
|
10
|
|
|
|
|
34
|
my $ret = $xpath->query($path);
|
241
|
|
|
|
|
|
|
|
242
|
10
|
50
|
|
|
|
29
|
if (!$ret){
|
243
|
0
|
|
|
|
|
0
|
print "Error: $xpath->{error}\n";
|
244
|
0
|
|
|
|
|
0
|
ok(0);
|
245
|
0
|
|
|
|
|
0
|
ok(0);
|
246
|
0
|
|
|
|
|
0
|
return;
|
247
|
|
|
|
|
|
|
}
|
248
|
|
|
|
|
|
|
|
249
|
10
|
|
|
|
|
45
|
ok($ret->{type} eq 'boolean');
|
250
|
|
|
|
|
|
|
|
251
|
10
|
50
|
|
|
|
2979
|
if ($ret->{type} eq 'boolean'){
|
252
|
10
|
|
|
|
|
16
|
my $ok = 0;
|
253
|
10
|
100
|
66
|
|
|
49
|
$ok = 1 if $expected && $ret->{value};
|
254
|
10
|
50
|
66
|
|
|
35
|
$ok = 1 if !$expected && !$ret->{value};
|
255
|
|
|
|
|
|
|
|
256
|
10
|
|
|
|
|
28
|
ok($ok);
|
257
|
|
|
|
|
|
|
|
258
|
10
|
50
|
|
|
|
2300
|
unless ($ok){
|
259
|
0
|
|
|
|
|
|
print "# expected $expected, got $ret->{value}\n";
|
260
|
|
|
|
|
|
|
}
|
261
|
|
|
|
|
|
|
}else{
|
262
|
0
|
|
|
|
|
|
print "# got a $ret->{type} result\n";
|
263
|
0
|
|
|
|
|
|
ok(0);
|
264
|
|
|
|
|
|
|
}
|
265
|
|
|
|
|
|
|
}
|
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
1;
|