blib/lib/XML/BindData.pm

filter: Statementstmt 98.4% 64 / 65 Branchbran 89.2% 25 / 28 Conditioncond 86.6% 13 / 15 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 7 / 7 Podpod 50.0% 1 / 2 totaltotal 91.2% 114 / 125 SCAR 32.2
CC
25
Cov
94%
parse_node
20.1
_get
4.0
_strip_attr
3.0
CRAP 25.1
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package XML::BindData;
5 2 $XML::BindData::VERSION = '0.3.1';
6 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 sub bind {
9 24 ▶ my ($class, $xml_string, $data) = @_;
Subroutine bind: called CC=1 SCAR=0.0
Pod documented
10  
11 24 my $xml = XML::LibXML->load_xml(string => $xml_string);
12 24 parse_node($xml->documentElement, $data);
13  
14 24 return $xml->toStringC14N(1);
15 }
16  
17 sub parse_node {
18 65 ▶ my ($node, $context) = @_;
Subroutine parse_node: called CC=20 SCAR=30.0
Pod undocumented
19  
20 65 ▶ if (my $if_key = _strip_attr($node, 'tmpl-if')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $if_key = _strip_attr($node, "tmpl-if")) 8 57 65
21 8 my $unless = $if_key =~ s/^!//;
22 8 my $val = _get($context, $if_key);
23 8 ▶ if ( (!$unless && ! defined $val)
Branch: 1 branch
BranchTrueFalseTotal
if (not $unless || defined $val or $unless and defined $val) 3 5 8
Condition100%9/9
not $unless || defined $val or $unless and defined $val
l!l&&r!l&&!r
215
$unless || defined $val
l!l&&r!l&&!r
242
$unless and defined $val
!ll&&!rl&&r
411
MC/DC50%2/4
not $unless || defined $val or $unless and defined $val
$unless defined $val $unless defined $val
Truth table42%3/7
not(A || B) or C and D
A= $unlessB= defined $valC= $unlessD= defined $val
ABCDresult
00XX1
1X111
01111
1X0X0
1X100
010X0
01100
24 || ( $unless && defined $val)) {
25 3 $node->unbindNode;
26 }
27 }
28  
29 65 ▶ if (my $each_key = _strip_attr($node, 'tmpl-each')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $each_key = _strip_attr($node, "tmpl-each")) 10 55 65
30 10 my $parent = $node->parentNode;
31  
32 10 my $to_add = _get($context, $each_key);
33 10 ▶ if (!$to_add || ref $to_add ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $to_add or ref $to_add ne "ARRAY") 0 10 10
Condition33%1/3
not $to_add or ref $to_add ne "ARRAY"
l!l&&r!l&&!r
0010
MC/DC0%0/2
not $to_add or ref $to_add ne "ARRAY"
not $to_add ref $to_add ne "ARRAY"
Truth table33%1/3
not(A) or B
A= not $to_addB= ref $to_add ne "ARRAY"
ABresult
1X1
011
000
34 0 $to_add = [];
35 }
36  
37 10 foreach my $subcontext ( reverse @$to_add ) {
38 21 my $new = $node->cloneNode(1); # deep clone
39 21 parse_node($new, $subcontext);
40 21 $parent->insertAfter( $new, $node );
41 }
42 10 $node->unbindNode;
43 10 return;
44 }
45  
46 55 ▶ if (my $binding = _strip_attr($node, 'tmpl-bind')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $binding = _strip_attr($node, "tmpl-bind")) 24 31 55
47 24 my $val = _get($context, $binding);
48  
49 24 my $default = _strip_attr($node, 'tmpl-default');
50  
51 24 ▶ unless (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 2 22 24
52 2 ▶ $val = defined $default ? $default : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $default ? : 1 1 2
53 }
54  
55 24 $node->appendTextNode($val);
56 }
57  
58 55 ▶ if (my $attr_map = _strip_attr($node, 'tmpl-attr-map')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr_map = _strip_attr($node, "tmpl-attr-map")) 5 50 55
59 5 my @attributes = map { [ split qr/:/ ] } split qr/,/, $attr_map;
60  
61 5 foreach (@attributes) {
62 10 $node->setAttribute($_->[0], _get($context, $_->[1]));
63 }
64 }
65  
66 55 ▶ if ( my $attr_defaults = _strip_attr( $node, 'tmpl-attr-defaults' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr_defaults = _strip_attr($node, "tmpl-attr-defaults")) 3 52 55
67 my @attributes
68 = map {
69 3 [ map { s/\\([:,])/$1/g; $_ } # remove backslash escaping
70 split qr/(?<!\\):/ # split on non-backslashed colons
71 ]
72 }
73 split qr/(?<!\\),/, # split on non-backslashed commas
74 $attr_defaults;
75  
76 3 foreach (@attributes) {
77 8 my $value = $node->getAttribute( $_->[0] );
78 8 ▶ $node->setAttribute( $_->[0], $_->[1] )
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 5 3 8
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
413
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
79 if ! defined $value || $value eq '';
80 }
81 }
82  
83 my @children = grep {
84 55 $_->nodeType eq XML_ELEMENT_NODE
85 } $node->childNodes;
86 55 parse_node($_, $context) foreach @children;
87 }
88  
89 sub _get {
90 51 ▶ my ($context, $key) = @_;
Subroutine _get: called CC=4 SCAR=13.9
91  
92 51 ▶ return '' if !defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 51 51
93  
94 51 ▶ return $context if $key eq 'this';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "this" 16 35 51
95  
96 35 my @parts = split qr/\./, $key;
97 35 foreach (@parts) {
98 37 $context = $context->{$_};
99 }
100 35 return $context;
101 }
102  
103 sub _strip_attr {
104 319 ▶ my ($node, $attr_name) = @_;
Subroutine _strip_attr: called CC=3 SCAR=11.0
105  
106 319 ▶ if (my $attributes = $node->attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attributes = $node->attributes) 319 0 319
107 319 ▶ if (my $attr = $attributes->removeNamedItem($attr_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr = $attributes->removeNamedItem($attr_name)) 52 267 319
108 52 return $attr->nodeValue;
109 }
110 }
111 }
112  
113 2 1;
114  
115 __END__
↑ Summary