blib/lib/XML/WriterX/Simple.pm

filter: Statementstmt 88.8% 56 / 63 Branchbran 77.4% 48 / 62 Conditioncond 100% 11 / 11 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 7 / 7 Podpod 75.0% 3 / 4 totaltotal 84.3% 129 / 153 CC 36 SCAR 25.5
CC
36
Cov
85%
stringify
25.3
arrange_args
14.0
produce
10.0
CRAP 12.8
↑ Summary
1 package XML::WriterX::Simple;
2 3 $XML::WriterX::Simple::VERSION = '0.151401';
3 #ABSTRACT:Make XML production simpler.
4 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9  
10 21 ▶sub XML::Writer::produce{
Subroutine produce: called CC=10 SCAR=23.0
Pod undocumented
11 21 my $writer = shift;
12
13 TAG:
14 21 ▶ while( my $tagName = shift ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $tagName = shift()) 22 21 43
15 22 my ($attr, $children, $text) = XML::WriterX::Simple::arrange_args( $writer, shift );
16 22 ▶ if(($text//'') eq '' and !@$children){
Branch: 1 branch
BranchTrueFalseTotal
if (($text // '') eq '' and not @$children) { } 1 21 22
Condition100%5/5
($text // "") eq "" and not @$children
!ll&&!rl&&r
3181
$text // ""
l!l
319
MC/DC0%0/2
($text // "") eq "" and not @$children
$text not @$children
Truth table0%0/3
A and not(B)
A= $textB= not @$children
ABresult
0X0
100
111
17 1 $writer->emptyTag( $tagName, @$attr );
18 }
19 else{
20 21 ▶ if(ref($text) eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $text eq 'CODE') { } 3 18 21
21 3 XML::WriterX::Simple::produce_content( $writer, $tagName => $text );
22 }
23 else{
24 18 $writer->startTag( $tagName, @$attr );
25 18 ▶ $writer->characters( $text ) if defined $text;
Branch: 1 branch
BranchTrueFalseTotal
if defined $text 0 18 18
26 18 ▶ while( @$children ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$children) 26 18 44
27 26 my ($tag, $content) = (shift(@$children), shift(@$children));
28 26 ▶ $tag = ".$tagName" if $tag =~ /^[&"]$/ and ref $content eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /^[&"]$/ and ref $content eq "CODE" 2 24 26
Condition100%3/3
$tag =~ /^[&"]$/ and ref $content eq "CODE"
!ll&&!rl&&r
10142
MC/DC100%2/2
$tag =~ /^[&"]$/ and ref $content eq "CODE"
$tag =~ /^[&"]$/ ref $content eq "CODE"
Truth table100%3/3
A and B
A= $tag =~ /^[&"]$/B= ref $content eq "CODE"
ABresult
0X0
100
111
29 26 XML::WriterX::Simple::produce_content( $writer, $tag => $content );
30 }
31 18 $writer->endTag( $tagName );
32 }
33 }
34 }
35 #return writer to allow chaining
36 21 $writer;
37 }
38  
39  
40 16 ▶sub stringify{
Subroutine stringify: called CC=6 SCAR=32.3
Pod documented
41 16 local $_ = shift;
42 16 ▶ return $_ unless ref;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 16 0 16
43 0 ▶ if(ref eq 'ARRAY'){
Branch: 4 branches
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 0 0 0
elsif (ref $_ eq 'HASH') { } 0 0 0
elsif (ref $_ eq 'SCALAR') { } 0 0 0
elsif (ref $_ eq 'REF') { } 0 0 0
44 }
45 elsif(ref eq 'HASH'){
46 }
47 elsif(ref eq 'SCALAR'){
48 0 return $$_;
49 }
50 elsif(ref eq 'REF'){
51 0 return stringify( $$_ );
52 }
53 0 return "$_";#? REGEXP or GLOB or blessed object... : let's use perl stringifier
54 }
55  
56 22 ▶sub arrange_args{
Subroutine arrange_args: called CC=14 SCAR=26.4
Pod documented
57 22 my ($writer, $tagContent) = @_;
58 22 my (@attr, @children, $text);
59 22 ▶ if(ref $tagContent eq 'CODE'){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $tagContent eq 'CODE') { } 3 19 22
elsif (defined $tagContent) { } 18 1 19
60 3 $text = $tagContent;
61 }
62 elsif(defined $tagContent){
63 18 ▶ @_ = @$tagContent if ref $tagContent eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $tagContent eq "ARRAY" 5 13 18
64 18 ▶ @_ = %$tagContent if ref $tagContent eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $tagContent eq "HASH" 1 17 18
65 18 ▶ @_ = ('"', $$tagContent) if ref($tagContent) =~ /^(SCALAR|REF)$/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($tagContent) =~ /^(SCALAR|REF)$/ 1 17 18
66 18 ▶ @_ = ('"', $tagContent) if !ref $tagContent;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tagContent 11 7 18
67 18 ▶ while( @_ ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 31 18 49
68 31 my ($tag,$content) = (shift, shift);
69 31 ▶ if($tag =~ /^#/){
Branch: 3 branches
BranchTrueFalseTotal
if ($tag =~ /^#/) { } 2 29 31
elsif ($tag =~ /^:attr/ and ref($content) =~ /^(ARRAY|HASH)$/) { } 1 28 29
elsif ($tag =~ /^:(.*)/) { } 4 24 28
70 #comment is added as a children
71 2 push @children, $tag => $content;
72 }
73 ▶ elsif($tag =~ /^:attr/ and ref($content) =~ /^(ARRAY|HASH)$/){
Condition100%3/3
$tag =~ /^:attr/ and ref($content) =~ /^(ARRAY|HASH)$/
!ll&&!rl&&r
2711
MC/DC100%2/2
$tag =~ /^:attr/ and ref($content) =~ /^(ARRAY|HASH)$/
$tag =~ /^:attr/ ref($content) =~ /^(ARRAY|HASH)$/
Truth table100%3/3
A and B
A= $tag =~ /^:attr/B= ref($content) =~ /^(ARRAY|HASH)$/
ABresult
0X0
100
111
74 #multiple attributes in $content
75 1 ▶ my @attrs = $1 eq 'ARRAY' ? @$content : %$content;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'ARRAY' ? : 1 0 1
76 #!warning if %attr != @$content : attribute name must be unique!
77 1 ▶ push @attr, shift(@attrs), shift(@attrs) while @attrs;
Branch: 1 branch
BranchTrueFalseTotal
@attrs and do { push @attr, shift @attrs, shift @attrs } 2 1 3
78 }
79 elsif($tag =~ /^:(.*)/){
80 #single attribute named $1 value is stringify($content)
81 4 push @attr, $1 => $content;
82 }
83 else{
84 #(processing instruction are added as a children like all other cases)
85 24 push @children, $tag => $content;
86 }
87 }
88 }
89 22 return (\@attr, \@children, $text);
90 }
91  
92  
93 #CANNOT:
94 # - produce a comment from a sub{}
95 # - produce a tag attributes from a sub{}
96 # - prodice attributes from a sub{} ?
97 29 ▶sub produce_content{
Subroutine produce_content: called CC=9 SCAR=22.6
Pod documented
98 29 my ($writer, $tag, $content) = @_;
99  
100 29 ▶ unless($tag){
Branch: 1 branch
BranchTrueFalseTotal
unless ($tag) 0 29 29
101 0 warn "try to produce content without any tag name!";
102 0 $DB::single=1;
103 0 return;
104 }
105  
106 29 ▶ if(ref($content) eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "CODE") 5 24 29
107 5 ▶ $writer->startTag( $tag ) unless $tag =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /^\./ 2 3 5
108 5 $content->( $writer, $tag );
109 5 ▶ $writer->endTag( $tag ) unless $tag =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /^\./ 2 3 5
110 5 return;
111 }
112  
113 24 ▶ return $writer->characters( stringify( $content ) )
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "\"" 14 10 24
114 if $tag eq '"';
115  
116 10 ▶ return $writer->comment( stringify $content )
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /^#/ 2 8 10
117 if $tag =~ /^#/;
118  
119 8 ▶ return $writer->pi( $1 => stringify $content )
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /^\?(.*)/ 0 8 8
120 if $tag =~ /^\?(.*)/;
121
122 8 ▶ return $writer->produce( $tag, $$content )
Branch: 1 branch
BranchTrueFalseTotal
if ref $content eq "SCALAR" 0 8 8
123 if ref $content eq 'SCALAR';
124
125 8 return $writer->produce( $tag, $content );
126 }
127  
128  
129 3 1; # End of XML::WriterX::Simple
130  
131 __END__
↑ Summary