PRANG/Graph/Context.pm

filter: Statementstmt 94.2% 131 / 139 Branchbran 80.4% 37 / 46 Conditioncond 58.6% 17 / 29 MC/DCmcdc 33.3% 6 / 18 Subroutinesub 92.0% 23 / 25 Podpod 60.0% 6 / 10 totaltotal 82.4% 220 / 267 CC 34 SCAR 19.8
CC
34
Cov
86%
show_node
17.3
next_ctx
8.2
thing_xmlns
5.2
CRAP 7.3
« PRANG/Graph/Text.pm ↑ Summary PRANG/Util.pm »
1  
2 package PRANG::Graph::Context;
3 11 $PRANG::Graph::Context::VERSION = '0.21';
4 11 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 class_type "XML::LibXML::Element";
11 }
12  
13 has 'seq_pos' =>
14 is => "rw",
15 isa => "Int",
16 lazy => 1,
17 default => 1,
18 752 ▶ trigger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
19 752 my $self = shift;
20 752 $self->clear_quant;
21 752 $self->clear_chosen;
22 752 $self->clear_element_ok;
23 },
24 11 clearer => "clear_seq_pos",
25 ;
26  
27 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod undocumented
28 0 my $self = shift;
29
30 0 $self->clear_seq_pos;
31 }
32  
33 has 'quant_found' =>
34 is => "rw",
35 isa => "Int",
36 lazy => 1,
37 default => 0,
38 clearer => 'clear_quant',
39 361 ▶ trigger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
40 361 my $self = shift;
41 361 $self->clear_chosen;
42 361 $self->clear_element_ok;
43 },
44 11 ;
45  
46 has 'chosen' =>
47 is => "rw",
48 isa => "Int",
49 clearer => "clear_chosen",
50 124 ▶ trigger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
51 124 $_[0]->clear_element_ok;
52 }
53 11 ;
54  
55 11 has 'element_ok' =>
56 is => "rw",
57 isa => "Bool",
58 clearer => "clear_element_ok",
59 ;
60  
61 # For recursion, we need to know a couple of extra things.
62 11 has 'base' =>
63 is => "ro",
64 isa => 'PRANG::Marshaller',
65 ;
66  
67 11 has 'xpath' =>
68 is => "ro",
69 isa => "Str",
70 ;
71  
72 has 'xsi' =>
73 is => "rw",
74 isa => "HashRef",
75 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 11 ;
77  
78 has 'old_xsi' =>
79 is => "rw",
80 isa => "HashRef",
81 614 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
82 11 ;
83  
84 has 'rxsi' =>
85 is => "rw",
86 isa => "HashRef",
87 lazy => 1,
88 114 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
89 114 my $self = shift;
90 114 +{ reverse %{ $self->xsi } };
91 },
92 11 ;
93  
94 11 has 'xsi_virgin' =>
95 is => "rw",
96 isa => "Bool",
97 default => 1,
98 ;
99  
100 28 ▶sub thing_xmlns {
Subroutine thing_xmlns: called CC=4 SCAR=16.4
Pod undocumented
101 28 my $thing = shift;
102 28 ▶ return unless blessed $thing;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($thing) 0 28 28
103 28 my $xmlns = shift;
104 28 ▶ if ( $thing->can("preferred_prefix") ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($thing->can('preferred_prefix')) { } 0 28 28
elsif ($thing->can('xmlns_prefix')) { } 0 28 28
105 0 $thing->preferred_prefix($xmlns);
106 }
107 elsif ( $thing->can("xmlns_prefix") ) {
108 0 $thing->xmlns_prefix($xmlns);
109 }
110 }
111  
112 487 ▶sub next_ctx {
Subroutine next_ctx: called CC=8 SCAR=21.0
Pod documented
113 487 my $self = shift;
114 487 my ( $xmlns, $newnode_name, $thing) = pos_validated_list(
115 \@_,
116 { isa => 'Maybe[Str]' },
117 { isa => 'Maybe[Str]' },
118 { optional => 1 },
119 );
120
121 487 my $prefix = $self->prefix;
122 487 my $new_prefix;
123 487 ▶ if ($xmlns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmlns) 62 425 487
124 62 ▶ if ( !exists $self->rxsi->{$xmlns} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->rxsi->{$xmlns}) { } 15 47 62
125 15 $new_prefix = 1;
126 15 ▶ $prefix = thing_xmlns($thing, $xmlns) //
Condition33%1/3
thing_xmlns($thing, $xmlns) // $self->base->generate_prefix($xmlns)
l!l&&r!l&&!r
0150
MC/DC0%0/2
thing_xmlns($thing, $xmlns) // $self->base->generate_prefix($xmlns)
thing_xmlns($thing, $xmlns) $self->base->generate_prefix($xmlns)
Truth table33%1/3
A // B
A= thing_xmlns($thing, $xmlns)B= $self->base->generate_prefix($xmlns)
ABresult
1X1
011
000
127 $self->base->generate_prefix($xmlns);
128 }
129 else {
130 47 $prefix = $self->get_prefix($xmlns);
131 }
132 }
133 487 ▶ my $nodename = (($newnode_name && $prefix) ? "$prefix:" : "") .
Branch: 1 branch
BranchTrueFalseTotal
$newnode_name && $prefix ? : 112 375 487
Condition60%3/5
$newnode_name and $prefix
!ll&&!rl&&r
0375112
$newnode_name || 'text()'
l!l
4870
MC/DC50%1/2
$newnode_name and $prefix
$newnode_name $prefix
MC/DC0%0/1
$newnode_name || 'text()'
$newnode_name
Truth table66%2/3
A and B
A= $newnode_nameB= $prefix
ABresult
0X0
100
111
Truth table50%1/2
A
A= $newnode_name
Aresult
11
00
134 ($newnode_name||"text()");
135  
136 487 my $clone = (ref $self)->new(
137 prefix => $prefix,
138 base => $self->base,
139 xpath => $self->xpath."/".$nodename,
140 xsi => $self->xsi,
141 rxsi => $self->rxsi,
142 );
143 487 ▶ if ($new_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_prefix) 15 472 487
144 15 $clone->add_xmlns($prefix, $xmlns);
145 }
146 487 $clone;
147 }
148  
149 224 ▶sub prefix_new {
Subroutine prefix_new: called CC=2 SCAR=7.0
Pod documented
150 224 my $self = shift;
151 224 my ( $prefix) = pos_validated_list(
152 \@_,
153 { isa => 'Str' },
154 );
155
156 224 ▶ !$self->xsi_virgin and not exists $self->old_xsi->{$prefix};
Condition66%2/3
$self->xsi_virgin || exists $self->old_xsi->{$prefix}
l!l&&r!l&&!r
209015
MC/DC50%1/2
$self->xsi_virgin || exists $self->old_xsi->{$prefix}
$self->xsi_virgin exists $self->old_xsi->{$prefix}
Truth table66%2/3
A || B
A= $self->xsi_virginB= exists $self->old_xsi->{$prefix}
ABresult
1X1
011
000
157 }
158  
159 # this one is to know if the prefix was different to the parent type.
160 11 has 'prefix' =>
161 is => "ro",
162 isa => "Str",
163 ;
164  
165 11 ▶BEGIN { class_type "XML::LibXML::Node" }
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 61 ▶sub get_prefix {
Subroutine get_prefix: called CC=4 SCAR=14.1
Pod documented
168 61 my $self = shift;
169 61 my ( $xmlns, $thing, $victim ) = pos_validated_list(
170 \@_,
171 { isa => 'Str' },
172 { isa => 'Object', optional => 1 },
173 { isa => 'XML::LibXML::Element', optional => 1 },
174 );
175
176 61 ▶ if ( defined(my $prefix = $self->rxsi->{$xmlns}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prefix = $self->rxsi->{$xmlns})) { } 48 13 61
177 48 $prefix;
178 }
179 else {
180 13 ▶ my $new_prefix = thing_xmlns($thing, $xmlns)
Condition33%1/3
thing_xmlns($thing, $xmlns) // $self->base->generate_prefix($xmlns)
l!l&&r!l&&!r
0130
MC/DC0%0/2
thing_xmlns($thing, $xmlns) // $self->base->generate_prefix($xmlns)
thing_xmlns($thing, $xmlns) $self->base->generate_prefix($xmlns)
Truth table33%1/3
A // B
A= thing_xmlns($thing, $xmlns)B= $self->base->generate_prefix($xmlns)
ABresult
1X1
011
000
181 // $self->base->generate_prefix($xmlns);
182 13 $self->add_xmlns($new_prefix, $xmlns);
183 13 ▶ if ($victim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($victim) 13 0 13
184 13 $victim->setAttribute(
185 "xmlns:".$new_prefix,
186 $xmlns,
187 );
188 }
189 13 $new_prefix;
190 }
191 }
192  
193 89 ▶sub add_xmlns {
Subroutine add_xmlns: called CC=3 SCAR=11.0
Pod documented
194 89 my $self = shift;
195 89 my ( $prefix, $xmlns ) = pos_validated_list(
196 \@_,
197 { isa => 'Str' },
198 { isa => 'Str' },
199 );
200
201 89 ▶ if ( $self->xsi_virgin ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->xsi_virgin) { } 44 45 89
202 44 $self->xsi_virgin(0);
203 44 $self->old_xsi($self->xsi);
204 44 $self->xsi({ %{$self->xsi}, $prefix => $xmlns });
205 44 ▶ if ( $self->rxsi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rxsi) 44 0 44
206 44 $self->rxsi({ %{$self->rxsi}, $xmlns => $prefix });
207 }
208 }
209 else {
210 45 $self->xsi->{$prefix} = $xmlns;
211 45 $self->rxsi->{$xmlns} = $prefix;
212 }
213 }
214  
215 26 ▶sub get_xmlns{
Subroutine get_xmlns: called CC=1 SCAR=0.0
Pod documented
216 26 my $self = shift;
217 26 my ( $prefix, ) = pos_validated_list(
218 \@_,
219 { isa => 'Str' },
220 );
221
222 26 $self->xsi->{$prefix};
223 }
224  
225 # this is a very convenient class to put a rich and useful exception
226 # method on; all important methods use it, and it has just the
227 # information to make the error message very useful.
228 26 ▶sub exception {
Subroutine exception: called CC=3 SCAR=11.0
Pod documented
229 26 my $self = shift;
230 26 my ( $message, $node, $skip_ok ) = pos_validated_list(
231 \@_,
232 { isa => 'Str' },
233 { isa => 'XML::LibXML::Node', optional => 1 },
234 { isa => 'Bool', optional => 1 },
235 );
236
237
238 26 ▶ my $error = PRANG::Graph::Context::Error->new(
Branch: 2 branches
BranchTrueFalseTotal
$node ? : 17 9 26
$skip_ok ? : 0 26 26
239 ($node ? (node => $node) : ()),
240 message => $message,
241 xpath => $self->xpath,
242 ($skip_ok ? (skip_ok => 1) : ()),
243 );
244 26 die $error;
245 }
246  
247 package PRANG::Graph::Context::Error;
248 11 $PRANG::Graph::Context::Error::VERSION = '0.21';
249 11 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
250 11 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
251  
252 11 has 'node' =>
253 is => "ro",
254 isa => "XML::LibXML::Node",
255 predicate => "has_node",
256 ;
257  
258 11 has 'message' =>
259 is => "ro",
260 isa => "Str",
261 ;
262  
263 11 has 'xpath' =>
264 is => "ro",
265 isa => "Str",
266 ;
267  
268 11 has 'skip_ok' =>
269 is => "ro",
270 isa => "Bool",
271 ;
272  
273 37 ▶sub show_node {
Subroutine show_node: called CC=16 SCAR=28.5
Pod undocumented
274 37 my $self = shift;
275
276 37 ▶ return "" unless $self->has_node;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_node 15 22 37
277 22 my $extra = "";
278 22 my $node = $self->node;
279 22 ▶ if ( $node->isa("XML::LibXML::Element") ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($node->isa('XML::LibXML::Element')) { } 18 4 22
elsif ($node->isa('XML::LibXML::Text')) { } 4 0 4
elsif ($node) { } 0 0 0
280 18 $extra = " (parsing: <".$node->nodeName;
281 18 ▶ if ( $node->hasAttributes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->hasAttributes) 4 14 18
282 $extra .= join(
283 " ", "",
284 map {
285 4 $_->name."='".$_->value."'"
286 } $node->attributes
287 );
288 }
289 my @nodes = grep {
290 18 ▶ !( $_->isa("XML::LibXML::Comment")
Condition50%3/6
$_->isa('XML::LibXML::Comment') || $_->isa('XML::LibXML::Text') && $_->data =~ /\A\s+\Z/
l!l&&r!l&&!r
0022
$_->isa("XML::LibXML::Text") and $_->data =~ /\A\s+\Z/
!ll&&!rl&&r
10120
MC/DC0%0/3
$_->isa('XML::LibXML::Comment') || $_->isa('XML::LibXML::Text') && $_->data =~ /\A\s+\Z/
$_->isa('XML::LibXML::Comment') $_->isa("XML::LibXML::Text") $_->data =~ /\A\s+\Z/
Truth table50%2/4
A || B and C
A= $_->isa('XML::LibXML::Comment')B= $_->isa("XML::LibXML::Text")C= $_->data =~ /\A\s+\Z/
ABCresult
1XX1
0111
00X0
0100
291 or
292 $_->isa("XML::LibXML::Text") and $_->data =~ /\A\s+\Z/
293 )
294 } $node->childNodes;
295 18 ▶ if (@nodes > 1
Branch: 3 branches
BranchTrueFalseTotal
if (@nodes > 1 and grep {not $_->isa('XML::LibXML::Element');} @nodes) { } 2 16 18
elsif (@nodes and $nodes[0]->isa('XML::LibXML::Element')) { } 6 10 16
elsif (@nodes and $nodes[0]->isa('XML::LibXML::Text')) { } 6 8 14
Condition66%2/3
@nodes > 1 and grep {not $_->isa("XML::LibXML::Element");} @nodes
!ll&&!rl&&r
1602
MC/DC50%1/2
@nodes > 1 and grep {not $_->isa("XML::LibXML::Element");} @nodes
@nodes > 1 grep {not $_->isa("XML::LibXML::Element");} @nodes
Truth table66%2/3
A and B
A= @nodes > 1B= grep {not $_->isa("XML::LibXML::Element");} @nodes
ABresult
0X0
100
111
296 10 and grep { !$_->isa("XML::LibXML::Element") }
297 @nodes
298 )
299 2 { $extra .= ">(mixed content)";
300 }
301 ▶ elsif (@nodes and $nodes[0]->isa("XML::LibXML::Element")) {
Condition100%3/3
@nodes and $nodes[0]->isa("XML::LibXML::Element")
!ll&&!rl&&r
466
MC/DC100%2/2
@nodes and $nodes[0]->isa("XML::LibXML::Element")
@nodes $nodes[0]->isa("XML::LibXML::Element")
Truth table100%3/3
A and B
A= @nodesB= $nodes[0]->isa("XML::LibXML::Element")
ABresult
0X0
100
111
302 6 $extra .= "><!-- ".@nodes
303 ." child XML nodes -->";
304 }
305 ▶ elsif ( @nodes and $nodes[0]->isa("XML::LibXML::Text") ) {
Condition66%2/3
@nodes and $nodes[0]->isa("XML::LibXML::Text")
!ll&&!rl&&r
406
MC/DC50%1/2
@nodes and $nodes[0]->isa("XML::LibXML::Text")
@nodes $nodes[0]->isa("XML::LibXML::Text")
Truth table66%2/3
A and B
A= @nodesB= $nodes[0]->isa("XML::LibXML::Text")
ABresult
0X0
100
111
306 6 $extra .= ">(text content)";
307 }
308 18 ▶ if ( @nodes == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nodes == 0) { } 4 14 18
309 4 $extra .= " />";
310 }
311 else {
312 14 $extra .= "</".$node->nodeName.">";
313 }
314 18 $extra .= ")";
315 }
316 elsif ( $node->isa("XML::LibXML::Text") ) {
317 4 my $val = $node->data;
318 4 ▶ if ( length($val) > 15 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $val > 15) 2 2 4
319 2 $val = substr($val, 0, 13);
320 2 $val .= "...";
321 }
322 4 $extra .= " (at text node: '$val')";
323 }
324 elsif ($node) {
325 0 my $type = ref $node;
326 0 $type =~ s{XML::LibXML::}{};
327 0 $extra .= " (bogon? $type node)";
328 }
329 22 $extra;
330 }
331  
332 37 ▶sub build_error {
Subroutine build_error: called CC=1 SCAR=0.0
Pod undocumented
333 37 my $self = shift;
334 37 my $message = $self->message;
335 37 my $extra = $self->show_node;
336 37 return "$message at ".$self->xpath."$extra\n";
337 }
338  
339 use overload
340 11 ▶ '""' => \&build_error,
Subroutine BEGIN: called CC=1 SCAR=0.0
341 11 fallback => 1;
342  
343 11 1;
344  
345 __END__
« PRANG/Graph/Text.pm ↑ Summary PRANG/Util.pm »