blib/lib/Mojolicious/Plugin/PlainRoutes.pm

filter: Statementstmt 86.9% 107 / 123 Branchbran 79.4% 54 / 68 Conditioncond 75.0% 15 / 20 MC/DCmcdc 61.5% 8 / 13 Subroutinesub 87.5% 7 / 8 Podpod 33.3% 1 / 3 totaltotal 81.7% 192 / 235 CC 45 SCAR 33.5
CC
45
Cov
83%
_tokenise
40.7
process
9.1
tokenise
3.1
CRAP 28.5
↑ Summary
1 4 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Mojolicious::Plugin::PlainRoutes;
3 # ABSTRACT: Plaintext route definitions for Mojolicious
4 4 $Mojolicious::Plugin::PlainRoutes::VERSION = '0.07';
5 4 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Mojo::Util qw/decamelize/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 has autoname => 0;
9  
10 4 ▶sub register {
Subroutine register: called CC=2 SCAR=7.1
Pod documented
11 4 my ($self, $app, $conf) = @_;
12  
13 4 $self->autoname($conf->{autoname});
14  
15 4 ▶ $conf->{file} //= $app->home->rel_file("lib/".$app->moniker.".routes");
Condition33%1/3
$conf->{"file"} //= $app->home->rel_file('lib/' . $app->moniker . '.routes')
l!l&&r!l&&!r
400
MC/DC0%0/2
$conf->{"file"} //= $app->home->rel_file('lib/' . $app->moniker . '.routes')
$conf->{"file"} $app->home->rel_file('lib/' . $app->moniker . '.routes')
Truth table33%1/3
A //= B
A= $conf->{"file"}B= $app->home->rel_file('lib/' . $app->moniker . '.routes')
ABresult
1X1
011
000
16  
17 4 open my $fh, '<:encoding(UTF-8)', $conf->{file};
18 4 my $tree = $self->tokenise($fh);
19 4 close $fh;
20  
21 4 $self->process($app->routes, $tree);
22 }
23  
24 13 ▶sub tokenise {
Subroutine tokenise: called CC=3 SCAR=11.4
Pod undocumented
25 13 my ($self, $input) = @_;
26  
27 13 ▶ if (ref $input eq 'GLOB') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $input eq 'GLOB') { } 4 9 13
elsif (ref $input) { } 0 9 9
28 4 $input = do { local $/; <$input> };
29 } elsif (ref $input) {
30 0 Carp::carp "Non-filehandle reference passed to tokenise";
31 0 return [];
32 }
33  
34 13 return $self->_tokenise($input);
35 }
36  
37 13 ▶sub _tokenise {
Subroutine _tokenise: called CC=34 SCAR=37.1
38 13 my ($self, $input) = @_;
39  
40 13 $input =~ s/\r\n/\n/g;
41 13 $input =~ s/\n\r/\n/g;
42 13 $input =~ s/\r/\n/g;
43  
44 13 my %grammar = (
45 comment => qr{ \# [^\n]* }x,
46 verb => qr{ ANY | DELETE | GET | PATCH | POST | PUT }x,
47 path => qr{ / [^#\s]* }x,
48 arrow => qr{ -> }x,
49 scope => qr( { | } )x,
50 action => qr{ [\w\-:]* \. \w* }x,
51 name => qr{ \( [^)]+ \) }x,
52 eol => qr{ \n }x,
53 space => qr{ [^\S\n]+ }x,
54 );
55  
56 13 ▶ my @words = grep { defined && length }
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
0263263
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
57 split m{( $grammar{comment}
58 | $grammar{verb}
59 | $grammar{path}
60 | $grammar{arrow}
61 | $grammar{scope}
62 | $grammar{action}
63 | $grammar{name}
64 | $grammar{eol}
65 | $grammar{space}
66 )}x, $input;
67  
68 # Include the lexical category with the word, e.g., map:
69 # "/foo" -> { text => "/foo", category => "path" }
70 13 my @annotated_words;
71 13 for my $word (@words) {
72 263 my @cats = grep { $word =~ /^$grammar{$_}$/ } keys %grammar;
73  
74 263 ▶ if (@cats > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cats > 1) 0 263 263
75 0 warn "$word has multiple lexical categories: @cats";
76 }
77  
78 263 ▶ push @annotated_words, { text => $word, category => $cats[0] // '' };
Condition50%1/2
$cats[0] // ""
l!l
2630
MC/DC0%0/1
$cats[0] // ""
$cats[0]
Truth table50%1/2
A
A= $cats[0]
Aresult
11
00
79 }
80  
81 # Add special EOF word to act as a clause terminator if necessary
82 13 push @annotated_words, { text => '', category => 'eof' };
83  
84 # Initialise
85 13 my $root = [];
86 13 my @nodes = ($root);
87 13 my %clause = ();
88 13 my $context = 'default';
89  
90 # Track for helpful error messages
91 13 my $col = 1;
92 13 my $line = 1;
93 13 my $error = 0;
94  
95 # Define outside the loop scope so that the closure can access it
96 13 my %word;
97  
98 # Called whenever a syntax error is encountered.
99 0 ▶ my $syntax_error = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 $error = 1;
101 0 my $_col = $col - length $word{text};
102 0 print STDERR qq{Syntax error in routes on line $line, col $_col: }
103 . qq{"$word{text}" (expected a @_)\n};
104 13 };
105  
106 13 for (@annotated_words) {
107 276 %word = %$_;
108 276 $col += length $word{text};
109 276 ▶ if ($word{category} eq 'eol') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word{"category"} eq "eol") 27 249 276
110 27 $line += 1;
111 27 $col = 1;
112 }
113  
114 # While in comment context, the parser checks for newlines and
115 # otherwise does nothing.
116 276 ▶ if ($context eq 'comment') {
Branch: 6 branches
BranchTrueFalseTotal
if ($context eq 'comment') { } 2 274 276
elsif ($word{'category'} eq 'comment') { } 2 272 274
elsif ($word{'category'} eq 'space' or $word{'category'} eq 'eol') { } 132 140 272
elsif (not exists $clause{'verb'}) { } 27 113 140
elsif (not exists $clause{'path'}) { } 28 85 113
elsif (not exists $clause{'action'}) { } 55 30 85
117 2 ▶ if ($word{category} eq 'eol') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word{"category"} eq "eol") 2 0 2
118 2 $context = 'default';
119 }
120 }
121  
122 # The comment indicator puts the parser into comment context and
123 # otherwise does nothing.
124 elsif ($word{category} eq 'comment') {
125 2 $context = 'comment';
126 }
127  
128 # Whitespace is ignored
129 ▶ elsif ($word{category} eq 'space' || $word{category} eq 'eol') {}
Condition100%3/3
$word{"category"} eq "space" or $word{"category"} eq "eol"
l!l&&r!l&&!r
10725140
MC/DC100%2/2
$word{"category"} eq "space" or $word{"category"} eq "eol"
$word{"category"} eq "space" $word{"category"} eq "eol"
Truth table100%3/3
A or B
A= $word{"category"} eq "space"B= $word{"category"} eq "eol"
ABresult
1X1
011
000
130  
131 # First word in clause must be a HTTP verb
132 elsif (!exists $clause{verb}) {
133 27 ▶ if ($word{category} eq 'verb') {
Branch: 3 branches
BranchTrueFalseTotal
if ($word{'category'} eq 'verb') { } 22 5 27
elsif ($word{'category'} eq 'scope' and $word{'text'} eq '}') { } 1 4 5
elsif ($word{'category'} ne 'eof') { } 0 4 4
134 22 $clause{verb} = $word{text};
135 }
136  
137 # The end of scope may be encountered here if there were two ends
138 # of scope in a row.
139 ▶ elsif ($word{category} eq 'scope' && $word{text} eq '}') {
Condition66%2/3
$word{"category"} eq "scope" and $word{"text"} eq "}"
!ll&&!rl&&r
401
MC/DC50%1/2
$word{"category"} eq "scope" and $word{"text"} eq "}"
$word{"category"} eq "scope" $word{"text"} eq "}"
Truth table66%2/3
A and B
A= $word{"category"} eq "scope"B= $word{"text"} eq "}"
ABresult
0X0
100
111
140 1 ▶ if (@nodes == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nodes == 1) { } 0 1 1
141 0 'verb'->$syntax_error;
142 } else {
143 1 pop @nodes;
144 }
145 }
146  
147 # It's possible we encounter the EOF word here if we just
148 # encountered the end of a scope (or if the input is empty).
149 # Anything else is still a syntax error.
150 elsif ($word{category} ne 'eof') {
151 0 'verb'->$syntax_error;
152 }
153 }
154  
155 # Second word must be a path part
156 elsif (!exists $clause{path}) {
157 28 ▶ if ($word{category} eq 'path') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word{'category'} eq 'path') { } 28 0 28
158 28 $clause{path} = $word{text};
159 } else {
160 0 'path'->$syntax_error;
161 }
162 }
163  
164 # Third word must be an action, optionally preceded by an arrow (->)
165 elsif (!exists $clause{action}) {
166 55 ▶ if (!exists $clause{arrow} && $word{category} eq 'arrow') {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $clause{'arrow'} and $word{'category'} eq 'arrow') { } 27 28 55
elsif ($word{'category'} eq 'action') { } 28 0 28
Condition100%3/3
not exists $clause{"arrow"} and $word{"category"} eq "arrow"
!ll&&!rl&&r
27127
MC/DC100%2/2
not exists $clause{"arrow"} and $word{"category"} eq "arrow"
not exists $clause{"arrow"} $word{"category"} eq "arrow"
Truth table100%3/3
not(A) and B
A= not exists $clause{"arrow"}B= $word{"category"} eq "arrow"
ABresult
0X0
100
111
167 27 $clause{arrow} = 1;
168 } elsif ($word{category} eq 'action') {
169 28 my ($action, $controller) = split /\./, $word{text};
170 28 $clause{action} = decamelize($action) . "#$controller";
171  
172 # The clause needn't carry this useless information after this
173 # point.
174 28 delete $clause{arrow};
175 } else {
176 0 'action'->$syntax_error;
177 }
178 }
179  
180 # The final word should be some kind of terminator: scope indicators,
181 # the beginning of a new clause (i.e., a verb), or the end of input.
182 else {
183 # An optional name for the clause can be appended before the
184 # terminator.
185 30 ▶ if (!exists $clause{name} && $word{category} eq 'name') {
Branch: 4 branches
BranchTrueFalseTotal
if (not exists $clause{'name'} and $word{'category'} eq 'name') { } 2 28 30
elsif ($word{'category'} eq 'scope') { } 13 15 28
elsif ($word{'category'} eq 'verb') { } 6 9 15
elsif ($word{'category'} eq 'eof') { } 9 0 9
Condition100%3/3
not exists $clause{"name"} and $word{"category"} eq "name"
!ll&&!rl&&r
2262
MC/DC100%2/2
not exists $clause{"name"} and $word{"category"} eq "name"
not exists $clause{"name"} $word{"category"} eq "name"
Truth table100%3/3
not(A) and B
A= not exists $clause{"name"}B= $word{"category"} eq "name"
ABresult
0X0
100
111
186 2 $clause{name} = $word{text} =~ s/ ^\( | \)$ //xgr;
187 }
188  
189 # The clause is terminated by a new scope.
190 elsif ($word{category} eq 'scope') {
191 # A new scope means that the preceding clause is a bridge, and
192 # therefore the head of a new branch in the tree.
193 13 ▶ if ($word{text} eq '{') {
Branch: 2 branches
BranchTrueFalseTotal
if ($word{'text'} eq '{') { } 7 6 13
elsif ($word{'text'} eq '}') { } 6 0 6
194 7 my $newNode = [ { %clause } ];
195 7 push @{ $nodes[-1] }, $newNode;
196 7 push @nodes, $newNode;
197  
198 7 %clause = ();
199 }
200  
201 # The end of a scope means that the preceding clause is the
202 # last clause in a bridge.
203 elsif ($word{text} eq '}') {
204 6 push @{ $nodes[-1] }, { %clause };
205 6 %clause = ();
206  
207 # Can't exit a scope if we haven't entered one
208 6 ▶ if (@nodes == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nodes == 1) { } 0 6 6
209 0 'verb'->$syntax_error;
210 } else {
211 6 pop @nodes;
212 }
213 }
214 }
215  
216 # The clause is terminated by the start of a new one
217 elsif ($word{category} eq 'verb') {
218 6 push @{ $nodes[-1] }, { %clause };
219 6 %clause = ( verb => $word{text} );
220 }
221  
222 # Last chance, the clause is terminated by eof
223 elsif ($word{category} eq 'eof') {
224 9 push @{ $nodes[-1] }, { %clause };
225 9 %clause = ();
226 }
227  
228 else {
229 0 'terminator'->$syntax_error;
230 }
231 }
232 }
233  
234 13 ▶ if (@nodes != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nodes != 1) 0 13 13
235 0 'verb or end of scope'->$syntax_error;
236 }
237  
238 13 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 13 13
239 0 Carp::croak "Parsing routes failed due to syntax errors";
240 }
241  
242 13 $root;
243 }
244  
245 5 ▶sub process {
Subroutine process: called CC=9 SCAR=22.1
Pod undocumented
246 5 my ($self, $bridge, $tree) = @_;
247  
248 5 for my $node (@$tree) {
249 9 ▶ my $token = ref $node eq 'ARRAY' ? shift @$node : $node;
Branch: 1 branch
BranchTrueFalseTotal
ref $node eq 'ARRAY' ? : 1 8 9
250  
251 my $route = $bridge->any($token->{path})
252 9 ->to($token->{action});
253 9 ▶ if ($token->{verb} ne 'ANY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"verb"} ne "ANY") 8 1 9
254 8 $route->methods($token->{verb});
255 }
256  
257 9 my $p = $route->pattern;
258 9 ▶ if (exists $token->{name}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $token->{'name'}) { } 0 9 9
elsif (ref $self->autoname eq 'CODE') { } 1 8 9
elsif ($self->autoname) { } 1 7 8
259 0 $route->name($token->{name});
260 }
261 elsif (ref $self->autoname eq 'CODE') {
262 my $name = $self->autoname->($route->methods->[0], $p->unparsed,
263 1 @{$p->defaults}{qw/controller action/});
264  
265 1 ▶ if (ref $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name) 0 1 1
266 0 Carp::croak "Autoname callback did not return a string";
267 }
268  
269 1 $route->name($name);
270 }
271 elsif ($self->autoname) {
272 1 $route->name(join '-', @{$p->defaults}{qw/controller action/});
273 }
274  
275 9 ▶ if (ref $node eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node eq "ARRAY") 1 8 9
276 1 $route->inline(1);
277 1 $self->process($route, $node);
278 }
279 }
280 }
281  
282 4 1;
283  
284 __END__
↑ Summary