blib/lib/Test/Regexp/Pattern.pm

filter: Statementstmt 17.4% 32 / 183 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 47.3% 9 / 19 Podpod 66.6% 2 / 3 totaltotal 12.2% 43 / 352 CC 67 SCAR 53.8
CC
67
Cov
10%
__ANON__
506.0
all_modules
272.0
_test_regexp_pattern
182.0
CRAP 217.8
↑ Summary
1 ## no critic: ValuesAndExpressions::ProhibitCommaSeparatedStatements BuiltinFunctions::RequireBlockMap
2  
3 package Test::Regexp::Pattern;
4  
5 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
6 1 our $DATE = '2022-07-24'; # DATE
7 1 our $DIST = 'Test-Regexp-Pattern'; # DIST
8 1 our $VERSION = '0.010'; # VERSION
9  
10 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict 'subs', 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Hash::DefHash; # exports defhash()
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Regexp::Pattern qw(re);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Test::More ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 my $Test = Test::Builder->new;
21  
22 0 ▶sub import {
Subroutine import: not called CC=1 SCAR=3.3
23 0 my $self = shift;
24 0 my $caller = caller;
25 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 0 *{$caller.'::regexp_patterns_in_module_ok'} = \&regexp_patterns_in_module_ok;
27 0 *{$caller.'::regexp_patterns_in_all_modules_ok'} = \&regexp_patterns_in_all_modules_ok;
28  
29 0 $Test->exported_to($caller);
30 0 $Test->plan(@_);
31 }
32  
33 0 ▶sub _test_regexp_pattern {
Subroutine _test_regexp_pattern: not called CC=13 SCAR=52.0
34 0 my ($re, $parent, $fqname, $opts) = @_;
35 0 my $ok = 1;
36  
37 GENERAL: {
38 0 my $dh;
39 0 eval { $dh = Hash::DefHash->new($re, parent=>$parent); 1 };
40 0 my $eval_err = $@;
41 0 ▶ $Test->ok(!$eval_err, "Must be a valid defhash") or do {
Branch: 1 branch
BranchTrueFalseTotal
$Test->ok(!$eval_err, "Must be a valid defhash") or do { $Test->diag("error in defhash check: $eval_err"); $ok = 0 } 0 0 0
42 0 $Test->diag("error in defhash check: $eval_err");
43 0 $ok = 0;
44 };
45 0 ▶ $Test->ok(($re->{pat} xor $re->{gen}), "Must declare pat OR gen but not both") or $ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
$Test->ok($re->{"pat"} ^^ $re->{"gen"}, "Must declare pat OR gen but not both") or $ok = 0 0 0 0
Condition0%0/4
$re->{"pat"} ^^ $re->{"gen"}
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$re->{"pat"} ^^ $re->{"gen"}
$re->{"pat"} $re->{"gen"}
Truth table0%0/4
A ^^ B
A= $re->{"pat"}B= $re->{"gen"}
ABresult
110
101
011
000
46 }
47  
48 EXAMPLES: {
49 0 ▶ last unless $opts->{test_examples} && $re->{examples};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"test_examples"} and $re->{"examples"} 0 0 0
Condition0%0/3
$opts->{"test_examples"} and $re->{"examples"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"test_examples"} and $re->{"examples"}
$opts->{"test_examples"} $re->{"examples"}
Truth table0%0/3
A and B
A= $opts->{"test_examples"}B= $re->{"examples"}
ABresult
0X0
100
111
50 0 my $i = 0;
51 0 for my $eg (@{ $re->{examples} }) {
52 0 $i++;
53 0 ▶ next unless $eg->{test} // 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $eg->{"test"} // 1 0 0 0
Condition0%0/2
$eg->{"test"} // 1
l!l
00
MC/DC0%0/1
$eg->{"test"} // 1
$eg->{"test"}
Truth table0%0/2
A
A= $eg->{"test"}
Aresult
11
00
54 $Test->subtest(
55 "example #$i" .
56 ($eg->{name} ? " ($eg->{name})" :
57 ($eg->{summary} ? " ($eg->{summary})" :
58 (defined $eg->{str} ? " (str $eg->{str})" :
59 ""))),
60 0 ▶ sub {
Subroutine __ANON__: not called CC=22 SCAR=62.3
61 0 ▶ $Test->ok(defined($eg->{str}), 'example provides string to match') or do {
Branch: 1 branch
BranchTrueFalseTotal
$Test->ok(defined $eg->{"str"}, "example provides string to match") or do { $ok = 0; return } 0 0 0
62 0 $ok = 0;
63 0 return;
64 };
65  
66 0 my %args;
67 0 ▶ if ($eg->{gen_args}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eg->{"gen_args"}) 0 0 0
68 0 $args{$_} = $eg->{gen_args}{$_} for keys %{$eg->{gen_args}};
69 }
70 0 ▶ if (defined $eg->{anchor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $eg->{"anchor"}) 0 0 0
71 0 $args{-anchor} = $eg->{anchor};
72 }
73 0 my $pat = re($fqname, %args);
74  
75 0 ▶ my $actual_match = $eg->{str} =~ $pat ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$eg->{'str'} =~ /$pat/ ? : 0 0 0
76 0 ▶ if (ref $eg->{matches} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $eg->{'matches'} eq 'ARRAY') { } 0 0 0
elsif (ref $eg->{'matches'} eq 'HASH') { } 0 0 0
77 0 my $len = @{ $eg->{matches} };
78 0 my @actual_matches;
79 0 for (1..$len) {
80 0 push @actual_matches, ${$_};
81 }
82 0 ▶ my $should_match = $len ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$len ? : 0 0 0
83 0 ▶ if ($should_match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should_match) { } 0 0 0
84 0 ▶ $Test->ok( $actual_match, 'string should match') or do {
Branch: 1 branch
BranchTrueFalseTotal
$Test->ok($actual_match, "string should match") or do { $ok = 0; return } 0 0 0
85 0 $ok = 0;
86 0 return;
87 };
88 0 ▶ Test::More::is_deeply(\@actual_matches, $eg->{matches}, 'matches') or do {
Branch: 1 branch
BranchTrueFalseTotal
Test::More::is_deeply(\@actual_matches, $eg->{"matches"}, "matches") or do { $Test->diag($Test->explain(\@actual_matches)); $ok = 0 } 0 0 0
89 0 $Test->diag($Test->explain(\@actual_matches));
90 0 $ok = 0;
91 };
92 } else {
93 0 ▶ $Test->ok(!$actual_match, 'string should not match') or do {
Branch: 1 branch
BranchTrueFalseTotal
$Test->ok(!$actual_match, "string should not match") or do { $ok = 0; return } 0 0 0
94 0 $ok = 0;
95 0 return;
96 };
97 }
98 } elsif (ref $eg->{matches} eq 'HASH') {
99 0 my %actual_matches = %+;
100 0 ▶ my $should_match = %{ $eg->{matches} } ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
%{$eg->{'matches'};} ? : 0 0 0
101 0 ▶ if ($should_match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should_match) { } 0 0 0
102 0 ▶ $Test->ok( $actual_match, 'string should match') or do {
Branch: 1 branch
BranchTrueFalseTotal
$Test->ok($actual_match, "string should match") or do { $ok = 0; return } 0 0 0
103 0 $ok = 0;
104 0 return;
105 };
106 0 ▶ Test::More::is_deeply(\%actual_matches, $eg->{matches}, 'matches') or do {
Branch: 1 branch
BranchTrueFalseTotal
Test::More::is_deeply(\%actual_matches, $eg->{"matches"}, "matches") or do { $Test->diag($Test->explain(\%actual_matches)); $ok = 0 } 0 0 0
107 0 $Test->diag($Test->explain(\%actual_matches));
108 0 $ok = 0;
109 };
110 } else {
111 0 ▶ $Test->ok(!$actual_match, 'string should not match') or do {
Branch: 1 branch
BranchTrueFalseTotal
$Test->ok(!$actual_match, "string should not match") or do { $ok = 0; return } 0 0 0
112 0 $ok = 0;
113 0 return;
114 };
115 }
116 } else {
117 0 ▶ if ($eg->{matches}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eg->{'matches'}) { } 0 0 0
118 0 ▶ $Test->ok( $actual_match, 'string should match') or do {
Branch: 1 branch
BranchTrueFalseTotal
$Test->ok($actual_match, "string should match") or do { $ok = 0; return } 0 0 0
119 0 $ok = 0;
120 0 return;
121 };
122 } else {
123 0 ▶ $Test->ok(!$actual_match, 'string should not match') or do {
Branch: 1 branch
BranchTrueFalseTotal
$Test->ok(!$actual_match, "string should not match") or do { $ok = 0; return } 0 0 0
124 0 $ok = 0;
125 0 return;
126 };
127 }
128 }
129 0 ▶ }) or $ok = 0;
Branch: 4 branches
BranchTrueFalseTotal
$Test->subtest("example #$i" . ($eg->{"name"} ? qq[ ($eg->{"name"})] : ($eg->{"summary"} ? qq[ ($eg->{"summary"})] : (defined $eg->{"str"} ? qq[ (str $eg->{"str"})] : ""))), sub { $Test->ok(defined $eg->{"str"}, "example provides string to match") or do { $ok = 0; return }; my %args; if ($eg->{"gen_args"}) { $args{$_} = $eg->{"gen_args"}{$_} foreach (keys %{$$eg{"gen_args"};}); } ; if (defined $eg->{"anchor"}) { $args{"-anchor"} = $eg->{"anchor"}; } ; my $pat = re($fqname, %args); my $actual_match = $eg->{"str"} =~ /$pat/ ? 1 : 0; if (ref $eg->{"matches"} eq "ARRAY") { my $len = @{$eg->{"matches"};}; my @actual_matches; foreach $_ (1 .. $len) { push @actual_matches, ${$_;}; } ; my $should_match = $len ? 1 : 0; if ($should_match) { $Test->ok($actual_match, "string should match") or do { $ok = 0; return }; Test::More::is_deeply(\@actual_matches, $eg->{"matches"}, "matches") or do { $Test->diag($Test->explain(\@actual_matches)); $ok = 0 }; } else { $Test->ok(!$actual_match, "string should not match") or do { $ok = 0; return }; } ; } elsif (ref $eg->{"matches"} eq "HASH") { my(%actual_matches) = %+; my $should_match = %{$eg->{"matches"};} ? 1 : 0; if ($should_match) { $Test->ok($actual_match, "string should match") or do { $ok = 0; return }; Test::More::is_deeply(\%actual_matches, $eg->{"matches"}, "matches") or do { $Test->diag($Test->explain(\%actual_matches)); $ok = 0 }; } else { $Test->ok(!$actual_match, "string should not match") or do { $ok = 0; return }; } ; } else { if ($eg->{"matches"}) { $Test->ok($actual_match, "string should match") or do { $ok = 0; return }; } else { $Test->ok(!$actual_match, "string should not match") or do { $ok = 0; return }; } ; } ; } ) or $ok = 0 0 0 0
$eg->{'name'} ? : 0 0 0
$eg->{'summary'} ? : 0 0 0
defined $eg->{'str'} ? : 0 0 0
130 }
131 }
132 0 $ok;
133 }
134  
135 0 ▶sub regexp_patterns_in_module_ok {
Subroutine regexp_patterns_in_module_ok: not called CC=6 SCAR=37.4
Pod documented
136 0 my $module = shift;
137 0 ▶ my %opts = (@_ && (ref $_[0] eq "HASH")) ? %{(shift)} : ();
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
138 0 ▶ my $msg = @_ ? shift : "Regexp patterns in module $module";
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
139 0 my $res;
140 0 my $ok = 1;
141  
142 0 ▶ $opts{test_examples} //= 1;
Condition0%0/2
$opts{"test_examples"} //= 1
l!l
00
MC/DC0%0/1
$opts{"test_examples"} //= 1
$opts{"test_examples"}
Truth table0%0/2
A
A= $opts{"test_examples"}
Aresult
11
00
143  
144 0 my $has_tests;
145  
146 $Test->subtest(
147 $msg,
148 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
149 0 (my $modulepm = "$module.pm") =~ s!::!/!g;
150 0 require $modulepm;
151  
152 0 my $prefix = '';
153 0 ▶ if ($module =~ /\ARegexp::Pattern::(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ /\ARegexp::Pattern::(.+)/) { } 0 0 0
154 0 $prefix = "$1\::";
155 } else {
156 0 goto L1;
157 }
158  
159 0 my $RE = \%{ "$module\::RE" };
160 0 my $dh = defhash($RE);
161 0 for my $name ($dh->props) {
162 0 my $re = $RE->{$name};
163 0 $has_tests++;
164 $Test->subtest(
165 "pattern $prefix$name",
166 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
167 0 ▶ _test_regexp_pattern($re, $RE, "$prefix$name", \%opts) or $ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
_test_regexp_pattern($re, $RE, "$prefix$name", \%opts) or $ok = 0 0 0 0
168 },
169 0 ▶ ) or $ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
$Test->subtest("pattern $prefix$name", sub { _test_regexp_pattern($re, $RE, "$prefix$name", \%opts) or $ok = 0; } ) or $ok = 0 0 0 0
170 }
171  
172 L1:
173 0 ▶ unless ($has_tests) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_tests) 0 0 0
174 0 $Test->ok(1);
175 0 $Test->diag("No regexp patterns to test in $modulepm");
176 }
177 } # subtest
178 0 ▶ ) or $ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
$Test->subtest($msg, sub { (my $modulepm = "$module.pm") =~ s[::][/]g; require $modulepm; my $prefix = ""; if ($module =~ /\ARegexp::Pattern::(.+)/) { $prefix = "$1::"; } else { goto L1; } ; my $RE = \%{"${module}::RE";}; my $dh = defhash($RE); foreach my $name ($dh->props) { my $re = $RE->{$name}; ++$has_tests; $Test->subtest("pattern $prefix$name", sub { _test_regexp_pattern($re, $RE, "$prefix$name", \%opts) or $ok = 0; } ) or $ok = 0; } ; L1: unless ($has_tests) { $Test->ok(1); $Test->diag("No regexp patterns to test in $modulepm"); } ; } ) or $ok = 0 0 0 0
179  
180 0 $ok;
181 }
182  
183 # BEGIN copy-pasted from Test::Pod::Coverage, with a bit modification
184  
185 0 ▶sub all_modules {
Subroutine all_modules: not called CC=16 SCAR=56.1
Pod undocumented
186 0 ▶ my @starters = @_ ? @_ : _starting_points();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
187 0 my %starters = map {$_,1} @starters;
188  
189 0 my @queue = @starters;
190  
191 0 my @modules;
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 local *DH;
196 0 ▶ opendir DH, $file or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir DH, $file or next 0 0 0
197 0 my @newfiles = readdir DH;
198 0 closedir DH;
199  
200 0 @newfiles = File::Spec->no_upwards( @newfiles );
201 0 ▶ @newfiles = grep { $_ ne "CVS" && $_ ne ".svn" } @newfiles;
Condition0%0/3
$_ ne 'CVS' && $_ ne '.svn'
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne 'CVS' && $_ ne '.svn'
$_ ne 'CVS' $_ ne '.svn'
Truth table0%0/3
A && B
A= $_ ne 'CVS'B= $_ ne '.svn'
ABresult
0X0
100
111
202  
203 0 push @queue, map "$file/$_", @newfiles;
204 }
205 0 ▶ if ( -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 0 0 0
206 0 ▶ next unless $file =~ /\.pm$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\.pm$/ 0 0 0
207  
208 0 my @parts = File::Spec->splitdir( $file );
209 0 ▶ shift @parts if @parts && exists $starters{$parts[0]};
Branch: 1 branch
BranchTrueFalseTotal
if @parts and exists $starters{$parts[0]} 0 0 0
Condition0%0/3
@parts and exists $starters{$parts[0]}
!ll&&!rl&&r
000
MC/DC0%0/2
@parts and exists $starters{$parts[0]}
@parts exists $starters{$parts[0]}
Truth table0%0/3
A and B
A= @partsB= exists $starters{$parts[0]}
ABresult
0X0
100
111
210 0 ▶ shift @parts if @parts && $parts[0] eq "lib";
Branch: 1 branch
BranchTrueFalseTotal
if @parts and $parts[0] eq "lib" 0 0 0
Condition0%0/3
@parts and $parts[0] eq "lib"
!ll&&!rl&&r
000
MC/DC0%0/2
@parts and $parts[0] eq "lib"
@parts $parts[0] eq "lib"
Truth table0%0/3
A and B
A= @partsB= $parts[0] eq "lib"
ABresult
0X0
100
111
211 0 ▶ $parts[-1] =~ s/\.pm$// if @parts;
Branch: 1 branch
BranchTrueFalseTotal
if @parts 0 0 0
212  
213 # Untaint the parts
214 0 for ( @parts ) {
215 0 ▶ if ( /^([a-zA-Z0-9_\.\-]*)$/ && ($_ eq $1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^([a-zA-Z0-9_\.\-]*)$/ and $_ eq $1) { } 0 0 0
Condition0%0/3
/^([a-zA-Z0-9_\.\-]*)$/ and $_ eq $1
!ll&&!rl&&r
000
MC/DC0%0/2
/^([a-zA-Z0-9_\.\-]*)$/ and $_ eq $1
/^([a-zA-Z0-9_\.\-]*)$/ $_ eq $1
Truth table0%0/3
A and B
A= /^([a-zA-Z0-9_\.\-]*)$/B= $_ eq $1
ABresult
0X0
100
111
216 0 $_ = $1; # Untaint the original
217 }
218 else {
219 0 die qq{Invalid and untaintable filename "$file"!};
220 }
221 }
222 0 my $module = join( "::", grep {length} @parts );
223 0 push( @modules, $module );
224 }
225 } # while
226  
227 0 return @modules;
228 }
229  
230 0 ▶sub _starting_points {
Subroutine _starting_points: not called CC=2 SCAR=17.9
231 0 ▶ return 'blib' if -e 'blib';
Branch: 1 branch
BranchTrueFalseTotal
if -e "blib" 0 0 0
232 0 return 'lib';
233 }
234  
235 # END copy-pasted from Test::Pod::Coverage
236  
237 0 ▶sub regexp_patterns_in_all_modules_ok {
Subroutine regexp_patterns_in_all_modules_ok: not called CC=5 SCAR=34.0
Pod documented
238 0 ▶ my $opts = (@_ && (ref $_[0] eq "HASH")) ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
239 0 my $msg = shift;
240 0 my $ok = 1;
241  
242 0 my @starters = _starting_points();
243 0 local @INC = (@starters, @INC);
244  
245 0 $Test->plan(tests => 1);
246  
247 0 my @modules = all_modules(@starters);
248 0 ▶ if (@modules) {
Branch: 1 branch
BranchTrueFalseTotal
if (@modules) { } 0 0 0
249 $Test->subtest(
250 "Regexp patterns on all dist's modules",
251 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
252 0 for my $module (@modules) {
253 0 ▶ my $thismsg = defined $msg ? $msg :
Branch: 1 branch
BranchTrueFalseTotal
defined $msg ? : 0 0 0
254 "Regexp patterns in module $module";
255 0 ▶ my $thisok = regexp_patterns_in_module_ok(
Branch: 1 branch
BranchTrueFalseTotal
my $thisok = regexp_patterns_in_module_ok($module, $opts, $thismsg) or $ok = 0 0 0 0
256 $module, $opts, $thismsg)
257 or $ok = 0;
258 }
259 }
260 0 ▶ ) or $ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
$Test->subtest("Regexp patterns on all dist's modules", sub { foreach my $module (@modules) { my $thismsg = defined $msg ? $msg : "Regexp patterns in module $module"; my $thisok = regexp_patterns_in_module_ok($module, $opts, $thismsg) or $ok = 0; } ; } ) or $ok = 0 0 0 0
261 } else {
262 0 $Test->ok(1, "No modules found.");
263 }
264 0 $ok;
265 }
266  
267 1 1;
268 # ABSTRACT: Test Regexp::Pattern patterns
269  
270 __END__
↑ Summary