3 # Tests for the source filters in coderef-in-@INC
8 unless (find PerlIO::Layer 'perlio') {
9 print "1..0 # Skip: not perlio\n";
15 use Filter::Util::Call;
20 no warnings 'uninitialized';
21 ref $_[1] eq 'ARRAY' ? @{$_[1]} : $_[1];
26 open $fh, "<", \'pass("Can return file handles from \@INC");';
29 my @origlines = ("# This is a blank line\n",
30 "pass('Can return generators from \@INC');\n",
31 "pass('Which return multiple lines');\n",
34 my @lines = @origlines;
37 # Return of 0 marks EOF
38 return defined $_ ? 1 : 0;
41 do \&generator or die;
44 # Check that the array dereferencing works ready for the more complex tests:
45 do [\&generator] or die;
47 sub generator_with_state {
49 is (ref $param, 'ARRAY', "Got our parameter");
51 return defined $_ ? 1 : 0;
54 do [\&generator_with_state,
55 ["pass('Can return generators which take state');\n",
56 "pass('And return multiple lines');\n",
60 open $fh, "<", \'fail("File handles and filters work from \@INC");';
62 do [$fh, sub {s/fail/pass/; return;}] or die;
64 open $fh, "<", \'fail("File handles and filters with state work from \@INC");';
66 do [$fh, sub {s/$_[1]/pass/; return;}, 'fail'] or die;
68 print "# 2 tests with pipes from subprocesses.\n";
70 open $fh, 'echo pass|' or die $!;
74 open $fh, 'echo fail|' or die $!;
76 do [$fh, sub {s/$_[1]/pass/; return;}, 'fail'] or die;
80 my $status = filter_read();
81 tr/A-Za-z/N-ZA-Mn-za-m/;
86 open $fh, "<", \<<'EOC';
88 cnff("This will rot13'ed prepend");
93 open $fh, "<", \<<'EOC';
95 pass("This will rot13'ed twice");
98 do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
101 sub prepend_rot13_filter {
104 # Filters should append to any existing data in $_
105 # But (logically) shouldn't filter it twice.
108 my $status = filter_read();
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/;
113 die "Looping infinitely" unless $count--;
118 open $fh, "<", \<<'EOC';
119 ORTVA {cercraq_ebg13_svygre};
120 pass("This will rot13'ed twice");
123 do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
125 # This generates a heck of a lot of oks, but I think it's necessary.
127 sub prepend_block_counting_filter {
133 my $status = filter_read($amount);
134 cmp_ok (length $_, '<=', $amount, "block mode works?");
136 if ($status <= 0 or /\n/s) {
141 die "Looping infinitely";
146 open $fh, "<", \<<'EOC';
147 BEGIN {prepend_block_counting_filter};
152 do [$fh, sub {return;}] or die;
154 open $fh, "<", \<<'EOC';
155 BEGIN {prepend_block_counting_filter};
156 pas("SSS make s fast SSS");
159 do [$fh, sub {s/s/ss/gs; s/([\nS])/$1$1$1/gs; return;}] or die;
161 sub prepend_line_counting_filter {
165 my $status = filter_read();
166 my $newlines = tr/\n//;
167 cmp_ok ($newlines, '<=', 1, "1 line at most?");
168 $_ = $output . $_ if defined $output;
173 open $fh, "<", \<<'EOC';
174 BEGIN {prepend_line_counting_filter};
175 pass("You should see this line thrice");
178 do [$fh, sub {$_ .= $_ . $_; return;}] or die;
180 do \"pass\n(\n'Scalar references are treated as initial file contents'\n)\n"
183 open $fh, "<", \"ss('The file is concatentated');";
185 do [\'pa', $fh] or die;
187 open $fh, "<", \"ff('Gur svygre vf bayl eha ba gur svyr');";
189 do [\'pa', $fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
191 open $fh, "<", \"SS('State also works');";
193 do [\'pa', $fh, sub {s/($_[1])/lc $1/ge; return;}, "S"] or die;
195 @lines = ('ss', '(', "'you can use a generator'", ')');
197 do [\'pa', \&generator] or die;
199 do [\'pa', \&generator_with_state,
200 ["ss('And generators which take state');\n",
201 "pass('And return multiple lines');\n",