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   992 use strict;
  4         19  
  4         136  
3 4     4   24 use warnings;
  4         9  
  4         120  
4 4     4   32 use parent 'App::MigrateToTest2V0::Rule';
  4         8  
  4         36  
5 4     4   808 use PPIx::Utils qw(is_function_call);
  4         15318  
  4         253  
6 4     4   29 use List::Util qw(any);
  4         9  
  4         261  
7 4     4   2717 use Test::Deep ();
  4         35416  
  4         159  
8 4     4   36 use constant TEST_DEEP_EXPORTS => [@Test::Deep::EXPORT];
  4         11  
  4         1704  
9              
10             sub apply {
11 14     14 0 40 my ($class, $doc) = @_;
12              
13             my $stmts = $doc->find(sub {
14 343     343   3964 my (undef, $elem) = @_;
15 343   100     1249 return $elem->isa('PPI::Statement::Include') && $elem->module eq 'Test::Deep';
16 14         91 });
17 14 100       231 return unless $stmts;
18              
19             my $tokens = $doc->find(sub {
20 53     53   623 my (undef, $elem) = @_;
21 53   100     199 return $elem->isa('PPI::Token::Word') && any { $elem->content eq $_ } @{ TEST_DEEP_EXPORTS() };
22 4         21 });
23 4   100     55 $tokens ||= [];
24              
25 4         11 for my $token (@$tokens) {
26 4 100       23 next unless is_function_call($token);
27 2         657 $token->set_content('Test::Deep::' . $token->content);
28             }
29              
30             # replace use
31 4         345 for my $stmt (@$stmts) {
32 4         13 my $module_name_token = $stmt->schild(1);
33              
34             # remove arguments
35 4         66 my $elem = $module_name_token->next_sibling;
36 4         84 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         30 $module_name_token->insert_after(PPI::Token::Structure->new(')'));
44 4         199 $module_name_token->insert_after(PPI::Token::Structure->new('('));
45 4         161 $module_name_token->insert_after(PPI::Token::Whitespace->new(' '));
46             }
47             }
48              
49             1;