blib/lib/Test/Synopsis.pm

filter: Statementstmt 100% 100 / 100 Branchbran 84.6% 22 / 26 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 18 / 18 Podpod 100% 2 / 2 totaltotal 95.0% 153 / 161 CC 20 SCAR 16.3
CC
20
Cov
96%
_handle_text
9.1
synopsis_ok
9.0
all_synopsis_ok
2.0
CRAP 5.1
↑ Summary
1 package Test::Synopsis;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 our $VERSION = '0.18'; # VERSION
8  
9 15 ▶use parent qw( Test::Builder::Module );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 our @EXPORT = qw( synopsis_ok all_synopsis_ok );
11  
12 15 ▶use ExtUtils::Manifest qw( maniread );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 my %ARGS;
14 # = ( dump_all_code_on_error => 1 ); ### REMOVE THIS FOR PRODUCTION!!!
15 1 ▶sub all_synopsis_ok {
Subroutine all_synopsis_ok: called CC=2 SCAR=7.0
Pod documented
16 1 %ARGS = @_;
17  
18 1 my $manifest = maniread();
19 1 ▶ my @files = grep m!^lib/.*\.p(od|m)$!, keys %$manifest
Branch: 1 branch
BranchTrueFalseTotal
my(@files) = grep(m[^lib/.*\.p(od|m)$], keys %$manifest) or __PACKAGE__->builder->skip_all("No files in lib to test") 1 0 1
20 or __PACKAGE__->builder->skip_all('No files in lib to test');
21  
22 1 __PACKAGE__->builder->no_plan();
23  
24 1 synopsis_ok(@files);
25 }
26  
27 15 ▶sub synopsis_ok {
Subroutine synopsis_ok: called CC=9 SCAR=22.0
Pod documented
28 15 my @files = @_;
29  
30 15 for my $file (@files) {
31 16 my $blocks = _extract_synopsis($file);
32 16 ▶ unless (@$blocks) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$blocks) 1 15 16
33 1 __PACKAGE__->builder->ok(1, "No SYNOPSIS code");
34 1 next;
35 }
36  
37 15 my $block_num = 0;
38 15 for my $block (@$blocks) {
39 17 $block_num++;
40 17 my ($line, $code, $options) = @$block;
41  
42 # don't want __END__ blocks in SYNOPSIS chopping our '}' in wrapper sub
43 # same goes for __DATA__ and although we'll be sticking an extra '}'
44 # into its contents; it shouldn't matter since the code shouldn't be
45 # run anyways.
46 17 $code =~ s/(?=(?:__END__|__DATA__)\s*$)/}\n/m;
47  
48 17 $options = join(";", @$options);
49 17 my $test = qq($options;\nsub{\n#line $line "$file"\n$code\n;});
50 #use Test::More (); Test::More::note "=========\n$test\n========";
51 17 my $ok = _compile($test);
52  
53 # See if the user is trying to skip this test using the =for block
54 17 ▶ if ( !$ok and $@=~/^SKIP:.+BEGIN failed--compilation aborted/si ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok and $@ =~ /^SKIP:.+BEGIN failed--compilation aborted/is) { } 1 16 17
Condition100%3/3
not $ok and $@ =~ /^SKIP:.+BEGIN failed--compilation aborted/is
!ll&&!rl&&r
1241
MC/DC100%2/2
not $ok and $@ =~ /^SKIP:.+BEGIN failed--compilation aborted/is
not $ok $@ =~ /^SKIP:.+BEGIN failed--compilation aborted/is
Truth table100%3/3
not(A) and B
A= not $okB= $@ =~ /^SKIP:.+BEGIN failed--compilation aborted/is
ABresult
0X0
100
111
55 1 $@ =~ s/^SKIP:\s*//;
56 1 $@ =~ s/\nBEGIN failed--compilation aborted at.+//s;
57 1 __PACKAGE__->builder->skip($@, 1);
58 } else {
59 16 my $block_name = $file;
60 ## Show block number only if more than one block
61 16 ▶ if (@$blocks > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$blocks > 1) 4 12 16
62 4 $block_name .= " (section $block_num)";
63 }
64 __PACKAGE__->builder->ok($ok, $block_name)
65 or __PACKAGE__->builder->diag(
66 $ARGS{dump_all_code_on_error}
67 16 ▶ ? "$@\nEVALED CODE:\n$test"
Branch: 2 branches
BranchTrueFalseTotal
__PACKAGE__->builder->ok($ok, $block_name) or __PACKAGE__->builder->diag($ARGS{"dump_all_code_on_error"} ? "$@\nEVALED CODE:\n$test" : $@) 12 4 16
$ARGS{'dump_all_code_on_error'} ? : 0 4 4
68 : $@
69 );
70 }
71 }
72 }
73 }
74  
75 15 my $sandbox = 0;
76 17 ▶sub _compile {
Subroutine _compile: called CC=1 SCAR=0.0
77 package
78 Test::Synopsis::Sandbox;
79 17 eval sprintf "package\nTest::Synopsis::Sandbox%d;\n%s",
80 ++$sandbox, $_[0]; ## no critic
81 }
82  
83 sub _extract_synopsis
84 16 ▶{
Subroutine _extract_synopsis: called CC=2 SCAR=6.9
85 16 my $file = shift;
86  
87 16 my $parser = Test::Synopsis::Parser->new;
88 16 $parser->parse_file($file);
89 # Attach the fully-collected =for test_synopsis options to every block,
90 # so directives apply regardless of their position relative to the
91 # SYNOPSIS code block (restores pre-0.14, position-independent behavior).
92 16 $_->[2] = $parser->{tsyn_options} for @{ $parser->{tsyn_blocks} };
93 $parser->{tsyn_blocks}
94 16 }
95  
96 package
97 Test::Synopsis::Parser; # on new line to avoid indexing
98  
99 15 ▶use Pod::Simple 3.09;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 15 ▶use parent 'Pod::Simple';
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 sub new
103 16 ▶{
Subroutine new: called CC=1 SCAR=0.0
104 16 my $self = shift->SUPER::new(@_);
105 16 $self->accept_targets('test_synopsis');
106 16 $self->merge_text(1);
107 16 $self->no_errata_section(1);
108 37 ▶ $self->strip_verbatim_indent(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
109 37 my $lines = shift;
110 37 my ($indent) = $lines->[0] =~ /^(\s*)/;
111 37 $indent
112 16 });
113  
114 16 $self->{tsyn_stack} = [];
115 16 $self->{tsyn_options} = [];
116 16 $self->{tsyn_blocks} = [];
117 16 $self->{tsyn_in_synopsis} = '';
118  
119 16 $self
120 }
121  
122 sub _handle_element_start
123 322 ▶{
Subroutine _handle_element_start: called CC=1 SCAR=0.0
124 322 my ($self, $element_name, $attrs) = @_;
125  
126 #Test::More::note Test::More::explain($element_name);
127 #Test::More::note Test::More::explain($attrs);
128 322 push @{$self->{tsyn_stack}}, [ $element_name, $attrs ];
129 }
130  
131 sub _handle_element_end
132 322 ▶{
Subroutine _handle_element_end: called CC=2 SCAR=6.9
133 322 ▶ return unless $_[0]->{tsyn_stack};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"tsyn_stack"} 11 311 322
134 311 pop @{ $_[0]->{tsyn_stack} };
135 }
136  
137 sub _handle_text
138 374 ▶{
Subroutine _handle_text: called CC=9 SCAR=22.1
139 374 ▶ return unless $_[0]->{tsyn_stack};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"tsyn_stack"} 0 374 374
140 374 my ($self, $text) = @_;
141 374 my $elt = $self->{tsyn_stack}[-1][0];
142 374 ▶ if ($elt eq 'head1') {
Branch: 3 branches
BranchTrueFalseTotal
if ($elt eq 'head1') { } 52 322 374
elsif ($elt eq 'Data') { } 6 316 322
elsif ($elt eq 'Verbatim' and $self->{'tsyn_in_synopsis'}) { } 17 578 595
143 52 ▶ if ($self->{tsyn_in_synopsis}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tsyn_in_synopsis"}) 11 41 52
144 # Exiting SYNOPSIS => Skip everything to the end
145 11 delete $self->{tsyn_stack};
146 }
147 52 $self->{tsyn_in_synopsis} = $text =~ /SYNOPSIS\s*$/;
148 } elsif ($elt eq 'Data') {
149 # use Test::More; Test::More::note "XXX";
150 6 my $up = $self->{tsyn_stack}[-2];
151 6 ▶ if ($up->[0] eq 'for' && $up->[1]->{target} eq 'test_synopsis') {
Branch: 1 branch
BranchTrueFalseTotal
if ($up->[0] eq "for" and $up->[1]{"target"} eq "test_synopsis") 6 0 6
Condition33%1/3
$up->[0] eq "for" and $up->[1]{"target"} eq "test_synopsis"
!ll&&!rl&&r
006
MC/DC0%0/2
$up->[0] eq "for" and $up->[1]{"target"} eq "test_synopsis"
$up->[0] eq "for" $up->[1]{"target"} eq "test_synopsis"
Truth table33%1/3
A and B
A= $up->[0] eq "for"B= $up->[1]{"target"} eq "test_synopsis"
ABresult
0X0
100
111
152 6 my $line = $up->[1]{start_line};
153 6 my $file = $self->source_filename;
154 6 push @{$self->{tsyn_options}}, qq<#line $line "$file"\n$text\n>;
155 }
156 ▶ } elsif ($elt eq 'Verbatim' && $self->{tsyn_in_synopsis}) {
Condition100%3/3
$elt eq "Verbatim" and $self->{"tsyn_in_synopsis"}
!ll&&!rl&&r
2792017
MC/DC100%2/2
$elt eq "Verbatim" and $self->{"tsyn_in_synopsis"}
$elt eq "Verbatim" $self->{"tsyn_in_synopsis"}
Truth table100%3/3
A and B
A= $elt eq "Verbatim"B= $self->{"tsyn_in_synopsis"}
ABresult
0X0
100
111
157 17 my $line = $self->{tsyn_stack}[-1][1]{start_line};
158 17 push @{ $self->{tsyn_blocks} }, [
159 $line,
160 $text,
161 ];
162 }
163 }
164  
165  
166 15 1;
167 __END__
↑ Summary