blib/lib/Test/PureASCII.pm

filter: Statementstmt 65.5% 101 / 154 Branchbran 42.1% 43 / 102 Conditioncond 26.6% 8 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 80.0% 12 / 15 Podpod 42.8% 3 / 7 totaltotal 51.2% 167 / 326 CC 70 SCAR 39.2
CC
70
Cov
53%
all_files
132.0
file_is_pure_ascii
83.2
_skip_file_p
28.3
CRAP 50.6
↑ Summary
1 package Test::PureASCII;
2  
3 1 our $VERSION = '0.02';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 my $test = Test::Builder->new;
12  
13 1 our @TESTED;
14  
15 1 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
16 1 my $self = shift;
17 1 my $caller = caller;
18  
19 1 for my $func ( qw( file_is_pure_ascii all_perl_files_are_pure_ascii all_files_are_pure_ascii) ) {
20 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 *{$caller."::".$func} = \&$func;
22 }
23  
24 1 $test->exported_to($caller);
25 1 $test->plan(@_);
26 }
27  
28 1 ▶sub _skip_file_p {
Subroutine _skip_file_p: called CC=8 SCAR=33.4
29 1 my %opts;
30 1 ▶ %opts = %{shift @_} if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 1 0 1
31 1 ▶ if (defined(my $skip = $opts{skip})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $skip = $opts{"skip"})) 0 1 1
32 0 my $file = shift;
33 0 ▶ for my $s (ref $skip eq 'ARRAY' ? @$skip : $skip) {
Branch: 1 branch
BranchTrueFalseTotal
ref $skip eq 'ARRAY' ? : 0 0 0
34 0 ▶ if (ref $s eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s eq 'Regexp') { } 0 0 0
35 0 ▶ return 1 if $file =~ $s;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /$s/ 0 0 0
36 }
37 else {
38 0 ▶ return 1 if $file eq $s;
Branch: 1 branch
BranchTrueFalseTotal
if $file eq $s 0 0 0
39 }
40 }
41 }
42 1 return 0;
43 }
44  
45 0 ▶sub _make_error {
Subroutine _make_error: not called CC=2 SCAR=17.9
46 0 my ($bad, $error, $ln, $file) = @_;
47 0 my @chars = map sprintf("0x%02x", ord $_), split //, $bad;
48 0 my $chars = join(', ', @chars);
49 0 ▶ my $s = @chars > 1 ? ' sequence' : '';
Branch: 1 branch
BranchTrueFalseTotal
@chars > 1 ? : 0 0 0
50 0 ' ' . sprintf($error, $s, $chars) . " at line $ln in $file";
51 }
52  
53 1 ▶sub file_is_pure_ascii {
Subroutine file_is_pure_ascii: called CC=27 SCAR=44.2
Pod documented
54 1 my %opts;
55 1 ▶ %opts = %{shift @_} if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 1 0 1
56 1 my $skip_data = $opts{skip_data};
57 1 my $forbid_control = $opts{forbid_control};
58 1 my $forbid_cr = $opts{forbid_cr};
59 1 my $forbid_tab = $opts{forbid_tab};
60 1 my $require_crlf = $opts{require_crlf};
61  
62 1 my $file = shift;
63  
64 1 ▶ _skip_file_p(\%opts, $file) and return 1;
Branch: 1 branch
BranchTrueFalseTotal
_skip_file_p(\%opts, $file) and return 1 0 1 1
65  
66 1 ▶ my $name = @_ ? shift : "Pure ASCII test for $file";
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 1 1
67  
68 1 push @TESTED, $file;
69 # $test->diag("FILE: $file");
70  
71 1 my $fh;
72 1 ▶ unless (open $fh, '<', $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $file) 0 1 1
73 0 $test->ok(0, $name);
74 0 $test->diag(" unable to open '$file': $!");
75 0 return 0;
76 }
77 1 binmode $fh, ':bytes';
78  
79 1 my $failed = 0;
80 1 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 365 0 365
81 # $test->diag("line $.: $_");
82 365 ▶ next if /\bpa_test_ignore\b/;
Branch: 1 branch
BranchTrueFalseTotal
if /\bpa_test_ignore\b/ 1 364 365
83 364 ▶ last if /\bpa_test_end\b/;
Branch: 1 branch
BranchTrueFalseTotal
if /\bpa_test_end\b/ 1 363 364
84 363 ▶ if (my ($lines) = /pa_test_skip_lines\(\d+\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($lines) = /pa_test_skip_lines\(\d+\)/) 0 363 363
85 0 <$fh> for 1..$lines;
86 0 next;
87 }
88  
89 363 my @errors;
90 363 ▶ /([^\x00-\x7f]+)/ and
Branch: 1 branch
BranchTrueFalseTotal
/([^\x00-\x7f]+)/ and push @errors, _make_error($1, "non ASCII character%s %s", $., $file) 0 363 363
91 push @errors, _make_error($1, "non ASCII character%s %s",
92 $., $file);
93 363 ▶ $forbid_control and /([\x00-\x08\x0b-\x1F])/ and
Branch: 1 branch
BranchTrueFalseTotal
$forbid_control and /([\x00-\x08\x0b-\x1F])/ and push @errors, _make_error($1, "forbidden control character%s %s", $., $file) 0 363 363
Condition33%1/3
$forbid_control and /([\x00-\x08\x0b-\x1F])/
!ll&&!rl&&r
36300
MC/DC0%0/2
$forbid_control and /([\x00-\x08\x0b-\x1F])/
$forbid_control /([\x00-\x08\x0b-\x1F])/
Truth table33%1/3
A and B
A= $forbid_controlB= /([\x00-\x08\x0b-\x1F])/
ABresult
0X0
100
111
94 push @errors, _make_error($1, "forbidden control character%s %s",
95 $., $file);
96 363 ▶ $forbid_tab and /([\x09])/ and
Branch: 1 branch
BranchTrueFalseTotal
$forbid_tab and /([\x09])/ and push @errors, _make_error($1, "forbidden tab character%s %s", $., $file) 0 363 363
Condition33%1/3
$forbid_tab and /([\x09])/
!ll&&!rl&&r
36300
MC/DC0%0/2
$forbid_tab and /([\x09])/
$forbid_tab /([\x09])/
Truth table33%1/3
A and B
A= $forbid_tabB= /([\x09])/
ABresult
0X0
100
111
97 push @errors, _make_error($1, "forbidden tab character%s %s",
98 $., $file);
99 363 ▶ $forbid_cr and /([\x0d])/ and
Branch: 1 branch
BranchTrueFalseTotal
$forbid_cr and /([\x0d])/ and push @errors, _make_error($1, "forbidden CR character%s %s", $., $file) 0 363 363
Condition33%1/3
$forbid_cr and /([\x0d])/
!ll&&!rl&&r
36300
MC/DC0%0/2
$forbid_cr and /([\x0d])/
$forbid_cr /([\x0d])/
Truth table33%1/3
A and B
A= $forbid_crB= /([\x0d])/
ABresult
0X0
100
111
100 push @errors, _make_error($1, "forbidden CR character%s %s",
101 $., $file);
102 363 ▶ $require_crlf and /(\x0d(?!\x0a)|(?<!\x0d)\x0a)/ and
Branch: 1 branch
BranchTrueFalseTotal
$require_crlf and /(\x0d(?!\x0a)|(?<!\x0d)\x0a)/ and push @errors, _make_error($a, "forbidden end of line character%s %s", $., $file) 0 363 363
Condition33%1/3
$require_crlf and /(\x0d(?!\x0a)|(?<!\x0d)\x0a)/
!ll&&!rl&&r
36300
MC/DC0%0/2
$require_crlf and /(\x0d(?!\x0a)|(?<!\x0d)\x0a)/
$require_crlf /(\x0d(?!\x0a)|(?<!\x0d)\x0a)/
Truth table33%1/3
A and B
A= $require_crlfB= /(\x0d(?!\x0a)|(?<!\x0d)\x0a)/
ABresult
0X0
100
111
103 push @errors, _make_error($a, "forbidden end of line character%s %s",
104 $., $file);
105  
106 363 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 0 363 363
107 0 ▶ $test->ok(0, $name) unless $failed;
Branch: 1 branch
BranchTrueFalseTotal
unless $failed 0 0 0
108 0 $test->diag($_) for @errors;
109 0 $failed = 1;
110 }
111  
112 363 ▶ last if ($skip_data and /^__DATA__$/);
Branch: 1 branch
BranchTrueFalseTotal
if $skip_data and /^__DATA__$/ 0 363 363
Condition33%1/3
$skip_data and /^__DATA__$/
!ll&&!rl&&r
36300
MC/DC0%0/2
$skip_data and /^__DATA__$/
$skip_data /^__DATA__$/
Truth table33%1/3
A and B
A= $skip_dataB= /^__DATA__$/
ABresult
0X0
100
111
113 }
114 1 ▶ unless (close $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $fh) 0 1 1
115 0 ▶ $test->ok(0, $name) unless $failed;
Branch: 1 branch
BranchTrueFalseTotal
unless $failed 0 0 0
116 0 $test->diag(" unable to read from '$file': $!");
117 0 return 0;
118 }
119 1 ▶ $failed ? 0 : $test->ok(1, $name);
Branch: 1 branch
BranchTrueFalseTotal
$failed ? : 0 1 1
120 }
121  
122 1 ▶sub all_perl_files_are_pure_ascii {
Subroutine all_perl_files_are_pure_ascii: called CC=4 SCAR=14.3
Pod documented
123 1 my %opts;
124 0 ▶ %opts = %{shift @_} if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 1 1
125  
126 1 my @files = all_perl_files(\%opts, @_);
127  
128 1 $test->plan( tests => scalar @files );
129  
130 1 my $ok = 1;
131 1 foreach my $file (@files) {
132 1 ▶ file_is_pure_ascii(\%opts, $file) or undef $ok;
Branch: 1 branch
BranchTrueFalseTotal
file_is_pure_ascii(\%opts, $file) or undef $ok 1 0 1
133 }
134 1 return $ok;
135 }
136  
137 0 ▶sub all_files_are_pure_ascii {
Subroutine all_files_are_pure_ascii: not called CC=4 SCAR=30.0
Pod documented
138 0 my %opts;
139 0 ▶ %opts = %{shift @_} if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 0 0
140  
141 0 my @files = all_files(\%opts, @_);
142 0 $test->plan( tests => scalar @files );
143  
144 0 my $ok = 1;
145 0 foreach my $file (@files) {
146 0 ▶ file_is_pure_ascii(\%opts, $file) or undef $ok;
Branch: 1 branch
BranchTrueFalseTotal
file_is_pure_ascii(\%opts, $file) or undef $ok 0 0 0
147 }
148 0 return $ok;
149 }
150  
151 1 ▶sub all_perl_files {
Subroutine all_perl_files: called CC=12 SCAR=25.3
Pod undocumented
152 1 my %opts;
153 1 ▶ %opts = %{shift @_} if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 1 0 1
154  
155 1 ▶ my @queue = @_ ? @_ : starting_points();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 1 1
156 1 my @perl = ();
157  
158 1 ▶ while ( @queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 23 1 24
159 23 my $file = shift @queue;
160 23 ▶ if ( -d $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file) 14 9 23
161 14 ▶ opendir my $dh, $file or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $file or next 14 0 14
162 14 my @newfiles = readdir $dh;
163 14 closedir $dh;
164  
165 14 @newfiles = File::Spec->no_upwards( @newfiles );
166 14 ▶ @newfiles = grep { $_ ne "CVS" and $_ ne ".svn" and !/~$/ } @newfiles;
Condition33%2/6
$_ ne 'CVS' && $_ ne '.svn' && !/~$/
!ll&&!rl&&r
0022
$_ ne "CVS" and $_ ne ".svn"
!ll&&!rl&&r
0022
MC/DC0%0/3
$_ ne 'CVS' && $_ ne '.svn' && !/~$/
$_ ne "CVS" $_ ne ".svn" !/~$/
Truth table25%1/4
A and B && not(C)
A= $_ ne "CVS"B= $_ ne ".svn"C= !/~$/
ABCresult
0XX0
10X0
1100
1111
167  
168 14 foreach my $newfile (@newfiles) {
169 22 my $filename = File::Spec->catfile( $file, $newfile );
170 22 ▶ if ( -f $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filename) { } 9 13 22
171 9 push @queue, $filename;
172 }
173 else {
174 13 push @queue, File::Spec->catdir( $file, $newfile );
175 }
176 }
177 }
178 23 ▶ if ( -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 9 14 23
179 9 ▶ push @perl, $file if is_perl( $file );
Branch: 1 branch
BranchTrueFalseTotal
if is_perl($file) 1 8 9
180 }
181 }
182 1 return @perl;
183 }
184  
185 0 ▶sub all_files {
Subroutine all_files: not called CC=11 SCAR=48.8
Pod undocumented
186 0 my %opts;
187 0 ▶ %opts = %{shift @_} if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 0 0
188  
189 0 ▶ my @queue = @_ ? @_ : '.';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
190 0 my @all = ();
191  
192 0 ▶ while ( @queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 0 0 0
193 0 my $file = shift @queue;
194 0 ▶ if ( -d $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file) 0 0 0
195 0 ▶ opendir my $dh, $file or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $file or next 0 0 0
196 0 my @newfiles = readdir $dh;
197 0 closedir $dh;
198  
199 0 @newfiles = File::Spec->no_upwards( @newfiles );
200 0 ▶ @newfiles = grep { $_ ne "CVS" and $_ ne ".svn" and !/~$/ } @newfiles;
Condition0%0/6
$_ ne 'CVS' && $_ ne '.svn' && !/~$/
!ll&&!rl&&r
000
$_ ne "CVS" and $_ ne ".svn"
!ll&&!rl&&r
000
MC/DC0%0/3
$_ ne 'CVS' && $_ ne '.svn' && !/~$/
$_ ne "CVS" $_ ne ".svn" !/~$/
Truth table0%0/4
A and B && not(C)
A= $_ ne "CVS"B= $_ ne ".svn"C= !/~$/
ABCresult
0XX0
10X0
1100
1111
201  
202 0 foreach my $newfile (@newfiles) {
203 0 my $filename = File::Spec->catfile( $file, $newfile );
204 0 ▶ if ( -f $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filename) { } 0 0 0
205 0 push @queue, $filename;
206 }
207 else {
208 0 push @queue, File::Spec->catdir( $file, $newfile );
209 }
210 }
211 }
212 0 ▶ push @all, $file if -f $file
Branch: 1 branch
BranchTrueFalseTotal
if -f $file 0 0 0
213 }
214 0 return @all;
215 }
216  
217 1 ▶sub starting_points {
Subroutine starting_points: called CC=2 SCAR=9.2
Pod undocumented
218 1 ▶ return 'blib' if -e 'blib';
Branch: 1 branch
BranchTrueFalseTotal
if -e "blib" 1 0 1
219 0 return 'lib';
220 }
221  
222 9 ▶sub is_perl {
Subroutine is_perl: called CC=7 SCAR=20.8
Pod undocumented
223 9 my $file = shift;
224  
225 9 ▶ return 1 if $file =~ /\.PL$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.PL$/ 0 9 9
226 9 ▶ return 1 if $file =~ /\.p(l|m|od)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.p(l|m|od)$/ 1 8 9
227 8 ▶ return 1 if $file =~ /\.t$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.t$/ 0 8 8
228  
229 8 ▶ open my $fh, $file or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $file or return 8 0 8
230 8 my $first = <$fh>;
231 8 close $fh;
232  
233 8 ▶ return 1 if defined $first && ($first =~ /^#!.*perl/);
Branch: 1 branch
BranchTrueFalseTotal
if defined $first and $first =~ /^#!.*perl/ 0 8 8
Condition33%1/3
defined $first and $first =~ /^#!.*perl/
!ll&&!rl&&r
800
MC/DC0%0/2
defined $first and $first =~ /^#!.*perl/
defined $first $first =~ /^#!.*perl/
Truth table33%1/3
A and B
A= defined $firstB= $first =~ /^#!.*perl/
ABresult
0X0
100
111
234  
235 8 return;
236 }
237  
238  
239 1 1;
240  
241 __END__
↑ Summary