3 # Tests for the source filters in coderef-in-@INC
8 if ($ENV{PERL_CORE_MINITEST}) {
9 print "1..0 # Skip: no dynamic loading on miniperl\n";
12 unless (find PerlIO::Layer 'perlio') {
13 print "1..0 # Skip: not perlio\n";
20 use Filter::Util::Call;
25 no warnings 'uninitialized';
26 ref $_[1] eq 'ARRAY' ? @{$_[1]} : $_[1];
31 open $fh, "<", \'pass("Can return file handles from \@INC");';
34 my @origlines = ("# This is a blank line\n",
35 "pass('Can return generators from \@INC');\n",
36 "pass('Which return multiple lines');\n",
39 my @lines = @origlines;
42 # Return of 0 marks EOF
43 return defined $_ ? 1 : 0;
46 do \&generator or die;
49 # Check that the array dereferencing works ready for the more complex tests:
50 do [\&generator] or die;
52 sub generator_with_state {
54 is (ref $param, 'ARRAY', "Got our parameter");
56 return defined $_ ? 1 : 0;
59 do [\&generator_with_state,
60 ["pass('Can return generators which take state');\n",
61 "pass('And return multiple lines');\n",
65 open $fh, "<", \'fail("File handles and filters work from \@INC");';
67 do [$fh, sub {s/fail/pass/; return;}] or die;
69 open $fh, "<", \'fail("File handles and filters with state work from \@INC");';
71 do [$fh, sub {s/$_[1]/pass/; return;}, 'fail'] or die;
73 print "# 2 tests with pipes from subprocesses.\n";
75 my ($echo_command, $pass_arg, $fail_arg);
78 $echo_command = 'write sys$output';
83 $echo_command = 'echo';
88 open $fh, "$echo_command $pass_arg|" or die $!;
92 open $fh, "$echo_command $fail_arg|" or die $!;
94 do [$fh, sub {s/$_[1]/pass/; return;}, 'fail'] or die;
98 my $status = filter_read();
99 tr/A-Za-z/N-ZA-Mn-za-m/;
104 open $fh, "<", \<<'EOC';
105 BEGIN {rot13_filter};
106 cnff("This will rot13'ed prepend");
111 open $fh, "<", \<<'EOC';
112 ORTVA {ebg13_svygre};
113 pass("This will rot13'ed twice");
116 do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
119 sub prepend_rot13_filter {
122 # Filters should append to any existing data in $_
123 # But (logically) shouldn't filter it twice.
126 my $status = filter_read();
127 my $got = substr $_, 0, length $test, '';
128 is $got, $test, "Upstream didn't alter existing data";
129 tr/A-Za-z/N-ZA-Mn-za-m/;
131 die "Looping infinitely" unless $count--;
136 open $fh, "<", \<<'EOC';
137 ORTVA {cercraq_ebg13_svygre};
138 pass("This will rot13'ed twice");
141 do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
143 # This generates a heck of a lot of oks, but I think it's necessary.
145 sub prepend_block_counting_filter {
151 my $status = filter_read($amount);
152 cmp_ok (length $_, '<=', $amount, "block mode works?");
154 if ($status <= 0 or /\n/s) {
159 die "Looping infinitely";
164 open $fh, "<", \<<'EOC';
165 BEGIN {prepend_block_counting_filter};
170 do [$fh, sub {return;}] or die;
172 open $fh, "<", \<<'EOC';
173 BEGIN {prepend_block_counting_filter};
174 pas("SSS make s fast SSS");
178 todo_skip "disabled under -Dmad", 50 if $Config{mad};
179 do [$fh, sub {s/s/ss/gs; s/([\nS])/$1$1$1/gs; return;}] or die;
182 sub prepend_line_counting_filter {
186 my $status = filter_read();
187 my $newlines = tr/\n//;
188 cmp_ok ($newlines, '<=', 1, "1 line at most?");
189 $_ = $output . $_ if defined $output;
194 open $fh, "<", \<<'EOC';
195 BEGIN {prepend_line_counting_filter};
196 pass("You should see this line thrice");
199 do [$fh, sub {$_ .= $_ . $_; return;}] or die;
201 do \"pass\n(\n'Scalar references are treated as initial file contents'\n)\n"
204 open $fh, "<", \"ss('The file is concatentated');";
206 do [\'pa', $fh] or die;
208 open $fh, "<", \"ff('Gur svygre vf bayl eha ba gur svyr');";
210 do [\'pa', $fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
212 open $fh, "<", \"SS('State also works');";
214 do [\'pa', $fh, sub {s/($_[1])/lc $1/ge; return;}, "S"] or die;
216 @lines = ('ss', '(', "'you can use a generator'", ')');
218 do [\'pa', \&generator] or die;
220 do [\'pa', \&generator_with_state,
221 ["ss('And generators which take state');\n",
222 "pass('And return multiple lines');\n",