This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make VMS-specific device name encoding routine say no more politely.
[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);
b4244ddc
JH
8 if ($ENV{PERL_CORE_MINITEST}) {
9 print "1..0 # Skip: no dynamic loading on miniperl\n";
10 exit 0;
11 }
bde61959
NC
12 unless (find PerlIO::Layer 'perlio') {
13 print "1..0 # Skip: not perlio\n";
14 exit 0;
15 }
16 require "test.pl";
17}
18use strict;
16d5c2f8 19use Config;
5675696b 20use Filter::Util::Call;
bde61959 21
34113e50 22plan(tests => 141);
bde61959
NC
23
24unshift @INC, sub {
25 no warnings 'uninitialized';
26 ref $_[1] eq 'ARRAY' ? @{$_[1]} : $_[1];
27};
28
29my $fh;
30
31open $fh, "<", \'pass("Can return file handles from \@INC");';
5675696b 32do $fh or die;
bde61959
NC
33
34my @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 );
39my @lines = @origlines;
40sub generator {
41 $_ = shift @lines;
42 # Return of 0 marks EOF
43 return defined $_ ? 1 : 0;
44};
45
5675696b 46do \&generator or die;
bde61959
NC
47
48@lines = @origlines;
49# Check that the array dereferencing works ready for the more complex tests:
5675696b 50do [\&generator] or die;
bde61959 51
34113e50
NC
52sub 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
59do [\&generator_with_state,
60 ["pass('Can return generators which take state');\n",
61 "pass('And return multiple lines');\n",
62 ]] or die;
bde61959
NC
63
64
65open $fh, "<", \'fail("File handles and filters work from \@INC");';
66
bccf3f3d 67do [$fh, sub {s/fail/pass/; return;}] or die;
bde61959
NC
68
69open $fh, "<", \'fail("File handles and filters with state work from \@INC");';
70
bccf3f3d 71do [$fh, sub {s/$_[1]/pass/; return;}, 'fail'] or die;
5675696b
NC
72
73print "# 2 tests with pipes from subprocesses.\n";
74
75open $fh, 'echo pass|' or die $!;
76
77do $fh or die;
78
79open $fh, 'echo fail|' or die $!;
80
bccf3f3d 81do [$fh, sub {s/$_[1]/pass/; return;}, 'fail'] or die;
5675696b
NC
82
83sub 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
91open $fh, "<", \<<'EOC';
92BEGIN {rot13_filter};
93cnff("This will rot13'ed prepend");
94EOC
95
96do $fh or die;
97
98open $fh, "<", \<<'EOC';
99ORTVA {ebg13_svygre};
100pass("This will rot13'ed twice");
101EOC
102
bccf3f3d 103do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
5675696b
NC
104
105my $count = 32;
106sub prepend_rot13_filter {
107 filter_add(sub {
8498a518 108 my $previous = $_;
5675696b
NC
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();
5675696b
NC
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
123open $fh, "<", \<<'EOC';
124ORTVA {cercraq_ebg13_svygre};
125pass("This will rot13'ed twice");
126EOC
127
bccf3f3d 128do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
937b367d
NC
129
130# This generates a heck of a lot of oks, but I think it's necessary.
131my $amount = 1;
132sub prepend_block_counting_filter {
133 filter_add(sub {
8498a518 134 my $output = $_;
937b367d
NC
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
151open $fh, "<", \<<'EOC';
152BEGIN {prepend_block_counting_filter};
153pass("one by one");
154pass("and again");
155EOC
156
157do [$fh, sub {return;}] or die;
158
159open $fh, "<", \<<'EOC';
160BEGIN {prepend_block_counting_filter};
161pas("SSS make s fast SSS");
162EOC
163
16d5c2f8
AT
164TODO: {
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}
941a98a0
NC
168
169sub 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
181open $fh, "<", \<<'EOC';
182BEGIN {prepend_line_counting_filter};
183pass("You should see this line thrice");
184EOC
185
186do [$fh, sub {$_ .= $_ . $_; return;}] or die;
34113e50
NC
187
188do \"pass\n(\n'Scalar references are treated as initial file contents'\n)\n"
189or die;
190
191open $fh, "<", \"ss('The file is concatentated');";
192
193do [\'pa', $fh] or die;
194
195open $fh, "<", \"ff('Gur svygre vf bayl eha ba gur svyr');";
196
197do [\'pa', $fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
198
199open $fh, "<", \"SS('State also works');";
200
201do [\'pa', $fh, sub {s/($_[1])/lc $1/ge; return;}, "S"] or die;
202
203@lines = ('ss', '(', "'you can use a generator'", ')');
204
205do [\'pa', \&generator] or die;
206
207do [\'pa', \&generator_with_state,
208 ["ss('And generators which take state');\n",
209 "pass('And return multiple lines');\n",
210 ]] or die;