blib/lib/YAML/PP/Representer.pm

filter: Statementstmt 96.0% 144 / 150 Branchbran 83.3% 80 / 96 Conditioncond 74.4% 32 / 43 MC/DCmcdc 59.2% 16 / 27 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 8 totaltotal 84.7% 288 / 340 CC 69 SCAR 30.1
CC
69
Cov
89%
_represent_noderef
27.5
represent_node
26.3
_represent_node_nonref
18.9
CRAP 20.4
« blib/lib/YAML/PP/Render.pm ↑ Summary blib/lib/YAML/PP/Common.pm »
1 49 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 49 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package YAML::PP::Representer;
4  
5 49 our $VERSION = 'v0.41.1'; # TRIAL VERSION
6  
7 49 ▶use Scalar::Util qw/ reftype blessed refaddr /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 49 ▶use YAML::PP::Common qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
10 YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE
11 YAML_DOUBLE_QUOTED_SCALAR_STYLE
12 YAML_ANY_SCALAR_STYLE
13 YAML_LITERAL_SCALAR_STYLE YAML_FOLDED_SCALAR_STYLE
14 YAML_FLOW_SEQUENCE_STYLE YAML_FLOW_MAPPING_STYLE
15 YAML_BLOCK_MAPPING_STYLE YAML_BLOCK_SEQUENCE_STYLE
16 PRESERVE_ORDER PRESERVE_SCALAR_STYLE PRESERVE_FLOW_STYLE PRESERVE_ALIAS
17 49 /;
18 49 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 764 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod undocumented
21 764 my ($class, %args) = @_;
22 764 ▶ my $preserve = delete $args{preserve} || 0;
Condition100%2/2
delete $args{'preserve'} || 0
l!l
12752
MC/DC100%1/1
delete $args{'preserve'} || 0
delete $args{'preserve'}
Truth table100%2/2
A
A= delete $args{'preserve'}
Aresult
11
00
23 764 ▶ if ($preserve == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preserve == 1) 1 763 764
24 1 $preserve = PRESERVE_ORDER | PRESERVE_SCALAR_STYLE | PRESERVE_FLOW_STYLE | PRESERVE_ALIAS;
25 }
26 my $self = bless {
27 schema => delete $args{schema},
28 764 preserve => $preserve,
29 }, $class;
30 764 ▶ if (keys %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %args) 0 764 764
31 0 die "Unexpected arguments: " . join ', ', sort keys %args;
32 }
33 764 return $self;
34 }
35  
36 9 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
37 9 my ($self) = @_;
38 my $clone = {
39 schema => $self->schema,
40 preserve => $self->{preserve},
41 9 };
42 9 return bless $clone, ref $self;
43 }
44  
45 3959 ▶sub schema { return $_[0]->{schema} }
Subroutine schema: called CC=1 SCAR=0.0
Pod undocumented
46 458 ▶sub preserve_order { return $_[0]->{preserve} & PRESERVE_ORDER }
Subroutine preserve_order: called CC=1 SCAR=0.0
Pod undocumented
47 3817 ▶sub preserve_scalar_style { return $_[0]->{preserve} & PRESERVE_SCALAR_STYLE }
Subroutine preserve_scalar_style: called CC=1 SCAR=0.0
Pod undocumented
48 724 ▶sub preserve_flow_style { return $_[0]->{preserve} & PRESERVE_FLOW_STYLE }
Subroutine preserve_flow_style: called CC=1 SCAR=0.0
Pod undocumented
49 3888 ▶sub preserve_alias { return $_[0]->{preserve} & PRESERVE_ALIAS }
Subroutine preserve_alias: called CC=1 SCAR=0.0
Pod undocumented
50  
51 3817 ▶sub represent_node {
Subroutine represent_node: called CC=26 SCAR=32.7
Pod undocumented
52 3817 my ($self, $node) = @_;
53  
54 3817 my $preserve_alias = $self->preserve_alias;
55 3817 my $preserve_style = $self->preserve_scalar_style;
56 3817 ▶ if ($preserve_style or $preserve_alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preserve_style or $preserve_alias) 131 3686 3817
Condition100%3/3
$preserve_style or $preserve_alias
l!l&&r!l&&!r
49823686
MC/DC100%2/2
$preserve_style or $preserve_alias
$preserve_style $preserve_alias
Truth table100%3/3
A or B
A= $preserve_styleB= $preserve_alias
ABresult
1X1
011
000
57 131 ▶ if (ref $node->{value} eq 'YAML::PP::Preserve::Scalar') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node->{"value"} eq "YAML::PP::Preserve::Scalar") 87 44 131
58 87 my $value = $node->{value}->value;
59 87 ▶ if ($preserve_style) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preserve_style) 21 66 87
60 21 $node->{style} = $node->{value}->style;
61 }
62 # $node->{tag} = $node->{value}->tag;
63 87 $node->{value} = $value;
64 }
65 }
66 3817 $node->{reftype} = reftype($node->{value});
67 3817 ▶ if (not $node->{reftype} and reftype(\$node->{value}) eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $node->{"reftype"} and builtin::reftype(\$node->{"value"}) eq "GLOB") 6 3811 3817
Condition100%3/3
not $node->{"reftype"} and builtin::reftype(\$node->{"value"}) eq "GLOB"
!ll&&!rl&&r
80130106
MC/DC100%2/2
not $node->{"reftype"} and builtin::reftype(\$node->{"value"}) eq "GLOB"
not $node->{"reftype"} builtin::reftype(\$node->{"value"}) eq "GLOB"
Truth table100%3/3
not(A) and B
A= not $node->{"reftype"}B= builtin::reftype(\$node->{"value"}) eq "GLOB"
ABresult
0X0
100
111
68 6 $node->{reftype} = 'GLOB';
69 }
70  
71 3817 ▶ if ($node->{reftype}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{'reftype'}) { } 807 3010 3817
72 807 $self->_represent_noderef($node);
73 }
74 else {
75 3010 $self->_represent_node_nonref($node);
76 }
77 3817 ▶ $node->{reftype} = (reftype $node->{data}) || '';
Condition100%2/2
builtin::reftype($node->{'data'}) || ''
l!l
7253092
MC/DC100%1/1
builtin::reftype($node->{'data'}) || ''
builtin::reftype($node->{'data'})
Truth table100%2/2
A
A= builtin::reftype($node->{'data'})
Aresult
11
00
78  
79 3817 ▶ if ($node->{reftype} eq 'HASH' and my $tied = tied(%{ $node->{data} })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"reftype"} eq "HASH" and my $tied = tied %{$$node{"data"};}) 34 3783 3817
Condition100%3/3
$node->{"reftype"} eq "HASH" and my $tied = tied %{$$node{"data"};}
!ll&&!rl&&r
335942434
MC/DC100%2/2
$node->{"reftype"} eq "HASH" and my $tied = tied %{$$node{"data"};}
$node->{"reftype"} eq "HASH" my $tied = tied %{$$node{"data"};}
Truth table100%3/3
A and B
A= $node->{"reftype"} eq "HASH"B= my $tied = tied %{$$node{"data"};}
ABresult
0X0
100
111
80 34 my $representers = $self->schema->representers;
81 34 $tied = ref $tied;
82 34 ▶ if (my $def = $representers->{tied_equals}->{ $tied }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $def = $representers->{"tied_equals"}{$tied}) 0 34 34
83 0 my $code = $def->{code};
84 0 my $done = $code->($self, $node);
85 }
86 }
87  
88 3817 ▶ if ($node->{reftype} eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ($node->{'reftype'} eq 'HASH') { } 458 3359 3817
elsif ($node->{'reftype'} eq 'ARRAY') { } 266 3093 3359
elsif ($node->{'reftype'}) { } 1 3092 3093
89 458 ▶ unless (defined $node->{items}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node->{"items"}) 458 0 458
90 # by default we sort hash keys
91 458 my @keys;
92 458 ▶ if ($self->preserve_order) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->preserve_order) { } 24 434 458
93 24 @keys = keys %{ $node->{data} };
94 }
95 else {
96 434 @keys = sort keys %{ $node->{data} };
97 }
98 458 for my $key (@keys) {
99 876 push @{ $node->{items} }, $key, $node->{data}->{ $key };
100 }
101 }
102 458 my %args;
103 458 ▶ if ($self->preserve_flow_style and reftype $node->{value} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->preserve_flow_style and builtin::reftype($node->{"value"}) eq "HASH") 19 439 458
Condition66%2/3
$self->preserve_flow_style and builtin::reftype($node->{"value"}) eq "HASH"
!ll&&!rl&&r
439019
MC/DC50%1/2
$self->preserve_flow_style and builtin::reftype($node->{"value"}) eq "HASH"
$self->preserve_flow_style builtin::reftype($node->{"value"}) eq "HASH"
Truth table66%2/3
A and B
A= $self->preserve_flow_styleB= builtin::reftype($node->{"value"}) eq "HASH"
ABresult
0X0
100
111
104 19 ▶ if (my $tied = tied %{ $node->{value} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tied = tied %{$$node{"value"};}) 18 1 19
105 18 $args{style} = $tied->{style};
106 }
107 }
108 458 return [ mapping => $node, %args ];
109 }
110 elsif ($node->{reftype} eq 'ARRAY') {
111 266 ▶ unless (defined $node->{items}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node->{"items"}) 266 0 266
112 266 @{ $node->{items} } = @{ $node->{data} };
113 }
114 266 my %args;
115 266 ▶ if ($self->preserve_flow_style and reftype $node->{value} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->preserve_flow_style and builtin::reftype($node->{"value"}) eq "ARRAY") 10 256 266
Condition66%2/3
$self->preserve_flow_style and builtin::reftype($node->{"value"}) eq "ARRAY"
!ll&&!rl&&r
256010
MC/DC50%1/2
$self->preserve_flow_style and builtin::reftype($node->{"value"}) eq "ARRAY"
$self->preserve_flow_style builtin::reftype($node->{"value"}) eq "ARRAY"
Truth table66%2/3
A and B
A= $self->preserve_flow_styleB= builtin::reftype($node->{"value"}) eq "ARRAY"
ABresult
0X0
100
111
116 10 ▶ if (my $tied = tied @{ $node->{value} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tied = tied @{$$node{"value"};}) 10 0 10
117 10 $args{style} = $tied->{style};
118 }
119 }
120 266 return [ sequence => $node, %args ];
121 }
122 elsif ($node->{reftype}) {
123 1 die "Cannot handle reftype '$node->{reftype}' (you might want to enable YAML::PP::Schema::Perl)";
124 }
125 else {
126 3092 ▶ unless (defined $node->{items}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node->{"items"}) 3026 66 3092
127 3026 $node->{items} = [$node->{data}];
128 }
129 3092 return [ scalar => $node ];
130 }
131  
132 }
133  
134 49 my $bool_code = <<'EOM';
135 sub {
136 my ($x) = @_;
137 use experimental qw/ builtin /;
138 builtin::is_bool($x);
139 }
140 EOM
141 49 my $is_bool;
142  
143 3010 ▶sub _represent_node_nonref {
Subroutine _represent_node_nonref: called CC=18 SCAR=29.4
144 3010 my ($self, $node) = @_;
145 3010 my $representers = $self->schema->representers;
146  
147 3010 ▶ if (not defined $node->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node->{"value"}) 110 2900 3010
148 110 ▶ if (my $undef = $representers->{undef}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $undef = $representers->{'undef'}) { } 110 0 110
149 110 ▶ return 1 if $undef->($self, $node);
Branch: 1 branch
BranchTrueFalseTotal
if &$undef($self, $node) 110 0 110
150 }
151 else {
152 0 $node->{style} = YAML_SINGLE_QUOTED_SCALAR_STYLE;
153 0 $node->{data} = '';
154 0 return 1;
155 }
156 }
157 2900 ▶ if ($] >= 5.036000 and my $rep = $representers->{bool}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.036" and my $rep = $representers->{"bool"}) 748 2152 2900
Condition66%2/3
$] >= "5.036" and my $rep = $representers->{"bool"}
!ll&&!rl&&r
02152748
MC/DC50%1/2
$] >= "5.036" and my $rep = $representers->{"bool"}
$] >= "5.036" my $rep = $representers->{"bool"}
Truth table66%2/3
A and B
A= $] >= "5.036"B= my $rep = $representers->{"bool"}
ABresult
0X0
100
111
158 748 ▶ $is_bool ||= eval $bool_code;
Subroutine BEGIN: called CC=1 SCAR=0.0
Condition66%2/3
$is_bool ||= eval $bool_code
l!l&&r!l&&!r
733150
MC/DC0%0/2
$is_bool ||= eval $bool_code
$is_bool eval $bool_code
Truth table66%2/3
A ||= B
A= $is_boolB= eval $bool_code
ABresult
1X1
011
000
159 748 ▶ if ($is_bool->($node->{value})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$is_bool($node->{"value"})) 2 746 748
160 2 return $rep->{code}->($self, $node);
161 }
162 }
163 2898 for my $rep (@{ $representers->{flags} }) {
164 4969 my $check_flags = $rep->{flags};
165 4969 my $flags = B::svref_2object(\$node->{value})->FLAGS;
166 4969 ▶ if ($flags & $check_flags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & $check_flags) 517 4452 4969
167 517 ▶ return 1 if $rep->{code}->($self, $node);
Branch: 1 branch
BranchTrueFalseTotal
if $rep->{"code"}->($self, $node) 506 11 517
168 }
169  
170 }
171 2392 ▶ if (my $rep = $representers->{equals}->{ $node->{value} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rep = $representers->{"equals"}{$node->{"value"}}) 138 2254 2392
172 138 ▶ return 1 if $rep->{code}->($self, $node);
Branch: 1 branch
BranchTrueFalseTotal
if $rep->{"code"}->($self, $node) 138 0 138
173 }
174 2254 for my $rep (@{ $representers->{regex} }) {
175 2050 ▶ if ($node->{value} =~ $rep->{regex}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"value"} =~ /$rep->{"regex"}/) 101 1949 2050
176 101 ▶ return 1 if $rep->{code}->($self, $node);
Branch: 1 branch
BranchTrueFalseTotal
if $rep->{"code"}->($self, $node) 95 6 101
177 }
178 }
179 2159 ▶ unless (defined $node->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node->{"data"}) 2159 0 2159
180 2159 $node->{data} = $node->{value};
181 }
182 2159 ▶ unless (defined $node->{style}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node->{"style"}) 2143 16 2159
183 2143 $node->{style} = YAML_ANY_SCALAR_STYLE;
184 2143 $node->{style} = "";
185 }
186 }
187  
188 807 ▶sub _represent_noderef {
Subroutine _represent_noderef: called CC=24 SCAR=33.1
189 807 my ($self, $node) = @_;
190 807 my $representers = $self->schema->representers;
191  
192 807 ▶ if (my $classname = blessed($node->{value})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $classname = builtin::blessed($node->{"value"})) 112 695 807
193 112 ▶ if (my $def = $representers->{class_equals}->{ $classname }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $def = $representers->{"class_equals"}{$classname}) 66 46 112
194 66 my $code = $def->{code};
195 66 ▶ return 1 if $code->($self, $node);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($self, $node) 66 0 66
196 }
197 46 for my $matches (@{ $representers->{class_matches} }) {
198 43 my ($re, $code) = @$matches;
199 43 ▶ if (ref $re and $classname =~ $re or $re) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $re and $classname =~ /$re/ or $re) 43 0 43
Condition33%2/6
ref $re and $classname =~ /$re/ or $re
l!l&&r!l&&!r
0430
ref $re and $classname =~ /$re/
!ll&&!rl&&r
4300
MC/DC0%0/3
ref $re and $classname =~ /$re/ or $re
ref $re $classname =~ /$re/ $re
Truth table20%1/5
A and B or C
A= ref $reB= $classname =~ /$re/C= $re
ABCresult
11X1
0X11
1011
0X00
1000
200 43 ▶ return 1 if $code->($self, $node);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($self, $node) 42 1 43
201 }
202 }
203 4 for my $isa (@{ $representers->{class_isa} }) {
204 3 my ($class_name, $code) = @$isa;
205 3 ▶ if ($node->{ value }->isa($class_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"value"}->isa($class_name)) 2 1 3
206 2 ▶ return 1 if $code->($self, $node);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($self, $node) 2 0 2
207 }
208 }
209 }
210 697 ▶ if ($node->{reftype} eq 'SCALAR' and my $scalarref = $representers->{scalarref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"reftype"} eq "SCALAR" and my $scalarref = $representers->{"scalarref"}) 4 693 697
Condition100%3/3
$node->{"reftype"} eq "SCALAR" and my $scalarref = $representers->{"scalarref"}
!ll&&!rl&&r
69214
MC/DC100%2/2
$node->{"reftype"} eq "SCALAR" and my $scalarref = $representers->{"scalarref"}
$node->{"reftype"} eq "SCALAR" my $scalarref = $representers->{"scalarref"}
Truth table100%3/3
A and B
A= $node->{"reftype"} eq "SCALAR"B= my $scalarref = $representers->{"scalarref"}
ABresult
0X0
100
111
211 4 my $code = $scalarref->{code};
212 4 ▶ return 1 if $code->($self, $node);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($self, $node) 4 0 4
213 }
214 693 ▶ if ($node->{reftype} eq 'REF' and my $refref = $representers->{refref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"reftype"} eq "REF" and my $refref = $representers->{"refref"}) 4 689 693
Condition66%2/3
$node->{"reftype"} eq "REF" and my $refref = $representers->{"refref"}
!ll&&!rl&&r
68904
MC/DC50%1/2
$node->{"reftype"} eq "REF" and my $refref = $representers->{"refref"}
$node->{"reftype"} eq "REF" my $refref = $representers->{"refref"}
Truth table66%2/3
A and B
A= $node->{"reftype"} eq "REF"B= my $refref = $representers->{"refref"}
ABresult
0X0
100
111
215 4 my $code = $refref->{code};
216 4 ▶ return 1 if $code->($self, $node);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($self, $node) 4 0 4
217 }
218 689 ▶ if ($node->{reftype} eq 'CODE' and my $coderef = $representers->{coderef}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"reftype"} eq "CODE" and my $coderef = $representers->{"coderef"}) 5 684 689
Condition66%2/3
$node->{"reftype"} eq "CODE" and my $coderef = $representers->{"coderef"}
!ll&&!rl&&r
68405
MC/DC50%1/2
$node->{"reftype"} eq "CODE" and my $coderef = $representers->{"coderef"}
$node->{"reftype"} eq "CODE" my $coderef = $representers->{"coderef"}
Truth table66%2/3
A and B
A= $node->{"reftype"} eq "CODE"B= my $coderef = $representers->{"coderef"}
ABresult
0X0
100
111
219 5 my $code = $coderef->{code};
220 5 ▶ return 1 if $code->($self, $node);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($self, $node) 5 0 5
221 }
222 684 ▶ if ($node->{reftype} eq 'GLOB' and my $glob = $representers->{glob}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"reftype"} eq "GLOB" and my $glob = $representers->{"glob"}) 6 678 684
Condition66%2/3
$node->{"reftype"} eq "GLOB" and my $glob = $representers->{"glob"}
!ll&&!rl&&r
67806
MC/DC50%1/2
$node->{"reftype"} eq "GLOB" and my $glob = $representers->{"glob"}
$node->{"reftype"} eq "GLOB" my $glob = $representers->{"glob"}
Truth table66%2/3
A and B
A= $node->{"reftype"} eq "GLOB"B= my $glob = $representers->{"glob"}
ABresult
0X0
100
111
223 6 my $code = $glob->{code};
224 6 ▶ return 1 if $code->($self, $node);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($self, $node) 6 0 6
225 }
226 678 $node->{data} = $node->{value};
227  
228 }
229  
230 49 1;
« blib/lib/YAML/PP/Render.pm ↑ Summary blib/lib/YAML/PP/Common.pm »