File: Class.pm

package info (click to toggle)
libcode-tidyall-perl 0.78~ds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,048 kB
  • sloc: perl: 5,061; lisp: 47; sh: 4; makefile: 2
file content (75 lines) | stat: -rw-r--r-- 1,973 bytes parent folder | download
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
package TestHelper::Test::Class;

use Capture::Tiny qw(capture_stdout);
use Code::TidyAll;
use Code::TidyAll::Util qw(tempdir_simple);
use File::Which qw(which);
use Test::Class::Most;
use strict;
use warnings;

__PACKAGE__->SKIP_CLASS("abstract base class");

sub require_executable {
    my $self = shift;
    my $exe  = shift;

    return 1 if which($exe);

    $self->builder->skip("These tests require that $exe be in your \$PATH");

    return 0;
}

sub create_dir {
    my ( $self, $files ) = @_;

    my $root_dir = tempdir_simple();
    while ( my ( $path, $content ) = each(%$files) ) {
        my $full_path = $root_dir->child($path);
        $full_path->parent->mkpath( { mode => 0755 } );
        $full_path->spew($content);
    }
    return $root_dir;
}

sub tidy {
    my ( $self, %params ) = @_;
    my $desc = $params{desc};
    if ( !defined($desc) ) {
        ($desc) = ( ( caller(1) )[3] =~ /([^:]+$)/ );
    }

    my $root_dir = $self->create_dir( $params{source} );

    my $options = $params{options} || {};
    my $ct      = Code::TidyAll->new(
        plugins  => $params{plugins},
        root_dir => $root_dir,
        %$options
    );

    my @results;
    my $output      = capture_stdout { @results = $ct->process_all() };
    my $error_count = grep { $_->error } @results;
    if ( $params{errors} ) {
        ok( $error_count > 0, "$desc - error_count > 0" );
        if ( eval { @{ $params{errors} } } ) {
            like( $output, $_, "$desc - errors" ) for @{ $params{errors} };
        }
        else {
            like( $output, $params{errors}, "$desc - errors" );
        }
    }
    else {
        is( $error_count, 0, "$desc - error_count == 0" );
    }
    while ( my ( $path, $content ) = each( %{ $params{dest} } ) ) {
        is( $root_dir->child($path)->slurp, $content, "$desc - $path content" );
    }
    if ( my $like_output = $params{like_output} ) {
        like( $output, $like_output, "$desc - output" );
    }
}

1;