blib/lib/Mojolicious/Command/swat.pm

filter: Statementstmt 22.8% 16 / 70 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 57.1% 4 / 7 Podpod 100% 1 / 1 totaltotal 17.2% 21 / 122 CC 22 SCAR 50.1
CC
22
Cov
15%
_walk
240.0
run
72.0
__ANON__
1.1
CRAP 149.3
↑ Summary
1 package Mojolicious::Command::swat;
2  
3 1 our $VERSION = '0.0.5';
4  
5 1 ▶use Mojo::Base 'Mojolicious::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use re 'regexp_pattern';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 has description => 'Generates swat tests scaffolding for mojo application';
12 0 ▶has usage => sub { shift->extract_usage };
Subroutine __ANON__: not called CC=1 SCAR=1.2
13  
14 0 ▶sub run {
Subroutine run: not called CC=8 SCAR=42.8
Pod documented
15  
16 0 my ($self, @args) = @_;
17  
18 0 GetOptionsFromArray \@args, 'f|force' => \my $force;
19  
20 0 mkdir 'swat';
21  
22 0 my $hostname_file = 'swat/host';
23  
24 0 ▶ open(my $fh, '>', $hostname_file) or die "Could not open file $hostname_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $hostname_file or die "Could not open file ${hostname_file}: $!" 0 0 0
25 0 print $fh "http://127.0.0.1:3000";
26 0 close $fh;
27
28
29 0 my $rows = [];
30  
31 0 _walk($_, 0, $rows, 0) for @{$self->app->routes->children};
32  
33 0 ROUTE: for my $i (@$rows){
34  
35 0 my $http_method = $i->[1];
36 0 my $route = $i->[0];
37  
38 0 ▶ unless ($http_method=~/GET|POST|DELETE|PUT/i){
Branch: 1 branch
BranchTrueFalseTotal
unless ($http_method =~ /GET|POST|DELETE|PUT/iu) 0 0 0
39 0 print "sorry, swat does not support $http_method methods yet \n";
40 0 next ROUTE;
41 }
42  
43 0 my $filename = "swat/$route/";
44
45 0 ▶ if (-e $filename and !$force){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename and not $force) { } 0 0 0
Condition0%0/3
-e $filename and not $force
!ll&&!rl&&r
000
MC/DC0%0/2
-e $filename and not $force
-e $filename not $force
Truth table0%0/3
A and not(B)
A= -e $filenameB= not $force
ABresult
0X0
100
111
46  
47 0 print "skip route $route - swat test already exist, use --force to override existed routes \n";
48 0 next ROUTE;
49  
50 } else {
51  
52 0 print "generate swat route for $route ... \n";
53 0 make_path("swat/$route");
54
55 0 print "generate swat data for $http_method $route ... \n";
56
57 0 $filename.=lc($http_method); $filename.=".txt";
58 0 ▶ open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or die "Could not open file '${filename}' $!" 0 0 0
59 0 print $fh "200 OK\n";
60 0 close $fh;
61 }
62  
63 }
64  
65 }
66  
67 0 ▶sub _walk {
Subroutine _walk: not called CC=15 SCAR=54.8
68  
69 0 my ($route, $depth, $rows, $verbose) = @_;
70  
71 # Pattern
72 0 my $prefix = '';
73 0 ▶ if (my $i = $depth * 2) { $prefix .= ' ' x $i . '+' }
Branch: 1 branch
BranchTrueFalseTotal
if (my $i = $depth * 2) 0 0 0
74 0 ▶ push @$rows, my $row = [$prefix . ($route->pattern->unparsed || '/')];
Condition0%0/2
$route->pattern->unparsed || '/'
l!l
00
MC/DC0%0/1
$route->pattern->unparsed || '/'
$route->pattern->unparsed
Truth table0%0/2
A
A= $route->pattern->unparsed
Aresult
11
00
75  
76 # Flags
77 0 my @flags;
78 0 ▶ push @flags, @{$route->over || []} ? 'C' : '.';
Branch: 1 branch
BranchTrueFalseTotal
@{$route->over or [];} ? : 0 0 0
Condition0%0/2
$route->over || []
l!l
00
MC/DC0%0/1
$route->over || []
$route->over
Truth table0%0/2
A
A= $route->over
Aresult
11
00
79 0 ▶ push @flags, (my $partial = $route->partial) ? 'D' : '.';
Branch: 1 branch
BranchTrueFalseTotal
(my $partial = $route->partial) ? : 0 0 0
80 0 ▶ push @flags, $route->inline ? 'U' : '.';
Branch: 1 branch
BranchTrueFalseTotal
$route->inline ? : 0 0 0
81 0 ▶ push @flags, $route->is_websocket ? 'W' : '.';
Branch: 1 branch
BranchTrueFalseTotal
$route->is_websocket ? : 0 0 0
82 0 ▶ push @$row, join('', @flags) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
83  
84 # Methods
85 0 my $via = $route->via;
86 0 ▶ push @$row, !$via ? '*' : uc join ',', @$via;
Branch: 1 branch
BranchTrueFalseTotal
!$via ? : 0 0 0
87  
88 # Name
89 0 my $name = $route->name;
90 0 ▶ push @$row, $route->has_custom_name ? qq{"$name"} : $name;
Branch: 1 branch
BranchTrueFalseTotal
$route->has_custom_name ? : 0 0 0
91  
92 # Regex (verbose)
93 0 my $pattern = $route->pattern;
94 0 ▶ $pattern->match('/', $route->is_endpoint && !$partial);
Condition0%0/3
$route->is_endpoint && !$partial
!ll&&!rl&&r
000
MC/DC0%0/2
$route->is_endpoint && !$partial
$route->is_endpoint !$partial
Truth table0%0/3
A && not(B)
A= $route->is_endpointB= !$partial
ABresult
0X0
100
111
95 0 my $regex = (regexp_pattern $pattern->regex)[0];
96 0 my $format = (regexp_pattern($pattern->format_regex))[0];
97 0 ▶ push @$row, $regex, $format ? $format : '' if $verbose;
Branch: 2 branches
BranchTrueFalseTotal
if $verbose 0 0 0
$format ? : 0 0 0
98  
99 0 $depth++;
100 0 _walk($_, $depth, $rows, $verbose) for @{$route->children};
101 0 $depth--;
102 }
103  
104 1 1;
105  
106 __END__
↑ Summary