Code-TidyAll

 view release on metacpan or  search on metacpan

t/lib/TestFor/Code/TidyAll/Basic.pm  view on Meta::CPAN

            test_plugin("CheckUpper $_") => { select => '**/*.txt' },

            # note without the weight here this would run first, and the
            # letters in the photetic words themselves would be reversed
            test_plugin('AlwaysPhonetic') => { select => '**/*.txt', weight => 51 }
        )
    } ( 1 .. 3 );

    $self->tidy(
        plugins     => {@plugins},
        options     => { verbose   => 1 },
        source      => { 'foo.txt' => 'abc' },
        dest        => { 'foo.txt' => 'CHARLIE-BRAVO-ALFA' },
        like_output =>
            qr/.*ReverseFoo, .*UpperText 1, .*UpperText 2, .*UpperText 3, .*CheckUpper 1, .*CheckUpper 2, .*CheckUpper 3/
    );

    $self->tidy(
        plugins     => { %AToZ, %ReverseFoo, %CheckUpper },
        options     => { verbose   => 1 },
        source      => { 'foo.txt' => 'abc' },
        dest        => { 'foo.txt' => 'abc' },
        errors      => qr/lowercase found/,
        like_output => qr/foo.txt (.*ReverseFoo, .*CheckUpper)/
    );

}

sub test_quiet_and_verbose : Tests {
    my $self = shift;

    foreach my $state ( 'normal', 'quiet', 'verbose' ) {
        foreach my $error ( 0, 1 ) {
            my $root_dir = $self->create_dir( { 'foo.txt' => ( $error ? '123' : 'abc' ) } );
            my $output   = capture_stdout {
                my $ct = Code::TidyAll->new(
                    plugins  => {%UpperText},
                    root_dir => $root_dir,
                    ( $state eq 'normal' ? () : ( $state => 1 ) )
                );
                $ct->process_paths("$root_dir/foo.txt");
            };
            if ($error) {
                like( $output, qr/non-alpha content found/, "non-alpha content found ($state)" );
            }
            else {
                is( $output, "[tidied]  foo.txt\n" ) if $state eq 'normal';
                is( $output, q{} )                   if $state eq 'quiet';
                like( $output, qr/purging old backups/, "purging old backups ($state)" )
                    if $state eq 'verbose';
                like(
                    $output,
                    qr/\[tidied\]  foo\.txt \(\+TestHelper::Plugin::UpperText\)/s,
                    "foo.txt ($state)"
                ) if $state eq 'verbose';
            }
        }
    }
}

sub test_iterations : Tests {
    my $self     = shift;
    my $root_dir = $self->create_dir( { 'foo.txt' => 'abc' } );
    my $ct       = Code::TidyAll->new(
        plugins    => { test_plugin('RepeatFoo') => { select => '**/foo*', times => 3 } },
        root_dir   => $root_dir,
        iterations => 2
    );
    my $file = $root_dir->child('foo.txt');
    $ct->process_paths($file);
    is( $file->slurp, scalar( 'abc' x 9 ), '3^2 = 9' );
}

sub test_caching_and_backups : Tests {
    my $self = shift;

    my @chi_or_no_chi = (q{});
    if ( eval 'use CHI; 1' ) {
        push @chi_or_no_chi, 'chi';
    }

    foreach my $chi (@chi_or_no_chi) {
        foreach my $cache_model_class (
            qw(
            Code::TidyAll::CacheModel
            Code::TidyAll::CacheModel::Shared
            )
        ) {
            foreach my $no_cache ( 0 .. 1 ) {
                foreach my $no_backups ( 0 .. 1 ) {
                    my $desc
                        = "(no_cache=$no_cache, no_backups=$no_backups, model=$cache_model_class, cache_class=$chi)";
                    my $root_dir = $self->create_dir( { 'foo.txt' => 'abc' } );
                    my $ct       = Code::TidyAll->new(
                        plugins           => {%UpperText},
                        root_dir          => $root_dir,
                        cache_model_class => $cache_model_class,
                        ( $no_cache   ? ( no_cache   => 1 )      : () ),
                        ( $no_backups ? ( no_backups => 1 )      : () ),
                        ( $chi        ? ( cache      => _chi() ) : () ),
                    );
                    my $output;
                    my $file = path( $root_dir, 'foo.txt' );
                    my $go   = sub {
                        $output = capture_stdout { $ct->process_paths($file) };
                    };

                    $go->();
                    is( $file->slurp, "ABC",                 "first file change $desc" );
                    is( $output,      "[tidied]  foo.txt\n", "first output $desc" );

                    $go->();
                    if ($no_cache) {
                        is( $output, "[checked] foo.txt\n", "second output $desc" );
                    }
                    else {
                        is( $output, q{}, "second output $desc" );
                    }

                    $file->spew('ABCD');
                    $go->();
                    is( $output, "[checked] foo.txt\n", "third output $desc" );

                    $file->spew('def');
                    $go->();
                    is( $file->slurp, 'DEF',                 "fourth file change $desc" );
                    is( $output,      "[tidied]  foo.txt\n", "fourth output $desc" );



( run in 0.518 second using v1.01-cache-2.11-cpan-71847e10f99 )