File Coverage

blib/lib/Dancer2/Plugin/RoutePodCoverage.pm
Criterion Covered Total %
statement 71 75 94.6
branch 17 22 77.2
condition 5 12 41.6
subroutine 10 10 100.0
pod n/a
total 103 119 86.5


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::RoutePodCoverage;
2              
3 3     3   743281 use strict;
  3         7  
  3         124  
4 3     3   20 use warnings;
  3         7  
  3         101  
5              
6 3     3   1793 use Dancer2;
  3         1036379  
  3         24  
7 3     3   230881 use Dancer2::Plugin;
  3         7585  
  3         22  
8 3     3   5075 use Pod::Simple::Search;
  3         20010  
  3         115  
9 3     3   3551 use Pod::Simple::SimpleTree;
  3         137972  
  3         203  
10 3     3   39 use Carp 'croak';
  3         9  
  3         4136  
11              
12             our $VERSION = '0.071';
13              
14             my $PACKAGES_TO_COVER = [];
15              
16             register 'packages_to_cover' => sub {
17 2     2   28 my ( $dsl, $packages_to_cover ) = @_;
18 2 50 33     21 croak "no package(s) provided for 'packages_to_cover' "
      33        
19             if ( !$packages_to_cover
20             || ref $packages_to_cover ne 'ARRAY'
21             || !@$packages_to_cover );
22 2         4 $PACKAGES_TO_COVER = $packages_to_cover;
23             };
24              
25             register 'routes_pod_coverage' => sub {
26 3     3   57 return _get_routes(@_);
27             };
28              
29             sub _get_routes {
30 3     3   7 my ($dsl) = @_;
31 3         5 my @apps = @{ $dsl->runner->apps };
  3         33  
32 3         51 my $all_routes = {};
33              
34 3         8 for my $app (@apps) {
35             next
36 12 100 100     34 if ( @$PACKAGES_TO_COVER && !grep { $app->name eq $_ }
  8         70  
37             @$PACKAGES_TO_COVER );
38 6         195 my $routes = $app->routes;
39 6         58 my $available_routes = [];
40 6         40 foreach my $method ( sort { $b cmp $a } keys %$routes ) {
  65         79  
41 36         299 foreach my $r ( @{ $routes->{$method} } ) {
  36         88  
42              
43             # we don't need pod coverage for head
44 12 100       30 next if $method eq 'head';
45 8         50 push @$available_routes, $method . ' ' . $r->spec_route;
46             }
47             }
48 6 100       24 next unless @$available_routes;
49              
50             ## copy unreferenced array
51 4         23 $all_routes->{ $app->name }{routes} = [@$available_routes];
52              
53 4         8 my $undocumented_routes = [];
54 4         39 my $file = Pod::Simple::Search->new->find( $app->name );
55 4 50       6383 if ($file) {
56 4         19 $all_routes->{ $app->name }{ has_pod } = 1;
57 4         42 my $parser = Pod::Simple::SimpleTree->new->parse_file($file);
58 4         5773 my $pod_dataref = $parser->root;
59 4         28 my $found_routes = {};
60 4         14 for ( my $i = 0 ; $i < @$available_routes ; $i++ ) {
61              
62 8         13 my $r = $available_routes->[$i];
63 8         11 my $app_string = lc $r;
64 8         12 $app_string =~ s/\*/_REPLACED_STAR_/g;
65              
66 8         16 for ( my $idx = 0 ; $idx < @$pod_dataref ; $idx++ ) {
67 64         69 my $pod_part = $pod_dataref->[$idx];
68              
69 64 100       159 next if ref $pod_part ne 'ARRAY';
70 48         54 foreach my $ref_part (@$pod_part) {
71 148 100       274 if (ref($ref_part) eq "ARRAY") {
72 8         12 push @$pod_dataref, $ref_part;
73             }
74             }
75              
76 48         67 my $pod_string = lc $pod_part->[2];
77 48         103 $pod_string =~ s/['|"|\s]+/ /g;
78 48         69 $pod_string =~ s/\s$//g;
79 48         55 $pod_string =~ s/\*/_REPLACED_STAR_/g;
80 48 100       280 if ( $pod_string =~ m/^$app_string$/ ) {
81 4         8 $found_routes->{$app_string} = 1;
82 4         10 next;
83             }
84             }
85 8 100       52 if ( !$found_routes->{$app_string} ) {
86 6         34 push @$undocumented_routes, $r;
87             }
88             }
89             }
90             else { ### no POD found
91 0         0 $all_routes->{ $app->name }{ has_pod } = 0;
92             }
93 4 50 0     10 if (@$undocumented_routes) {
    0          
94 4         24 $all_routes->{ $app->name }{undocumented_routes} = $undocumented_routes;
95             }
96             elsif (! $all_routes->{ $app->name }{ has_pod }
97 0         0 && @{$all_routes->{ $app->name }{routes}} ){
98             ## copy dereferenced array
99 0         0 $all_routes->{ $app->name }{undocumented_routes} = [@{$all_routes->{ $app->name }{routes}}];
  0         0  
100             }
101             }
102 3         26 return $all_routes;
103             }
104              
105             register_plugin;
106              
107             1;
108              
109             __END__