PRANG/Graph/Choice.pm

filter: Statementstmt 94.7% 109 / 115 Branchbran 84.6% 44 / 52 Conditioncond 82.1% 23 / 28 MC/DCmcdc 64.7% 11 / 17 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 5 totaltotal 86.4% 198 / 229 CC 42 SCAR 30.1
CC
42
Cov
90%
output
32.3
accept
9.0
expected
3.1
CRAP 20.3
« PRANG/Marshaller.pm ↑ Summary PRANG/Graph/Meta/Class.pm »
1  
2 package PRANG::Graph::Choice;
3 11 $PRANG::Graph::Choice::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 11 ▶use Moose::Meta::TypeConstraint;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 class_type('Moose::Meta::TypeConstraint');
12 }
13  
14 has 'choices' =>
15 is => "ro",
16 isa => "ArrayRef[PRANG::Graph::Node]",
17 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 11 ;
19  
20 11 has 'attrName' =>
21 is => "ro",
22 isa => "Str",
23 required => 1,
24 ;
25  
26 11 has 'type_map' =>
27 is => "ro",
28 isa => "HashRef[Str|Moose::Meta::TypeConstraint]",
29 predicate => "has_type_map",
30 ;
31  
32 11 has 'type_map_prefix' =>
33 is => "ro",
34 isa => "HashRef[Str]",
35 predicate => "has_type_map_prefix",
36 ;
37  
38 11 has 'xml_nodeName' =>
39 is => "ro",
40 isa => "Str",
41 predicate => "has_xml_nodeName",
42 ;
43  
44 11 has 'name_attr' =>
45 is => "ro",
46 isa => "Str",
47 predicate => "has_name_attr",
48 ;
49  
50 11 has 'xmlns_attr' =>
51 is => "ro",
52 isa => "Str",
53 predicate => "has_xmlns_attr",
54 ;
55  
56 11 has 'xmlns' =>
57 is => "ro",
58 isa => "Str",
59 predicate => "has_xmlns",
60 ;
61
62 163 ▶sub node_ok {
Subroutine node_ok: called CC=3 SCAR=11.0
Pod undocumented
63 163 my $self = shift;
64 163 my ( $node, $ctx ) = pos_validated_list(
65 \@_,
66 { isa => 'XML::LibXML::Node' },
67 { isa => 'PRANG::Graph::Context' },
68 );
69
70 163 for my $choice ( @{ $self->choices } ) {
71 312 ▶ if ( defined $choice->node_ok($node, $ctx) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $choice->node_ok($node, $ctx)) 117 195 312
72 117 return 1;
73 }
74 }
75 46 return;
76 }
77  
78 132 ▶sub accept {
Subroutine accept: called CC=9 SCAR=22.0
Pod undocumented
79 132 my $self = shift;
80 132 my ( $node, $ctx, $lax ) = pos_validated_list(
81 \@_,
82 { isa => 'XML::LibXML::Node' },
83 { isa => 'PRANG::Graph::Context' },
84 { isa => 'Bool', optional => 1 },
85 );
86  
87 132 ▶ if ($ctx->chosen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->chosen) 2 130 132
88  
89 # this is a safe exception; the only time this graph
90 # node will be called repeatedly is if it is the root
91 # node for an element, due to the structure of
92 # PRANG::Graph::Context
93 2 $ctx->exception(
94 "Single child node expected, multiple found",
95 $node,
96 );
97 }
98  
99 130 my $num;
100 130 ▶ my $name = $node->isa("XML::LibXML::Text")
Branch: 1 branch
BranchTrueFalseTotal
$node->isa('XML::LibXML::Text') ? : 28 102 130
101 ? ""
102 : $node->localname;
103 130 ▶ my $xmlns = length($name) && $node->namespaceURI;
Condition100%3/3
length $name && $node->namespaceURI
!ll&&!rl&&r
287329
MC/DC100%2/2
length $name && $node->namespaceURI
length $name $node->namespaceURI
Truth table100%3/3
A && B
A= length $nameB= $node->namespaceURI
ABresult
0X0
100
111
104 130 my ($key, $val, $x, $ns);
105 130 for my $choice ( @{ $self->choices } ) {
106 221 $num++;
107 221 ▶ if ( defined $choice->node_ok($node, $ctx) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $choice->node_ok($node, $ctx)) 130 91 221
108 130 ($key, $val, $x, $ns) = $choice->accept($node, $ctx, $lax);
109 }
110 215 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 124 91 215
111 124 $ctx->chosen($num);
112 return (
113 124 ▶ $key, $val, $x||eval{$choice->nodeName}||"",
Condition100%5/5
$x || eval { do { $choice->nodeName } } || ''
l!l
9628
$x or eval { do { $choice->nodeName } }
l!l&&r!l&&!r
207628
MC/DC100%2/2
$x || eval { do { $choice->nodeName } } || ''
$x eval { do { $choice->nodeName } }
Truth table100%3/3
A or B
A= $xB= eval { do { $choice->nodeName } }
ABresult
1X1
011
000
114 $ns
115 );
116 }
117 }
118 0 return ();
119 }
120  
121 13 ▶sub complete {
Subroutine complete: called CC=1 SCAR=0.0
Pod undocumented
122 13 my $self = shift;
123 13 my ( $ctx ) = pos_validated_list(
124 \@_,
125 { isa => 'PRANG::Graph::Context' },
126 );
127
128 13 $ctx->chosen;
129 }
130  
131 3 ▶sub expected {
Subroutine expected: called CC=3 SCAR=11.2
Pod undocumented
132 3 my $self = shift;
133 3 my ( $ctx ) = pos_validated_list(
134 \@_,
135 { isa => 'PRANG::Graph::Context' },
136 );
137
138 3 ▶ if ( my $num = $ctx->chosen ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num = $ctx->chosen) { } 0 3 3
139 0 return $self->choices->[$num-1]->expected($ctx);
140 }
141 else {
142 3 my @choices;
143 3 for my $choice ( @{$self->choices} ) {
144 6 push @choices, $choice->expected($ctx);
145 }
146 3 return @choices;
147 }
148 }
149  
150 11 our $REGISTRY =
151 Moose::Util::TypeConstraints::get_type_constraint_registry();
152  
153 67 ▶sub output {
Subroutine output: called CC=30 SCAR=34.7
Pod undocumented
154 67 my $self = shift;
155
156 # First 3 args positional, rest are named
157 # Because we're making 2 validation calls, we have to use different cache keys
158 67 my ( $item, $node, $ctx ) = pos_validated_list(
159 [@_[0..2]],
160 { isa => 'Object' },
161 { isa => 'XML::LibXML::Element' },
162 { isa => 'PRANG::Graph::Context' },
163 MX_PARAMS_VALIDATE_CACHE_KEY => 'choice-output-positional',
164 );
165
166 67 my ( $value, $slot ) = validated_list(
167 [@_[3..$#_]],
168 value => { isa => 'Item', optional => 1 },
169 slot => { isa => 'Int', optional => 1 },
170 MX_PARAMS_VALIDATE_CACHE_KEY => 'choice-output-named',
171 );
172  
173 67 my $an = $self->attrName;
174 67 ▶ $value //= $item->$an;
Condition66%2/3
$value //= $item->$an
l!l&&r!l&&!r
6160
MC/DC0%0/2
$value //= $item->$an
$value $item->$an
Truth table66%2/3
A //= B
A= $valueB= $item->$an
ABresult
1X1
011
000
175 67 my ($name, $xmlns);
176 67 ▶ if ( $self->has_name_attr || $self->has_xmlns_attr ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_name_attr or $self->has_xmlns_attr) { } 21 46 67
elsif ($self->has_type_map) { } 46 0 46
Condition100%3/3
$self->has_name_attr or $self->has_xmlns_attr
l!l&&r!l&&!r
19246
MC/DC100%2/2
$self->has_name_attr or $self->has_xmlns_attr
$self->has_name_attr $self->has_xmlns_attr
Truth table100%3/3
A or B
A= $self->has_name_attrB= $self->has_xmlns_attr
ABresult
1X1
011
000
177 21 ▶ if ( $self->has_name_attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_name_attr) { } 19 2 21
178 19 my $x = $self->name_attr;
179 19 $name = $item->$x;
180 19 ▶ if ( defined $slot ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slot) 17 2 19
181 17 $name = $name->[$slot];
182 }
183 }
184 else {
185 2 ▶ $name = $self->xml_nodeName // $an;
Condition33%1/3
$self->xml_nodeName // $an
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->xml_nodeName // $an
$self->xml_nodeName $an
Truth table33%1/3
A // B
A= $self->xml_nodeNameB= $an
ABresult
1X1
011
000
186 }
187 21 ▶ if ( $self->has_xmlns_attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_xmlns_attr) { } 19 2 21
188 19 my $attr_getter = $self->xmlns_attr;
189 19 $xmlns = $item->$attr_getter;
190 19 ▶ if ( defined $slot ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slot) 17 2 19
191 17 $xmlns = $xmlns->[$slot];
192 }
193 }
194 else {
195 2 ▶ $xmlns = $self->xmlns // "";
Condition50%1/2
$self->xmlns // ""
l!l
20
MC/DC0%0/1
$self->xmlns // ""
$self->xmlns
Truth table50%1/2
A
A= $self->xmlns
Aresult
11
00
196 }
197 }
198 elsif ( $self->has_type_map ) {
199 46 my $map = $self->type_map;
200 46 my $nodeCount = 0;
201 46 for my $element ( keys %$map ) {
202 128 my $type = $map->{$element};
203 128 ▶ if ( !ref $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $type) 17 111 128
204 17 $type = $map->{$element} =
205 $REGISTRY->get_type_constraint($type);
206 }
207
208 128 ▶ if ( $type->check($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->check($value)) 46 82 128
209 46 $name = $element;
210 46 $nodeCount++;
211 }
212
213 128 ▶ if ($nodeCount > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodeCount > 1) 0 128 128
214 0 $ctx->exception("More than 1 node type matched for value: '$value' " .
215 "You need a stricter type definition for your xml_nodeName map, or PRANG can't figure out which element name to emit");
216 }
217 }
218 }
219
220 67 ▶ if ( !defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 67 67
221 0 $ctx->exception(
222 "don't know what to serialize $value to for slot "
223 .$self->attrName
224 );
225 }
226 67 ▶ if ( length $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name) { } 53 14 67
227 53 ▶ if ( $self->has_type_map_prefix and $name =~ /(.*):(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_type_map_prefix and $name =~ /(.*):(.*)/) 2 51 53
Condition66%2/3
$self->has_type_map_prefix and $name =~ /(.*):(.*)/
!ll&&!rl&&r
5102
MC/DC50%1/2
$self->has_type_map_prefix and $name =~ /(.*):(.*)/
$self->has_type_map_prefix $name =~ /(.*):(.*)/
Truth table66%2/3
A and B
A= $self->has_type_map_prefixB= $name =~ /(.*):(.*)/
ABresult
0X0
100
111
228 2 $name = $2;
229 2 $xmlns = $self->type_map_prefix->{$1};
230 }
231 53 my $found;
232 53 for my $choice ( @{ $self->choices } ) {
233 77 ▶ if ($xmlns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmlns) 13 64 77
234 13 ▶ next unless $choice->has_xmlns;
Branch: 1 branch
BranchTrueFalseTotal
unless $choice->has_xmlns 0 13 13
235 13 ▶ next unless $choice->xmlns eq $xmlns or
Branch: 1 branch
BranchTrueFalseTotal
unless $choice->xmlns eq $xmlns or $choice->xmlns eq "*" 2 11 13
Condition100%3/3
$choice->xmlns eq $xmlns or $choice->xmlns eq "*"
l!l&&r!l&&!r
472
MC/DC100%2/2
$choice->xmlns eq $xmlns or $choice->xmlns eq "*"
$choice->xmlns eq $xmlns $choice->xmlns eq "*"
Truth table100%3/3
A or B
A= $choice->xmlns eq $xmlnsB= $choice->xmlns eq "*"
ABresult
1X1
011
000
236 $choice->xmlns eq "*";
237 }
238 75 ▶ next unless $choice->nodeName eq $name or
Branch: 1 branch
BranchTrueFalseTotal
unless $choice->nodeName eq $name or $choice->nodeName eq "*" 22 53 75
Condition100%3/3
$choice->nodeName eq $name or $choice->nodeName eq "*"
l!l&&r!l&&!r
411222
MC/DC100%2/2
$choice->nodeName eq $name or $choice->nodeName eq "*"
$choice->nodeName eq $name $choice->nodeName eq "*"
Truth table100%3/3
A or B
A= $choice->nodeName eq $nameB= $choice->nodeName eq "*"
ABresult
1X1
011
000
239 $choice->nodeName eq "*";
240 53 $found++;
241 53 ▶ $choice->output(
Branch: 2 branches
BranchTrueFalseTotal
defined $slot ? : 39 14 53
defined $xmlns ? : 18 35 53
242 $item,$node,$ctx,
243 value => $value,
244 (defined $slot ? (slot => $slot) : ()),
245 name => $name,
246 (defined $xmlns ? (xmlns => $xmlns) : ()),
247 );
248 53 last;
249 }
250 53 ▶ if ( !$found ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found) 0 53 53
251 0 ▶ $ctx->exception(
Branch: 1 branch
BranchTrueFalseTotal
$xmlns ? : 0 0 0
252 "don't know what to serialize $value to for slot "
253 .$self->attrName
254 ." (looked for '$name' node"
255 .(
256 $xmlns?" xmlns='$xmlns'":""
257 )
258 .")",
259 );
260 }
261 }
262 else {
263  
264 # textnode ... jfdi
265 14 my $tn = $self->createTextNode($node->ownerDocument, $value);
266 14 $node->appendChild($tn);
267 }
268 }
269  
270 11 with 'PRANG::Graph::Node';
271  
272 11 1;
273  
274 __END__
« PRANG/Marshaller.pm ↑ Summary PRANG/Graph/Meta/Class.pm »