blib/lib/YAML/PP/Emitter.pm

filter: Statementstmt 96.3% 552 / 573 Branchbran 93.1% 352 / 378 Conditioncond 96.8% 92 / 95 MC/DCmcdc 94.6% 53 / 56 Subroutinesub 97.2% 35 / 36 Podpod 68.0% 17 / 25 totaltotal 94.6% 1101 / 1163 CC 229 SCAR 32.1
CC
229
Cov
95%
_find_best_scalar_style
42.0
scalar_event
40.0
sequence_start_event
29.1
CRAP 24.8
↑ Summary blib/lib/YAML/PP/Render.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::Emitter;
4  
5 49 our $VERSION = 'v0.43.0'; # VERSION
6  
7 49 ▶use YAML::PP::Common qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
8 YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE
9 YAML_DOUBLE_QUOTED_SCALAR_STYLE
10 YAML_LITERAL_SCALAR_STYLE YAML_FOLDED_SCALAR_STYLE
11 YAML_FLOW_SEQUENCE_STYLE YAML_FLOW_MAPPING_STYLE
12 49 /;
13  
14 49 ▶use constant DEBUG => $ENV{YAML_PP_EMIT_DEBUG} ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'YAML_PP_EMIT_DEBUG'} ? : 0 49 49
15 49 if (DEBUG) { require Data::Dumper }
16 49 ▶use constant DEFAULT_WIDTH => 80;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2243 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
19 2243 my ($class, %args) = @_;
20 my $self = bless {
21 indent => $args{indent} || 2,
22 writer => $args{writer},
23 2243 ▶ width => $args{width} || DEFAULT_WIDTH,
Condition75%3/4
$args{'indent'} || 2
l!l
1477766
$args{'width'} || 80
l!l
02243
MC/DC100%1/1
$args{'indent'} || 2
$args{'indent'}
MC/DC0%0/1
$args{'width'} || 80
$args{'width'}
Truth table100%2/2
A
A= $args{'indent'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'width'}
Aresult
11
00
24 }, $class;
25 2243 $self->init;
26 2243 return $self;
27 }
28  
29 9 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
30 9 my ($self) = @_;
31 9 my $clone = {
32 indent => $self->indent,
33 };
34 9 return bless $clone, ref $self;
35 }
36  
37 44735 ▶sub event_stack { return $_[0]->{event_stack} }
Subroutine event_stack: called CC=1 SCAR=0.0
Pod undocumented
38 6545 ▶sub set_event_stack { $_[0]->{event_stack} = $_[1] }
Subroutine set_event_stack: called CC=1 SCAR=0.0
Pod undocumented
39 4579 ▶sub indent { return $_[0]->{indent} }
Subroutine indent: called CC=1 SCAR=0.0
Pod documented
40 1415 ▶sub width { return $_[0]->{width} }
Subroutine width: called CC=1 SCAR=0.0
Pod undocumented
41 0 ▶sub line { return $_[0]->{line} }
Subroutine line: not called CC=1 SCAR=6.9
Pod undocumented
42 21567 ▶sub column { return $_[0]->{column} }
Subroutine column: called CC=1 SCAR=0.0
Pod undocumented
43 1 ▶sub set_indent { $_[0]->{indent} = $_[1] }
Subroutine set_indent: called CC=1 SCAR=0.0
Pod documented
44 28419 ▶sub writer { $_[0]->{writer} }
Subroutine writer: called CC=1 SCAR=0.0
Pod documented
45 5207 ▶sub set_writer { $_[0]->{writer} = $_[1] }
Subroutine set_writer: called CC=1 SCAR=0.0
Pod documented
46 649 ▶sub tagmap { return $_[0]->{tagmap} }
Subroutine tagmap: called CC=1 SCAR=0.0
Pod undocumented
47 5308 ▶sub set_tagmap { $_[0]->{tagmap} = $_[1] }
Subroutine set_tagmap: called CC=1 SCAR=0.0
Pod undocumented
48  
49 5308 ▶sub init {
Subroutine init: called CC=2 SCAR=6.9
Pod documented
50 5308 my ($self) = @_;
51 5308 ▶ unless ($self->writer) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->writer) 2242 3066 5308
52 2242 $self->set_writer(YAML::PP::Writer->new);
53 }
54 $self->set_tagmap({
55 5308 'tag:yaml.org,2002:' => '!!',
56 });
57 5308 $self->{open_ended} = 0;
58 5308 $self->{line} = 0;
59 5308 $self->{column} = 0;
60 5308 $self->writer->init;
61 }
62  
63 1940 ▶sub mapping_start_event {
Subroutine mapping_start_event: called CC=28 SCAR=33.4
Pod documented
64 1940 DEBUG and warn __PACKAGE__.':'.__LINE__.": +++ mapping_start_event\n";
65 1940 my ($self, $info) = @_;
66 1940 my $stack = $self->event_stack;
67 1940 my $last = $stack->[-1];
68 1940 my $indent = $last->{indent};
69 1940 my $new_indent = $indent;
70 1940 my $yaml = '';
71  
72 1940 my $props = '';
73 1940 my $anchor = $info->{anchor};
74 1940 my $tag = $info->{tag};
75 1940 ▶ if (defined $anchor) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $anchor) 98 1842 1940
76 98 $anchor = "&$anchor";
77 }
78 1940 ▶ if (defined $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag) 110 1830 1940
79 110 $tag = $self->_emit_tag('map', $tag);
80 }
81 1940 $props = join ' ', grep defined, ($anchor, $tag);
82  
83 1940 ▶ my $flow = $last->{flow} || 0;
Condition100%2/2
$last->{'flow'} || 0
l!l
1381802
MC/DC100%1/1
$last->{'flow'} || 0
$last->{'flow'}
Truth table100%2/2
A
A= $last->{'flow'}
Aresult
11
00
84 1940 ▶ $flow++ if ($info->{style} || 0) eq YAML_FLOW_MAPPING_STYLE;
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"style"} || 0) eq 2 365 1575 1940
Condition100%2/2
$info->{'style'} || 0
l!l
8111129
MC/DC100%1/1
$info->{'style'} || 0
$info->{'style'}
Truth table100%2/2
A
A= $info->{'style'}
Aresult
11
00
85  
86 1940 my $newline = 0;
87 1940 ▶ if ($flow > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flow > 1) { } 138 1802 1940
88 138 ▶ if ($last->{type} eq 'SEQ') {
Branch: 3 branches
BranchTrueFalseTotal
if ($last->{'type'} eq 'SEQ') { } 95 43 138
elsif ($last->{'type'} eq 'MAP') { } 2 41 43
elsif ($last->{'type'} eq 'MAPVALUE') { } 41 0 41
89 95 ▶ if ($last->{newline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"newline"}) 17 78 95
90 17 $yaml .= ' ';
91 }
92 95 ▶ if ($last->{index} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'index'} == 0) { } 27 68 95
93 27 $yaml .= "[";
94 }
95 else {
96 68 $yaml .= ",";
97 }
98 }
99 elsif ($last->{type} eq 'MAP') {
100 2 ▶ if ($last->{newline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"newline"}) 0 2 2
101 0 $yaml .= ' ';
102 }
103 2 ▶ if ($last->{index} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'index'} == 0) { } 2 0 2
104 2 $yaml .= "{";
105 }
106 else {
107 0 $yaml .= ",";
108 }
109 }
110 elsif ($last->{type} eq 'MAPVALUE') {
111 41 ▶ if ($last->{index} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"index"} == 0) 0 41 41
112 0 die "Should not happen (index 0 in MAPVALUE)";
113 }
114 41 $yaml .= ": ";
115 }
116 138 ▶ if ($props) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props) 18 120 138
117 18 $yaml .= " $props ";
118 }
119 138 $new_indent .= ' ' x $self->indent;
120 }
121 else {
122 1802 ▶ if ($last->{type} eq 'DOC') {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'type'} eq 'DOC') { } 985 817 1802
123 985 $newline = $last->{newline};
124 }
125 else {
126 817 ▶ if ($last->{newline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"newline"}) 108 709 817
127 108 $yaml .= "\n";
128 108 $last->{column} = 0;
129 }
130 817 ▶ if ($last->{type} eq 'MAPVALUE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'type'} eq 'MAPVALUE') { } 263 554 817
131 263 $new_indent .= ' ' x $self->indent;
132 263 $newline = 1;
133 }
134 else {
135 554 $new_indent = $indent;
136 554 ▶ if (not $props and $self->indent == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $props and $self->indent == 1) { } 91 463 554
Condition100%3/3
not $props and $self->indent == 1
!ll&&!rl&&r
4441991
MC/DC100%2/2
not $props and $self->indent == 1
not $props $self->indent == 1
Truth table100%3/3
not(A) and B
A= not $propsB= $self->indent == 1
ABresult
0X0
100
111
137 91 $new_indent .= ' ' x 2;
138 }
139 else {
140 463 $new_indent .= ' ' x $self->indent;
141 }
142  
143 554 ▶ if ($last->{column}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'column'}) { } 15 539 554
144 15 ▶ my $space = $self->indent > 1 ? ' ' x ($self->indent - 1) : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$self->indent > 1 ? : 12 3 15
145 15 $yaml .= $space;
146 }
147 else {
148 539 $yaml .= $indent;
149 }
150 554 ▶ if ($last->{type} eq 'SEQ') {
Branch: 3 branches
BranchTrueFalseTotal
if ($last->{'type'} eq 'SEQ') { } 526 28 554
elsif ($last->{'type'} eq 'MAP') { } 19 9 28
elsif ($last->{'type'} eq 'COMPLEXVALUE') { } 9 0 9
151 526 $yaml .= '-';
152 }
153 elsif ($last->{type} eq 'MAP') {
154 19 $yaml .= "?";
155 19 $last->{type} = 'COMPLEX';
156 }
157 elsif ($last->{type} eq 'COMPLEXVALUE') {
158 9 $yaml .= ":";
159 }
160 else {
161 0 die "Should not happen ($last->{type} in mapping_start)";
162 }
163 554 $last->{column} = 1;
164 }
165 817 $last->{newline} = 0;
166 }
167 1802 ▶ if ($props) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props) 159 1643 1802
168 159 ▶ $yaml .= $last->{column} ? ' ' : $indent;
Branch: 1 branch
BranchTrueFalseTotal
$last->{'column'} ? : 153 6 159
169 159 $yaml .= $props;
170 159 $newline = 1;
171 }
172 }
173 1940 $self->_write($yaml);
174 1940 my $new_info = {
175 index => 0, indent => $new_indent, info => $info,
176 newline => $newline,
177 column => $self->column,
178 flow => $flow,
179 };
180 1940 $new_info->{type} = 'MAP';
181 1940 push @{ $stack }, $new_info;
182 1940 $last->{index}++;
183 1940 $self->{open_ended} = 0;
184 }
185  
186 1939 ▶sub mapping_end_event {
Subroutine mapping_end_event: called CC=11 SCAR=24.0
Pod documented
187 1939 DEBUG and warn __PACKAGE__.':'.__LINE__.": +++ mapping_end_event\n";
188 1939 my ($self, $info) = @_;
189 1939 my $stack = $self->event_stack;
190  
191 1939 my $last = pop @{ $stack };
192 1939 ▶ if ($last->{index} == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($last->{'index'} == 0) { } 28 1911 1939
elsif ($last->{'flow'}) { } 359 1552 1911
193 28 my $indent = $last->{indent};
194 28 my $zero_indent = $last->{zero_indent};
195 28 ▶ if ($last->{zero_indent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"zero_indent"}) 0 28 28
196 0 $indent .= ' ' x $self->indent;
197 }
198 28 ▶ if ($self->column) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->column) { } 22 6 28
199 22 $self->_write(" {}\n");
200 }
201 else {
202 6 $self->_write("$indent\{}\n");
203 }
204 }
205 elsif ($last->{flow}) {
206 359 my $yaml = "}";
207 359 ▶ if ($last->{flow} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"flow"} == 1) 223 136 359
208 223 $yaml .= "\n";
209 }
210 359 $self->_write("$yaml");
211 }
212 1939 $last = $stack->[-1];
213 1939 $last->{column} = $self->column;
214 1939 ▶ if ($last->{type} eq 'SEQ') {
Branch: 5 branches
BranchTrueFalseTotal
if ($last->{'type'} eq 'SEQ') { } 621 1318 1939
elsif ($last->{'type'} eq 'MAP') { } 2 1316 1318
elsif ($last->{'type'} eq 'MAPVALUE') { } 304 1012 1316
elsif ($last->{'type'} eq 'COMPLEX') { } 19 993 1012
elsif ($last->{'type'} eq 'COMPLEXVALUE') { } 9 984 993
215 }
216 elsif ($last->{type} eq 'MAP') {
217 2 $last->{type} = 'MAPVALUE';
218 }
219 elsif ($last->{type} eq 'MAPVALUE') {
220 304 $last->{type} = 'MAP';
221 }
222 elsif ($last->{type} eq 'COMPLEX') {
223 19 $last->{type} = 'COMPLEXVALUE';
224 }
225 elsif ($last->{type} eq 'COMPLEXVALUE') {
226 9 $last->{type} = 'MAP';
227 }
228 }
229  
230 1355 ▶sub sequence_start_event {
Subroutine sequence_start_event: called CC=29 SCAR=33.7
Pod documented
231 1355 DEBUG and warn __PACKAGE__.':'.__LINE__.": +++ sequence_start_event\n";
232 1355 my ($self, $info) = @_;
233 1355 my $stack = $self->event_stack;
234 1355 my $last = $stack->[-1];
235 1355 my $indent = $last->{indent};
236 1355 my $new_indent = $indent;
237 1355 my $yaml = '';
238  
239 1355 my $props = '';
240 1355 my $anchor = $info->{anchor};
241 1355 my $tag = $info->{tag};
242 1355 ▶ if (defined $anchor) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $anchor) 44 1311 1355
243 44 $anchor = "&$anchor";
244 }
245 1355 ▶ if (defined $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag) 47 1308 1355
246 47 $tag = $self->_emit_tag('seq', $tag);
247 }
248 1355 $props = join ' ', grep defined, ($anchor, $tag);
249  
250 1355 ▶ my $flow = $last->{flow} || 0;
Condition100%2/2
$last->{'flow'} || 0
l!l
1221233
MC/DC100%1/1
$last->{'flow'} || 0
$last->{'flow'}
Truth table100%2/2
A
A= $last->{'flow'}
Aresult
11
00
251 1355 ▶ $flow++ if $flow or ($info->{style} || 0) eq YAML_FLOW_SEQUENCE_STYLE;
Branch: 1 branch
BranchTrueFalseTotal
if $flow or ($info->{"style"} || 0) eq 2 229 1126 1355
Condition100%5/5
$flow or ($info->{"style"} || 0) eq 2
l!l&&r!l&&!r
1221071126
$info->{'style'} || 0
l!l
364869
MC/DC100%2/2
$flow or ($info->{"style"} || 0) eq 2
$flow $info->{'style'}
Truth table100%3/3
A or B
A= $flowB= $info->{'style'}
ABresult
1X1
011
000
252 1355 my $newline = 0;
253 1355 my $zero_indent = 0;
254 1355 ▶ if ($flow > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flow > 1) { } 122 1233 1355
255 122 ▶ if ($last->{type} eq 'SEQ') {
Branch: 3 branches
BranchTrueFalseTotal
if ($last->{'type'} eq 'SEQ') { } 52 70 122
elsif ($last->{'type'} eq 'MAP') { } 14 56 70
elsif ($last->{'type'} eq 'MAPVALUE') { } 56 0 56
256 52 ▶ if ($last->{newline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"newline"}) 0 52 52
257 0 $yaml .= ' ';
258 }
259 52 ▶ if ($last->{index} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'index'} == 0) { } 16 36 52
260 16 $yaml .= "[";
261 }
262 else {
263 36 $yaml .= ",";
264 }
265 }
266 elsif ($last->{type} eq 'MAP') {
267 14 ▶ if ($last->{newline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"newline"}) 1 13 14
268 1 $yaml .= ' ';
269 }
270 14 ▶ if ($last->{index} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'index'} == 0) { } 8 6 14
271 8 $yaml .= "{";
272 }
273 else {
274 6 $yaml .= ",";
275 }
276 }
277 elsif ($last->{type} eq 'MAPVALUE') {
278 56 ▶ if ($last->{index} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"index"} == 0) 0 56 56
279 0 die "Should not happen (index 0 in MAPVALUE)";
280 }
281 56 $yaml .= ": ";
282 }
283 122 ▶ if ($props) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props) 10 112 122
284 10 $yaml .= " $props ";
285 }
286 122 $new_indent .= ' ' x $self->indent;
287 }
288 else {
289 1233 ▶ if ($last->{type} eq 'DOC') {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'type'} eq 'DOC') { } 634 599 1233
290 634 $newline = $last->{newline};
291 }
292 else {
293 599 ▶ if ($last->{newline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"newline"}) 51 548 599
294 51 $yaml .= "\n";
295 51 $last->{column} = 0;
296 }
297 599 ▶ if ($last->{type} eq 'MAPVALUE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'type'} eq 'MAPVALUE') { } 245 354 599
298 245 $zero_indent = 1;
299 245 $newline = 1;
300 }
301 else {
302 354 ▶ if (not $props and $self->indent == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $props and $self->indent == 1) { } 68 286 354
Condition100%3/3
not $props and $self->indent == 1
!ll&&!rl&&r
1527168
MC/DC100%2/2
not $props and $self->indent == 1
not $props $self->indent == 1
Truth table100%3/3
not(A) and B
A= not $propsB= $self->indent == 1
ABresult
0X0
100
111
303 68 $new_indent .= ' ' x 2;
304 }
305 else {
306 286 $new_indent .= ' ' x $self->indent;
307 }
308 354 ▶ if ($last->{column}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'column'}) { } 24 330 354
309 24 ▶ my $space = $self->indent > 1 ? ' ' x ($self->indent - 1) : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$self->indent > 1 ? : 20 4 24
310 24 $yaml .= $space;
311 }
312 else {
313 330 $yaml .= $indent;
314 }
315 354 ▶ if ($last->{type} eq 'SEQ') {
Branch: 3 branches
BranchTrueFalseTotal
if ($last->{'type'} eq 'SEQ') { } 278 76 354
elsif ($last->{'type'} eq 'MAP') { } 47 29 76
elsif ($last->{'type'} eq 'COMPLEXVALUE') { } 29 0 29
316 278 $yaml .= "-";
317 }
318 elsif ($last->{type} eq 'MAP') {
319 47 $last->{type} = 'COMPLEX';
320 47 $zero_indent = 1;
321 47 $yaml .= "?";
322 }
323 elsif ($last->{type} eq 'COMPLEXVALUE') {
324 29 $yaml .= ":";
325 29 $zero_indent = 1;
326 }
327 else {
328 0 die "Should not happen ($last->{type} in sequence_start)";
329 }
330 354 $last->{column} = 1;
331 }
332 599 $last->{newline} = 0;
333 }
334 1233 ▶ if ($props) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props) 73 1160 1233
335 73 ▶ $yaml .= $last->{column} ? ' ' : $indent;
Branch: 1 branch
BranchTrueFalseTotal
$last->{'column'} ? : 68 5 73
336 73 $yaml .= $props;
337 73 $newline = 1;
338 }
339 }
340 1355 $self->_write($yaml);
341 1355 $last->{index}++;
342 1355 my $new_info = {
343 index => 0,
344 indent => $new_indent,
345 info => $info,
346 zero_indent => $zero_indent,
347 newline => $newline,
348 column => $self->column,
349 flow => $flow,
350 };
351 1355 $new_info->{type} = 'SEQ';
352 1355 push @{ $stack }, $new_info;
353 1355 $self->{open_ended} = 0;
354 }
355  
356 1355 ▶sub sequence_end_event {
Subroutine sequence_end_event: called CC=12 SCAR=24.8
Pod documented
357 1355 DEBUG and warn __PACKAGE__.':'.__LINE__.": +++ sequence_end_event\n";
358 1355 my ($self, $info) = @_;
359 1355 my $stack = $self->event_stack;
360  
361 1355 my $last = pop @{ $stack };
362 1355 ▶ if ($last->{index} == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($last->{'index'} == 0) { } 59 1296 1355
elsif ($last->{'flow'}) { } 212 1084 1296
363 59 my $indent = $last->{indent};
364 59 my $zero_indent = $last->{zero_indent};
365 59 ▶ if ($last->{zero_indent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"zero_indent"}) 7 52 59
366 7 $indent .= ' ' x $self->indent;
367 }
368 59 ▶ my $yaml .= $self->column ? ' ' : $indent;
Branch: 1 branch
BranchTrueFalseTotal
$self->column ? : 58 1 59
369 59 $yaml .= "[]";
370 59 ▶ if ($last->{flow} < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"flow"} < 2) 51 8 59
371 51 $yaml .= "\n";
372 }
373 59 $self->_write($yaml);
374 }
375 elsif ($last->{flow}) {
376 212 my $yaml = "]";
377 212 ▶ if ($last->{flow} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"flow"} == 1) 98 114 212
378 98 $yaml .= "\n";
379 }
380 212 $self->_write($yaml);
381 }
382 1355 $last = $stack->[-1];
383 1355 $last->{column} = $self->column;
384 1355 ▶ if ($last->{type} eq 'SEQ') {
Branch: 5 branches
BranchTrueFalseTotal
if ($last->{'type'} eq 'SEQ') { } 330 1025 1355
elsif ($last->{'type'} eq 'MAP') { } 14 1011 1025
elsif ($last->{'type'} eq 'MAPVALUE') { } 301 710 1011
elsif ($last->{'type'} eq 'COMPLEX') { } 47 663 710
elsif ($last->{'type'} eq 'COMPLEXVALUE') { } 29 634 663
385 }
386 elsif ($last->{type} eq 'MAP') {
387 14 $last->{type} = 'MAPVALUE';
388 }
389 elsif ($last->{type} eq 'MAPVALUE') {
390 301 $last->{type} = 'MAP';
391 }
392 elsif ($last->{type} eq 'COMPLEX') {
393 47 $last->{type} = 'COMPLEXVALUE';
394 }
395 elsif ($last->{type} eq 'COMPLEXVALUE') {
396 29 $last->{type} = 'MAP';
397 }
398 }
399  
400 49 my %forbidden_first = (qw/
401 ! 1 & 1 * 1 { 1 } 1 [ 1 ] 1 | 1 > 1 @ 1 ` 1 " 1 ' 1
402 /, '#' => 1, '%' => 1, ',' => 1, " " => 1);
403 49 my %forbidden_first_plus_space = (qw/
404 ? 1 - 1 : 1
405 /);
406  
407 49 my %control = (
408 "\x00" => '\0',
409 "\x01" => '\x01',
410 "\x02" => '\x02',
411 "\x03" => '\x03',
412 "\x04" => '\x04',
413 "\x05" => '\x05',
414 "\x06" => '\x06',
415 "\x07" => '\a',
416 "\x08" => '\b',
417 "\x0b" => '\v',
418 "\x0c" => '\f',
419 "\x0e" => '\x0e',
420 "\x0f" => '\x0f',
421 "\x10" => '\x10',
422 "\x11" => '\x11',
423 "\x12" => '\x12',
424 "\x13" => '\x13',
425 "\x14" => '\x14',
426 "\x15" => '\x15',
427 "\x16" => '\x16',
428 "\x17" => '\x17',
429 "\x18" => '\x18',
430 "\x19" => '\x19',
431 "\x1a" => '\x1a',
432 "\x1b" => '\e',
433 "\x1c" => '\x1c',
434 "\x1d" => '\x1d',
435 "\x1e" => '\x1e',
436 "\x1f" => '\x1f',
437 "\x7f" => '\x7f',
438 "\x80" => '\x80',
439 "\x81" => '\x81',
440 "\x82" => '\x82',
441 "\x83" => '\x83',
442 "\x84" => '\x84',
443 "\x86" => '\x86',
444 "\x87" => '\x87',
445 "\x88" => '\x88',
446 "\x89" => '\x89',
447 "\x8a" => '\x8a',
448 "\x8b" => '\x8b',
449 "\x8c" => '\x8c',
450 "\x8d" => '\x8d',
451 "\x8e" => '\x8e',
452 "\x8f" => '\x8f',
453 "\x90" => '\x90',
454 "\x91" => '\x91',
455 "\x92" => '\x92',
456 "\x93" => '\x93',
457 "\x94" => '\x94',
458 "\x95" => '\x95',
459 "\x96" => '\x96',
460 "\x97" => '\x97',
461 "\x98" => '\x98',
462 "\x99" => '\x99',
463 "\x9a" => '\x9a',
464 "\x9b" => '\x9b',
465 "\x9c" => '\x9c',
466 "\x9d" => '\x9d',
467 "\x9e" => '\x9e',
468 "\x9f" => '\x9f',
469 "\x{2029}" => '\P',
470 "\x{2028}" => '\L',
471 "\x85" => '\N',
472 "\xa0" => '\_',
473 );
474  
475 49 my $control_re = '\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x84\x86-\x9f\x{d800}-\x{dfff}\x{fffe}\x{ffff}\x{2028}\x{2029}\x85\xa0';
476 49 my %to_escape = (
477 "\n" => '\n',
478 "\t" => '\t',
479 "\r" => '\r',
480 '\\' => '\\\\',
481 '"' => '\\"',
482 %control,
483 );
484 49 my $escape_re = $control_re . '\n\t\r';
485 49 my $escape_re_without_lb = $control_re . '\t\r';
486  
487  
488 9486 ▶sub scalar_event {
Subroutine scalar_event: called CC=40 SCAR=36.9
Pod documented
489 9486 DEBUG and warn __PACKAGE__.':'.__LINE__.": +++ scalar_event\n";
490 9486 my ($self, $info) = @_;
491 9486 my $stack = $self->event_stack;
492 9486 my $last = $stack->[-1];
493 9486 my $indent = $last->{indent};
494 9486 my $value = $info->{value};
495 9486 my $flow = $last->{flow};
496  
497 9486 my $props = '';
498 9486 my $anchor = $info->{anchor};
499 9486 my $tag = $info->{tag};
500 9486 ▶ if (defined $anchor) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $anchor) 460 9026 9486
501 460 $anchor = "&$anchor";
502 }
503 9486 ▶ if (defined $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag) 492 8994 9486
504 492 $tag = $self->_emit_tag('scalar', $tag);
505 }
506 9486 $props = join ' ', grep defined, ($anchor, $tag);
507  
508 9486 DEBUG and local $Data::Dumper::Useqq = 1;
509 9486 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 9486 9486
510  
511 9486 my $style = $self->_find_best_scalar_style(
512 info => $info,
513 value => $value,
514 );
515  
516 9486 my $open_ended = 0;
517  
518 9486 ▶ if ($style == YAML_PLAIN_SCALAR_STYLE) {
Branch: 4 branches
BranchTrueFalseTotal
if ($style == 1) { } 7726 1760 9486
elsif ($style == 2) { } 508 1252 1760
elsif ($style == 4) { } 365 887 1252
elsif ($style == 5) { } 120 767 887
519 7726 $value =~ s/\n/\n\n/g;
520 }
521 elsif ($style == YAML_SINGLE_QUOTED_SCALAR_STYLE) {
522 508 my $new_indent = $last->{indent} . (' ' x $self->indent);
523 508 $value =~ s/(\n+)/"\n" x (1 + (length $1))/eg;
524 508 my @lines = split m/\n/, $value, -1;
525 508 ▶ if (@lines > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines > 1) 10 498 508
526 10 for my $line (@lines[1 .. $#lines]) {
527 20 ▶ $line = $new_indent . $line
Branch: 1 branch
BranchTrueFalseTotal
if length $line 10 10 20
528 if length $line;
529 }
530 }
531 508 $value = join "\n", @lines;
532 508 $value =~ s/'/''/g;
533 508 $value = "'" . $value . "'";
534 }
535 elsif ($style == YAML_LITERAL_SCALAR_STYLE) {
536 365 DEBUG and warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$value], ['value']);
537 365 my $indicators = '';
538 365 ▶ if ($value =~ m/\A\n* +/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\A\n* +/) 36 329 365
539 36 $indicators .= $self->indent;
540 }
541 365 my $indent = $indent . ' ' x $self->indent;
542 365 ▶ if ($value !~ m/\n\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $value =~ /\n\z/) { } 87 278 365
elsif ($value =~ /(\n|\A)\n\z/) { } 36 242 278
543 87 $indicators .= '-';
544 87 $value .= "\n";
545 }
546 elsif ($value =~ m/(\n|\A)\n\z/) {
547 36 $indicators .= '+';
548 36 $open_ended = 1;
549 }
550 365 $value =~ s/^(?=.)/$indent/gm;
551 365 $value = "|$indicators\n$value";
552 }
553 elsif ($style == YAML_FOLDED_SCALAR_STYLE) {
554 120 DEBUG and warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$value], ['value']);
555 120 my @lines = split /\n/, $value, -1;
556 120 DEBUG and warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\@lines], ['lines']);
557 120 my $trailing = -1;
558 120 ▶ while (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 218 1 219
559 218 ▶ last if $lines[-1] ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $lines[-1] ne "" 119 99 218
560 99 pop @lines;
561 99 $trailing++;
562 }
563 120 my %start_with_space;
564 120 for my $i (0 .. $#lines) {
565 211 ▶ if ($lines[ $i ] =~ m/^[ \t]+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[$i] =~ /^[ \t]+/) 31 180 211
566 31 $start_with_space{ $i } = 1;
567 }
568 }
569 120 my $indicators = '';
570 120 ▶ if ($value =~ m/\A\n* +/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\A\n* +/) 19 101 120
571 19 $indicators .= $self->indent;
572 }
573 120 my $indent = $indent . ' ' x $self->indent;
574 120 ▶ if ($trailing > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($trailing > 0) { } 3 117 120
elsif ($trailing < 0) { } 24 93 117
575 3 $indicators .= '+';
576 3 $open_ended = 1;
577 }
578 elsif ($trailing < 0) {
579 24 $indicators .= '-';
580 }
581 120 $value = ">$indicators\n";
582 120 my $got_content = 0;
583 120 for my $i (0 .. $#lines) {
584 211 my $line = $lines[ $i ];
585 211 ▶ my $sp = $start_with_space{ $i } || 0;
Condition100%2/2
$start_with_space{$i} || 0
l!l
31180
MC/DC100%1/1
$start_with_space{$i} || 0
$start_with_space{$i}
Truth table100%2/2
A
A= $start_with_space{$i}
Aresult
11
00
586 211 ▶ my $spnext = $i == $#lines ? 1 : $start_with_space{ $i+1 } || 0;
Branch: 1 branch
BranchTrueFalseTotal
$i == $#lines ? : 119 92 211
Condition100%2/2
$start_with_space{$i + 1} or 0
l!l
1874
MC/DC100%1/1
$start_with_space{$i + 1} or 0
$start_with_space{$i + 1}
Truth table100%2/2
A
A= $start_with_space{$i + 1}
Aresult
11
00
587 211 ▶ my $spprev = $i == 0 ? 1 : $start_with_space{ $i-1 } || 0;
Branch: 1 branch
BranchTrueFalseTotal
$i == 0 ? : 119 92 211
Condition100%2/2
$start_with_space{$i - 1} or 0
l!l
1280
MC/DC100%1/1
$start_with_space{$i - 1} or 0
$start_with_space{$i - 1}
Truth table100%2/2
A
A= $start_with_space{$i - 1}
Aresult
11
00
588 211 ▶ my $empty = length $line ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
length $line ? : 164 47 211
589 211 ▶ my $emptynext = $i == $#lines ? '' : length $lines[$i+1] ? 0 : 1;
Branch: 2 branches
BranchTrueFalseTotal
$i == $#lines ? : 119 92 211
length $lines[$i + 1] ? : 55 37 92
590 211 my $nl = 0;
591 211 ▶ if ($empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($empty) { } 47 164 211
592 47 ▶ if ($spnext and $spprev) {
Branch: 3 branches
BranchTrueFalseTotal
if ($spnext and $spprev) { } 8 39 47
elsif (not $spnext) { } 37 2 39
elsif (not $got_content) { } 1 1 2
Condition100%3/3
$spnext and $spprev
!ll&&!rl&&r
3728
MC/DC100%2/2
$spnext and $spprev
$spnext $spprev
Truth table100%3/3
A and B
A= $spnextB= $spprev
ABresult
0X0
100
111
593 8 $nl = 1;
594 }
595 elsif (not $spnext) {
596 37 $nl = 1;
597 }
598 elsif (not $got_content) {
599 1 $nl = 1;
600 }
601 }
602 else {
603 164 $got_content = 1;
604 164 $value .= "$indent$line\n";
605 164 ▶ if (not $sp and not $spnext) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sp or $spnext) 32 132 164
Condition100%3/3
$sp or $spnext
l!l&&r!l&&!r
3110132
MC/DC100%2/2
$sp or $spnext
$sp $spnext
Truth table100%3/3
A or B
A= $spB= $spnext
ABresult
1X1
011
000
606 32 $nl = 1;
607 }
608 }
609 211 ▶ if ($nl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl) 78 133 211
610 78 $value .= "\n";
611 }
612 }
613 120 ▶ $value .= "\n" x ($trailing) if $trailing > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $trailing > 0 3 117 120
614 }
615 else {
616 767 ▶ $value =~ s/([$escape_re"\\])/$to_escape{ $1 } || sprintf '\\u%04x', ord($1)/eg;
Condition33%1/3
$to_escape{$1} || sprintf('\\u%04x', ord $1)
l!l&&r!l&&!r
72200
MC/DC0%0/2
$to_escape{$1} || sprintf('\\u%04x', ord $1)
$to_escape{$1} sprintf('\\u%04x', ord $1)
Truth table33%1/3
A || B
A= $to_escape{$1}B= sprintf('\\u%04x', ord $1)
ABresult
1X1
011
000
617 767 $value = '"' . $value . '"';
618 }
619  
620 9486 DEBUG and warn __PACKAGE__.':'.__LINE__.": (@$stack)\n";
621 9486 my $yaml = $self->_emit_scalar(
622 indent => $indent,
623 props => $props,
624 value => $value,
625 style => $style,
626 );
627  
628 9486 $last->{index}++;
629 9486 $last->{newline} = 0;
630 9486 $self->_write($yaml);
631 9486 $last->{column} = $self->column;
632 9486 $self->{open_ended} = $open_ended;
633 }
634  
635 9486 ▶sub _find_best_scalar_style {
Subroutine _find_best_scalar_style: called CC=42 SCAR=37.4
636 9486 my ($self, %args) = @_;
637 9486 my $info = $args{info};
638 9486 my $style = $info->{style};
639 9486 my $value = $args{value};
640 9486 my $stack = $self->event_stack;
641 9486 my $last = $stack->[-1];
642 9486 my $flow = $last->{flow};
643  
644 9486 my $first = substr($value, 0, 1);
645 9486 ▶ if ($value eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($value eq '') { } 897 8589 9486
elsif ($value =~ /[$control_re]/) { } 55 8534 8589
646 897 ▶ if ($flow and $last->{type} ne 'MAPVALUE' and $last->{type} ne 'MAP') {
Branch: 2 branches
BranchTrueFalseTotal
if ($flow and $last->{'type'} ne 'MAPVALUE' and $last->{'type'} ne 'MAP') { } 24 873 897
elsif (not $style) { } 2 871 873
Condition100%6/6
$flow and $last->{"type"} ne "MAPVALUE" and $last->{"type"} ne "MAP"
!ll&&!rl&&r
7987524
$flow and $last->{"type"} ne "MAPVALUE"
!ll&&!rl&&r
6999999
MC/DC100%3/3
$flow and $last->{"type"} ne "MAPVALUE" and $last->{"type"} ne "MAP"
$flow $last->{"type"} ne "MAPVALUE" $last->{"type"} ne "MAP"
Truth table100%4/4
A and B and C
A= $flowB= $last->{"type"} ne "MAPVALUE"C= $last->{"type"} ne "MAP"
ABCresult
0XX0
10X0
1100
1111
647 24 $style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
648 }
649 elsif (not $style) {
650 2 $style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
651 }
652 }
653 # no control characters anywhere
654 elsif ($value =~ m/[$control_re]/) {
655 55 $style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
656 }
657 9486 ▶ $style ||= YAML_PLAIN_SCALAR_STYLE;
Condition100%2/2
$style ||= 1
l!l
73572129
MC/DC100%1/1
$style ||= 1
$style
Truth table100%2/2
A
A= $style
Aresult
11
00
658  
659 9486 ▶ if ($style == YAML_SINGLE_QUOTED_SCALAR_STYLE) {
Branch: 3 branches
BranchTrueFalseTotal
if ($style == 2) { } 356 9130 9486
elsif ($style == 4 or $style == 5) { } 444 8686 9130
elsif ($style == 1) { } 8112 574 8686
660 356 ▶ if ($value =~ m/ \n/ or $value =~ m/\n / or $value =~ m/^\n/ or $value =~ m/\n$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ / \n/ or $value =~ /\n / or $value =~ /^\n/ or $value =~ /\n$/) { } 16 340 356
elsif ($value eq "\n") { } 0 340 340
Condition100%9/9
$value =~ / \n/ or $value =~ /\n / or $value =~ /^\n/ or $value =~ /\n$/
l!l&&r!l&&!r
151340
$value =~ / \n/ or $value =~ /\n / or $value =~ /^\n/
l!l&&r!l&&!r
105341
$value =~ / \n/ or $value =~ /\n /
l!l&&r!l&&!r
82346
MC/DC100%4/4
$value =~ / \n/ or $value =~ /\n / or $value =~ /^\n/ or $value =~ /\n$/
$value =~ / \n/ $value =~ /\n / $value =~ /^\n/ $value =~ /\n$/
Truth table100%5/5
A or B or C or D
A= $value =~ / \n/B= $value =~ /\n /C= $value =~ /^\n/D= $value =~ /\n$/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
661 16 $style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
662 }
663 elsif ($value eq "\n") {
664 0 $style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
665 }
666 }
667 ▶ elsif ($style == YAML_LITERAL_SCALAR_STYLE or $style == YAML_FOLDED_SCALAR_STYLE) {
Condition100%3/3
$style == 4 or $style == 5
l!l&&r!l&&!r
2891558686
MC/DC100%2/2
$style == 4 or $style == 5
$style == 4 $style == 5
Truth table100%3/3
A or B
A= $style == 4B= $style == 5
ABresult
1X1
011
000
668 444 ▶ if ($value eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($value eq '') { } 22 422 444
elsif ($flow) { } 69 353 422
669 22 $style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
670 }
671 elsif ($flow) {
672 # no block scalars in flow
673 69 ▶ if ($value =~ tr/\n//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ tr/\n//) { } 61 8 69
674 61 $style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
675 }
676 else {
677 8 $style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
678 }
679 }
680 }
681 elsif ($style == YAML_PLAIN_SCALAR_STYLE) {
682 8112 ▶ if (not length $value) {
Branch: 14 branches
BranchTrueFalseTotal
if (not length $value) { } 772 7340 8112
elsif ($value =~ /[$escape_re_without_lb]/) { } 52 7288 7340
elsif ($value eq "\n") { } 8 7280 7288
elsif (not $value =~ tr/ //c) { } 10 7270 7280
elsif (not $value =~ tr/\n //c) { } 12 7258 7270
elsif ($value =~ tr/\n//) { } 134 7124 7258
elsif ($forbidden_first{$first}) { } 94 7030 7124
elsif ($flow and $value =~ tr/,[]{}//) { } 5 7025 7030
elsif (substr($value, 0, 3) =~ /^(?:---|\.\.\.)/) { } 12 7013 7025
elsif ($value =~ /: /) { } 8 7005 7013
elsif ($value =~ / #/) { } 8 6997 7005
elsif ($value =~ /[: \t]\z/) { } 27 6970 6997
elsif ($value =~ /[^\x20-\x3A\x3B-\x7E\x85\xA0-\x{D7FF}\x{E000}-\x{FEFE}\x{FF00}-\x{FFFD}\x{10000}-\x{10FFFF}]/) { } 0 6970 6970
elsif ($forbidden_first_plus_space{$first}) { } 211 6759 6970
683 }
684 elsif ($value =~ m/[$escape_re_without_lb]/) {
685 52 $style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
686 }
687 elsif ($value eq "\n") {
688 8 $style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
689 }
690 elsif ($value !~ tr/ //c) {
691 10 $style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
692 }
693 elsif ($value !~ tr/ \n//c) {
694 12 $style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
695 }
696 elsif ($value =~ tr/\n//) {
697 134 ▶ $style = $flow ? YAML_DOUBLE_QUOTED_SCALAR_STYLE : YAML_LITERAL_SCALAR_STYLE;
Branch: 1 branch
BranchTrueFalseTotal
$flow ? : 2 132 134
698 }
699 elsif ($forbidden_first{ $first }) {
700 94 $style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
701 }
702 ▶ elsif ($flow and $value =~ tr/,[]{}//) {
Condition100%3/3
$flow and $value =~ tr/,[]{}//
!ll&&!rl&&r
598010455
MC/DC100%2/2
$flow and $value =~ tr/,[]{}//
$flow $value =~ tr/,[]{}//
Truth table100%3/3
A and B
A= $flowB= $value =~ tr/,[]{}//
ABresult
0X0
100
111
703 5 $style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
704 }
705 elsif (substr($value, 0, 3) =~ m/^(?:---|\.\.\.)/) {
706 12 $style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
707 }
708 elsif ($value =~ m/: /) {
709 8 $style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
710 }
711 elsif ($value =~ m/ #/) {
712 8 $style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
713 }
714 elsif ($value =~ m/[: \t]\z/) {
715 27 $style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
716 }
717 elsif ($value =~ m/[^\x20-\x3A\x3B-\x7E\x85\xA0-\x{D7FF}\x{E000}-\x{FEFE}\x{FF00}-\x{FFFD}\x{10000}-\x{10FFFF}]/) {
718 0 $style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
719 }
720 elsif ($forbidden_first_plus_space{ $first }) {
721 211 ▶ if (length ($value) == 1 or substr($value, 1, 1) =~ m/^\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value == 1 or substr($value, 1, 1) =~ /^\s/) 16 195 211
Condition100%3/3
length $value == 1 or substr($value, 1, 1) =~ /^\s/
l!l&&r!l&&!r
88195
MC/DC100%2/2
length $value == 1 or substr($value, 1, 1) =~ /^\s/
length $value == 1 substr($value, 1, 1) =~ /^\s/
Truth table100%3/3
A or B
A= length $value == 1B= substr($value, 1, 1) =~ /^\s/
ABresult
1X1
011
000
722 16 $style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
723 }
724 }
725 }
726 9486 ▶ if ($style == YAML_SINGLE_QUOTED_SCALAR_STYLE and not $info->{style}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style == 2 and not $info->{"style"}) 158 9328 9486
Condition100%3/3
$style == 2 and not $info->{"style"}
!ll&&!rl&&r
8958370158
MC/DC100%2/2
$style == 2 and not $info->{"style"}
$style == 2 not $info->{"style"}
Truth table100%3/3
A and not(B)
A= $style == 2B= not $info->{"style"}
ABresult
0X0
100
111
727 158 ▶ if ($value =~ tr/'// and $value !~ tr/"//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ tr/'// and not $value =~ tr/"//) 20 138 158
Condition100%3/3
$value =~ tr/'// and not $value =~ tr/"//
!ll&&!rl&&r
134420
MC/DC100%2/2
$value =~ tr/'// and not $value =~ tr/"//
$value =~ tr/'// not $value =~ tr/"//
Truth table100%3/3
A and not(B)
A= $value =~ tr/'//B= not $value =~ tr/"//
ABresult
0X0
100
111
728 20 $style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
729 }
730 }
731 9486 return $style;
732 }
733  
734 9486 ▶sub _emit_scalar {
Subroutine _emit_scalar: called CC=7 SCAR=19.5
735 9486 my ($self, %args) = @_;
736 9486 my $props = $args{props};
737 9486 my $value = $args{value};
738 9486 my $style = $args{style};
739 9486 my $stack = $self->event_stack;
740 9486 my $last = $stack->[-1];
741 9486 my $flow = $last->{flow};
742  
743 9486 my $yaml = '';
744 9486 my $pvalue = $props;
745 9486 ▶ if ($props and length $value) {
Branch: 2 branches
BranchTrueFalseTotal
if ($props and length $value) { } 524 8962 9486
elsif (length $value) { } 8190 772 8962
Condition100%3/3
$props and length $value
!ll&&!rl&&r
8693269524
MC/DC100%2/2
$props and length $value
$props length $value
Truth table100%3/3
A and B
A= $propsB= length $value
ABresult
0X0
100
111
746 524 $pvalue .= " $value";
747 }
748 elsif (length $value) {
749 8190 $pvalue .= $value;
750 }
751 9486 ▶ if ($flow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flow) { } 1415 8071 9486
752 1415 ▶ if ($props and not length $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props and not length $value) 52 1363 1415
Condition100%3/3
$props and not length $value
!ll&&!rl&&r
12679652
MC/DC100%2/2
$props and not length $value
$props not length $value
Truth table100%3/3
A and not(B)
A= $propsB= not length $value
ABresult
0X0
100
111
753 52 $pvalue .= ' ';
754 }
755 $yaml = $self->_emit_flow_scalar(
756 value => $value,
757 pvalue => $pvalue,
758 style => $args{style},
759 1415 );
760 }
761 else {
762 $yaml = $self->_emit_block_scalar(
763 props => $props,
764 value => $value,
765 pvalue => $pvalue,
766 indent => $args{indent},
767 style => $args{style},
768 8071 );
769 }
770 9486 return $yaml;
771 }
772  
773 8071 ▶sub _emit_block_scalar {
Subroutine _emit_block_scalar: called CC=22 SCAR=30.9
774 8071 my ($self, %args) = @_;
775 8071 my $props = $args{props};
776 8071 my $value = $args{value};
777 8071 my $pvalue = $args{pvalue};
778 8071 my $indent = $args{indent};
779 8071 my $style = $args{style};
780 8071 my $stack = $self->event_stack;
781 8071 my $last = $stack->[-1];
782  
783 8071 my $yaml;
784 8071 ▶ if ($last->{type} eq 'MAP' or $last->{type} eq 'SEQ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"type"} eq "MAP" or $last->{"type"} eq "SEQ") 4220 3851 8071
Condition100%3/3
$last->{"type"} eq "MAP" or $last->{"type"} eq "SEQ"
l!l&&r!l&&!r
273314873851
MC/DC100%2/2
$last->{"type"} eq "MAP" or $last->{"type"} eq "SEQ"
$last->{"type"} eq "MAP" $last->{"type"} eq "SEQ"
Truth table100%3/3
A or B
A= $last->{"type"} eq "MAP"B= $last->{"type"} eq "SEQ"
ABresult
1X1
011
000
785 4220 ▶ if ($last->{index} == 0 and $last->{newline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"index"} == 0 and $last->{"newline"}) 994 3226 4220
Condition100%3/3
$last->{"index"} == 0 and $last->{"newline"}
!ll&&!rl&&r
19031323994
MC/DC100%2/2
$last->{"index"} == 0 and $last->{"newline"}
$last->{"index"} == 0 $last->{"newline"}
Truth table100%3/3
A and B
A= $last->{"index"} == 0B= $last->{"newline"}
ABresult
0X0
100
111
786 994 $yaml .= "\n";
787 994 $last->{column} = 0;
788 994 $last->{newline} = 0;
789 }
790 }
791 8071 my $space = ' ';
792 8071 ▶ my $multiline = ($style == YAML_LITERAL_SCALAR_STYLE or $style == YAML_FOLDED_SCALAR_STYLE);
Condition100%3/3
$style == 4 || $style == 5
l!l&&r!l&&!r
3651207586
MC/DC100%2/2
$style == 4 || $style == 5
$style == 4 $style == 5
Truth table100%3/3
A || B
A= $style == 4B= $style == 5
ABresult
1X1
011
000
793 8071 ▶ if ($last->{type} eq 'MAP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'type'} eq 'MAP') { } 2733 5338 8071
794  
795 2733 ▶ if ($last->{column}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'column'}) { } 448 2285 2733
796 448 ▶ my $space = $self->indent > 1 ? ' ' x ($self->indent - 1) : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$self->indent > 1 ? : 360 88 448
797 448 $yaml .= $space;
798 }
799 else {
800 2285 $yaml .= $indent;
801 }
802 2733 ▶ if ($props and not length $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props and not length $value) 100 2633 2733
Condition100%3/3
$props and not length $value
!ll&&!rl&&r
254489100
MC/DC100%2/2
$props and not length $value
$props not length $value
Truth table100%3/3
A and not(B)
A= $propsB= not length $value
ABresult
0X0
100
111
803 100 $pvalue .= ' ';
804 }
805 2733 $last->{type} = 'MAPVALUE';
806 2733 ▶ if ($multiline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiline) 17 2716 2733
807 # oops, a complex key
808 17 $yaml .= "? ";
809 17 $last->{type} = 'COMPLEXVALUE';
810 }
811 2733 ▶ if (not $multiline) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($multiline) 2716 17 2733
812 2716 $pvalue .= ":";
813 }
814 }
815 else {
816 5338 ▶ if ($last->{type} eq 'MAPVALUE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($last->{'type'} eq 'MAPVALUE') { } 2155 3183 5338
elsif ($last->{'type'} eq 'DOC') { } 1654 1529 3183
817 2155 $last->{type} = 'MAP';
818 }
819 elsif ($last->{type} eq 'DOC') {
820 }
821 else {
822 1529 ▶ if ($last->{column}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'column'}) { } 260 1269 1529
823 260 ▶ my $space = $self->indent > 1 ? ' ' x ($self->indent - 1) : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$self->indent > 1 ? : 204 56 260
824 260 $yaml .= $space;
825 }
826 else {
827 1269 $yaml .= $indent;
828 }
829 1529 ▶ if ($last->{type} eq 'COMPLEXVALUE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($last->{'type'} eq 'COMPLEXVALUE') { } 42 1487 1529
elsif ($last->{'type'} eq 'SEQ') { } 1487 0 1487
830 42 $last->{type} = 'MAP';
831 42 $yaml .= ":";
832 }
833 elsif ($last->{type} eq 'SEQ') {
834 1487 $yaml .= "-";
835 }
836 else {
837 0 die "Should not happen ($last->{type} in scalar_event)";
838  
839 }
840 1529 $last->{column} = 1;
841 }
842  
843 5338 ▶ if (length $pvalue) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $pvalue) 5044 294 5338
844 5044 ▶ if ($last->{column}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"column"}) 3881 1163 5044
845 3881 $pvalue = "$space$pvalue";
846 }
847 }
848 5338 ▶ if (not $multiline) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($multiline) 4870 468 5338
849 4870 $pvalue .= "\n";
850 }
851 }
852 8071 $yaml .= $pvalue;
853 8071 return $yaml;
854 }
855  
856 1415 ▶sub _emit_flow_scalar {
Subroutine _emit_flow_scalar: called CC=10 SCAR=23.1
857 1415 my ($self, %args) = @_;
858 1415 my $value = $args{value};
859 1415 my $pvalue = $args{pvalue};
860 1415 my $stack = $self->event_stack;
861 1415 my $last = $stack->[-1];
862  
863 1415 my $yaml;
864 1415 ▶ if ($last->{type} eq 'SEQ') {
Branch: 3 branches
BranchTrueFalseTotal
if ($last->{'type'} eq 'SEQ') { } 326 1089 1415
elsif ($last->{'type'} eq 'MAP') { } 589 500 1089
elsif ($last->{'type'} eq 'MAPVALUE') { } 500 0 500
865 326 ▶ if ($last->{index} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'index'} == 0) { } 168 158 326
866 168 ▶ if ($self->column) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->column) 131 37 168
867 131 $yaml .= ' ';
868 }
869 168 $yaml .= "[";
870 }
871 else {
872 158 $yaml .= ", ";
873 }
874 }
875 elsif ($last->{type} eq 'MAP') {
876 589 ▶ if ($last->{index} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'index'} == 0) { } 347 242 589
877 347 ▶ if ($self->column) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->column) 237 110 347
878 237 $yaml .= ' ';
879 }
880 347 $yaml .= "{";
881 }
882 else {
883 242 $yaml .= ", ";
884 }
885 589 $last->{type} = 'MAPVALUE';
886 }
887 elsif ($last->{type} eq 'MAPVALUE') {
888 500 ▶ if ($last->{index} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"index"} == 0) 0 500 500
889 0 die "Should not happen (index 0 in MAPVALUE)";
890 }
891 500 $yaml .= ": ";
892 500 $last->{type} = 'MAP';
893 }
894 1415 ▶ if ($self->column + length $pvalue > $self->width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->column + length($pvalue) > $self->width) 51 1364 1415
895 51 $yaml .= "\n";
896 51 $yaml .= $last->{indent};
897 51 $yaml .= ' ' x $self->indent;
898 }
899 1415 $yaml .= $pvalue;
900 1415 return $yaml;
901 }
902  
903 202 ▶sub alias_event {
Subroutine alias_event: called CC=22 SCAR=32.0
Pod documented
904 202 DEBUG and warn __PACKAGE__.':'.__LINE__.": +++ alias_event\n";
905 202 my ($self, $info) = @_;
906 202 my $stack = $self->event_stack;
907 202 my $last = $stack->[-1];
908 202 my $indent = $last->{indent};
909 202 my $flow = $last->{flow};
910  
911 202 my $alias = '*' . $info->{value};
912  
913 202 my $yaml = '';
914 202 ▶ if ($last->{type} eq 'MAP' or $last->{type} eq 'SEQ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"type"} eq "MAP" or $last->{"type"} eq "SEQ") 107 95 202
Condition100%3/3
$last->{"type"} eq "MAP" or $last->{"type"} eq "SEQ"
l!l&&r!l&&!r
327595
MC/DC100%2/2
$last->{"type"} eq "MAP" or $last->{"type"} eq "SEQ"
$last->{"type"} eq "MAP" $last->{"type"} eq "SEQ"
Truth table100%3/3
A or B
A= $last->{"type"} eq "MAP"B= $last->{"type"} eq "SEQ"
ABresult
1X1
011
000
915 107 ▶ if ($last->{index} == 0 and $last->{newline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"index"} == 0 and $last->{"newline"}) 12 95 107
Condition100%3/3
$last->{"index"} == 0 and $last->{"newline"}
!ll&&!rl&&r
90512
MC/DC100%2/2
$last->{"index"} == 0 and $last->{"newline"}
$last->{"index"} == 0 $last->{"newline"}
Truth table100%3/3
A and B
A= $last->{"index"} == 0B= $last->{"newline"}
ABresult
0X0
100
111
916 12 $yaml .= "\n";
917 12 $last->{column} = 0;
918 12 $last->{newline} = 0;
919 }
920 }
921 202 ▶ $yaml .= $last->{column} ? ' ' : $indent;
Branch: 1 branch
BranchTrueFalseTotal
$last->{'column'} ? : 106 96 202
922 202 ▶ if ($flow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flow) { } 27 175 202
923 27 my $space = '';
924 27 ▶ if ($last->{type} eq 'SEQ') {
Branch: 3 branches
BranchTrueFalseTotal
if ($last->{'type'} eq 'SEQ') { } 5 22 27
elsif ($last->{'type'} eq 'MAP') { } 7 15 22
elsif ($last->{'type'} eq 'MAPVALUE') { } 15 0 15
925 5 ▶ if ($last->{index} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'index'} == 0) { } 1 4 5
926 1 ▶ if ($flow == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flow == 1) 0 1 1
927 0 $yaml .= ' ';
928 }
929 1 $yaml .= "[";
930 }
931 else {
932 4 $yaml .= ", ";
933 }
934 }
935 elsif ($last->{type} eq 'MAP') {
936 7 ▶ if ($last->{index} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'index'} == 0) { } 2 5 7
937 2 ▶ if ($flow == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flow == 1) 0 2 2
938 0 $yaml .= ' ';
939 }
940 2 $yaml .= "{";
941 }
942 else {
943 5 $yaml .= ", ";
944 }
945 7 $last->{type} = 'MAPVALUE';
946 7 $space = ' ';
947 }
948 elsif ($last->{type} eq 'MAPVALUE') {
949 15 ▶ if ($last->{index} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'index'} == 0) { } 0 15 15
950 0 die 23;
951 0 ▶ if ($flow == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flow == 1) 0 0 0
952 0 $yaml .= ' ';
953 }
954 0 $yaml .= "{";
955 }
956 else {
957 15 $yaml .= ": ";
958 }
959 15 $last->{type} = 'MAP';
960 }
961 27 $yaml .= "$alias$space";
962 }
963 else {
964 175 ▶ if ($last->{type} eq 'MAP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'type'} eq 'MAP') { } 25 150 175
965 25 $yaml .= "$alias :";
966 25 $last->{type} = 'MAPVALUE';
967 }
968 else {
969  
970 150 ▶ if ($last->{type} eq 'MAPVALUE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($last->{'type'} eq 'MAPVALUE') { } 77 73 150
elsif ($last->{'type'} eq 'DOC') { } 0 73 73
971 77 $last->{type} = 'MAP';
972 }
973 elsif ($last->{type} eq 'DOC') {
974 # TODO an alias at document level isn't actually valid
975 }
976 else {
977 73 ▶ if ($last->{type} eq 'COMPLEXVALUE') {
Branch: 3 branches
BranchTrueFalseTotal
if ($last->{'type'} eq 'COMPLEXVALUE') { } 3 70 73
elsif ($last->{'type'} eq 'COMPLEX') { } 0 70 70
elsif ($last->{'type'} eq 'SEQ') { } 70 0 70
978 3 $last->{type} = 'MAP';
979 3 $yaml .= ": ";
980 }
981 elsif ($last->{type} eq 'COMPLEX') {
982 0 $yaml .= ": ";
983 }
984 elsif ($last->{type} eq 'SEQ') {
985 70 $yaml .= "- ";
986 }
987 else {
988 0 die "Unexpected";
989 }
990 }
991 150 $yaml .= "$alias\n";
992 }
993 }
994  
995 202 $self->_write("$yaml");
996 202 $last->{index}++;
997 202 $last->{column} = $self->column;
998 202 $self->{open_ended} = 0;
999 }
1000  
1001 3273 ▶sub document_start_event {
Subroutine document_start_event: called CC=4 SCAR=13.9
Pod documented
1002 3273 DEBUG and warn __PACKAGE__.':'.__LINE__.": +++ document_start_event\n";
1003 3273 my ($self, $info) = @_;
1004 3273 my $newline = 0;
1005 3273 my $implicit = $info->{implicit};
1006 3273 ▶ if ($info->{version_directive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"version_directive"}) 18 3255 3273
1007 18 ▶ if ($self->{open_ended}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"open_ended"}) 10 8 18
1008 10 $self->_write("...\n");
1009 }
1010 18 $self->_write("%YAML $info->{version_directive}->{major}.$info->{version_directive}->{minor}\n");
1011 18 $self->{open_ended} = 0;
1012 18 $implicit = 0; # we need ---
1013 }
1014 3273 ▶ unless ($implicit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($implicit) 1196 2077 3273
1015 1196 $newline = 1;
1016 1196 $self->_write("---");
1017 }
1018 $self->set_event_stack([
1019 {
1020 3273 type => 'DOC', index => 0, indent => '', info => $info,
1021 newline => $newline, column => $self->column,
1022 }
1023 ]);
1024 }
1025  
1026 3272 ▶sub document_end_event {
Subroutine document_end_event: called CC=3 SCAR=11.0
Pod documented
1027 3272 DEBUG and warn __PACKAGE__.':'.__LINE__.": +++ document_end_event\n";
1028 3272 my ($self, $info) = @_;
1029 3272 $self->set_event_stack([]);
1030 3272 ▶ if ($self->{open_ended} or not $info->{implicit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'open_ended'} or not $info->{'implicit'}) { } 243 3029 3272
Condition100%3/3
$self->{"open_ended"} or not $info->{"implicit"}
l!l&&r!l&&!r
322113029
MC/DC100%2/2
$self->{"open_ended"} or not $info->{"implicit"}
$self->{"open_ended"} not $info->{"implicit"}
Truth table100%3/3
A or not(B)
A= $self->{"open_ended"}B= not $info->{"implicit"}
ABresult
1X1
011
000
1031 243 $self->_write("...\n");
1032 243 $self->{open_ended} = 0;
1033 }
1034 else {
1035 3029 $self->{open_ended} = 1;
1036 }
1037 }
1038  
1039 3065 ▶sub stream_start_event {
Subroutine stream_start_event: called CC=1 SCAR=0.0
Pod documented
1040 }
1041  
1042 3064 ▶sub stream_end_event {
Subroutine stream_end_event: called CC=1 SCAR=0.0
Pod documented
1043 }
1044  
1045 649 ▶sub _emit_tag {
Subroutine _emit_tag: called CC=4 SCAR=13.9
1046 649 my ($self, $type, $tag) = @_;
1047 649 my $map = $self->tagmap;
1048 649 for my $key (sort keys %$map) {
1049 649 ▶ if ($tag =~ m/^\Q$key\E(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^\Q$key\E(.*)/) 487 162 649
1050 487 $tag = $map->{ $key } . $1;
1051 487 return $tag;
1052 }
1053 }
1054 162 ▶ if ($tag =~ m/^(!.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^(!.*)/) { } 107 55 162
1055 107 $tag = "$1";
1056 }
1057 else {
1058 55 $tag = "!<$tag>";
1059 }
1060 162 return $tag;
1061 }
1062  
1063 1585 ▶sub finish {
Subroutine finish: called CC=1 SCAR=0.0
Pod documented
1064 1585 my ($self) = @_;
1065 1585 $self->writer->finish;
1066 }
1067  
1068 15108 ▶sub _write {
Subroutine _write: called CC=4 SCAR=13.9
1069 15108 my ($self, $yaml) = @_;
1070 15108 ▶ return unless length $yaml;
Branch: 1 branch
BranchTrueFalseTotal
unless length $yaml 1954 13154 15108
1071 13154 my @lines = split m/\n/, $yaml, -1;
1072 13154 my $newlines = @lines - 1;
1073 13154 $self->{line} += $newlines;
1074 13154 ▶ if (length $lines[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $lines[-1]) { } 6978 6176 13154
1075 6978 ▶ if ($newlines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newlines) { } 881 6097 6978
1076 881 $self->{column} = length $lines[-1];
1077 }
1078 else {
1079 6097 $self->{column} += length $lines[-1];
1080 }
1081 }
1082 else {
1083 6176 $self->{column} = 0;
1084 }
1085 13154 $self->writer->write($yaml);
1086 }
1087  
1088 49 1;
1089  
1090 __END__
↑ Summary blib/lib/YAML/PP/Render.pm »