File Coverage

blib/lib/App/MigrateToTest2V0/Rule/AvoidNameConflictWithTestDeep.pm
Criterion Covered Total %
statement 40 43 93.0
branch 4 4 100.0
condition 8 8 100.0
subroutine 10 10 100.0
pod 0 1 0.0
total 62 66 93.9


line stmt bran cond sub pod time code
1             package App::MigrateToTest2V0::Rule::AvoidNameConflictWithTestDeep;
2 4     4   1162 use strict;
  4         8  
  4         133  
3 4     4   21 use warnings;
  4         10  
  4         116  
4 4     4   31 use parent 'App::MigrateToTest2V0::Rule';
  4         10  
  4         33  
5 4     4   811 use PPIx::Utils qw(is_function_call);
  4         16513  
  4         248  
6 4     4   33 use List::Util qw(any);
  4         18  
  4         268  
7 4     4   2867 use Test::Deep ();
  4         35778  
  4         171  
8 4     4   41 use constant TEST_DEEP_EXPORTS => [@Test::Deep::EXPORT];
  4         11  
  4         1701  
9              
10             sub apply {
11 14     14 0 58 my ($class, $doc) = @_;
12              
13             my $stmts = $doc->find(sub {
14 343     343   3930 my (undef, $elem) = @_;
15 343   100     1306 return $elem->isa('PPI::Statement::Include') && $elem->module eq 'Test::Deep';
16 14         118 });
17 14 100       242 return unless $stmts;
18              
19             my $tokens = $doc->find(sub {
20 53     53   679 my (undef, $elem) = @_;
21 53   100     222 return $elem->isa('PPI::Token::Word') && any { $elem->content eq $_ } @{ TEST_DEEP_EXPORTS() };
22 4         27 });
23 4   100     63 $tokens ||= [];
24              
25 4         18 for my $token (@$tokens) {
26 4 100       30 next unless is_function_call($token);
27 2         662 $token->set_content('Test::Deep::' . $token->content);
28             }
29              
30             # replace use
31 4         482 for my $stmt (@$stmts) {
32 4         17 my $module_name_token = $stmt->schild(1);
33              
34             # remove arguments
35 4         73 my $elem = $module_name_token->next_sibling;
36 4         106 while ($elem->content ne ';') {
37 0         0 my $next = $elem->next_sibling;
38 0         0 $elem->remove;
39 0         0 $elem = $next;
40             }
41              
42             # add ()
43 4         39 $module_name_token->insert_after(PPI::Token::Structure->new(')'));
44 4         293 $module_name_token->insert_after(PPI::Token::Structure->new('('));
45 4         167 $module_name_token->insert_after(PPI::Token::Whitespace->new(' '));
46             }
47             }
48              
49             1;