blib/lib/XML/WriterX/Simple.pm

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