App-PRT

 view release on metacpan or  search on metacpan

t/App-PRT-Command-ReplaceToken.t  view on Meta::CPAN

    my $file = "$directory/dinner.pl";
    $command->execute($file);
    is file($file)->slurp, <<'CODE';
use strict;
use warnings;
use lib 'lib';

use My::Human;
use My::Food;

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

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

$human->eat($food);
CODE
}

sub execute_replace_token_sequences_in_statement : Tests {
    my $directory = t::test::prepare_test_code('dinner');
    my $command = App::PRT::Command::ReplaceToken->new;
    $command->register('new(' => 'new->bake('); # `new` and `(`
    $command->set_replace_only_statement_which_has_token('My::Food');

    my $file = "$directory/dinner.pl";
    $command->execute($file);
    is file($file)->slurp, <<'CODE', 'new( in statement with My::Food was replaced';
use strict;
use warnings;
use lib 'lib';

use My::Human;
use My::Food;

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

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

$human->eat($food);
CODE
}

sub execute_with_whitespace : Tests {
    my $directory = t::test::prepare_test_code('method_call_with_whitespace');
    my $command = App::PRT::Command::ReplaceToken->new;
    $command->register("hello('World')"  => "hello('Work')");

    my $file = "$directory/hello.pl";
    $command->execute($file);
    is file($file)->slurp, <<'CODE', 'all hello are replaced';
hello('Work');
hello('Work');
hello('Work');
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::ReplaceToken->new;
    $command->register('alice' => 'bob');
    $command->execute($readme);
    ok -f $readme, 'README exists';
}

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


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

        cmp_deeply $command, methods(
            source_tokens => [ 'foo' ],
            destination_tokens => [ 'bar' ],
            replace_only_statement_which_has_token => undef,
        ), 'registered';

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

    subtest "when source, destination, and --in-statement specified" => sub {
        my $command = App::PRT::Command::ReplaceToken->new;
        my @args = qw(foo bar --in-statement bazz a.pl lib/B.pm);


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

        cmp_deeply $command, methods(
            source_tokens => [ 'foo' ],
            destination_tokens => [ 'bar' ],
            replace_only_statement_which_has_token => 'bazz',
        ), 'registered';

        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::ReplaceToken->new;

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

}



( run in 1.194 second using v1.01-cache-2.11-cpan-39bf76dae61 )