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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
package t::App::PRT::Command::RenameClass;
use t::test;
sub _require : Test(startup => 1) {
my ($self) = @_;
use_ok 'App::PRT::Command::RenameClass';
}
sub instantiate : Tests {
isa_ok App::PRT::Command::RenameClass->new, 'App::PRT::Command::RenameClass';
}
sub register_rule : Tests {
my $command = App::PRT::Command::RenameClass->new;
$command->register('Foo' => 'Bar');
is $command->source_class_name, 'Foo';
is $command->destination_class_name, 'Bar';
}
sub execute : Tests {
my $directory = t::test::prepare_test_code('dinner');
my $command = App::PRT::Command::RenameClass->new;
$command->register('My::Food' => 'My::Meal');
subtest 'target class' => sub {
my $food_file = "$directory/lib/My/Food.pm";
my $meal_file = "$directory/lib/My/Meal.pm";
is $command->execute($food_file), $meal_file, 'returns destination file when success';
ok ! -f $food_file, "Food.pm doesn't exist";
ok -e $meal_file, "Meal.pm exists";
is file($meal_file)->slurp, <<'CODE', 'package statement was rewritten';
package My::Meal;
use strict;
use warnings;
$My::Meal::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
};
subtest 'client file' => sub {
my $dinner_file = "$directory/dinner.pl";
is $command->execute($dinner_file), $dinner_file, 'returns source file when success and not moved';
ok -f $dinner_file, 'dinner.pl exists';
is file($dinner_file)->slurp, <<'CODE', 'use statement and class-method invocation were rewritten';
use strict;
use warnings;
use lib 'lib';
use My::Human;
use My::Meal;
undef *My::Meal::new;
undef *My::Food::Foo::new;
my $human = My::Human->new('Alice');
my $food = My::Meal->new('Pizza');
$human->eat($food);
CODE
};
}
sub execute_package_with_comment_first : Tests {
my $directory = t::test::prepare_test_code('tokens_before_package');
my $command = App::PRT::Command::RenameClass->new;
$command->register('My::Commented' => 'My::Commented2');
my $file_in = "$directory/lib/My/Commented.pm";
my $file_out = "$directory/lib/My/Commented2.pm";
is $command->execute($file_in), $file_out, 'returns destination file when success';
ok ! -f $file_in, "$file_in doesn't exist";
ok -e $file_out, "$file_out exists";
is file($file_out)->slurp, <<'CODE', 'package statement was rewritten';
# A package with a comment before the `package` statement
package My::Commented2;
use strict;
use warnings;
sub new { bless {}, shift; }
1;
CODE
}
sub execute_package_with_pod_first : Tests {
my $directory = t::test::prepare_test_code('tokens_before_package');
my $command = App::PRT::Command::RenameClass->new;
$command->register('My::POD' => 'My::POD2');
my $file_in = "$directory/lib/My/POD.pm";
my $file_out = "$directory/lib/My/POD2.pm";
is $command->execute($file_in), $file_out, 'returns destination file when success';
ok ! -f $file_in, "$file_in doesn't exist";
ok -e $file_out, "$file_out exists";
is file($file_out)->slurp, <<'CODE', 'package statement was rewritten';
=head1 NAME
My::POD - A package with POD before the `package` statement
=cut
package My::POD2;
use strict;
use warnings;
sub new { bless {}, shift; }
1;
CODE
}
sub execute_with_inherit : Tests {
my $directory = t::test::prepare_test_code('inherit');
my $command = App::PRT::Command::RenameClass->new;
$command->register('Parent' => 'Boss');
subtest 'target class' => sub {
my $file = "$directory/inherit.pl";
$command->execute($file);
ok -e $file, "script file exists";
is file($file)->slurp, <<'CODE', 'use parent, use base statements were rewritten';
package Child1 {
use DateTime;
use utf8;
use parent 'Boss';
};
package Child2 {
use parent qw(Boss AnotherParent YetAnother::Parent);
};
package Child3 {
use base 'Boss';
};
package Child4 {
use base 'Boss';
};
package Child5 {
use base 'Boss';
};
package Child6 {
use base 'Boss';
};
package GrandChild {
use base 'Child';
};
CODE
};
}
sub execute_test_more_style_test_file : Tests {
my $directory = t::test::prepare_test_code('dinner');
my $command = App::PRT::Command::RenameClass->new;
$command->register('My::Food' => 'My::Meal');
my $file = "$directory/t/001-my-food._t";
$command->execute($file);
is file($file)->slurp, <<'CODE', 'test replaced';
use Test::More tests => 5;
use_ok 'My::Meal';
require_ok 'My::Meal';
new_ok 'My::Meal';
isa_ok My::Meal->new, 'My::Meal';
subtest 'name' => sub {
my $pizza = My::Meal->new('Pizza');
is $pizza->name, 'Pizza';
};
CODE
}
sub execute_test_class_style_test_file: Tests {
my $directory = t::test::prepare_test_code('dinner');
my $food_file = "$directory/t/My-Food._t";
my $meal_file = "$directory/t/My-Meal._t";
my $command1 = App::PRT::Command::RenameClass->new;
$command1->register('t::My::Food' => 't::My::Meal');
$command1->execute($food_file);
ok ! -f $food_file, "Food._t doesn't exist";
ok -e $meal_file, "Meal._t exists";
is file($meal_file)->slurp, <<'CODE', 'package statement replaced';
package t::My::Meal;
use base qw(Test::Class);
use Test::More;
sub _load : Test(startup => 1) {
use_ok 'My::Food';
}
sub instantiate : Test(1) {
isa_ok My::Food->new('banana'), 'My::Food';
}
sub name : Test(1) {
my $food = My::Food->new('banana');
is $food->name, 'banana';
}
__PACKAGE__->runtests;
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::RenameClass->new;
$command->register('Alice' => 'Bob');
$command->execute($readme);
ok -f $readme, 'README exists';
}
sub execute_rename_to_deeper_directory : Tests {
my $directory = t::test::prepare_test_code('dinner');
my $command = App::PRT::Command::RenameClass->new;
$command->register('My::Food' => 'My::Special::Great::Food');
subtest 'target class' => sub {
my $food_file = "$directory/lib/My/Food.pm";
my $special_food_file = "$directory/lib/My/Special/Great/Food.pm";
is $command->execute($food_file), $special_food_file, 'success';
ok ! -f $food_file, "Food.pm doesn't exist";
ok -e $special_food_file, "Special::Great::Food.pm exists";
};
}
sub execute_rename_package_in_block_statement : Tests {
my $directory = t::test::prepare_test_code('package_in_block');
my $command = App::PRT::Command::RenameClass->new;
$command->register('Hello' => 'Greeting');
subtest '{ package } style' => sub {
my $script = "$directory/package_in_block.pl";
is $command->execute($script), $script, 'success';
ok -e $script, "script exists";
is file($script)->slurp, <<'CODE', 'package statement replaced';
use strict;
use warnings;
{
package Greeting;
sub hello { "hello!" }
};
print Greeting->hello;
CODE
};
subtest 'package { } style' => sub {
my $script = "$directory/package_block_statement.pl";
is $command->execute($script), $script, 'success';
ok -e $script, "script exists";
is file($script)->slurp, <<'CODE', 'package statement replaced';
use strict;
use warnings;
package Greeting {
sub hello { "hello" }
};
print Greeting->hello;
CODE
};
subtest 'multi packages' => sub {
my $script = "$directory/multi_packages.pl";
is $command->execute($script), $script, 'success';
ok -e $script, "script exists";
is file($script)->slurp, <<'CODE', 'package statement replaced';
use strict;
use warnings;
package Bye {
sub bye { "bye" }
};
package Greeting {
sub hello { "hello" }
};
print Greeting->hello;
CODE
};
}
sub parse_arguments : Tests {
subtest "when source and destination specified" => sub {
my $command = App::PRT::Command::RenameClass->new;
my @args = qw(From To a.pl lib/B.pm);
my @args_after = $command->parse_arguments(@args);
is $command->source_class_name, 'From';
is $command->destination_class_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::RenameClass->new;
ok exception {
$command->parse_arguments('hi');
}, 'died';
};
}
|