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