Tidy.pm

filter: Statementstmt 91.3% 509 / 557 Branchbran 87.0% 228 / 262 Conditioncond 88.8% 128 / 144 MC/DCmcdc 71.6% 58 / 81 Subroutinesub 94.1% 32 / 34 Podpod 80.0% 4 / 5 totaltotal 88.5% 959 / 1083 CC 201 SCAR 35.8
CC
201
Cov
90%
_process
81.6
_process_doc
26.9
_tree
18.0
CRAP 35.9
↑ Summary Tidy/Config.pm »
1 # ABSTRACT: Tidy YAML files
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings FATAL => qw/ substr /;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use experimental qw/ signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 package YAML::Tidy;
9  
10 7 our $VERSION = '0.007'; # VERSION
11  
12 7 ▶use YAML::Tidy::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use YAML::Tidy::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use YAML::LibYAML::API::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 use YAML::PP::Common qw/
16 YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE
17 YAML_DOUBLE_QUOTED_SCALAR_STYLE YAML_LITERAL_SCALAR_STYLE
18 YAML_FOLDED_SCALAR_STYLE
19 YAML_FLOW_SEQUENCE_STYLE YAML_FLOW_MAPPING_STYLE
20 7 ▶/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use YAML::PP::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶use YAML::PP::Highlight;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 7 ▶use constant DEBUG => $ENV{YAML_TIDY_DEBUG} ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'YAML_TIDY_DEBUG'} ? : 0 7 7
26  
27 ▶sub new($class, %args) {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
28 22 ▶ my $cfg = delete $args{cfg} || YAML::Tidy::Config->new();
Condition66%2/3
delete $args{'cfg'} || 'YAML::Tidy::Config'->new
l!l&&r!l&&!r
2110
MC/DC0%0/2
delete $args{'cfg'} || 'YAML::Tidy::Config'->new
delete $args{'cfg'} 'YAML::Tidy::Config'->new
Truth table66%2/3
A || B
A= delete $args{'cfg'}B= 'YAML::Tidy::Config'->new
ABresult
1X1
011
000
29 my $self = bless {
30 partial => delete $args{partial},
31 22 cfg => $cfg,
32 }, $class;
33 22 return $self;
34 }
35  
36 67802 ▶sub cfg($self) { $self->{cfg} }
Subroutine cfg: called CC=1 SCAR=0.0
Pod documented
37 22505 ▶sub partial($self) { $self->{partial} }
Subroutine partial: called CC=1 SCAR=0.0
Pod undocumented
38  
39 ▶sub tidy($self, $yaml) {
Subroutine tidy: called CC=3 SCAR=11.0
Pod documented
40 3037 local $Data::Dumper::Sortkeys = 1;
41 3037 my @lines = split /\n/, $yaml, -1;
42 3037 my $tree = $self->_tree($yaml, \@lines);
43 3037 $self->{tree} = $tree;
44 3037 $self->{lines} = \@lines;
45 # warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\@lines], ['lines']);
46 3037 ▶ if (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 3024 13 3037
47 3024 my $from = 0;
48 3024 ▶ $self->_trimspaces(\$from, $tree) if $self->cfg->trimtrailing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->cfg->trimtrailing 2856 168 3024
49 3024 $self->_process(undef, $tree);
50 }
51 3037 $yaml = join "\n", @{ $self->{lines} };
52 # warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$yaml], ['yaml']);
53 3037 return $yaml;
54 }
55  
56 ▶sub _process($self, $parent, $node) {
Subroutine _process: called CC=80 SCAR=44.0
57 22424 ▶ my $type = $node->{type} || '';
Condition100%2/2
$node->{'type'} || ''
l!l
1130411120
MC/DC100%1/1
$node->{'type'} || ''
$node->{'type'}
Truth table100%2/2
A
A= $node->{'type'}
Aresult
11
00
58 # warn __PACKAGE__.':'.__LINE__.": ======== _process($parent, $node) $type\n";
59 22424 ▶ if ($node->{flow}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"flow"}) 975 21449 22424
60 975 $self->_process_flow($parent, $node);
61 975 return;
62 }
63 21449 my $level = $node->{level};
64 21449 my $indent = $self->cfg->indent;
65 21449 my $lines = $self->{lines};
66 21449 ▶ return unless @$lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @$lines 0 21449 21449
67  
68 21449 ▶ if ($level == -1 and $type eq 'DOC') {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == -1 and $type eq "DOC") 3347 18102 21449
Condition100%3/3
$level == -1 and $type eq "DOC"
!ll&&!rl&&r
1507830243347
MC/DC100%2/2
$level == -1 and $type eq "DOC"
$level == -1 $type eq "DOC"
Truth table100%3/3
A and B
A= $level == -1B= $type eq "DOC"
ABresult
0X0
100
111
69 3347 $self->_process_doc($parent, $node);
70 }
71 21449 my $start = $node->start;
72  
73  
74 21449 my $indenttoplevelscalar = 1;
75 21449 my $trimtrailing = $self->cfg->trimtrailing;
76  
77 21449 my $col = $node->indent;
78 21449 ▶ my $lastcol = $parent ? $parent->indent : -99;
Branch: 1 branch
BranchTrueFalseTotal
$parent ? : 18425 3024 21449
79 21449 my $realindent = $col - $lastcol;
80 21449 my $startline = $node->line;
81 21449 my $line = $lines->[ $startline ];
82 21449 ▶ unless (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 21449 21449
83 0 die "Line $startline not found";
84 }
85 21449 my $before = substr($line, 0, $col);
86  
87  
88 21449 ▶ if ($node->is_collection) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->is_collection) { } 10329 11120 21449
89 10329 ▶ my $ignore_firstlevel = ($self->partial and $level == 0);
Condition100%3/3
$self->partial && $level == 0
!ll&&!rl&&r
1032171
MC/DC100%2/2
$self->partial && $level == 0
$self->partial $level == 0
Truth table100%3/3
A && B
A= $self->partialB= $level == 0
ABresult
0X0
100
111
90 10329 ▶ if ($level < 0 or $ignore_firstlevel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level < 0 or $ignore_firstlevel) 6372 3957 10329
Condition100%3/3
$level < 0 or $ignore_firstlevel
l!l&&r!l&&!r
637113957
MC/DC100%2/2
$level < 0 or $ignore_firstlevel
$level < 0 $ignore_firstlevel
Truth table100%3/3
A or B
A= $level < 0B= $ignore_firstlevel
ABresult
1X1
011
000
91 6372 for my $c (@{ $node->{children} }) {
92 6696 $self->_process($node, $c);
93 }
94 6372 return;
95 }
96  
97 3957 ▶ if ($level == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 0) 2274 1683 3957
98 2274 $indent = 0;
99 }
100 3957 ▶ if ($type eq 'MAP') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'MAP') { } 2492 1465 3957
elsif ($type eq 'SEQ') { } 1465 0 1465
101 2492 ▶ if ($before =~ tr/ //c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before =~ tr/ //c) 317 2175 2492
102 317 ▶ if ($indent == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent == 1) 12 305 317
103 12 $indent = 2;
104 }
105 }
106 }
107 elsif ($type eq 'SEQ') {
108 1465 ▶ if ($before =~ tr/ //c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before =~ tr/ //c) { } 247 1218 1465
109 247 ▶ if ($indent == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent == 1) 12 235 247
110 12 $indent = 2;
111 }
112 }
113 else {
114 1218 ▶ if ($parent->{type} eq 'MAP' and not $node->{index} % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->{"type"} eq "MAP" and not $node->{"index"} % 2) 370 848 1218
Condition100%3/3
$parent->{"type"} eq "MAP" and not $node->{"index"} % 2
!ll&&!rl&&r
81434370
MC/DC100%2/2
$parent->{"type"} eq "MAP" and not $node->{"index"} % 2
$parent->{"type"} eq "MAP" not $node->{"index"} % 2
Truth table100%3/3
A and not(B)
A= $parent->{"type"} eq "MAP"B= not $node->{"index"} % 2
ABresult
0X0
100
111
115 # zero indented sequence?
116 370 $indent = $self->cfg->indent_seq_in_map;
117 }
118 }
119  
120 }
121 3957 my $diff = $indent - $realindent;
122 3957 ▶ if ($diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff) 588 3369 3957
123 588 $self->_fix_indent($node, $diff, $col);
124 588 $node->fix_node_indent($diff);
125 }
126 3957 for my $c (@{ $node->{children} }) {
127 12704 $self->_process($node, $c);
128 }
129 3957 return;
130 }
131 else {
132 11120 ▶ my $ignore_firstlevel = ($self->partial and $level == 0);
Condition100%3/3
$self->partial && $level == 0
!ll&&!rl&&r
1111352
MC/DC100%2/2
$self->partial && $level == 0
$self->partial $level == 0
Truth table100%3/3
A && B
A= $self->partialB= $level == 0
ABresult
0X0
100
111
133 11120 ▶ if ($node->empty_scalar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->empty_scalar) 282 10838 11120
134 282 return;
135 }
136 10838 ▶ if ($node->{name} eq 'alias_event') {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"name"} eq "alias_event") 261 10577 10838
137 261 return;
138 }
139 10577 ▶ if ($parent->{type} eq 'MAP' and ($node->{index} % 2 and not $node->multiline)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->{"type"} eq "MAP" and ($node->{"index"} % 2 and not $node->multiline)) 4395 6182 10577
Condition100%6/6
$parent->{"type"} eq "MAP" and ($node->{"index"} % 2 and not $node->multiline)
!ll&&!rl&&r
2797937687
$node->{"index"} % 2 and not $node->multiline
!ll&&!rl&&r
3313724395
MC/DC66%2/3
$parent->{"type"} eq "MAP" and ($node->{"index"} % 2 and not $node->multiline)
$parent->{"type"} eq "MAP" $node->{"index"} % 2 not $node->multiline
Truth table75%3/4
A and B and not(C)
A= $parent->{"type"} eq "MAP"B= $node->{"index"} % 2C= not $node->multiline
ABCresult
0XX0
10X0
1100
1111
140 4395 $self->_replace_quoting($node);
141 4395 return;
142 }
143 6182 my $new_indent = $parent->indent + $indent;
144 6182 my $new_spaces = ' ' x $new_indent;
145  
146 6182 my ($anchor, $tag, $comments, $scalar) = $self->_find_scalar_start($node);
147 6182 my $explicit_indent = 0;
148 6182 ▶ if ($scalar->[2] =~ m/[>|]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scalar->[2] =~ /[>|]/u) 901 5281 6182
149 901 my $l = $lines->[ $scalar->[0] ];
150 901 my ($ind) = substr($l, $scalar->[1]) =~ m/^[|>][+-]?([0-9]*)/;
151 901 $explicit_indent = $ind;
152 }
153 6182 my $skipfirst = 0;
154 6182 my $before = substr($line, 0, $col);
155 6182 ▶ if ($before =~ tr/ \t//c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before =~ tr/\t //c) 5311 871 6182
156 # same line as key
157 5311 my $remove = 0;
158 5311 ▶ $before =~ s/([\t ]+)$/ / and $remove = -1 + length $1;
Branch: 1 branch
BranchTrueFalseTotal
$before =~ s/([\t ]+)$/ /u and $remove = -1 + length($1) 5311 0 5311
159 5311 $node->open->{column} -= $remove;
160 5311 ▶ unless ($node->multiline) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->multiline) 4140 1171 5311
161 4140 $node->close->{column} -= $remove;
162 }
163 5311 $line = $before . substr($line, $col);
164 5311 $lines->[ $startline ] = $line;
165 5311 $skipfirst = 1;
166 }
167 6182 my $realstart = $scalar->[0];
168 6182 ▶ unless ($ignore_firstlevel) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignore_firstlevel) 6180 2 6182
169 6180 for my $i ($startline .. $realstart) {
170 6331 my $line = $lines->[ $i ];
171 6331 ▶ if ($i == $startline and $col > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $startline and $col > 0) 5591 740 6331
Condition100%3/3
$i == $startline and $col > 0
!ll&&!rl&&r
1515895591
MC/DC100%2/2
$i == $startline and $col > 0
$i == $startline $col > 0
Truth table100%3/3
A and B
A= $i == $startlineB= $col > 0
ABresult
0X0
100
111
172 5591 my $before = substr($line, 0, $col);
173 5591 ▶ if ($before =~ tr/ //c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before =~ tr/ //c) 5311 280 5591
174 5311 next;
175 }
176 }
177 1020 ▶ unless ($line =~ tr/ //c) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ tr/ //c) 0 1020 1020
178 0 next;
179 }
180 1020 my $remove = 0;
181 1020 ▶ $line =~ s/^( *)/$new_spaces/ and $remove = length($1) - length($new_spaces);
Branch: 1 branch
BranchTrueFalseTotal
$line =~ s/^( *)/$new_spaces/u and $remove = length($1) - length($new_spaces) 1020 0 1020
182 1020 ▶ if ($i == $startline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $startline) 869 151 1020
183 869 $node->open->{column} -= $remove;
184 869 ▶ unless ($node->multiline) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->multiline) 409 460 869
185 409 $node->close->{column} -= $remove;
186 }
187 }
188 1020 $lines->[ $i] = $line;
189 }
190 }
191 # leave alone explicitly indented block scalars
192 6182 ▶ return if $explicit_indent;
Branch: 1 branch
BranchTrueFalseTotal
if $explicit_indent 155 6027 6182
193  
194 6027 $startline = $realstart;
195 6027 my $endline = $node->realendline;
196 # warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$startline], ['startline']);
197 # warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$endline], ['endline']);
198  
199 6027 my $line = $lines->[ $startline ];
200 6027 my $realcol = $scalar->[1];
201 6027 $col = $realcol;
202  
203 6027 my $nextline = $node->{nextline};
204  
205 my $block = ($node->{style} eq YAML_LITERAL_SCALAR_STYLE
206 6027 ▶ or $node->{style} eq YAML_FOLDED_SCALAR_STYLE);
Condition100%3/3
$node->{'style'} eq 4 || $node->{'style'} eq 5
l!l&&r!l&&!r
4373095281
MC/DC100%2/2
$node->{'style'} eq 4 || $node->{'style'} eq 5
$node->{'style'} eq 4 $node->{'style'} eq 5
Truth table100%3/3
A || B
A= $node->{'style'} eq 4B= $node->{'style'} eq 5
ABresult
1X1
011
000
207 6027 ▶ if ($block) {
Branch: 2 branches
BranchTrueFalseTotal
if ($block) { } 746 5281 6027
elsif ($node->{'style'} == 1 or $node->{'style'} == 2 or $node->{'style'} == 3) { } 5281 0 5281
208  
209 746 $startline++;
210 746 ▶ while ($startline < $endline and $lines->[ $startline ] !~ tr/ //c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($startline < $endline and not $lines->[$startline] =~ tr/ //c) 73 746 819
Condition100%3/3
$startline < $endline and not $lines->[$startline] =~ tr/ //c
!ll&&!rl&&r
28146573
MC/DC100%2/2
$startline < $endline and not $lines->[$startline] =~ tr/ //c
$startline < $endline not $lines->[$startline] =~ tr/ //c
Truth table100%3/3
A and not(B)
A= $startline < $endlineB= not $lines->[$startline] =~ tr/ //c
ABresult
0X0
100
111
211 73 ▶ if ($trimtrailing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trimtrailing) 68 5 73
212 68 $self->_trim($startline, $startline);
213 }
214 73 $startline++;
215 }
216 746 ▶ if ($nextline > $endline + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nextline > $endline + 1) 68 678 746
217 68 $endline = $nextline - 1;
218 }
219 746 my @slice = @$lines[$startline .. $endline ];
220 746 my ($sp) = $lines->[ $startline ] =~ m/^( *)/;
221 746 ▶ if (not $ignore_firstlevel and length($sp) != $new_indent) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $ignore_firstlevel and length $sp != $new_indent) { } 373 373 746
elsif ($trimtrailing) { } 348 25 373
Condition100%3/3
not $ignore_firstlevel and length $sp != $new_indent
!ll&&!rl&&r
1372373
MC/DC100%2/2
not $ignore_firstlevel and length $sp != $new_indent
not $ignore_firstlevel length $sp != $new_indent
Truth table100%3/3
not(A) and B
A= not $ignore_firstlevelB= length $sp != $new_indent
ABresult
0X0
100
111
222 373 for my $line (@slice) {
223 1179 ▶ unless (length $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $line) 274 905 1179
224 274 next;
225 }
226 905 ▶ if ($line !~ tr/ //c and length($line) <= length($sp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line =~ tr/ //c and length $line <= length $sp) 132 773 905
Condition100%3/3
not $line =~ tr/ //c and length $line <= length $sp
!ll&&!rl&&r
75518132
MC/DC100%2/2
not $line =~ tr/ //c and length $line <= length $sp
not $line =~ tr/ //c length $line <= length $sp
Truth table100%3/3
not(A) and B
A= not $line =~ tr/ //cB= length $line <= length $sp
ABresult
0X0
100
111
227 132 ▶ if ($trimtrailing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trimtrailing) 124 8 132
228 124 $line = '';
229 }
230 132 next;
231 }
232 773 ▶ if ($line =~ m/^( *)\#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^( *)\#/u) { } 42 731 773
233 42 my $cindent = length $1;
234 42 my $diff = $new_indent - length $sp;
235 42 $cindent += $diff;
236 42 ▶ if ($diff > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($diff > 0) { } 31 11 42
elsif ($diff < 0) { } 11 0 11
237 31 $line = (' ' x $diff) . $line;
238 }
239 elsif ($diff < 0) {
240 11 ▶ if ($cindent < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cindent < 0) 0 11 11
241 0 $cindent = 0;
242 }
243 11 $new_spaces = ' ' x $cindent;
244 11 $line =~ s/^ */$new_spaces/;
245 }
246 }
247 else {
248 731 $line =~ s/^$sp/$new_spaces/;
249 }
250 }
251 373 @$lines[$startline .. $endline ] = @slice;
252 }
253 elsif ($trimtrailing) {
254 348 for my $line (@slice) {
255 917 ▶ if ($line !~ tr/ //c and length($line) <= length($sp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line =~ tr/ //c and length $line <= length $sp) 228 689 917
Condition100%3/3
not $line =~ tr/ //c and length $line <= length $sp
!ll&&!rl&&r
66722228
MC/DC100%2/2
not $line =~ tr/ //c and length $line <= length $sp
not $line =~ tr/ //c length $line <= length $sp
Truth table100%3/3
not(A) and B
A= not $line =~ tr/ //cB= length $line <= length $sp
ABresult
0X0
100
111
256 228 $line = '';
257 }
258 }
259 348 @$lines[$startline .. $endline ] = @slice;
260 }
261 }
262 ▶ elsif ($node->{style} == YAML_PLAIN_SCALAR_STYLE or
Condition83%5/6
$node->{"style"} == 1 or $node->{"style"} == 2 or $node->{"style"} == 3
l!l&&r!l&&!r
47385430
$node->{"style"} == 1 or $node->{"style"} == 2
l!l&&r!l&&!r
4525213543
MC/DC0%0/3
$node->{"style"} == 1 or $node->{"style"} == 2 or $node->{"style"} == 3
$node->{"style"} == 1 $node->{"style"} == 2 $node->{"style"} == 3
Truth table75%3/4
A or B or C
A= $node->{"style"} == 1B= $node->{"style"} == 2C= $node->{"style"} == 3
ABCresult
1XX1
01X1
0011
0000
263 $node->{style} == YAML_SINGLE_QUOTED_SCALAR_STYLE or
264 $node->{style} == YAML_DOUBLE_QUOTED_SCALAR_STYLE) {
265 5281 ▶ if ($node->empty_scalar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->empty_scalar) 0 5281 5281
266 0 return;
267 }
268 5281 my $remove = 0;
269 5281 ▶ if (not $skipfirst or $node->multiline) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $skipfirst or $node->multiline) 1141 4140 5281
Condition100%3/3
not $skipfirst or $node->multiline
l!l&&r!l&&!r
7503914140
MC/DC100%2/2
not $skipfirst or $node->multiline
not $skipfirst $node->multiline
Truth table100%3/3
not(A) or B
A= not $skipfirstB= $node->multiline
ABresult
1X1
011
000
270 1141 my $startline = $startline;
271 1141 ▶ $startline++ if $skipfirst;
Branch: 1 branch
BranchTrueFalseTotal
if $skipfirst 391 750 1141
272 1141 $endline = $node->close->{line};
273 1141 ▶ return if $startline >= @$lines;
Branch: 1 branch
BranchTrueFalseTotal
if $startline >= @$lines 0 1141 1141
274 1141 my $line = $lines->[ $startline ];
275 1141 my ($sp) = $line =~ m/^( *)/;
276 1141 ▶ if ($ignore_firstlevel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ignore_firstlevel) 1 1140 1141
277 1 $new_indent = length $sp;
278 1 $new_spaces = ' ' x $new_indent;
279 }
280 1141 my @slice = @$lines[$startline .. $endline ];
281 1141 ▶ if ($level == 0 and not $indenttoplevelscalar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 0 and not $indenttoplevelscalar) 0 1141 1141
Condition66%2/3
$level == 0 and not $indenttoplevelscalar
!ll&&!rl&&r
6195220
MC/DC0%0/2
$level == 0 and not $indenttoplevelscalar
$level == 0 not $indenttoplevelscalar
Truth table66%2/3
A and not(B)
A= $level == 0B= not $indenttoplevelscalar
ABresult
0X0
100
111
282 0 $new_spaces = ' ' x ($new_indent - $indent);
283 }
284 1141 for my $line (@slice) {
285 2048 ▶ if ($line =~ tr/ //c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ tr/ //c) 1747 301 2048
286 1747 ▶ $line =~ s/^([\t ]*)/$new_spaces/
Branch: 1 branch
BranchTrueFalseTotal
$line =~ s/^([\t ]*)/$new_spaces/u and $remove = length($1) - length($new_spaces) 1747 0 1747
287 and $remove = length($1) - length($new_spaces);
288 }
289 }
290 1141 $node->close->{column} -= $remove;
291 1141 @$lines[$startline .. $endline ] = @slice;
292 }
293 5281 ▶ if (not $node->multiline) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $node->multiline) 4549 732 5281
294 4549 $self->_replace_quoting($node);
295 }
296 }
297 }
298 }
299  
300 7 my $RE_INT_CORE = qr{^([+-]?(?:[0-9]+))$};
301 7 my $RE_FLOAT_CORE = qr{^([+-]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)(?:[eE][+-]?[0-9]+)?)$};
302 7 my $RE_INT_OCTAL = qr{^0o([0-7]+)$};
303 7 my $RE_INT_HEX = qr{^0x([0-9a-fA-F]+)$};
304 7 my @null = (qw/ null NULL Null ~ /, '');
305 7 my @true = qw/ true TRUE True /;
306 7 my @false = qw/ false FALSE False /;
307 7 my @inf = qw/ .inf .Inf .INF +.inf +.Inf +.INF -.inf -.Inf -.INF /;
308 7 my @nan = qw/ .nan .NaN .NAN /;
309 7 my @re = ($RE_INT_CORE, $RE_INT_OCTAL, $RE_INT_HEX, $RE_FLOAT_CORE);
310 7 my $re = join '|', @re;
311 7 my @all = (@null, @true, @false, @inf, @nan);
312  
313 ▶sub _replace_quoting($self, $node) {
Subroutine _replace_quoting: called CC=8 SCAR=20.8
314 11294 ▶ return if $node->{tag};
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"tag"} 875 10419 11294
315 10419 my $default_style = $self->cfg->default_scalar_style;
316 # single line flow scalars
317 10419 ▶ if (defined $default_style and $node->{style} != $default_style) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default_style and $node->{"style"} != $default_style) 1508 8911 10419
Condition100%3/3
defined $default_style and $node->{"style"} != $default_style
!ll&&!rl&&r
81577541508
MC/DC100%2/2
defined $default_style and $node->{"style"} != $default_style
defined $default_style $node->{"style"} != $default_style
Truth table100%3/3
A and B
A= defined $default_styleB= $node->{"style"} != $default_style
ABresult
0X0
100
111
318 1508 my ($changed, $new_string, $new_style) = $self->_change_style($node, $default_style);
319 1508 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 1344 164 1508
320 1344 my $lines = $self->{lines};
321 1344 my $line = $lines->[ $node->open->{line} ];
322 1344 my ($from, $to) = ($node->open->{column}, $node->close->{column});
323 1344 ▶ if (defined $node->{anchor} or $node->{tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{"anchor"} or $node->{"tag"}) 58 1286 1344
Condition66%2/3
defined $node->{"anchor"} or $node->{"tag"}
l!l&&r!l&&!r
5801286
MC/DC50%1/2
defined $node->{"anchor"} or $node->{"tag"}
defined $node->{"anchor"} $node->{"tag"}
Truth table66%2/3
A or B
A= defined $node->{"anchor"}B= $node->{"tag"}
ABresult
1X1
011
000
324 58 my ($anchor, $tag, $comments, $scalar) = $self->_find_scalar_start($node);
325 58 $from = $scalar->[1];
326 }
327 1344 substr($line, $from, $to - $from, $new_string);
328 1344 my $diff = length($new_string) - ($to - $from);
329 1344 ▶ if ($diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff) 1317 27 1344
330 1317 $self->{tree}->_move_columns($node->open->{line}, $node->close->{column} + 1, $diff);
331 }
332 1344 $node->{style} = $new_style;
333 1344 $node->close->{column} += $diff;
334 1344 $lines->[ $node->open->{line} ] = $line;
335 }
336 }
337 }
338  
339 ▶sub _change_style($self, $node, $style) {
Subroutine _change_style: called CC=9 SCAR=22.0
340 1508 my $value = $node->{value};
341 1508 ▶ if (grep { $_ eq $value } @all or $value =~ m/($re)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $value;} @all or $value =~ /($re)/u) 150 1358 1508
Condition100%3/3
grep {$_ eq $value;} @all or $value =~ /($re)/u
l!l&&r!l&&!r
181321358
MC/DC100%2/2
grep {$_ eq $value;} @all or $value =~ /($re)/u
grep {$_ eq $value;} @all $value =~ /($re)/u
Truth table100%3/3
A or B
A= grep {$_ eq $value;} @allB= $value =~ /($re)/u
ABresult
1X1
011
000
342 # leave me alone
343 150 ▶ if ($node->{style} eq YAML_PLAIN_SCALAR_STYLE or $style eq YAML_PLAIN_SCALAR_STYLE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"style"} eq 1 or $style eq 1) 149 1 150
Condition100%3/3
$node->{"style"} eq 1 or $style eq 1
l!l&&r!l&&!r
14811
MC/DC100%2/2
$node->{"style"} eq 1 or $style eq 1
$node->{"style"} eq 1 $style eq 1
Truth table100%3/3
A or B
A= $node->{"style"} eq 1B= $style eq 1
ABresult
1X1
011
000
344 149 return (0);
345 }
346 }
347  
348 1359 my $emit = $self->_emit_value($value, $style);
349 1359 chomp $emit;
350 1359 ▶ return (0) if $emit =~ tr/\n//;
Branch: 1 branch
BranchTrueFalseTotal
if $emit =~ tr/\n// 0 1359 1359
351 1359 my $first = substr($emit, 0, 1);
352 1359 ▶ my $new_style =
Branch: 2 branches
BranchTrueFalseTotal
$first eq q['] ? : 663 696 1359
$first eq '"' ? : 665 31 696
353 $first eq "'" ? YAML_SINGLE_QUOTED_SCALAR_STYLE
354 : $first eq '"' ? YAML_DOUBLE_QUOTED_SCALAR_STYLE
355 : YAML_PLAIN_SCALAR_STYLE;
356 1359 ▶ if ($new_style eq $style) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_style eq $style) 1344 15 1359
357 1344 return (1, $emit, $new_style);
358 }
359  
360 15 return (0);
361 }
362  
363 ▶sub _emit_value($self, $value, $style) {
Subroutine _emit_value: called CC=1 SCAR=0.0
364 1359 my $options = {};
365 1359 my $events = [
366 { name => 'stream_start_event' },
367 { name => 'document_start_event', implicit => 1 },
368 { name => 'scalar_event', style => $style, value => $value },
369 { name => 'document_end_event', implicit => 1 },
370 { name => 'stream_end_event' },
371 ];
372 1359 return YAML::LibYAML::API::XS::emit_string_events($events, $options);
373 }
374  
375 ▶sub _process_doc($self, $parent, $node) {
Subroutine _process_doc: called CC=25 SCAR=32.9
376 3347 DEBUG and say STDERR "_process_doc($node)";
377 3347 my $lines = $self->{lines};
378 3347 my $open = $node->open;
379 3347 my $close = $node->close;
380 3347 ▶ if ($node->open->{implicit} and $self->cfg->addheader and not $self->partial) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->open->{'implicit'} and $self->cfg->addheader and not $self->partial) { } 339 3008 3347
elsif ($node->{'index'} == 1 and not $open->{'implicit'} and $self->cfg->removeheader and not $self->partial) { } 159 5698 5857
Condition83%5/6
$node->open->{"implicit"} and $self->cfg->addheader and not $self->partial
!ll&&!rl&&r
30080339
$node->open->{"implicit"} and $self->cfg->addheader
!ll&&!rl&&r
13041704339
MC/DC66%2/3
$node->open->{"implicit"} and $self->cfg->addheader and not $self->partial
$node->open->{"implicit"} $self->cfg->addheader not $self->partial
Truth table75%3/4
A and B and not(C)
A= $node->open->{"implicit"}B= $self->cfg->addheaderC= not $self->partial
ABCresult
0XX0
10X0
1100
1111
381 # add ---
382 339 splice @$lines, $open->{start}->{line}, 0, '---';
383 339 $self->{tree}->fix_lines($open->{start}->{line}, +1);
384 339 $open->{start}->{line}--;
385 339 $open->{end}->{line}--;
386 339 $open->{end}->{column} = 3;
387 339 $open->{implicit} = 0;
388 339 DEBUG and say STDERR "$node";
389 }
390 ▶ elsif ($node->{index} == 1 and not $open->{implicit} and $self->cfg->removeheader and not $self->partial) {
Condition88%8/9
$node->{"index"} == 1 and not $open->{"implicit"} and $self->cfg->removeheader and not $self->partial
!ll&&!rl&&r
28490159
$node->{"index"} == 1 and not $open->{"implicit"} and $self->cfg->removeheader
!ll&&!rl&&r
2053796159
$node->{"index"} == 1 and not $open->{"implicit"}
!ll&&!rl&&r
3491704955
MC/DC75%3/4
$node->{"index"} == 1 and not $open->{"implicit"} and $self->cfg->removeheader and not $self->partial
$node->{"index"} == 1 not $open->{"implicit"} $self->cfg->removeheader not $self->partial
Truth table80%4/5
A and not(B) and C and not(D)
A= $node->{"index"} == 1B= not $open->{"implicit"}C= $self->cfg->removeheaderD= not $self->partial
ABCDresult
0XXX0
10XX0
110X0
11100
11111
391 # remove first ---
392 159 my $child = $node->{children}->[0];
393 159 ▶ if ($open->{version_directive} or $open->{tag_directives} or not $child->is_collection and $child->empty_scalar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open->{'version_directive'} or $open->{'tag_directives'} or not $child->is_collection and $child->empty_scalar) { } 21 138 159
Condition100%9/9
$open->{"version_directive"} or $open->{"tag_directives"} or not $child->is_collection and $child->empty_scalar
l!l&&r!l&&!r
183138
$open->{"version_directive"} or $open->{"tag_directives"}
l!l&&r!l&&!r
315141
not $child->is_collection and $child->empty_scalar
!ll&&!rl&&r
84543
MC/DC100%4/4
$open->{"version_directive"} or $open->{"tag_directives"} or not $child->is_collection and $child->empty_scalar
$open->{"version_directive"} $open->{"tag_directives"} not $child->is_collection $child->empty_scalar
Truth table100%5/5
A or B or not(C) and D
A= $open->{"version_directive"}B= $open->{"tag_directives"}C= not $child->is_collectionD= $child->empty_scalar
ABCDresult
1XXX1
01XX1
00111
000X0
00100
394 }
395 else {
396 138 my $startline = $open->{start}->{line};
397 138 my $line = $lines->[ $startline ];
398 138 ▶ if ($line =~ m/^---[ \t]*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^---[ \t]*$/u) { } 99 39 138
elsif ($line =~ s/^---[ \t]+(?=#)//u) { } 0 39 39
399 99 splice @$lines, $startline, 1;
400 99 $self->{tree}->fix_lines($open->{start}->{line}+1, -1);
401 99 DEBUG and say STDERR "$node";
402 99 $open->{implicit} = 1;
403 }
404 elsif ($line =~ s/^---[ \t]+(?=#)//) {
405 0 $lines->[ $startline ] = $line;
406 0 DEBUG and say STDERR "$node";
407 0 $open->{implicit} = 1;
408 }
409 }
410 }
411 3347 ▶ if ($close->{implicit} and $self->cfg->addfooter and not $self->partial) {
Branch: 2 branches
BranchTrueFalseTotal
if ($close->{'implicit'} and $self->cfg->addfooter and not $self->partial) { } 531 2816 3347
elsif (not $close->{'implicit'} and $self->cfg->removefooter and not $self->partial) { } 27 5578 5605
Condition83%5/6
$close->{"implicit"} and $self->cfg->addfooter and not $self->partial
!ll&&!rl&&r
28160531
$close->{"implicit"} and $self->cfg->addfooter
!ll&&!rl&&r
1472669531
MC/DC66%2/3
$close->{"implicit"} and $self->cfg->addfooter and not $self->partial
$close->{"implicit"} $self->cfg->addfooter not $self->partial
Truth table75%3/4
A and B and not(C)
A= $close->{"implicit"}B= $self->cfg->addfooterC= not $self->partial
ABCresult
0XX0
10X0
1100
1111
412 # add ...
413 531 splice @$lines, $close->{start}->{line}, 0, '...';
414 531 $self->{tree}->fix_lines($close->{start}->{line}, +1);
415 531 $close->{end}->{column} = 3;
416 531 $close->{implicit} = 0;
417 531 DEBUG and say STDERR "$node";
418 }
419 ▶ elsif (not $close->{implicit} and $self->cfg->removefooter and not $self->partial) {
Condition83%5/6
not $close->{"implicit"} and $self->cfg->removefooter and not $self->partial
!ll&&!rl&&r
2789027
not $close->{"implicit"} and $self->cfg->removefooter
!ll&&!rl&&r
266912027
MC/DC66%2/3
not $close->{"implicit"} and $self->cfg->removefooter and not $self->partial
not $close->{"implicit"} $self->cfg->removefooter not $self->partial
Truth table75%3/4
not(A) and B and not(C)
A= not $close->{"implicit"}B= $self->cfg->removefooterC= not $self->partial
ABCresult
0XX0
10X0
1100
1111
420 # remove ...
421 27 my $next = $parent->{children}->[ $node->{index} ];
422 27 ▶ if ($next and ($next->open->{version_directive} or $next->open->{tag_directives})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next and $next->open->{'version_directive'} || $next->open->{'tag_directives'}) { } 15 12 27
Condition66%4/6
$next and $next->open->{"version_directive"} || $next->open->{"tag_directives"}
!ll&&!rl&&r
12015
$next->open->{"version_directive"} or $next->open->{"tag_directives"}
l!l&&r!l&&!r
690
MC/DC0%0/3
$next and $next->open->{"version_directive"} || $next->open->{"tag_directives"}
$next $next->open->{"version_directive"} $next->open->{"tag_directives"}
Truth table25%1/4
A and B or C
A= $nextB= $next->open->{"version_directive"}C= $next->open->{"tag_directives"}
ABCresult
0XX0
1000
11X1
1011
423 }
424 else {
425 12 my $startline = $close->{start}->{line};
426 12 my $line = $lines->[ $startline ];
427 12 ▶ if ($line =~ m/^\.\.\.[ \t]*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\.\.\.[ \t]*$/u) { } 12 0 12
elsif ($line =~ s/^\.\.\.[ \t]+(?=#)//u) { } 0 0 0
428 12 splice @$lines, $startline, 1;
429 12 $self->{tree}->fix_lines($close->{start}->{line}+1, -1);
430 12 $close->{implicit} = 1;
431 }
432 elsif ($line =~ s/^\.\.\.[ \t]+(?=#)//) {
433 0 $lines->[ $startline ] = $line;
434 0 $close->{implicit} = 1;
435 }
436 12 DEBUG and say STDERR "$node";
437 }
438 }
439 }
440  
441 ▶sub _trimspaces($self, $from, $node) {
Subroutine _trimspaces: called CC=7 SCAR=19.5
442 24415 ▶ if ($node->is_collection) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->is_collection) { } 11171 13244 24415
elsif (defined $node->{'style'}) { } 12988 256 13244
443 11171 my $level = $node->{level};
444 11171 for my $c (@{ $node->{children} }) {
445 21559 $self->_trimspaces($from, $c);
446 }
447 11171 ▶ if ($level == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == -1) 6017 5154 11171
448 6017 $self->_trim($$from, $node->end->{line});
449 }
450 }
451 elsif (defined $node->{style}) {
452 # Only spaces in block scalars must be left alone
453 12988 ▶ if ($node->{style} eq YAML_LITERAL_SCALAR_STYLE
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"style"} eq 4 or $node->{"style"} eq 5) 847 12141 12988
Condition100%3/3
$node->{"style"} eq 4 or $node->{"style"} eq 5
l!l&&r!l&&!r
47737012141
MC/DC100%2/2
$node->{"style"} eq 4 or $node->{"style"} eq 5
$node->{"style"} eq 4 $node->{"style"} eq 5
Truth table100%3/3
A or B
A= $node->{"style"} eq 4B= $node->{"style"} eq 5
ABresult
1X1
011
000
454 or $node->{style} eq YAML_FOLDED_SCALAR_STYLE) {
455 847 my ($anchor, $tag, $comments, $scalar) = $self->_find_scalar_start($node);
456 847 $self->_trim($$from, $scalar->[0]);
457 847 $$from = $node->end->{line};
458 }
459 }
460 }
461  
462 3996 ▶sub _process_flow($self, $parent, $node, $block_indent = undef) {
Subroutine _process_flow: called CC=13 SCAR=25.7
463 3996 ▶ return unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 3996 3996
464 3996 my $level = $node->{level};
465 3996 ▶ my $flow = $node->{flow} || 0;
Condition50%1/2
$node->{'flow'} || 0
l!l
39960
MC/DC0%0/1
$node->{'flow'} || 0
$node->{'flow'}
Truth table50%1/2
A
A= $node->{'flow'}
Aresult
11
00
466 3996 ▶ $block_indent //= $parent->indent + $self->cfg->indent;
Condition66%2/3
$block_indent //= $parent->indent + $self->cfg->indent
l!l&&r!l&&!r
30219750
MC/DC0%0/2
$block_indent //= $parent->indent + $self->cfg->indent
$block_indent $parent->indent + $self->cfg->indent
Truth table66%2/3
A //= B
A= $block_indentB= $parent->indent + $self->cfg->indent
ABresult
1X1
011
000
467 3996 ▶ $block_indent = 0 if $level == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $level == 0 226 3770 3996
468  
469 3996 ▶ unless ($node->is_collection) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->is_collection) 2591 1405 3996
470 2591 $self->_process_flow_scalar($parent, $node, $block_indent);
471 2591 return;
472 }
473 1405 ▶ if ($parent->{type} eq 'MAP' and $node->{index} % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->{"type"} eq "MAP" and $node->{"index"} % 2) 97 1308 1405
Condition100%3/3
$parent->{"type"} eq "MAP" and $node->{"index"} % 2
!ll&&!rl&&r
102428497
MC/DC100%2/2
$parent->{"type"} eq "MAP" and $node->{"index"} % 2
$parent->{"type"} eq "MAP" $node->{"index"} % 2
Truth table100%3/3
A and B
A= $parent->{"type"} eq "MAP"B= $node->{"index"} % 2
ABresult
0X0
100
111
474 97 return;
475 }
476 1308 my $lines = $self->{lines};
477 1308 my $startline = $node->start->{line};
478 1308 my $end = $node->end;
479 1308 my $endline = $end->{line};
480  
481 1308 my $before = substr($lines->[ $startline ], 0, $node->start->{column});
482 1308 ▶ if ($before =~ tr/ \t//c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before =~ tr/\t //c) 919 389 1308
483 919 $startline++;
484 }
485 1308 my @lines = ($startline .. $node->open->{end}->{line});
486 1308 my $before_end = substr($lines->[ $endline ], 0, $end->{column} - 1);
487 1308 ▶ unless ($before_end =~ tr/ \t//c) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($before_end =~ tr/\t //c) 294 1014 1308
488 294 push @lines, $endline;
489 }
490 1308 for my $i (@lines) {
491 699 my $new_spaces = ' ' x $block_indent;
492 699 $lines->[ $i ] =~ s/^([ \t]*)/$new_spaces/;
493 699 my $old = length $1;
494 699 $node->_fix_flow_indent(line => $i, diff => $block_indent - $old);
495 }
496  
497 1308 for my $c (@{ $node->{children} }) {
498 3021 $self->_process_flow($node, $c, $block_indent + $self->cfg->indent);
499 }
500 }
501  
502 ▶sub _process_flow_scalar($self, $parent, $node, $block_indent) {
Subroutine _process_flow_scalar: called CC=7 SCAR=19.5
503 2591 ▶ if ($node->empty_scalar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->empty_scalar) 128 2463 2591
504 128 return;
505 }
506 2463 my $startline = $node->line;
507 2463 my $lines = $self->{lines};
508 2463 my $line = $lines->[ $startline ];
509 2463 my $col = $node->start->{column};
510 2463 my $before = substr($line, 0, $col);
511 2463 ▶ if ($before =~ tr/ \t//c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before =~ tr/\t //c) 2101 362 2463
512 2101 $startline++;
513 }
514 2463 my $endline = $node->end->{line};
515 2463 for my $i ($startline .. $endline) {
516 466 my $line = $lines->[ $i ];
517 466 my $new_spaces = ' ' x $block_indent;
518 466 $line =~ s/^([ \t]*)/$new_spaces/;
519 466 my $old = length $1;
520 466 ▶ if ($block_indent != $old) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_indent != $old) 415 51 466
521 415 $self->{tree}->_fix_flow_indent(line => $i, diff => $block_indent - $old);
522 }
523 466 $lines->[ $i ] = $line;
524 }
525 2463 ▶ if (not $node->multiline) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $node->multiline) 2359 104 2463
526 2359 $self->_check_adjacency($node, $parent);
527 2359 ▶ if ($node->{name} eq 'scalar_event') {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"name"} eq "scalar_event") 2350 9 2359
528 2350 $self->_replace_quoting($node);
529 }
530 }
531 }
532  
533 ▶sub _check_adjacency($self, $node, $parent) {
Subroutine _check_adjacency: called CC=10 SCAR=23.1
534 2359 ▶ return unless $node->{flow};
Branch: 1 branch
BranchTrueFalseTotal
unless $node->{"flow"} 0 2359 2359
535 2359 ▶ return unless $node->is_mapping_value($parent);
Branch: 1 branch
BranchTrueFalseTotal
unless $node->is_mapping_value($parent) 1610 749 2359
536 # allowed: "foo":bar, "foo":*alias
537 # not allowed: foo:bar, foo:*alias
538 749 my $prev = $parent->sibling($node, -1);
539 749 my $tidy_adjacency = $self->cfg->adjacency;
540 749 ▶ if (not $prev->is_collection and not $prev->is_quoted) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prev->is_collection or $prev->is_quoted) 527 222 749
Condition100%3/3
$prev->is_collection or $prev->is_quoted
l!l&&r!l&&!r
42180527
MC/DC100%2/2
$prev->is_collection or $prev->is_quoted
$prev->is_collection $prev->is_quoted
Truth table100%3/3
A or B
A= $prev->is_collectionB= $prev->is_quoted
ABresult
1X1
011
000
541 527 $tidy_adjacency = 0; # adjacency would be invalid here
542 }
543 749 my $start = $node->open;
544 749 my $adjacent = 0;
545 749 my $line = $self->{lines}->[ $start->{line} ];
546 749 ▶ if ($start->{column} > 0 and substr($line, $start->{column} - 1, 1) eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($start->{"column"} > 0 and substr($line, $start->{"column"} - 1, 1) eq ":") 56 693 749
Condition66%2/3
$start->{"column"} > 0 and substr($line, $start->{"column"} - 1, 1) eq ":"
!ll&&!rl&&r
069356
MC/DC50%1/2
$start->{"column"} > 0 and substr($line, $start->{"column"} - 1, 1) eq ":"
$start->{"column"} > 0 substr($line, $start->{"column"} - 1, 1) eq ":"
Truth table66%2/3
A and B
A= $start->{"column"} > 0B= substr($line, $start->{"column"} - 1, 1) eq ":"
ABresult
0X0
100
111
547 56 $adjacent = 1;
548 }
549 749 ▶ return unless defined $tidy_adjacency; # keep as is
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tidy_adjacency 222 527 749
550 527 ▶ if ($tidy_adjacency) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tidy_adjacency) 0 527 527
551 0 die "Not implemented yet: enforce adjacency";
552 }
553 527 ▶ return unless $adjacent;
Branch: 1 branch
BranchTrueFalseTotal
unless $adjacent 525 2 527
554 2 substr($line, $start->{column}, 0, ' ');
555 2 $self->{tree}->_move_columns($start->{line}, $start->{column} + 1, +1);
556 2 $self->{lines}->[ $start->{line} ] = $line;
557 }
558  
559 ▶sub _find_scalar_start($self, $node) {
Subroutine _find_scalar_start: called CC=12 SCAR=25.8
560 # warn __PACKAGE__.':'.__LINE__.": ========= _find_scalar_start $node\n";
561 7087 my $lines = $self->{lines};
562 7087 my $from = $node->line;
563 7087 my $to = $node->realendline;
564 7087 my $col = $node->indent;
565 7087 my $end = $node->end;
566 7087 my $endcol = $end->{column};
567 7087 my @slice = @$lines[ $from .. $to ];
568 7087 my $anchor;
569 my $tag;
570 7087 my @comments;
571 7087 my $start;
572 7087 my $scalar;
573 7087 for my $i (0 .. $#slice) {
574 7253 my $line = $slice[ $i ];
575 7253 ▶ my $f = $i == 0 ? $col : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i == 0 ? : 7087 166 7253
576 7253 ▶ my $t = $i == $#slice ? ($endcol || length($line)) : length($line);
Branch: 1 branch
BranchTrueFalseTotal
$i == $#slice ? : 4690 2563 7253
Condition33%1/3
$endcol or length $line
l!l&&r!l&&!r
469000
MC/DC0%0/2
$endcol or length $line
$endcol length $line
Truth table33%1/3
A or B
A= $endcolB= length $line
ABresult
1X1
011
000
577 7253 my $part = substr($line, $f, $t - $f);
578 7253 ▶ if ($part =~ m/^ *(\#.*)$/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /^ *(\#.*)$/gu) 0 7253 7253
579 0 my $comment = $1;
580 0 my $pos1 = length($line) - length($comment);
581 0 push @comments, [$i + $from, $pos1, $comment];
582 0 next;
583 }
584 7253 my $cur;
585 7253 ▶ while ($part =~ m/\G\s*([&!])(\S*)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /\G\s*([&!])(\S*)/gu) 1354 7253 8607
586 1354 my $type = $1;
587 1354 my $name = $2;
588 1354 $cur = pos $part;
589 1354 my $pos = $cur - 1;
590 1354 my $pos1 = $pos - length $name;
591 1354 my $prop = substr($part, $pos1, 1+ length $name);
592 1354 ▶ if ($type eq '&') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq '&') { } 501 853 1354
elsif ($type eq '!') { } 853 0 853
593 501 $anchor = [$i + $from, $pos1 + $f, $prop];
594 }
595 elsif ($type eq '!') {
596 853 $tag = [$i + $from, $pos1 + $f, $prop];
597 }
598 }
599 7253 pos($part) = $cur;
600 7253 ▶ if ($part =~ m/\G *(\#.*)$/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /\G *(\#.*)$/gu) 0 7253 7253
601 0 my $comment = $1;
602 0 $cur = pos $part;
603 0 my $pos1 = length($line) - length($comment);
604 0 push @comments, [$i + $from, $pos1, $comment];
605 0 next;
606 }
607 7253 pos($part) = $cur;
608 7253 ▶ if ($part =~ m/\G *(\S)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /\G *(\S)/gu) 6911 342 7253
609 6911 $scalar = $1;
610 6911 my $pos1 = (pos $part) - 1;
611 6911 $scalar = [$i + $from, $pos1 + $f, $scalar];
612 6911 last;
613 }
614 }
615 7087 ▶ $scalar ||= [$to, length($slice[ -1 ]), ''];
Condition100%2/2
$scalar ||= [$to, length $slice[-1], ""]
l!l
6911176
MC/DC100%1/1
$scalar ||= [$to, length $slice[-1], ""]
$scalar
Truth table100%2/2
A
A= $scalar
Aresult
11
00
616 # warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$scalar], ['scalar']);
617 7087 return ($anchor, $tag, \@comments, $scalar);
618 }
619  
620 ▶sub _trim($self, $from, $to) {
Subroutine _trim: called CC=2 SCAR=6.9
621 6932 my $lines = $self->{lines};
622 6932 for my $line (@$lines[ $from .. $to ]) {
623 32434 $line =~ s/[\t ]+$//;
624 }
625 }
626  
627 ▶sub _fix_indent($self, $node, $fix, $offset) {
Subroutine _fix_indent: called CC=9 SCAR=22.5
628 588 ▶ $offset ||= 0;
Condition100%2/2
$offset ||= 0
l!l
54741
MC/DC100%1/1
$offset ||= 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
629 588 my $startline = $node->line;
630 588 my $lines = $self->{lines};
631 588 my $endline = $node->realendline;
632 588 my @slice = @$lines[$startline .. $endline];
633 588 for my $line (@slice) {
634 1449 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 19 1430 1449
635 1430 ▶ if ($fix < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fix < 0) { } 953 477 1430
636 953 my $offset = $offset;
637 953 my $fix = -$fix;
638 953 ▶ if ($offset > length $line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset > length $line) 0 953 953
639 0 $offset = -1 + length $line;
640 }
641 953 ▶ if ($line =~ tr/ //c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ tr/ //c) { } 953 0 953
642 953 ▶ if ($line =~ m/^ *\#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^ *\#/u) 18 935 953
643 18 $line =~ s/ {1,$fix}//;
644 18 next;
645 }
646 }
647 else {
648 0 $line =~ s/ {1,$fix}//;
649 0 next;
650 }
651 935 my $before = substr($line, 0, $offset);
652 935 $before =~ s/ {$fix,$fix}$//;
653 935 $line = $before . substr($line, $offset);
654 }
655 else {
656 477 ▶ unless ($line =~ tr/ //c) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ tr/ //c) 0 477 477
657 0 next;
658 }
659 477 substr($line, $offset, 0, ' ' x $fix);
660 }
661 }
662 588 @$lines[$startline .. $endline] = @slice;
663 }
664  
665 ▶sub _tree($self, $yaml, $lines) {
Subroutine _tree: called CC=18 SCAR=28.9
666 3037 my $events = $self->_parse($yaml);
667 3037 $self->{events} = $events;
668 3037 my $first = shift @$events;
669 3037 my $end = pop @$events;
670 3037 $_->{level} = -1 for ($first, $end);
671 3037 $first->{id} = -1;
672 3037 _pp($first) if DEBUG;
673 3037 my @stack;
674  
675 3037 my $level = -1;
676 3037 my $docs = YAML::Tidy::Node::Collection->new(
677 type => 'STR',
678 children => [],
679 indent => -1,
680 line => 0,
681 level => $level,
682 start => YAML::Tidy::Node::Collection->new(%$first),
683 end => YAML::Tidy::Node::Collection->new(%$end),
684 );
685 3037 my $ref = $docs;
686 3037 my $id = 0;
687 3037 my $flow = 0;
688 3037 for my $i (0 .. $#$events) {
689 31507 my $event = $events->[ $i ];
690 31507 my $name = $event->{name};
691 31507 $id++;
692  
693 31507 my $type;
694 31507 ▶ if ($name =~ m/document_start/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($name =~ /document_start/u) { } 3347 28160 31507
elsif ($name =~ /sequence_start/u) { } 2203 25957 28160
elsif ($name =~ /mapping_start/u) { } 3224 22733 25957
695 3347 $type = 'DOC';
696 }
697 elsif ($name =~ m/sequence_start/) {
698 2203 $type = 'SEQ';
699 }
700 elsif ($name =~ m/mapping_start/) {
701 3224 $type = 'MAP';
702 }
703  
704 31507 $event->{id} = $id;
705 31507 ▶ if ($name =~ m/_start_event/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /_start_event/u) { } 8774 22733 31507
elsif ($name =~ /_end_event/u) { } 8774 13959 22733
706 8774 $event->{level} = $level;
707 8774 ▶ if ($name eq 'sequence_start_event') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "sequence_start_event") 2203 6571 8774
708 # inconsistency in libyaml events?
709 2203 my $col = $event->{end}->{column};
710 2203 ▶ if ($col > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col > 0) 1469 734 2203
711 1469 my $line = $lines->[ $event->{end}->{line} ];
712 1469 my $chr = substr($line, $col - 1, 1);
713 1469 ▶ if ($chr eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($chr eq "-") 121 1348 1469
714 121 $event->{end}->{column}--;
715 }
716 }
717 }
718 8774 ▶ if ($flow or ($event->{style} // -1) == YAML_FLOW_SEQUENCE_STYLE
Branch: 1 branch
BranchTrueFalseTotal
if ($flow or ($event->{"style"} // -1) == 2 or ($event->{"style"} // -1) == 2) 975 7305 8280
Condition90%9/10
$flow or ($event->{"style"} // -1) == 2 or ($event->{"style"} // -1) == 2
l!l&&r!l&&!r
146907305
$flow or ($event->{"style"} // -1) == 2
l!l&&r!l&&!r
4949757305
$event->{"style"} // -1
l!l
49333347
$event->{"style"} // -1
l!l
39583347
MC/DC66%2/3
$flow or ($event->{"style"} // -1) == 2 or ($event->{"style"} // -1) == 2
$flow $event->{"style"} $event->{"style"}
Truth table75%3/4
A or B or C
A= $flowB= $event->{"style"}C= $event->{"style"}
ABCresult
1XX1
01X1
0011
0000
719 or ($event->{style} // -1) == YAML_FLOW_MAPPING_STYLE) {
720 1469 $flow++;
721 }
722 8774 my $node = YAML::Tidy::Node::Collection->new(
723 children => [],
724 type => $type,
725 level => $level,
726 start => $event,
727 flow => $flow,
728 );
729 8774 push @{ $ref->{children} }, $node;
730 8774 $ref->{elements}++;
731 8774 $node->{index} = $ref->{elements};
732 8774 push @stack, $ref;
733 8774 $ref = $node;
734 8774 $level++;
735 }
736 elsif ($name =~ m/_end_event/) {
737 8774 my $last = pop @stack;
738  
739 8774 $ref->{end} = $event;
740  
741 8774 $ref = $last;
742  
743 8774 $level--;
744 8774 $event->{level} = $level;
745 8774 ▶ $flow-- if $flow;
Branch: 1 branch
BranchTrueFalseTotal
if $flow 1469 7305 8774
746 }
747 else {
748 13959 $event = YAML::Tidy::Node::Scalar->new(%$event, flow => $flow);
749 13959 $ref->{elements}++;
750 13959 $event->{index} = $ref->{elements};
751 13959 $event->{level} = $level;
752 13959 push @{ $ref->{children} }, $event;
753 }
754 31507 $event->{nextline} = -1;
755 31507 ▶ if ($i < $#$events) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $events->$#*) 28509 2998 31507
756 28509 my $next = $events->[ $i + 1 ];
757 28509 my $nextline = $next->{start}->{line};
758 28509 $event->{nextline} = $nextline;
759 }
760 31507 _pp($event) if DEBUG;
761 }
762 3037 $end->{id} = $id + 1;
763 3037 _pp($end) if DEBUG;
764 3037 $self->{tree} = $docs;
765 3037 return $docs;
766 }
767  
768 ▶sub _parse($self, $yaml) {
Subroutine _parse: called CC=1 SCAR=0.0
769 6071 my @events;
770 6071 YAML::LibYAML::API::XS::parse_events($yaml, \@events);
771 6067 return \@events;
772 }
773  
774 ▶sub _pp($event) {
Subroutine _pp: not called CC=3 SCAR=24.8
775 0 my $name = $event->{name};
776 0 my $level = $event->{level};
777 0 $name =~ s/_event$//;
778 0 my $fmt = '%2d %-10s) <L %2d C %2d> <L %2d C %2d> %-14s';
779 0 my $indent = $level*2+2;
780 0 my $lstr = (' ' x $indent) . $level;
781 my @args = (
782 $event->{id}, $lstr,
783 $event->{start}->{line}, $event->{start}->{column},
784 $event->{end}->{line}, $event->{end}->{column},
785 0 $name,
786 );
787 0 ▶ if ($name =~ m/scalar|alias/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /scalar|alias/u) { } 0 0 0
elsif ($name =~ /end/u) { } 0 0 0
788 0 local $Data::Dumper::Useqq = 1;
789 0 my $str = Data::Dumper->Dump([$event->{value}], ['value']);
790 0 chomp $str;
791 0 $str =~ s/^\$value = //;
792 0 $fmt .= " %s";
793 0 push @args, $str;
794 }
795 elsif ($name =~ m/end/) {
796 }
797 else {
798 }
799 0 $fmt .= "\n";
800 0 printf $fmt, @args;
801 }
802  
803 ▶sub _debug_lines($self) {
Subroutine _debug_lines: not called CC=2 SCAR=17.9
804 0 say "====================================";
805 0 say for @{ $self->{lines} };
806 0 say "====================================";
807 }
808  
809 6068 ▶sub highlight($self, $yaml, $type = 'ansi') {
Subroutine highlight: called CC=6 SCAR=19.4
Pod documented
810 6068 my ($error, $tokens) = YAML::PP::Parser->yaml_to_tokens(string => $yaml);
811 6068 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 213 5855 6068
812 213 $tokens = [];
813 213 my @lines = split m/(?<=\n)/, $yaml;
814 213 for my $line (@lines) {
815 641 ▶ if ($line =~ s/( +\n)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/( +\n)//u) 0 641 641
816 0 push @$tokens, { value => $line, name => 'PLAIN' };
817 0 push @$tokens, { value => $1, name => 'TRAILING_SPACE' };
818 0 next;
819 }
820 641 push @$tokens, { value => $line, name => 'PLAIN' };
821 }
822 }
823 6068 ▶ if ($type eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "html") 0 6068 6068
824 0 return YAML::PP::Highlight->htmlcolored($tokens);
825 }
826 6068 return YAML::PP::Highlight->ansicolored($tokens);
827 }
828  
829 7 1;
830  
831 __END__
↑ Summary Tidy/Config.pm »