]> git.donarmstrong.com Git - perltidy.git/blob - t/filter_example.t
New upstream version 20210717
[perltidy.git] / t / filter_example.t
1 # Test use of prefilter and postfilter parameters
2 use strict;
3 use Carp;
4 use Perl::Tidy;
5 use Test::More;
6 my $name = 'filter_example';
7
8 BEGIN {
9     plan tests => 1;
10 }
11
12 my $source = <<'ENDS';
13 use Method::Signatures::Simple;
14
15  method foo1 { $self->bar }
16
17        # with signature
18     method foo2($bar, %opts) { $self->bar(reverse $bar) if $opts{rev};
19     }
20
21     # attributes
22     method foo3 : lvalue { $self->{foo} 
23 }
24
25  # change invocant name
26     method 
27 foo4 ( $class : $bar ) { $class->bar($bar) }
28 ENDS
29
30 my $expect = <<'ENDE';
31 use Method::Signatures::Simple;
32 method foo1 { $self->bar }
33
34 # with signature
35 method foo2 ( $bar, %opts ) {
36     $self->bar( reverse $bar ) if $opts{rev};
37 }
38
39 # attributes
40 method foo3 : lvalue {
41     $self->{foo};
42 }
43
44 # change invocant name
45 method foo4 ( $class : $bar ) { $class->bar($bar) }
46 ENDE
47
48 my $output;
49 my $stderr_string;
50 my $errorfile_string;
51 my $params = "";
52 my $err    = Perl::Tidy::perltidy(
53
54     #argv => '-npro',  # fix for RT#127679, avoid reading unwanted .perltidyrc
55     argv       => '',
56     perltidyrc => \$params,    # avoid reading unwanted .perltidyrc
57     prefilter =>
58       sub { $_ = $_[0]; s/^\s*method\s+(\w.*)/sub METHOD_$1/gm; return $_ },
59     postfilter  => sub { $_ = $_[0]; s/sub\s+METHOD_/method /gm; return $_ },
60     source      => \$source,
61     destination => \$output,
62     stderr      => \$stderr_string,
63     errorfile => \$errorfile_string,    # not used when -se flag is set
64 );
65 if ( $err || $stderr_string || $errorfile_string ) {
66     ok(0);
67 }
68 else {
69     is( $output, $expect, $name );
70 }