blib/lib/App/SimpleScan/TestSpec.pm

filter: Statementstmt 89.7% 96 / 107 Branchbran 68.4% 26 / 38 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 79.8% 139 / 174 CC 27 SCAR 25.8
CC
27
Cov
81%
as_tests
25.2
parse
9.0
_render_regex
7.3
CRAP 13.2
↑ Summary bin/simple_report »
1 package App::SimpleScan::TestSpec;
2 42 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 42 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 42 ▶use Regexp::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 42 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 42 our $VERSION = 0.24;
8  
9 42 __PACKAGE__->mk_accessors(qw(raw uri regex delim kind comment metaquote syntax_error flags test_count));
10  
11 42 my $app; # Will store a reference to the parent App::Simplescan
12  
13 42 my %test_type =
14 (
15 'Y' => <<"EOS",
16 page_like "<uri>",
17 qr<delim><regex><delim><flags>,
18 qq(<comment> [<uri>] [<qmregex> should match]);
19 EOS
20 'N' => <<"EOS",
21 page_unlike "<uri>",
22 qr<delim><regex><delim><flags>,
23 qq(<comment> [<uri>] [<qmregex> shouldn't match]);
24 EOS
25 'TY' => <<"EOS",
26 TODO: {
27 local \$Test::WWW::Simple::TODO = "Doesn't match now but should later";
28 page_like "<uri>",
29 qr<delim><regex><delim><flags>,
30 qq(<comment> [<uri>] [<qmregex> should match]);
31 }
32 EOS
33 'TN' => <<"EOS",
34 TODO: {
35 local \$Test::WWW::Simple::TODO = "Matches now but shouldn't later";
36 page_unlike "<uri>",
37 qr<delim><regex><delim><flags>,
38 qq(<comment> [<uri>] [<qmregex> shouldn't match]);
39 }
40 EOS
41 'SY' => <<"EOS",
42 SKIP: {
43 skip 'Deliberately skipping test that should match', 1;
44 page_like "<uri>",
45 qr<delim><regex><delim><flags>,
46 qq(<comment> [<uri>] [<qmregex> should match]);
47 }
48 EOS
49 'SN' => <<"EOS",
50 SKIP: {
51 skip "Deliberately skipping test that shouldn't match", 1;
52 page_unlike "<uri>",
53 qr<delim><regex><delim><flags>,
54 qq(<comment> [<uri>] [<qmregex> shouldn't match]);
55 }
56 EOS
57 );
58  
59 49 ▶sub app {
Subroutine app: called CC=2 SCAR=6.9
Pod documented
60 49 my ($class_or_object, $appref) = @_;
61 49 ▶ if (defined $appref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $appref) 48 1 49
62 48 $app = $appref;
63 }
64 49 return $app;
65 }
66  
67 103 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
68 103 my ($class, $spec) = @_;
69 103 my $self = {};
70 103 bless $self, $class;
71  
72 # Store the test spec.
73 103 $self->raw($spec);
74 103 $self->test_count(0);
75 103 $self->syntax_error(!$self->parse);
76  
77 103 return $self;
78 }
79  
80 107 ▶sub parse {
Subroutine parse: called CC=9 SCAR=22.0
Pod documented
81 107 my ($self, $line) = @_;
82 107 ▶ if (!defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 107 0 107
83 107 $line = $self->raw;
84 }
85 107 chomp $line;
86  
87 # Originally, we used Regex::Common to parse the URI and regex
88 # off the test spec line, but that's not going to work now since
89 # we've switched to keeping the text substitutions in place
90 # until we're ready to expand the spec into tests.
91 #
92 # So we'll do it like this: remove everything up to the first
93 # set of whitespace and call it the URI. *Reverse* the string,
94 # and match everything up to the whitespace before the kind of
95 # test; this grabs off the comment and the kind.
96 #
97 # We treat whatever is left at this point as the regex, in
98 # three phases. First, is it a standard slash-delimited
99 # regex? If not, is it an m-style regex (m| ...|, with
100 # arbitrary quote characters)? If not, then we treat it as
101 # a literal string to match (stripping off the slashes on
102 # either end if they are there.
103
104 # Remove URI portion.
105 107 my ($URI, $rest) = ($line =~ /^(.*?)\s+(.*)$/mx);
106  
107 107 ▶ if (! defined $URI) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $URI) 2 105 107
108 2 return 0;
109 }
110  
111 # Pull the scheme from the URI and pass it explicitly to
112 # Regexp::Common. Otherwise Regexp::Common::URI::http
113 # assumes 'HTTP', meaning that any other scheme won't match,
114 # causing this code to ignore (for instance) https: links.
115 #
116 # We also check for messed-up schemes here: a common error is
117 # to have left off on % on a pragma, causing the line to be
118 # passed into this code.
119 105 my ($scheme) = $URI =~ /^(\w+)/mx;
120 105 ▶ if (!defined $scheme) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scheme) 1 104 105
121 1 $app->stack_test(<<EOS);
122 fail "malformed pragma or URL scheme: '$URI'";
123 EOS
124 1 return 0;
125 }
126 # Not the canonical single-precent error. See if it's a good scheme.
127 104 ▶ return 0 if !($URI =~ /$RE{URI}{HTTP}{-scheme => $scheme }/mx);
Branch: 1 branch
BranchTrueFalseTotal
unless $URI =~ /$App::SimpleScan::TestSpec::RE{"URI"}{"HTTP"}{join $;, "-scheme", $scheme}/mx 48 56 104
128  
129 # Remove comment and kind.
130 56 my ($comment, undef, $kind, $maybe_regex) =
131 ((scalar reverse $rest) =~ /^(.*?)(\s+|\s*)\b(Y|N|YT|NT|YS|NS)\s+(.*)$/mx);
132 56 $self->comment(scalar reverse $comment);
133 56 $self->kind(scalar reverse $kind);
134 56 $self->uri($URI);
135  
136 56 my($clean, $delim, $flags);
137  
138 # Clean up regex if needed.
139 56 my $regex = reverse $maybe_regex;
140 56 ▶ if ((undef, undef, $clean, undef, $flags) =
Branch: 3 branches
BranchTrueFalseTotal
if ((undef, undef, $clean, undef, $flags) = $regex =~ m[^$App::SimpleScan::TestSpec::RE{'delimited'}{join $;, '-delim', '/'}{'-keep'}([ics]*)$]mx) { } 48 8 56
elsif (($delim, $clean, $flags) = $regex =~ /^m(.)(.*)\1([ics]*)$/mx) { } 5 3 8
elsif (($clean, $flags) = $regex =~ m[^/(.*)/([ics]*)$]mx) { } 2 1 3
141 ($regex =~ m|^$RE{delimited}{-delim=>'/'}{-keep}([ics]*)$|mx)) {
142 # Standard slash-delimited regex.
143 48 $self->regex($clean);
144 48 $self->delim('/');
145 48 $self->flags($flags);
146 }
147 elsif (($delim, $clean, $flags) = ($regex =~ /^m(.)(.*)\1([ics]*)$/mx)) {
148 # m-something-regex-something pattern.
149 5 $self->delim($1);
150 5 $self->regex($clean);
151 5 $self->flags($flags);
152 }
153 elsif (($clean, $flags) = ($regex =~ m|^/(.*)/([ics]*)$|mx)) {
154 # slash-delimited, with flags.
155 2 $self->delim('/');
156 2 $self->regex($clean);
157 2 $self->metaquote(1);
158 2 $self->flags($flags);
159 }
160 else {
161 # random string. We'll metaquote it and put slashes around it.
162 1 $self->delim('/');
163 1 $self->regex($regex);
164 1 $self->metaquote(1);
165 }
166  
167 56 ▶ if (! defined $self->flags) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->flags) 1 55 56
168 1 $self->flags(q{});
169 }
170  
171 # If we got this far, it's valid.
172 56 return 1;
173 }
174  
175 44 ▶sub _render_regex {
Subroutine _render_regex: called CC=6 SCAR=19.9
176 44 my ($self) = shift;
177 44 my $regex = $self->regex;
178 44 my $delim = $self->delim;
179 44 my $flags = $self->flags;
180 44 ▶ if (!defined $flags) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $flags) 0 44 44
181 0 $self->flags(q{});
182 0 $flags = q{};
183 }
184  
185 44 ▶ if ($self->metaquote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->metaquote) 0 44 44
186 0 $regex = "\\Q$regex\\E";
187 }
188 44 ▶ if ($delim ne '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($delim ne '/') { } 0 44 44
189 0 $regex = "m$delim$regex$delim";
190 }
191 else {
192 44 $regex = "/$regex/";
193 }
194 44 ▶ if ($flags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags) 0 44 44
195 0 $regex .= $flags;
196 }
197 44 ▶ if ($regex =~ /\\/mx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regex =~ /\\/mx) 1 43 44
198 # Have to escape backslashes.
199 1 $regex =~ s/\\/\\\\/mxg;
200 }
201  
202 44 return $regex;
203 }
204  
205 44 ▶sub as_tests {
Subroutine as_tests: called CC=13 SCAR=32.3
Pod documented
206 44 my ($self) = @_;
207 44 my @tests;
208 44 my $current = 0;
209 44 ▶ my $flags = $self->flags() || q{};
Condition50%1/2
$self->flags || ''
l!l
044
MC/DC0%0/1
$self->flags || ''
$self->flags
Truth table50%1/2
A
A= $self->flags
Aresult
11
00
210 44 my $uri = $self->uri;
211  
212 44 ▶ if (defined $uri and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri and defined(my $regex = $self->regex) and defined(my $delim = $self->delim) and defined(my $comment = $self->comment)) 44 0 44
Condition33%3/9
defined $uri and defined(my $regex = $self->regex) and defined(my $delim = $self->delim) and defined(my $comment = $self->comment)
!ll&&!rl&&r
0044
defined $uri and defined(my $regex = $self->regex) and defined(my $delim = $self->delim)
!ll&&!rl&&r
0044
defined $uri and defined(my $regex = $self->regex)
!ll&&!rl&&r
0044
MC/DC0%0/4
defined $uri and defined(my $regex = $self->regex) and defined(my $delim = $self->delim) and defined(my $comment = $self->comment)
defined $uri defined(my $regex = $self->regex) defined(my $delim = $self->delim) defined(my $comment = $self->comment)
Truth table20%1/5
A and B and C and D
A= defined $uriB= defined(my $regex = $self->regex)C= defined(my $delim = $self->delim)D= defined(my $comment = $self->comment)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
213 defined(my $regex = $self->regex) and
214 defined(my $delim = $self->delim) and
215 defined(my $comment = $self->comment)) { ##no critic
216 44 ▶ if (defined($tests[$current] = $test_type{$self->kind})) { ##no critic
Branch: 1 branch
BranchTrueFalseTotal
if (defined($tests[$current] = $test_type{$self->kind})) 44 0 44
217 44 $self->test_count($self->test_count()+1);
218 44 $tests[$current] =~ s/<uri>/$uri/mxg;
219 44 $tests[$current] =~ s/<delim>/$delim/mxg;
220 44 ▶ if ($self->metaquote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->metaquote) { } 0 44 44
221 0 $tests[$current] =~ s/<regex>/\Q$regex\E/mxg;
222 }
223 else {
224 44 $tests[$current] =~ s/<regex>/$regex/mxg;
225 }
226 44 $tests[$current] =~ s/<flags>/$flags/mxg;
227 44 $tests[$current] =~ s/<comment>/$comment/mx;
228 44 my $qregex = $self->_render_regex();
229 44 $tests[$current] =~ s/<qmregex>/$qregex/emx;
230 }
231 }
232  
233 # Call any plugin per_test routines.
234 44 for my $test_code (@tests) {
235 44 $app->stack_test($test_code);
236 44 for my $plugin ($app->plugins) {
237 0 ▶ next if ! $plugin->can('per_test');
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin->can("per_test") 0 0 0
238  
239 0 my ($added_tests, @per_test_code) = $plugin->per_test($self);
240 0 ▶ my $method = $added_tests ? 'stack_test' : 'stack_code';
Branch: 1 branch
BranchTrueFalseTotal
$added_tests ? : 0 0 0
241 0 for my $code_line (@per_test_code) {
242 0 $app->$method($code_line);
243 }
244 }
245 }
246 44 return;
247 }
248  
249 42 1; # Magic true value required at end of module
250 __END__
↑ Summary bin/simple_report »