File: App-PRT-CLI.t

package info (click to toggle)
prt 0.20-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 376 kB
  • sloc: perl: 1,154; makefile: 2
file content (186 lines) | stat: -rw-r--r-- 6,262 bytes parent folder | download | duplicates (3)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package t::App::PRT::CLI;
use t::test;
use FindBin;
use File::Basename ();

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

    use_ok 'App::PRT::CLI';
}

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

sub _command_name_to_command_class : Tests {
    my $cli = App::PRT::CLI->new;

    is $cli->_command_name_to_command_class('hello'), 'App::PRT::Command::Hello', 'ucfirst';
    is $cli->_command_name_to_command_class('replace_token'), 'App::PRT::Command::ReplaceToken', 'separate by _';

    subtest 'alias' => sub {
        is $cli->_command_name_to_command_class('rename_namespace'), 'App::PRT::Command::RenameNameSpace', 'rename_namespace is alias for rename_name_space. Namespace is not name space';
    };
}

sub set_io : Tests {
    my $cli = App::PRT::CLI->new;
    $cli->set_io(*STDIN, *STDOUT);

    is $cli->{input}, *STDIN;
    is $cli->{output}, *STDOUT;
}

sub parse : Tests {
    subtest 'when empty input' => sub {
        my $cli = App::PRT::CLI->new;
        like exception {
            $cli->parse();
        }, qr/prt <command> <args>/;
    };

    subtest 'when command specified, not a git directory' => sub {
        my $directory = t::test::prepare_test_code('contain_ignores');
        my $g = mock_guard 'Cwd' => {
            getcwd => "$directory",
        };

        my $cli = App::PRT::CLI->new;
        $cli->parse(qw{replace_token foo bar});
        cmp_deeply $cli->command, isa('App::PRT::Command::ReplaceToken') & methods(
            source_tokens => [ 'foo' ],
            destination_tokens => [ 'bar' ],
        ), 'ReplaceToken command loaded';

        cmp_deeply $cli->collector, isa('App::PRT::Collector::AllFiles') & methods(
            directory => $directory,
        );
    };

    subtest 'when command specified, git directory' => sub {
        my $directory = t::test::prepare_test_code('dinner');
        t::test::prepare_as_git_repository($directory);
        my $g = mock_guard 'Cwd' => {
            getcwd => "$directory",
        };

        my $cli = App::PRT::CLI->new;
        $cli->parse(qw{replace_token foo bar});
        cmp_deeply $cli->command, isa('App::PRT::Command::ReplaceToken') & methods(
            source_tokens => [ 'foo' ],
            destination_tokens => [ 'bar' ],
        ), 'ReplaceToken command loaded';

        cmp_deeply $cli->collector, isa('App::PRT::Collector::GitDirectory') & methods(
            directory => $directory,
        );
    };

    subtest 'when source, destination, target files specified' => sub {
        my $cli = App::PRT::CLI->new;
        my $directory = t::test::prepare_test_code('dinner');
        $cli->parse(
            qw{replace_token foo bar},
            qq{$directory/dinner.pl},
            qq{$directory/lib/My/Food.pm},
            qq{$directory/lib/My/Human.pm}
        );
        cmp_deeply $cli->command, isa('App::PRT::Command::ReplaceToken') & methods(
            source_tokens => [ 'foo' ],
            destination_tokens => [ 'bar' ],
        ), 'ReplaceToken command loaded and foo => bar registered';
        cmp_deeply $cli->collector, isa('App::PRT::Collector::Files') & methods(
            collect => [
                qq{$directory/dinner.pl},
                qq{$directory/lib/My/Food.pm},
                qq{$directory/lib/My/Human.pm}
            ],
        ), 'Files collector loaded and files are registered';
    };

    subtest 'when target ' => sub {
        my $directory = t::test::prepare_test_code('contain_ignores');
        my $g = mock_guard 'Cwd' => {
            getcwd => "$directory",
        };

        my $cli = App::PRT::CLI->new;
        $cli->parse(qw{replace_token foo bar});
        cmp_deeply $cli->command, isa('App::PRT::Command::ReplaceToken') & methods(
            source_tokens => [ 'foo' ],
            destination_tokens => [ 'bar' ],
        ), 'ReplaceToken command loaded';

        cmp_deeply $cli->collector, isa('App::PRT::Collector::AllFiles') & methods(
            directory => $directory,
        );
    };

    subtest 'when neither git directory or project root directory detected' => sub {
        my $directory = t::test::prepare_test_code('hello_world');
        my $g = mock_guard 'Cwd' => {
            getcwd => "$directory",
        };
        my $cli = App::PRT::CLI->new;
        like exception {
            $cli->parse(qw{replace_token foo bar});
        }, qr/Cannot decide target files/;
    };

    subtest 'when invalid command specified' => sub {
        my $cli = App::PRT::CLI->new;
        like exception {
            $cli->parse('invalid_command');
        }, qr/Command invalid_command not found/;
    };

    subtest 'when input is the pipe' => sub {
        my $cli = App::PRT::CLI->new;
        my $g = mock_guard 'App::PRT::CLI', { _input_is_pipe => 1 };
        $cli->parse('introduce_variables');
        cmp_deeply $cli->collector, isa('App::PRT::Collector::FileHandle');
    };


}

sub run : Tests {
    subtest 'command which can execute' => sub {
        my $directory = t::test::prepare_test_code('hello_world');

        my $cli = App::PRT::CLI->new;
        $cli->parse(qw(replace_token foo bar), "$directory/hello_world.pl");

        my $file;
        my $g = mock_guard 'App::PRT::Command::ReplaceToken' => {
            execute => sub {
                (undef, $file) = @_;
            },
        };

        $cli->run;

        is $g->call_count('App::PRT::Command::ReplaceToken', 'execute'), 1, 'execute called';
        is $file, "$directory/hello_world.pl", 'called with file'
    };

    subtest 'command which can execute_files' => sub {
        my $directory = t::test::prepare_test_code('dinner');

        my $cli = App::PRT::CLI->new;
        $cli->parse(qw(rename_namespace My Our), "$directory/lib/My/Food.pm", "$directory/lib/My/Human.pm");

        my $files;
        my $g = mock_guard 'App::PRT::Command::RenameNameSpace' => {
            execute_files => sub {
                (undef, $files) = @_;
            },
        };

        $cli->run;

        is $g->call_count('App::PRT::Command::RenameNameSpace', 'execute_files'), 1, 'execute_files called';
        cmp_deeply $files, ["$directory/lib/My/Food.pm", "$directory/lib/My/Human.pm"], 'called with files';
    };
}