blib/lib/XML/BindData.pm

filter: Statementstmt 98.2% 56 / 57 Branchbran 87.5% 21 / 24 Conditioncond 83.3% 10 / 12 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 7 / 7 Podpod 50.0% 1 / 2 totaltotal 89.8% 97 / 108 SCAR 30.5
CC
21
Cov
94%
parse_node
16.1
_get
4.0
_strip_attr
3.0
CRAP 21.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.1.2';
6 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 sub bind {
9 20 ▶ my ($class, $xml_string, $data) = @_;
Subroutine bind: called CC=1 SCAR=0.0
Pod documented
10  
11 20 my $xml = XML::LibXML->load_xml(string => $xml_string);
12 20 parse_node($xml->documentElement, $data);
13  
14 20 return $xml->toStringC14N;
15 }
16  
17 sub parse_node {
18 60 ▶ my ($node, $context) = @_;
Subroutine parse_node: called CC=16 SCAR=27.8
Pod undocumented
19  
20 60 ▶ if (my $if_key = _strip_attr($node, 'tmpl-if')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $if_key = _strip_attr($node, "tmpl-if")) 8 52 60
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 60 ▶ if (my $each_key = _strip_attr($node, 'tmpl-each')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $each_key = _strip_attr($node, "tmpl-each")) 10 50 60
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 50 ▶ if (my $binding = _strip_attr($node, 'tmpl-bind')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $binding = _strip_attr($node, "tmpl-bind")) 23 27 50
47 23 my $val = _get($context, $binding);
48  
49 23 my $default = _strip_attr($node, 'tmpl-default');
50  
51 23 ▶ unless (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 2 21 23
52 2 ▶ $val = defined $default ? $default : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $default ? : 1 1 2
53 }
54  
55 23 $node->appendTextNode($val);
56 }
57  
58 50 ▶ if (my $attr_map = _strip_attr($node, 'tmpl-attr-map')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr_map = _strip_attr($node, "tmpl-attr-map")) 2 48 50
59 2 my @attributes = map { [ split qr/:/ ] } split qr/,/, $attr_map;
60  
61 2 foreach (@attributes) {
62 3 $node->setAttribute($_->[0], _get($context, $_->[1]));
63 }
64 }
65  
66 my @children = grep {
67 50 $_->nodeType eq XML_ELEMENT_NODE
68 } $node->childNodes;
69 50 parse_node($_, $context) foreach @children;
70 }
71  
72 sub _get {
73 43 ▶ my ($context, $key) = @_;
Subroutine _get: called CC=4 SCAR=13.9
74  
75 43 ▶ return '' if !defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 43 43
76  
77 43 ▶ return $context if $key eq 'this';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "this" 16 27 43
78  
79 27 my @parts = split qr/\./, $key;
80 27 foreach (@parts) {
81 29 $context = $context->{$_};
82 }
83 27 return $context;
84 }
85  
86 sub _strip_attr {
87 243 ▶ my ($node, $attr_name) = @_;
Subroutine _strip_attr: called CC=3 SCAR=11.0
88  
89 243 ▶ if (my $attributes = $node->attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attributes = $node->attributes) 243 0 243
90 243 ▶ if (my $attr = $attributes->removeNamedItem($attr_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr = $attributes->removeNamedItem($attr_name)) 45 198 243
91 45 return $attr->nodeValue;
92 }
93 }
94 }
95  
96 2 1;
97  
98 __END__
↑ Summary