File: rename_subroutine.t

package info (click to toggle)
libdevel-refactor-perl 0.05-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 132 kB
  • sloc: perl: 520; makefile: 2
file content (126 lines) | stat: -rwxr-xr-x 4,708 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/perl
# $Header: $
#

use strict;
use Getopt::Long;
use Test::More tests => 73;
#use Test::More qw( no_plan );
use FindBin qw($Bin);  # Where was this script installed?
use lib "$Bin/.."; # Add .. to @INC;
use Data::Dumper;

use Refactor;

## Parse options
my ($verbose);
GetOptions( 
            "verbose"     => \$verbose,
          );


my $refactory = Devel::Refactor->new($verbose);

my ($where,$old_name,$new_name) = ('t/testfile_1.pl','oldSub','newSub');
ok ($refactory->is_perlfile($where),"$where is a perl file");
my $found;
eval { $found = $refactory->rename_subroutine($where,$old_name,$new_name);};
ok ( $found, "Call rename_subroutine with file '$where'") or
    die("Failed: rename_subroutine($where,$old_name,$new_name)");

# Check if we found all expected instances of $old_name in the file
my %file1_expected = (
    11 => q{# We will eventually want to change the name of newSub} . "\n",
    12 => q{my $string = newSub(1,2,3);} ."\n",
    16 => q{my $string2 = newSub ($string,'a','b');} . "\n",
    21 => q{my $string3 = $object->newSub(6,7);} . "\n",
    25 => q{newSub('d','e','f') or die("Couldn't execute newSub: $!");} . "\n",
    29 => 'sub newSub {' . "\n",
);
my @file1_expected_lines = sort keys %file1_expected;

_check_results ($old_name, $where, $found, \@file1_expected_lines, \%file1_expected);

($where,$old_name,$new_name) = ('t','oldSub','newSub');
eval { $found = $refactory->rename_subroutine($where,$old_name,$new_name);};
ok ( $found, "Call rename_subroutine with directory '$where' and depth of 0") or
    die("Failed: rename_subroutine($where,$old_name,$new_name)");

_check_results ($old_name, 't/testfile_1.pl', $found, \@file1_expected_lines, \%file1_expected);

my %file2_expected = (
    15 => '    $self->newSub(@args);' ."\n",
    18 => 'sub newSub {' . "\n",
);
my @file2_expected_lines = sort keys %file2_expected;

_check_results ($old_name, 't/testfile_2.pm', $found, \@file2_expected_lines, \%file2_expected);

my %file3_expected = (
    8 => 'finds this line of text, because it contains newSub, but it shouldn\'t find' . "\n",
);
my @file3_expected_lines = sort keys %file3_expected;

eval { $found = $refactory->rename_subroutine($where,$old_name,$new_name,1);};
ok ( $found, "Call rename_subroutine with directory '$where' and depth of 1") or
    die("Failed: rename_subroutine($where,$old_name,$new_name)");

_check_results ($old_name, 't/testfile_1.pl', $found, \@file1_expected_lines, \%file1_expected);
_check_results ($old_name, 't/testfile_2.pm', $found, \@file2_expected_lines, \%file2_expected);
_check_results ($old_name, 't/test_subdirectory/testfile_3.pod', $found, \@file3_expected_lines, \%file3_expected);


exit;

###############################################################################

sub _check_results {
    my $old_name       = shift;
    my $where          = shift;
    my $found          = shift;
    my $expected_lines = shift;
    my $expected       = shift;

    foreach my $exp_line (@$expected_lines) {
        my $hash = shift @{ $found->{$where} };
        my ( $line_num, $new_line ) = each %$hash;
      SKIP: {
            ok( $line_num, "Find expected line number $exp_line in $where") ||
                skip "Didn't get line_num/new_line pair for $exp_line in $where",2;
            ok( $exp_line == $line_num, "$where line $line_num - find $old_name" )
              || skip "Didn't find expected change in $where line $line_num", 1;
            ok( $new_line eq $expected->{$line_num},
                "$where line $line_num - replacement line looks correct" )
              || diag
              "Expected: '$expected->{$line_num}'\nGot       '$new_line'";
        }
    }
}

__END__
ok ($found == scalar @expected_line_numbers, "Found all $expected_count instances of $old_name") or
	diag "Found $found instances, expected $expected_count", Dumper($changed->{$where});
 
 # Try looking in a directory of files.

($where,$old_name,$new_name,$how_deep) = ('t','oldSub','newSub',1);
eval { $changed = $refactory->rename_subroutine($where,$old_name,$new_name,$how_deep);};
ok ( $changed && ($@ eq ''), "Call rename_subroutine with directory '$where'") or
   diag "$@\n", Dumper($changed);

# TODO: This hash could contain lists of expected line numbers
my %expected_files = ( 't/testfile_1.pl' => 1, 't/testfile_2.pm' => 1);
$found = 0;
my $extra = 0;
foreach my $f (keys %expected_files) {
    $found++ if exists $changed->{$f};
}

foreach my $f (keys %{$changed}) {
    $extra++ unless exists $expected_files{$f};
}

ok ($found == 2, "Found all files.") or
    diag "Found: $found\n", Dumper($changed);
ok ($extra == 0, "No extra files found.") or
    diag "Extra: $extra\n", Dumper($changed);