This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [perl #39126] possible memory related bug when using sprintf with an utf-8 encode...
[perl5.git] / t / op / incfilter.t
1 #!./perl -w
2
3 # Tests for the source filters in coderef-in-@INC
4
5 BEGIN {
6     chdir 't' if -d 't';
7     @INC = qw(. ../lib);
8     if ($ENV{PERL_CORE_MINITEST}) {
9         print "1..0 # Skip: no dynamic loading on miniperl\n";
10         exit 0;
11     }
12     unless (find PerlIO::Layer 'perlio') {
13         print "1..0 # Skip: not perlio\n";
14         exit 0;
15     }
16     require "test.pl";
17 }
18 use strict;
19 use Config;
20 use Filter::Util::Call;
21
22 plan(tests => 141);
23
24 unshift @INC, sub {
25     no warnings 'uninitialized';
26     ref $_[1] eq 'ARRAY' ? @{$_[1]} : $_[1];
27 };
28
29 my $fh;
30
31 open $fh, "<", \'pass("Can return file handles from \@INC");';
32 do $fh or die;
33
34 my @origlines = ("# This is a blank line\n",
35                  "pass('Can return generators from \@INC');\n",
36                  "pass('Which return multiple lines');\n",
37                  "1",
38                  );
39 my @lines = @origlines;
40 sub generator {
41     $_ = shift @lines;
42     # Return of 0 marks EOF
43     return defined $_ ? 1 : 0;
44 };
45
46 do \&generator or die;
47
48 @lines = @origlines;
49 # Check that the array dereferencing works ready for the more complex tests:
50 do [\&generator] or die;
51
52 sub generator_with_state {
53     my $param = $_[1];
54     is (ref $param, 'ARRAY', "Got our parameter");
55     $_ = shift @$param;
56     return defined $_ ? 1 : 0;
57 }
58
59 do [\&generator_with_state,
60     ["pass('Can return generators which take state');\n",
61      "pass('And return multiple lines');\n",
62     ]] or die;
63    
64
65 open $fh, "<", \'fail("File handles and filters work from \@INC");';
66
67 do [$fh, sub {s/fail/pass/; return;}] or die;
68
69 open $fh, "<", \'fail("File handles and filters with state work from \@INC");';
70
71 do [$fh, sub {s/$_[1]/pass/; return;}, 'fail'] or die;
72
73 print "# 2 tests with pipes from subprocesses.\n";
74
75 open $fh, 'echo pass|' or die $!;
76
77 do $fh or die;
78
79 open $fh, 'echo fail|' or die $!;
80
81 do [$fh, sub {s/$_[1]/pass/; return;}, 'fail'] or die;
82
83 sub rot13_filter {
84     filter_add(sub {
85                    my $status = filter_read();
86                    tr/A-Za-z/N-ZA-Mn-za-m/;
87                    $status;
88                })
89 }
90
91 open $fh, "<", \<<'EOC';
92 BEGIN {rot13_filter};
93 cnff("This will rot13'ed prepend");
94 EOC
95
96 do $fh or die;
97
98 open $fh, "<", \<<'EOC';
99 ORTVA {ebg13_svygre};
100 pass("This will rot13'ed twice");
101 EOC
102
103 do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
104
105 my $count = 32;
106 sub prepend_rot13_filter {
107     filter_add(sub {
108                    my $previous = $_;
109                    # Filters should append to any existing data in $_
110                    # But (logically) shouldn't filter it twice.
111                    my $test = "fzrt!";
112                    $_ = $test;
113                    my $status = filter_read();
114                    my $got = substr $_, 0, length $test, '';
115                    is $got, $test, "Upstream didn't alter existing data";
116                    tr/A-Za-z/N-ZA-Mn-za-m/;
117                    $_ = $previous . $_;
118                    die "Looping infinitely" unless $count--;
119                    $status;
120                })
121 }
122
123 open $fh, "<", \<<'EOC';
124 ORTVA {cercraq_ebg13_svygre};
125 pass("This will rot13'ed twice");
126 EOC
127
128 do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
129
130 # This generates a heck of a lot of oks, but I think it's necessary.
131 my $amount = 1;
132 sub prepend_block_counting_filter {
133     filter_add(sub {
134                    my $output = $_;
135                    my $count = 256;
136                    while (--$count) {
137                        $_ = '';
138                        my $status = filter_read($amount);
139                        cmp_ok (length $_, '<=', $amount, "block mode works?");
140                        $output .= $_;
141                        if ($status <= 0 or /\n/s) {
142                            $_ = $output;
143                            return $status;
144                        }
145                    }
146                    die "Looping infinitely";
147                           
148                })
149 }
150
151 open $fh, "<", \<<'EOC';
152 BEGIN {prepend_block_counting_filter};
153 pass("one by one");
154 pass("and again");
155 EOC
156
157 do [$fh, sub {return;}] or die;
158
159 open $fh, "<", \<<'EOC';
160 BEGIN {prepend_block_counting_filter};
161 pas("SSS make s fast SSS");
162 EOC
163
164 TODO: {
165     todo_skip "disabled under -Dmad", 50 if $Config{mad};
166     do [$fh, sub {s/s/ss/gs; s/([\nS])/$1$1$1/gs; return;}] or die;
167 }
168
169 sub prepend_line_counting_filter {
170     filter_add(sub {
171                    my $output = $_;
172                    $_ = '';
173                    my $status = filter_read();
174                    my $newlines = tr/\n//;
175                    cmp_ok ($newlines, '<=', 1, "1 line at most?");
176                    $_ = $output . $_ if defined $output;
177                    return $status;
178                })
179 }
180
181 open $fh, "<", \<<'EOC';
182 BEGIN {prepend_line_counting_filter};
183 pass("You should see this line thrice");
184 EOC
185
186 do [$fh, sub {$_ .= $_ . $_; return;}] or die;
187
188 do \"pass\n(\n'Scalar references are treated as initial file contents'\n)\n"
189 or die;
190
191 open $fh, "<", \"ss('The file is concatentated');";
192
193 do [\'pa', $fh] or die;
194
195 open $fh, "<", \"ff('Gur svygre vf bayl eha ba gur svyr');";
196
197 do [\'pa', $fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
198
199 open $fh, "<", \"SS('State also works');";
200
201 do [\'pa', $fh, sub {s/($_[1])/lc $1/ge; return;}, "S"] or die;
202
203 @lines = ('ss', '(', "'you can use a generator'", ')');
204
205 do [\'pa', \&generator] or die;
206
207 do [\'pa', \&generator_with_state,
208     ["ss('And generators which take state');\n",
209      "pass('And return multiple lines');\n",
210     ]] or die;