File Coverage

blib/lib/App/Midgen/Role/UseOk.pm
Criterion Covered Total %
statement 21 78 26.9
branch 0 40 0.0
condition 0 11 0.0
subroutine 7 10 70.0
pod 1 1 100.0
total 29 140 20.7


line stmt bran cond sub pod time code
1             package App::Midgen::Role::UseOk;
2              
3 2     2   992 use constant {BLANK => q{ }, NONE => q{}, TWO => 2, THREE => 3,};
  2         2  
  2         157  
4              
5 2     2   9 use Moo::Role;
  2         2  
  2         13  
6             requires
7             qw( ppi_document debug verbose format xtest _process_found_modules develop meta2 );
8              
9             # Load time and dependencies negate execution time
10             # use namespace::clean -except => 'meta';
11              
12             our $VERSION = '0.33_05';
13             $VERSION = eval $VERSION; ## no critic
14              
15 2     2   3559 use PPI;
  2         4  
  2         32  
16 2     2   6 use Try::Tiny;
  2         3  
  2         104  
17 2     2   7 use Data::Printer {caller_info => 1,};
  2         2  
  2         23  
18 2     2   1604 use Tie::Static qw(static);
  2         2  
  2         1280  
19              
20             #use List::MoreUtils qw( lastidx );
21              
22             #######
23             # composed method - _xtests_in_single_quote
24             #######
25             sub xtests_use_ok {
26 0     0 1   my $self = shift;
27 0   0       my $phase_relationship = shift || NONE;
28 0           my @modules;
29             my @version_strings;
30              
31             #PPI::Document
32             # PPI::Statement::Scheduled
33             # PPI::Token::Word 'BEGIN'
34             # PPI::Token::Whitespace ' '
35             # PPI::Structure::Block { ... }
36             # PPI::Token::Whitespace '\n'
37             # PPI::Token::Whitespace '\t'
38             # PPI::Statement
39             # PPI::Token::Word 'use_ok'
40             # PPI::Structure::List ( ... )
41             # PPI::Token::Whitespace ' '
42             # PPI::Statement::Expression
43             # PPI::Token::Quote::Single ''Term::ReadKey''
44             # PPI::Token::Operator ','
45             # PPI::Token::Whitespace ' '
46             # PPI::Token::Quote::Single ''2.30''
47              
48             my @chunks =
49              
50 0           map { [$_->schildren] }
51 0           grep { $_->child(0)->literal =~ m{\A(?:BEGIN)\z} }
52 0           grep { $_->child(0)->isa('PPI::Token::Word') }
53 0 0         @{$self->ppi_document->find('PPI::Statement::Scheduled') || []};
  0            
54              
55 0           foreach my $hunk (@chunks) {
56              
57             # looking for use_ok { 'Term::ReadKey' => '2.30' };
58 0 0         if (grep { $_->isa('PPI::Structure::Block') } @$hunk) {
  0            
59              
60             # hack for List
61 0           my @hunkdata = @$hunk;
62              
63 0           foreach my $ppi_sb (@hunkdata) {
64 0 0         if ($ppi_sb->isa('PPI::Structure::Block')) {
65 0           foreach my $ppi_s (@{$ppi_sb->{children}}) {
  0            
66 0 0         if ($ppi_s->isa('PPI::Statement')) {
67 0 0         p $ppi_s if $self->debug;
68 0 0         if ($ppi_s->{children}[0]->content eq 'use_ok') {
69 0           my $ppi_sl = $ppi_s->{children}[1];
70 0           foreach my $ppi_se (@{$ppi_sl->{children}}) {
  0            
71 0 0         if ($ppi_se->isa('PPI::Statement::Expression')) {
72 0           foreach my $element (@{$ppi_se->{children}}) {
  0            
73              
74             # some fudge to remember the module name if falied
75 0           static \my $previous_module;
76 0 0 0       if ( $element->isa('PPI::Token::Quote::Single')
77             || $element->isa('PPI::Token::Quote::Double'))
78             {
79              
80 0           my $module = $element->content;
81 0           $module =~ s/^(?:['|"])//;
82 0           $module =~ s/(?:['|"])$//;
83 0 0         if ($module =~ m{\A(?:[[a-zA-Z])}) {
84              
85 0 0         print "found module - $module\n" if $self->debug;
86 0           push @modules, $module;
87 0           $version_strings[$#modules] = undef;
88 0           $previous_module = $module;
89             }
90             }
91              
92              
93 0 0 0       if ( $element->isa('PPI::Token::Number::Float')
      0        
94             || $element->isa('PPI::Token::Quote::Single')
95             || $element->isa('PPI::Token::Quote::Double'))
96             {
97              
98 0           my $version_string = $element->content;
99              
100 0           $version_string =~ s/^(?:['|"])//;
101 0           $version_string =~ s/(?:['|"])$//;
102 0 0         next if $version_string !~ m{\A(?:[\d|v])};
103              
104 0 0         $version_string
105             = version::is_lax($version_string)
106             ? $version_string
107             : 0;
108              
109 0 0         print "found version_string - $version_string\n"
110             if $self->debug;
111             try {
112 0 0   0     if ($previous_module) {
113 0           $self->{found_version}{$previous_module}
114             = $version_string;
115 0           $version_strings[$#modules] = $version_string;
116             }
117              
118 0           $previous_module = undef;
119 0           };
120             }
121             }
122             }
123             }
124             }
125             }
126             }
127             }
128             }
129             }
130             }
131              
132 0 0         @version_strings = map { defined $_ ? $_ : 0 } @version_strings;
  0            
133 0 0         p @modules if $self->debug;
134 0 0         p @version_strings if $self->debug;
135              
136 0 0         if (scalar @modules > 0) {
137              
138 0           for (0 .. $#modules) {
139 0 0         print "Info: UseOk -> Sending $modules[$_] - $version_strings[$_]\n"
140             if ($self->verbose == TWO);
141             try {
142 0     0     $self->_process_found_modules(
143             $phase_relationship, $modules[$_], $version_strings[$_],
144             __PACKAGE__, $phase_relationship,
145             );
146 0           };
147             }
148             }
149 0           return;
150             }
151              
152 2     2   10 no Moo::Role;
  2         4  
  2         11  
153              
154             1;
155              
156             __END__
157              
158             =pod
159              
160             =encoding UTF-8
161              
162             =head1 NAME
163              
164             App::Midgen::Roles::UseOk - extra checks for test files, looking
165             for methods in use_ok in BEGIN blocks, used by L<App::Midgen>
166              
167             =head1 VERSION
168              
169             version: 0.33_05
170              
171             =head1 METHODS
172              
173             =over 4
174              
175             =item * xtests_use_ok
176              
177             Checking for the following, extracting module name and version string.
178              
179             BEGIN {
180             use_ok( 'Term::ReadKey', '2.30' );
181             use_ok( 'Term::ReadLine', '1.10' );
182             use_ok( 'Fred::BloggsOne', '1.01' );
183             use_ok( "Fred::BloggsTwo", "2.02" );
184             use_ok( 'Fred::BloggsThree', 3.03 );
185             }
186              
187             Used to check files in t/ and xt/ directories.
188              
189             =back
190              
191             =head1 AUTHOR
192              
193             See L<App::Midgen>
194              
195             =head2 CONTRIBUTORS
196              
197             See L<App::Midgen>
198              
199             =head1 COPYRIGHT
200              
201             See L<App::Midgen>
202              
203             =head1 LICENSE
204              
205             This library is free software; you can redistribute it and/or modify
206             it under the same terms as Perl itself.
207              
208             =cut