File: App-PRT-Command-MoveClassMethod.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 (321 lines) | stat: -rw-r--r-- 7,850 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package t::App::PRT::Command::MoveClassMethod;
use t::test;

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

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

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

sub register_rule : Tests {

    subtest 'valid rule' => sub {
        my $command = App::PRT::Command::MoveClassMethod->new;
        $command->register('Source#source' => 'Dest#dest');

        is $command->source_class_name, 'Source';
        is $command->source_method_name, 'source';
        is $command->destination_class_name, 'Dest';
        is $command->destination_method_name, 'dest';
    };

    subtest 'when destination method name is same' => sub {
        my $command = App::PRT::Command::MoveClassMethod->new;
        $command->register('Source#source' => 'Dest');

        is $command->source_class_name, 'Source';
        is $command->source_method_name, 'source';
        is $command->destination_class_name, 'Dest';
        is $command->destination_method_name, 'source';
    };

    subtest 'invalid syntax' => sub {
        my $command = App::PRT::Command::MoveClassMethod->new;
        ok exception {
            $command->register('Source' => 'Dest');
        }, qr/invalid format/;
        ok exception {
            $command->register('Source' => 'Dest#dest');
        }, qr/invalid format/;
    };
}

sub execute_method_body_when_destination_file_exists : Tests {
    my $directory = t::test::prepare_test_code('greeting');

    my $command = App::PRT::Command::MoveClassMethod->new;

    $command->register('Greeting#hi' => 'Hi#hello');

    my $file = "$directory/lib/Greeting.pm";
    $command->execute($file);

    ok -f $file, 'File exists';
    is file($file)->slurp, <<'CODE', 'hi method was removed';
package Greeting;
use strict;
use warnings;
use Hello;
use GoodAfternoon;

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

    "Bye, $name\n";
}

1;
CODE
    my $source_method = <<'METHOD';
sub hi {
    my ($class, $name) = @_;

    "Hi, $name\n";
}

METHOD

    is $command->source_method_body, $source_method, 'method body stored';

    my $destination_method = <<'METHOD';
sub hello {
    my ($class, $name) = @_;

    "Hi, $name\n";
}

METHOD

    is $command->destination_method_body, $destination_method, 'destination method prepared';

    my $destination_file = "$directory/lib/Hi.pm";
    ok -f $destination_file, 'destination file exists';

    is file($destination_file)->slurp, <<'CODE', 'hello method was added, use GoodAfternoon was added because it may be necessary';
package Hi;
use strict;
use warnings;
use Hello;
use GoodAfternoon;

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

    "Good morning, $name\n";
}

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

    "Hi, $name\n";
}

1;
CODE

}

sub execute_method_body_when_destination_file_not_exists : Tests {
    my $directory = t::test::prepare_test_code('greeting');

    my $command = App::PRT::Command::MoveClassMethod->new;

    $command->register('Greeting#hi' => 'Salutation#hello');

    my $file = "$directory/lib/Greeting.pm";
    $command->execute($file);
    ok -f $file, 'source file exists';

    my $destination_file = "$directory/lib/Salutation.pm";
    ok -f $destination_file, 'destination file exists';

        is file($destination_file)->slurp, <<'CODE', 'hello was added, uses are copied';
package Salutation;
use strict;
use warnings;
use Hello;
use GoodAfternoon;

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

    "Hi, $name\n";
}

1;
CODE
}

sub execute_method_move_comment_too : Tests {
    my $directory = t::test::prepare_test_code('method_with_comment');

    my $command = App::PRT::Command::MoveClassMethod->new;

    $command->register('FoodWithComment#new' => 'AnotherClass#another_new');

    my $file = "$directory/FoodWithComment.pm";
    $command->execute($file);

    my $destination_file = "$directory/AnotherClass.pm";
    ok -f $destination_file, 'destination file exists';

    is file($destination_file)->slurp, <<'CODE', 'method and comment was added';
package AnotherClass;
use strict;
use warnings;

# create a new food
# You can use when you want to create a new instance
sub another_new {
    my ($class, $name) = @_;

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

1;
CODE
}

sub execute_call_as_class_method : Tests {
    my $directory = t::test::prepare_test_code('greeting');

    my $command = App::PRT::Command::MoveClassMethod->new;

    $command->register('Bye#good' => 'Good#very_good');

    my $file = "$directory/lib/Bye.pm";
    $command->execute($file);
    ok -f $file, 'source file exists';

    is file($file)->slurp, <<'CODE', 'use was Added, $class->good was replaced';
package Bye;
use strict;
use warnings;
use Hello;
use Good;

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

    Good->very_good("bye, $name\n");
}

1;
CODE
}

sub execute_client_script : Tests {
    my $directory = t::test::prepare_test_code('greeting');

    my $command = App::PRT::Command::MoveClassMethod->new;

    $command->register('Greeting#hi' => 'Hi#hello');

    subtest 'client script with use Greeting' => sub {
        my $file = "$directory/use_greeting.pl";
        $command->execute($file);

        ok -f $file, 'File exists';
        is file($file)->slurp, <<'CODE', 'calling Greeting#hi was rewritten, use Hi was added';
use strict;
use warnings;
use lib 'lib';

use Greeting;
use Hi;

print Hi->hello('Alice');
print Greeting->bye('Bob');
CODE
    };

    subtest 'client script with use Greeting and Hi' => sub {
        my $file = "$directory/use_greeting_and_hi.pl";
        $command->execute($file);

        ok -f $file, 'File exists';
        is file($file)->slurp, <<'CODE', 'calling Greeting#hi was rewritten, Hi was not added';
use strict;
use warnings;
use lib 'lib';

use Greeting;
use Hi;

print Hi->hello('Alice');
print Greeting->bye('Bob');
CODE
    };

    subtest 'client script without Greeting or Hi' => sub {
        my $file = "$directory/no_use.pl";
        $command->execute($file);

        ok -f $file, 'File exists';
        is file($file)->slurp, <<'CODE', 'calling Greeting#hi was rewritten, Hi was added after last use';
use strict;
use warnings;
use lib 'lib';
use Hi;

print Hi->hello('Alice');
print Greeting->bye('Bob');
CODE
    };

    subtest 'client script without Greeting or Hi, with package statement' => sub {
        my $file = "$directory/no_use_but_package.pl";
        $command->execute($file);

        ok -f $file, 'File exists';
        is file($file)->slurp, <<'CODE', 'calling Greeting#hi was rewritten, Hi was added after package statement';
package Main;
use Hi;

print Hi->hello('Alice');
print Greeting->bye('Bob');
CODE
    };
}

sub execute_for_not_perl_file: Tests {
    my $directory = t::test::prepare_test_code('readme');
    my $readme = "$directory/README.md";

    my $command = App::PRT::Command::MoveClassMethod->new;
    $command->register('Greeting#hi' => 'Hi#hello');
    $command->execute($readme);
    ok -f $readme, 'README exists';
}

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


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

        is $command->source_class_name, 'From';
        is $command->source_method_name, 'from';
        is $command->destination_class_name, 'To';
        is $command->destination_method_name, '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::MoveClassMethod->new;

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

}