blib/lib/TAP/Parser/YAMLish/Reader.pm

filter: Statementstmt 94.1% 144 / 153 Branchbran 84.2% 64 / 76 Conditioncond 81.5% 31 / 38 MC/DCmcdc 57.8% 11 / 19 Subroutinesub 93.7% 15 / 16 Podpod 100% 2 / 2 totaltotal 87.8% 267 / 304 CC 53 SCAR 20.4
CC
53
Cov
90%
_read_block_scalar
13.0
_read_array
11.1
_read_hash
9.1
CRAP 7.7
« blib/lib/TAP/Parser/Scheduler.pm ↑ Summary blib/lib/TAP/Parser/SourceHandler/Perl.pm »
1 package TAP::Parser::YAMLish::Reader;
2  
3 37 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 37 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 37 ▶use base 'TAP::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 37 our $VERSION = '3.52';
9  
10 # No EBCDIC support on early perls
11 *to_native = (ord "A" == 65 || $] < 5.008)
12 26 ▶ ? sub { return shift }
Subroutine __ANON__: called CC=1 SCAR=0.0
13 0 ▶ : sub { utf8::unicode_to_native(shift) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
14  
15 # TODO:
16 # Handle blessed object syntax
17  
18 # Printable characters for escapes
19 37 my %UNESCAPES = (
20 z => "\x00", a => "\a", t => "\t",
21 n => "\n", v => "\cK", f => "\f",
22 r => "\r", e => "\e", '\\' => '\\',
23 );
24  
25 37 my $QQ_STRING = qr{ " (?:\\. | [^"])* " }x;
26 37 my $HASH_LINE = qr{ ^ ($QQ_STRING|\S+) \s* : \s* (?: (.+?) \s* )? $ }x;
27 37 my $IS_HASH_KEY = qr{ ^ [\w\'\"] }x;
28 37 my $IS_END_YAML = qr{ ^ \.\.\. \s* $ }x;
29 37 my $IS_QQ_STRING = qr{ ^ $QQ_STRING $ }x;
30 37 my $IS_ARRAY_LINE = qr{ ^ - \s* ($QQ_STRING|\S+) }x;
31  
32 # new() implementation supplied by TAP::Object
33  
34 82 ▶sub read {
Subroutine read: called CC=5 SCAR=16.1
Pod documented
35 82 my $self = shift;
36 82 my $obj = shift;
37  
38 82 ▶ die "Must have a code reference to read input from"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj eq "CODE" 0 82 82
39 unless ref $obj eq 'CODE';
40  
41 82 $self->{reader} = $obj;
42 82 $self->{capture} = [];
43  
44 # Prime the reader
45 82 $self->_next;
46 82 ▶ return unless $self->{next};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"next"} 1 81 82
47  
48 81 my $doc = $self->_read;
49  
50 # The terminator is mandatory otherwise we'd consume a line from the
51 # iterator that doesn't belong to us. If we want to remove this
52 # restriction we'll have to implement look-ahead in the iterators.
53 # Which might not be a bad idea.
54 77 my $dots = $self->_peek;
55 77 ▶ die "Missing '...' at end of YAMLish"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dots and $dots =~ /$IS_END_YAML/ 3 74 77
Condition66%2/3
defined $dots and $dots =~ /$IS_END_YAML/
!ll&&!rl&&r
0374
MC/DC50%1/2
defined $dots and $dots =~ /$IS_END_YAML/
defined $dots $dots =~ /$IS_END_YAML/
Truth table66%2/3
A and B
A= defined $dotsB= $dots =~ /$IS_END_YAML/
ABresult
0X0
100
111
56 unless defined $dots
57 and $dots =~ $IS_END_YAML;
58  
59 74 delete $self->{reader};
60 74 delete $self->{next};
61  
62 74 return $doc;
63 }
64  
65 65 ▶sub get_raw { join( "\n", grep defined, @{ shift->{capture} || [] } ) . "\n" }
Subroutine get_raw: called CC=2 SCAR=7.2
Pod documented
Condition50%1/2
+(shift())->{'capture'} || []
l!l
650
MC/DC0%0/1
+(shift())->{'capture'} || []
+(shift())->{'capture'}
Truth table50%1/2
A
A= +(shift())->{'capture'}
Aresult
11
00
66  
67 663 ▶sub _peek {
Subroutine _peek: called CC=2 SCAR=6.9
68 663 my $self = shift;
69 663 ▶ return $self->{next} unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 158 505 663
70 505 my $line = $self->{next};
71 505 $line =~ /^ (\s*) (.*) $ /x;
72 505 return ( $2, length $1 );
73 }
74  
75 405 ▶sub _next {
Subroutine _next: called CC=2 SCAR=7.0
76 405 my $self = shift;
77 die "_next called with no reader"
78 405 ▶ unless $self->{reader};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"reader"} 0 405 405
79 405 my $line = $self->{reader}->();
80 405 $self->{next} = $line;
81 405 push @{ $self->{capture} }, $line;
82 }
83  
84 81 ▶sub _read {
Subroutine _read: called CC=6 SCAR=18.0
85 81 my $self = shift;
86  
87 81 my $line = $self->_peek;
88  
89 # Do we have a document header?
90 81 ▶ if ( $line =~ /^ --- (?: \s* (.+?)? \s* )? $/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^ --- (?: \s* (.+?)? \s* )? $/x) { } 79 2 81
91 79 $self->_next;
92  
93 79 ▶ return $self->_read_scalar($1) if defined $1; # Inline?
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 30 49 79
94  
95 49 my ( $next, $indent ) = $self->_peek;
96  
97 49 ▶ if ( $next =~ /^ - /x ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($next =~ /^ - /x) { } 22 27 49
elsif ($next =~ /$IS_HASH_KEY/) { } 26 1 27
elsif ($next =~ /$IS_END_YAML/) { } 1 0 1
98 22 return $self->_read_array($indent);
99 }
100 elsif ( $next =~ $IS_HASH_KEY ) {
101 26 return $self->_read_hash( $next, $indent );
102 }
103 elsif ( $next =~ $IS_END_YAML ) {
104 1 die "Premature end of YAMLish";
105 }
106 else {
107 0 die "Unsupported YAMLish syntax: '$next'";
108 }
109 }
110 else {
111 2 die "YAMLish document header not found";
112 }
113 }
114  
115 # Parse a double quoted string
116 16 ▶sub _read_qq {
Subroutine _read_qq: called CC=3 SCAR=11.1
117 16 my $self = shift;
118 16 my $str = shift;
119  
120 16 ▶ unless ( $str =~ s/^ " (.*?) " $/$1/x ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ s/^ " (.*?) " $/$1/x) 0 16 16
121 0 die "Internal: not a quoted string";
122 }
123  
124 16 $str =~ s/\\"/"/gx;
125 16 $str =~ s/ \\ ( [tartan\\favez] | x([0-9a-fA-F]{2}) )
126 / (length($1) > 1) ? pack("H2", to_native($2))
127 42 ▶ : $UNESCAPES{$1} /gex;
Branch: 1 branch
BranchTrueFalseTotal
length $1 > 1 ? : 26 16 42
128 16 return $str;
129 }
130  
131 # Parse a scalar string to the actual scalar
132 330 ▶sub _read_scalar {
Subroutine _read_scalar: called CC=8 SCAR=20.8
133 330 my $self = shift;
134 330 my $string = shift;
135  
136 330 ▶ return undef if $string eq '~';
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "~" 14 316 330
137 316 ▶ return {} if $string eq '{}';
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "{}" 2 314 316
138 314 ▶ return [] if $string eq '[]';
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "[]" 2 312 314
139  
140 312 ▶ if ( $string =~ /^([>|])([+-]?)([1-9]?)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^([>|])([+-]?)([1-9]?)$/) 12 300 312
141 12 my ( $style, $chomping, $indent_base ) = ( $1, $2, $3 );
142 12 return $self->_read_block_scalar( $style, $chomping, $indent_base );
143 }
144  
145 300 ▶ if ( $string =~ /^ ' (.*) ' $/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^ ' (.*) ' $/x) 12 288 300
146 12 ( my $rv = $1 ) =~ s/''/'/g;
147 12 return $rv;
148 }
149  
150 288 ▶ if ( $string =~ $IS_QQ_STRING ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$IS_QQ_STRING/) 16 272 288
151 16 return $self->_read_qq($string);
152 }
153  
154 272 ▶ if ( $string =~ /^['"]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^['"]/) 0 272 272
155  
156 # A quote with folding... we don't support that
157 0 die __PACKAGE__ . " does not support multi-line quoted scalars";
158 }
159  
160 # Regular unquoted string
161 272 return $string;
162 }
163  
164 12 ▶sub _read_block_scalar {
Subroutine _read_block_scalar: called CC=13 SCAR=25.7
165 12 my ( $self, $style, $chomping, $indent_base ) = @_;
166  
167 12 my ( $line, $line_indent ) = $self->_peek;
168 12 ▶ die "Multi-line scalar content missing" unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 12 12
169  
170 12 ▶ $indent_base ||= $line_indent;
Condition66%2/3
$indent_base ||= $line_indent
l!l&&r!l&&!r
2100
MC/DC0%0/2
$indent_base ||= $line_indent
$indent_base $line_indent
Truth table66%2/3
A ||= B
A= $indent_baseB= $line_indent
ABresult
1X1
011
000
171  
172 12 my $pad = ' ' x ( $line_indent - $indent_base );
173 12 my @multi_lines = [ $pad, $line ];
174  
175 12 while (1) {
176 33 $self->_next;
177 33 my ( $content, $line_indent ) = $self->_peek;
178 33 ▶ last if $line_indent < $indent_base;
Branch: 1 branch
BranchTrueFalseTotal
if $line_indent < $indent_base 12 21 33
179  
180 21 my $pad = ' ' x ( $line_indent - $indent_base );
181 21 push @multi_lines, [ $pad, $content ];
182 }
183  
184 12 my $block = '';
185 12 my $previous_line;
186  
187 12 for my $current_line ( @multi_lines ) {
188 33 my ( $pad, $content ) = @$current_line;
189 33 ▶ unless ( defined $previous_line ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $previous_line) 12 21 33
190 12 $block .= join( '', $pad, $content );
191 12 $previous_line = $current_line;
192 12 next;
193 }
194  
195 21 ▶ if ( $style eq '>'
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq '>' and length $content > 0 and length $pad == 0 and length $previous_line->[0] == 0 and length $previous_line->[1] > 0) { } 6 15 21
Condition91%11/12
$style eq ">" and length $content > 0 and length $pad == 0 and length $previous_line->[0] == 0 and length $previous_line->[1] > 0
!ll&&!rl&&r
1416
$style eq ">" and length $content > 0 and length $pad == 0 and length $previous_line->[0] == 0
!ll&&!rl&&r
1407
$style eq ">" and length $content > 0 and length $pad == 0
!ll&&!rl&&r
1137
$style eq ">" and length $content > 0
!ll&&!rl&&r
7410
MC/DC80%4/5
$style eq ">" and length $content > 0 and length $pad == 0 and length $previous_line->[0] == 0 and length $previous_line->[1] > 0
$style eq ">" length $content > 0 length $pad == 0 length $previous_line->[0] == 0 length $previous_line->[1] > 0
Truth table83%5/6
A and B and C and D and E
A= $style eq ">"B= length $content > 0C= length $pad == 0D= length $previous_line->[0] == 0E= length $previous_line->[1] > 0
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
196 && length $content > 0
197 && length $pad == 0
198 && length $previous_line->[0] == 0
199 && length $previous_line->[1] > 0 )
200 {
201 6 $block .= ' ' . $content;
202 } else {
203 15 $block .= join( '', "\n", $pad, $content );
204 }
205 21 $previous_line = $current_line;
206 }
207  
208 12 $block .= "\n";
209  
210 12 ▶ if ( $chomping eq '-' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chomping eq '-') { } 3 9 12
elsif ($chomping eq '+') { } 2 7 9
211 3 $block =~ s/\n+$//;
212 } elsif ( $chomping eq '+' ) {
213 # noop: keep newlines
214 } else {
215 7 $block =~ s/\n+$/\n/;
216 }
217 12 return $block;
218 }
219  
220 33 ▶sub _read_nested {
Subroutine _read_nested: called CC=3 SCAR=11.2
221 33 my $self = shift;
222  
223 33 my ( $line, $indent ) = $self->_peek;
224  
225 33 ▶ if ( $line =~ /^ -/x ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^ -/x) { } 13 20 33
elsif ($line =~ /$IS_HASH_KEY/) { } 20 0 20
226 13 return $self->_read_array($indent);
227 }
228 elsif ( $line =~ $IS_HASH_KEY ) {
229 20 return $self->_read_hash( $line, $indent );
230 }
231 else {
232 0 die "Unsupported YAMLish syntax: '$line'";
233 }
234 }
235  
236 # Parse an array
237 35 ▶sub _read_array {
Subroutine _read_array: called CC=10 SCAR=24.1
238 35 my ( $self, $limit ) = @_;
239  
240 35 my $ar = [];
241  
242 35 while (1) {
243 130 my ( $line, $indent ) = $self->_peek;
244 last
245 130 ▶ if $indent < $limit
Branch: 1 branch
BranchTrueFalseTotal
if $indent < $limit or not defined $line or $line =~ /$IS_END_YAML/ 34 96 130
Condition83%5/6
$indent < $limit or not defined $line or $line =~ /$IS_END_YAML/
l!l&&r!l&&!r
112396
$indent < $limit or not defined $line
l!l&&r!l&&!r
110119
MC/DC66%2/3
$indent < $limit or not defined $line or $line =~ /$IS_END_YAML/
$indent < $limit not defined $line $line =~ /$IS_END_YAML/
Truth table75%3/4
A or not(B) or C
A= $indent < $limitB= not defined $lineC= $line =~ /$IS_END_YAML/
ABCresult
1XX1
01X1
0011
0000
246 || !defined $line
247 || $line =~ $IS_END_YAML;
248  
249 96 ▶ if ( $indent > $limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent > $limit) 0 96 96
250 0 die "Array line over-indented";
251 }
252  
253 96 ▶ if ( $line =~ /^ (- \s+) \S+ \s* : (?: \s+ | $ ) /x ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($line =~ /^ (- \s+) \S+ \s* : (?: \s+ | $ ) /x) { } 8 88 96
elsif ($line =~ /^ - \s* (.+?) \s* $/x) { } 77 11 88
elsif ($line =~ /^ - \s* $/x) { } 11 0 11
elsif ($line =~ /$IS_HASH_KEY/) { } 0 0 0
254 8 $indent += length $1;
255 8 $line =~ s/-\s+//;
256 8 push @$ar, $self->_read_hash( $line, $indent );
257 }
258 elsif ( $line =~ /^ - \s* (.+?) \s* $/x ) {
259 77 ▶ die "Unexpected start of YAMLish" if $line =~ /^---/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^---/ 1 76 77
260 76 $self->_next;
261 76 push @$ar, $self->_read_scalar($1);
262 }
263 elsif ( $line =~ /^ - \s* $/x ) {
264 11 $self->_next;
265 11 push @$ar, $self->_read_nested;
266 }
267 elsif ( $line =~ $IS_HASH_KEY ) {
268 0 $self->_next;
269 0 push @$ar, $self->_read_hash( $line, $indent, );
270 }
271 else {
272 0 die "Unsupported YAMLish syntax: '$line'";
273 }
274 }
275  
276 34 return $ar;
277 }
278  
279 54 ▶sub _read_hash {
Subroutine _read_hash: called CC=9 SCAR=22.0
280 54 my ( $self, $line, $limit ) = @_;
281  
282 54 my $indent;
283 54 my $hash = {};
284  
285 54 while (1) {
286 124 ▶ die "Badly formed hash line: '$line'"
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /$HASH_LINE/ 0 124 124
287 unless $line =~ $HASH_LINE;
288  
289 124 my ( $key, $value ) = ( $self->_read_scalar($1), $2 );
290 124 $self->_next;
291  
292 124 my ( $next_line, $next_indent ) = $self->_peek;
293  
294 124 ▶ if ( defined $value ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value) { } 100 24 124
elsif (not defined $value and $next_indent <= $limit and not $next_line =~ /$IS_ARRAY_LINE/) { } 2 22 24
295 100 $hash->{$key} = $self->_read_scalar($value);
296 }
297 ▶ elsif (not defined $value # no explicit undef ("~") given
Condition83%5/6
not defined $value and $next_indent <= $limit and not $next_line =~ /$IS_ARRAY_LINE/
!ll&&!rl&&r
2022
not defined $value and $next_indent <= $limit
!ll&&!rl&&r
0204
MC/DC66%2/3
not defined $value and $next_indent <= $limit and not $next_line =~ /$IS_ARRAY_LINE/
not defined $value $next_indent <= $limit not $next_line =~ /$IS_ARRAY_LINE/
Truth table75%3/4
not(A) and B and not(C)
A= not defined $valueB= $next_indent <= $limitC= not $next_line =~ /$IS_ARRAY_LINE/
ABCresult
0XX0
10X0
1100
1111
298 and $next_indent <= $limit # next line is same or less indentation
299 and $next_line !~ $IS_ARRAY_LINE) # arrays can start at same indent
300 {
301 2 $hash->{$key} = undef;
302 }
303 else {
304 22 $hash->{$key} = $self->_read_nested;
305 }
306  
307 124 ( $line, $indent ) = $self->_peek;
308 last
309 124 ▶ if $indent < $limit
Branch: 1 branch
BranchTrueFalseTotal
if $indent < $limit or not defined $line or $line =~ /$IS_END_YAML/ 54 70 124
Condition83%5/6
$indent < $limit or not defined $line or $line =~ /$IS_END_YAML/
l!l&&r!l&&!r
292570
$indent < $limit or not defined $line
l!l&&r!l&&!r
29095
MC/DC66%2/3
$indent < $limit or not defined $line or $line =~ /$IS_END_YAML/
$indent < $limit not defined $line $line =~ /$IS_END_YAML/
Truth table75%3/4
A or not(B) or C
A= $indent < $limitB= not defined $lineC= $line =~ /$IS_END_YAML/
ABCresult
1XX1
01X1
0011
0000
310 || !defined $line
311 || $line =~ $IS_END_YAML;
312 }
313  
314 54 return $hash;
315 }
316  
317 37 1;
318  
319 __END__
« blib/lib/TAP/Parser/Scheduler.pm ↑ Summary blib/lib/TAP/Parser/SourceHandler/Perl.pm »