blib/lib/XML/BindData.pm

filter: Statementstmt 98.4% 65 / 66 Branchbran 90.0% 27 / 30 Conditioncond 80.0% 12 / 15 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 7 / 7 Podpod 50.0% 1 / 2 totaltotal 91.4% 117 / 128 CC 26 SCAR 26.9
CC
26
Cov
94%
parse_node
21.1
_get
4.0
_strip_attr
3.0
CRAP 14.8
↑ Summary t/bind_data.t »
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package XML::BindData;
5 4 $XML::BindData::VERSION = '0.3.2';
6 4 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 52 ▶sub bind {
Subroutine bind: called CC=1 SCAR=0.0
Pod documented
9 52 my ($class, $xml_string, $data) = @_;
10  
11 52 my $xml = XML::LibXML->load_xml(string => $xml_string);
12 52 parse_node($xml->documentElement, $data);
13  
14 52 return $xml->toStringC14N(1);
15 }
16  
17 134 ▶sub parse_node {
Subroutine parse_node: called CC=21 SCAR=30.5
Pod undocumented
18 134 my ($node, $context) = @_;
19  
20 134 ▶ if (my $if_key = _strip_attr($node, 'tmpl-if')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $if_key = _strip_attr($node, "tmpl-if")) 16 118 134
21 16 my $unless = $if_key =~ s/^!//;
22 16 my $val = _get($context, $if_key);
23 16 ▶ if ( (!$unless && ! defined $val)
Branch: 1 branch
BranchTrueFalseTotal
if (not $unless || defined $val or $unless and defined $val) 6 10 16
Condition100%9/9
not $unless || defined $val or $unless and defined $val
l!l&&r!l&&!r
4210
$unless || defined $val
l!l&&r!l&&!r
484
$unless and defined $val
!ll&&!rl&&r
822
MC/DC100%4/4
not $unless || defined $val or $unless and defined $val
$unless defined $val $unless defined $val
Truth table57%4/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 6 $node->unbindNode;
26 }
27 }
28  
29 134 ▶ if (my $each_key = _strip_attr($node, 'tmpl-each')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $each_key = _strip_attr($node, "tmpl-each")) 20 114 134
30 20 my $parent = $node->parentNode;
31  
32 20 my $to_add = _get($context, $each_key);
33 20 ▶ if (!$to_add || ref $to_add ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $to_add or ref $to_add ne "ARRAY") 0 20 20
Condition33%1/3
not $to_add or ref $to_add ne "ARRAY"
l!l&&r!l&&!r
0020
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 20 foreach my $subcontext ( reverse @$to_add ) {
38 42 my $new = $node->cloneNode(1); # deep clone
39 42 parse_node($new, $subcontext);
40 42 $parent->insertAfter( $new, $node );
41 }
42 20 $node->unbindNode;
43 20 return;
44 }
45  
46 114 ▶ if (my $binding = _strip_attr($node, 'tmpl-bind')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $binding = _strip_attr($node, "tmpl-bind")) 48 66 114
47 48 my $val = _get($context, $binding);
48  
49 48 my $default = _strip_attr($node, 'tmpl-default');
50  
51 48 ▶ unless (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 4 44 48
52 4 ▶ $val = defined $default ? $default : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $default ? : 2 2 4
53 }
54  
55 48 $node->appendTextNode($val);
56 }
57  
58 114 ▶ if (my $attr_map = _strip_attr($node, 'tmpl-attr-map')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr_map = _strip_attr($node, "tmpl-attr-map")) 14 100 114
59 14 my @attributes = map { [ split qr/:/ ] } split qr/,/, $attr_map;
60  
61 14 foreach (@attributes) {
62 34 my $value = _get( $context, $_->[1] );
63 34 ▶ $node->setAttribute( $_->[0], $value ) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 24 10 34
64 }
65 }
66  
67 114 ▶ if ( my $attr_defaults = _strip_attr( $node, 'tmpl-attr-defaults' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr_defaults = _strip_attr($node, "tmpl-attr-defaults")) 8 106 114
68 my @attributes
69 = map {
70 8 [ map { s/\\([:,])/$1/g; $_ } # remove backslash escaping
71 split qr/(?<!\\):/ # split on non-backslashed colons
72 ]
73 }
74 split qr/(?<!\\),/, # split on non-backslashed commas
75 $attr_defaults;
76  
77 8 foreach (@attributes) {
78 20 my $value = $node->getAttribute( $_->[0] );
79 20 ▶ $node->setAttribute( $_->[0], $_->[1] )
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 14 6 20
Condition66%2/3
not defined $value or $value eq ""
l!l&&r!l&&!r
1406
MC/DC50%1/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table66%2/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
80 if ! defined $value || $value eq '';
81 }
82 }
83  
84 my @children = grep {
85 114 $_->nodeType eq XML_ELEMENT_NODE
86 } $node->childNodes;
87 114 parse_node($_, $context) foreach @children;
88 }
89  
90 116 ▶sub _get {
Subroutine _get: called CC=4 SCAR=13.9
91 116 my ($context, $key) = @_;
92  
93 116 ▶ return '' if !defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 116 116
94  
95 116 ▶ return $context if $key eq 'this';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "this" 32 84 116
96  
97 84 my @parts = split qr/\./, $key;
98 84 foreach (@parts) {
99 88 $context = $context->{$_};
100 }
101 84 return $context;
102 }
103  
104 658 ▶sub _strip_attr {
Subroutine _strip_attr: called CC=3 SCAR=11.0
105 658 my ($node, $attr_name) = @_;
106  
107 658 ▶ if (my $attributes = $node->attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attributes = $node->attributes) 658 0 658
108 658 ▶ if (my $attr = $attributes->removeNamedItem($attr_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr = $attributes->removeNamedItem($attr_name)) 110 548 658
109 110 return $attr->nodeValue;
110 }
111 }
112 }
113  
114 4 1;
115  
116 __END__
↑ Summary t/bind_data.t »