| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
-e $filename and not $force
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | push @$rows, my $row = [$prefix . ($route->pattern->unparsed || '/')]; | ||||||||||||||||||||||||||
|
Condition0%0/2
$route->pattern->unparsed || '/'
MC/DC0%0/1
$route->pattern->unparsed || '/'
$route->pattern->unparsed
Truth table0%0/2
A
A= $route->pattern->unparsed
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Flags | ||||||||||||||||||||||||||||
| 77 | 0 | my @flags; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | push @flags, @{$route->over || []} ? 'C' : '.'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$route->over || []
MC/DC0%0/1
$route->over || []
$route->over
Truth table0%0/2
A
A= $route->over
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | push @flags, (my $partial = $route->partial) ? 'D' : '.'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | push @flags, $route->inline ? 'U' : '.'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | push @flags, $route->is_websocket ? 'W' : '.'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | push @$row, join('', @flags) if $verbose; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Methods | ||||||||||||||||||||||||||||
| 85 | 0 | my $via = $route->via; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | push @$row, !$via ? '*' : uc join ',', @$via; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Name | ||||||||||||||||||||||||||||
| 89 | 0 | my $name = $route->name; | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | push @$row, $route->has_custom_name ? qq{"$name"} : $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$route->is_endpoint && !$partial
$route->is_endpoint
!$partial
Truth table0%0/3
A && not(B)
A= $route->is_endpointB= !$partial
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | $depth++; | |||||||||||||||||||||||||||
| 100 | 0 | _walk($_, $depth, $rows, $verbose) for @{$route->children}; | |||||||||||||||||||||||||||
| 101 | 0 | $depth--; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | 1; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | __END__ | ||||||||||||||||||||||||||||