XPath/Test.pm

filter: Statementstmt 58.6% 98 / 167 Branchbran 60.0% 36 / 60 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 7 totaltotal 59.5% 153 / 257 CC 40 SCAR 34.5
CC
40
Cov
59%
test_nodeset
65.9
test_boolean
11.1
test_tree
6.7
CRAP 31.4
« XPath/Token.pm ↑ Summary XPath/Tokener.pm »
1 package XML::Parser::Lite::Tree::XPath::Test;
2
3 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use vars qw(@ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 28 ▶use XML::Parser::Lite::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use XML::Parser::Lite::Tree::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 28 require Exporter;
12 28 @ISA = qw(Exporter);
13 28 @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 28 our $xpath;
24
25 29 ▶sub set_xml {
Subroutine set_xml: called CC=1 SCAR=0.0
Pod undocumented
26 29 my ($xml) = @_;
27
28 29 $xml =~ s/>\s+</></sg;
29 29 $xml =~ s/^\s*(.*?)\s*$/$1/;
30
31 29 my $parser = new XML::Parser::Lite::Tree(process_ns => 1);
32 29 my $tree = $parser->parse($xml);
33 29 $xpath = new XML::Parser::Lite::Tree::XPath($tree);
34 }
35
36 41 ▶sub test_tree {
Subroutine test_tree: called CC=4 SCAR=19.1
Pod undocumented
37 41 my ($path, $dump) = @_;
38
39 41 my $tokener = XML::Parser::Lite::Tree::XPath::Tokener->new();
40 41 ▶ if (!$tokener->parse($path)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($tokener->parse($path)) 0 41 41
41 0 print "Path: $path\n";
42 0 print "Failed toke: ($tokener->{error})\n";
43 0 ok(0);
44 0 return;
45 }
46
47 41 my $tree = XML::Parser::Lite::Tree::XPath::Tree->new();
48 41 ▶ if (!$tree->build_tree($tokener->{tokens})){
Branch: 1 branch
BranchTrueFalseTotal
unless ($tree->build_tree($tokener->{"tokens"})) 0 41 41
49 0 print "Path: $path\n";
50 0 print "Failed tree: ($tree->{error})\n";
51 #print Dumper $tree;
52 0 ok(0);
53 0 return;
54 }
55
56 41 my $dump_got = $tree->dump_flat();
57
58 41 ok($dump_got eq $dump);
59
60 41 ▶ unless ($dump_got eq $dump){
Branch: 1 branch
BranchTrueFalseTotal
unless ($dump_got eq $dump) 0 41 41
61 0 print "Path: $path\n";
62 0 print "Expected: $dump\n";
63 0 print "Dump: $dump_got\n";
64 0 print $tree->dump_tree();
65 }
66
67 41 return $dump_got;
68 }
69
70 73 ▶sub test_nodeset {
Subroutine test_nodeset: called CC=22 SCAR=41.9
Pod undocumented
71 73 my ($path, $expected) = @_;
72
73 73 my $nodes = $xpath->select_nodes($path);
74
75 73 ▶ unless ('ARRAY' eq ref $nodes){
Branch: 1 branch
BranchTrueFalseTotal
unless ("ARRAY" eq ref $nodes) 0 73 73
76
77 0 print "Error: $xpath->{error}\n";
78
79 0 ok(0);
80 0 ok(0) for @{$expected};
81 0 return;
82 }
83
84 73 my $bad = 0;
85
86 73 my $ok = scalar(@{$nodes}) == scalar(@{$expected});
87 73 ▶ $bad++ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 73 73
88 73 ok($ok);
89
90 73 ▶ if (!$ok){
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 73 73
91 0 print "# wrong node count. got ".scalar(@{$nodes}).", expected ".scalar(@{$expected})."\n";
92 }
93
94
95 73 my $i = 0;
96 73 for my $xnode(@{$expected}){
97
98 # $xnode is a hash ref which should match stuff in $nodes[$i]
99
100 247 for my $key(keys %{$xnode}){
101
102 473 ▶ if ($key eq 'nodename'){
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq 'nodename') { } 241 232 473
elsif ($key eq 'attributecount') { } 1 231 232
elsif ($key eq 'type') { } 8 223 231
elsif ($key eq 'value') { } 2 221 223
103
104 241 $ok = $nodes->[$i]->{name} eq $xnode->{$key};
105
106 241 ▶ print "# node name - expected: $xnode->{$key}, got: $nodes->[$i]->{name}\n" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 241 241
107
108 }elsif ($key eq 'attributecount'){
109
110 1 $ok = scalar(keys %{$nodes->[$i]->{attributes}}) == $xnode->{$key};
111
112 0 ▶ print "# attribute count - expected: $xnode->{$key}, got: ".scalar(keys %{$nodes->[$i]->{attributes}})."\n" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 1 1
113
114 }elsif ($key eq 'type'){
115
116 8 $ok = $nodes->[$i]->{type} eq $xnode->{$key};
117
118 8 ▶ print "# node type - expected: $xnode->{$key}, got: $nodes->[$i]->{type}\n" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 8 8
119
120 }elsif ($key eq 'value'){
121
122 2 $ok = $nodes->[$i]->{value} eq $xnode->{$key};
123
124 2 ▶ print "# value - expected: $xnode->{$key}, got: $nodes->[$i]->{value}\n" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 2 2
125
126 }else{
127 221 $ok = $nodes->[$i]->{attributes}->{$key} eq $xnode->{$key};
128
129 221 ▶ print "# attribute $key - expected: $xnode->{$key}, got: $nodes->[$i]->{attributes}->{$key}\n" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 221 221
130 }
131
132 473 ▶ $bad++ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 473 473
133 473 ok($ok);
134 }
135
136 247 $i++;
137 }
138
139 73 ▶ if ($bad){
Branch: 1 branch
BranchTrueFalseTotal
if ($bad) 0 73 73
140 0 print "# codes don't match. got:\n";
141 0 for my $node(@{$nodes}){
142 0 print "# \t";
143 0 print "($node->{type} : $node->{order}) ";
144 0 print "$node->{name}";
145 0 for my $key(keys %{$node->{attributes}}){
146 0 print ", $key=$node->{attributes}->{$key}";
147 }
148 0 print "\n";
149 }
150 0 print "# expected:\n";
151 0 my $i = 1;
152 0 for my $node(@{$expected}){
153 0 print "# \t$i";
154 0 for my $key(keys %{$node}){
155 0 print ", $key={$node->{$key}}";
156 }
157 0 print "\n";
158 0 $i++;
159 }
160 0 print Dumper $nodes;
161 }
162 }
163
164 15 ▶sub test_number {
Subroutine test_number: called CC=4 SCAR=17.9
Pod undocumented
165 15 my ($path, $expected) = @_;
166
167 15 my $ret = $xpath->query($path);
168
169 15 ▶ if (!$ret){
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 15 15
170 0 print "Error: $xpath->{error}\n";
171 0 ok(0);
172 0 ok(0);
173 0 return;
174 }
175
176 15 ok($ret->{type} eq 'number');
177
178 15 ▶ if ($ret->{type} eq 'number'){
Branch: 1 branch
BranchTrueFalseTotal
if ($ret->{'type'} eq 'number') { } 15 0 15
179 15 ok($ret->{value} == $expected);
180
181 15 ▶ if ($ret->{value} != $expected){
Branch: 1 branch
BranchTrueFalseTotal
if ($ret->{"value"} != $expected) 0 15 15
182 0 print "expected $expected, got $ret->{value}\n";
183 }
184 }else{
185 0 print "got a $ret->{type} result\n";
186 0 ok(0);
187 }
188 }
189
190 31 ▶sub test_string {
Subroutine test_string: called CC=4 SCAR=17.9
Pod undocumented
191 31 my ($path, $expected) = @_;
192
193 31 my $ret = $xpath->query($path);
194
195 31 ▶ if (!$ret){
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 31 31
196 0 print "Error: $xpath->{error}\n";
197 0 ok(0);
198 0 ok(0);
199 0 return;
200 }
201
202 31 ok($ret->{type} eq 'string');
203
204 31 ▶ if ($ret->{type} eq 'string'){
Branch: 1 branch
BranchTrueFalseTotal
if ($ret->{'type'} eq 'string') { } 31 0 31
205 31 ok($ret->{value} eq $expected);
206
207 31 ▶ if ($ret->{value} ne $expected){
Branch: 1 branch
BranchTrueFalseTotal
if ($ret->{"value"} ne $expected) 0 31 31
208 0 print "# expected $expected, got $ret->{value}\n";
209 }
210 }else{
211 0 print "# got a $ret->{type} result\n";
212 0 ok(0);
213 }
214 }
215
216 1 ▶sub test_error {
Subroutine test_error: called CC=3 SCAR=14.7
Pod undocumented
217 1 my ($path, $expected) = @_;
218
219 1 my $ret = $xpath->query($path);
220
221 1 ▶ if ($ret){
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) { } 0 1 1
222 0 print "# no error - but we expected one!\n";
223 0 ok(0);
224 }else{
225 1 ▶ if ($xpath->{error} =~ $expected){
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::Parser::Lite::Tree::XPath::Test::xpath->{'error'} =~ /$expected/) { } 1 0 1
226
227 1 ok(1);
228 }else{
229 0 print "# wrong error\n";
230 0 print "# expected: $expected\n";
231 0 print "# got: $xpath->{error}\n";
232 0 ok(0);
233 }
234 }
235 }
236
237 10 ▶sub test_boolean {
Subroutine test_boolean: called CC=8 SCAR=24.0
Pod undocumented
238 10 my ($path, $expected) = @_;
239
240 10 my $ret = $xpath->query($path);
241
242 10 ▶ if (!$ret){
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 10 10
243 0 print "Error: $xpath->{error}\n";
244 0 ok(0);
245 0 ok(0);
246 0 return;
247 }
248
249 10 ok($ret->{type} eq 'boolean');
250
251 10 ▶ if ($ret->{type} eq 'boolean'){
Branch: 1 branch
BranchTrueFalseTotal
if ($ret->{'type'} eq 'boolean') { } 10 0 10
252 10 my $ok = 0;
253 10 ▶ $ok = 1 if $expected && $ret->{value};
Branch: 1 branch
BranchTrueFalseTotal
if $expected and $ret->{"value"} 7 3 10
Condition66%2/3
$expected and $ret->{"value"}
!ll&&!rl&&r
307
MC/DC50%1/2
$expected and $ret->{"value"}
$expected $ret->{"value"}
Truth table66%2/3
A and B
A= $expectedB= $ret->{"value"}
ABresult
0X0
100
111
254 10 ▶ $ok = 1 if !$expected && !$ret->{value};
Branch: 1 branch
BranchTrueFalseTotal
unless $expected or $ret->{"value"} 3 7 10
Condition66%2/3
$expected or $ret->{"value"}
l!l&&r!l&&!r
703
MC/DC50%1/2
$expected or $ret->{"value"}
$expected $ret->{"value"}
Truth table66%2/3
A or B
A= $expectedB= $ret->{"value"}
ABresult
1X1
011
000
255
256 10 ok($ok);
257
258 10 ▶ unless ($ok){
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 10 10
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 28 1;
« XPath/Token.pm ↑ Summary XPath/Tokener.pm »