File: App-PRT-Command-RenameNameSpace.t

package info (click to toggle)
prt 0.22-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 404 kB
  • sloc: perl: 1,185; makefile: 3
file content (140 lines) | stat: -rw-r--r-- 3,586 bytes parent folder | download | duplicates (5)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package t::App::PRT::Command::RenameNameSpace;
use t::test;

sub _require : Test(startup => 1) {
    my ($self) = @_;

    use_ok 'App::PRT::Command::RenameNameSpace';
}

sub instantiate : Tests {
    isa_ok App::PRT::Command::RenameNameSpace->new, 'App::PRT::Command::RenameNameSpace';
}

sub register_rule : Tests {
    my $command = App::PRT::Command::RenameNameSpace->new;

    $command->register('Foo' => 'Bar');

    is $command->source_name_space, 'Foo';
    is $command->destination_name_space, 'Bar';
}

sub parse_arguments : Tests {
    subtest "when source and destination specified" => sub {
        my $command = App::PRT::Command::RenameNameSpace->new;
        my @args = qw(From To a.pl lib/B.pm);


        my @args_after = $command->parse_arguments(@args);

        is $command->source_name_space, 'From';
        is $command->destination_name_space, 'To';

        cmp_deeply \@args_after, [qw(a.pl lib/B.pm)], 'parse_arguments returns rest arguments';
    };

    subtest "when arguments are not enough" => sub {
        my $command = App::PRT::Command::RenameClass->new;

        ok exception {
            $command->parse_arguments('NotEnough');
        }, 'died';
    };

}

sub _collect_target_classes : Tests {
    my $command = App::PRT::Command::RenameNameSpace->new;
    $command->register('My' => 'Our');

    my $directory = t::test::prepare_test_code('dinner');
    cmp_bag $command->_collect_target_classes([
        "$directory/lib/My/Food.pm",
        "$directory/lib/My/Human.pm",
        "$directory/lib/Your/Food.pm",
        "$directory/dinner.pl",
    ]), [
        'My::Food',
        'My::Human',
    ], 'classes under My:: are collected';
}

sub _destination_class_name : Tests {
    my $command = App::PRT::Command::RenameNameSpace->new;
    $command->register('My' => 'Our');

    is $command->_destination_class_name('My::Food'), 'Our::Food', 'converted';
    is $command->_destination_class_name('The::My::Food'), undef, 'not match';
    is $command->_destination_class_name('Our::Food'), undef, 'not match';
}

sub execute : Tests {
    my $directory = t::test::prepare_test_code('dinner');

    my $command = App::PRT::Command::RenameNameSpace->new;
    $command->register('My' => 'Our');

    $command->execute_files([
        "$directory/dinner.pl",
        "$directory/lib/My/Food.pm",
        "$directory/lib/My/Human.pm",
        "$directory/lib/Your/Food.pm",
        "$directory/t/001-my-food._t",
        "$directory/t/My-Food._t",
    ]);

    ok -f "$directory/dinner.pl", "script exists";

    is file("$directory/dinner.pl")->slurp, <<'CODE', 'My:: was moved to Our::';
use strict;
use warnings;
use lib 'lib';

use Our::Human;
use Our::Food;

undef *Our::Food::new;
undef *My::Food::Foo::new;

my $human = Our::Human->new('Alice');
my $food = Our::Food->new('Pizza');

$human->eat($food);
CODE

    ok ! -f "$directory/lib/My/Food.pm", "My::Food doesn't exist";
    ok ! -f "$directory/lib/My/Human.pm", "My::Human doesn't exist";
    ok -f "$directory/lib/Our/Food.pm", "Our::Food exist";
    ok -f "$directory/lib/Our/Human.pm", "Our::Human exist";

    is file("$directory/lib/Our/Food.pm")->slurp, <<'CODE', 'target class replaced';
package Our::Food;
use strict;
use warnings;
$Our::Food::SOME_MAGIC_NUMBER = '0.01';
$My::Food::Foo::GLOBAL_VAR = 'foobar';

sub new {
    my ($class, $name) = @_;

    bless {
        name => $name,
    }, $class;
}

sub name {
    my ($self) = @_;

    $self->{name};
}

1;
CODE

    is file("$directory/lib/Your/Food.pm")->slurp, <<'CODE', 'not changed';
package Your::Food;

1;
CODE
}