3 # Tests for the source filters in coderef-in-@INC
8 unless (find PerlIO::Layer 'perlio') {
9 print "1..0 # Skip: not perlio\n";
16 use Filter::Util::Call;
21 no warnings 'uninitialized';
22 ref $_[1] eq 'ARRAY' ? @{$_[1]} : $_[1];
27 open $fh, "<", \'pass("Can return file handles from \@INC");';
30 my @origlines = ("# This is a blank line\n",
31 "pass('Can return generators from \@INC');\n",
32 "pass('Which return multiple lines');\n",
35 my @lines = @origlines;
38 # Return of 0 marks EOF
39 return defined $_ ? 1 : 0;
42 do \&generator or die;
45 # Check that the array dereferencing works ready for the more complex tests:
46 do [\&generator] or die;
48 sub generator_with_state {
50 is (ref $param, 'ARRAY', "Got our parameter");
52 return defined $_ ? 1 : 0;
55 do [\&generator_with_state,
56 ["pass('Can return generators which take state');\n",
57 "pass('And return multiple lines');\n",
61 open $fh, "<", \'fail("File handles and filters work from \@INC");';
63 do [$fh, sub {s/fail/pass/; return;}] or die;
65 open $fh, "<", \'fail("File handles and filters with state work from \@INC");';
67 do [$fh, sub {s/$_[1]/pass/; return;}, 'fail'] or die;
69 print "# 2 tests with pipes from subprocesses.\n";
71 open $fh, 'echo pass|' or die $!;
75 open $fh, 'echo fail|' or die $!;
77 do [$fh, sub {s/$_[1]/pass/; return;}, 'fail'] or die;
81 my $status = filter_read();
82 tr/A-Za-z/N-ZA-Mn-za-m/;
87 open $fh, "<", \<<'EOC';
89 cnff("This will rot13'ed prepend");
94 open $fh, "<", \<<'EOC';
96 pass("This will rot13'ed twice");
99 do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
102 sub prepend_rot13_filter {
105 # Filters should append to any existing data in $_
106 # But (logically) shouldn't filter it twice.
109 my $status = filter_read();
110 my $got = substr $_, 0, length $test, '';
111 is $got, $test, "Upstream didn't alter existing data";
112 tr/A-Za-z/N-ZA-Mn-za-m/;
114 die "Looping infinitely" unless $count--;
119 open $fh, "<", \<<'EOC';
120 ORTVA {cercraq_ebg13_svygre};
121 pass("This will rot13'ed twice");
124 do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
126 # This generates a heck of a lot of oks, but I think it's necessary.
128 sub prepend_block_counting_filter {
134 my $status = filter_read($amount);
135 cmp_ok (length $_, '<=', $amount, "block mode works?");
137 if ($status <= 0 or /\n/s) {
142 die "Looping infinitely";
147 open $fh, "<", \<<'EOC';
148 BEGIN {prepend_block_counting_filter};
153 do [$fh, sub {return;}] or die;
155 open $fh, "<", \<<'EOC';
156 BEGIN {prepend_block_counting_filter};
157 pas("SSS make s fast SSS");
161 todo_skip "disabled under -Dmad", 50 if $Config{mad};
162 do [$fh, sub {s/s/ss/gs; s/([\nS])/$1$1$1/gs; return;}] or die;
165 sub prepend_line_counting_filter {
169 my $status = filter_read();
170 my $newlines = tr/\n//;
171 cmp_ok ($newlines, '<=', 1, "1 line at most?");
172 $_ = $output . $_ if defined $output;
177 open $fh, "<", \<<'EOC';
178 BEGIN {prepend_line_counting_filter};
179 pass("You should see this line thrice");
182 do [$fh, sub {$_ .= $_ . $_; return;}] or die;
184 do \"pass\n(\n'Scalar references are treated as initial file contents'\n)\n"
187 open $fh, "<", \"ss('The file is concatentated');";
189 do [\'pa', $fh] or die;
191 open $fh, "<", \"ff('Gur svygre vf bayl eha ba gur svyr');";
193 do [\'pa', $fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die;
195 open $fh, "<", \"SS('State also works');";
197 do [\'pa', $fh, sub {s/($_[1])/lc $1/ge; return;}, "S"] or die;
199 @lines = ('ss', '(', "'you can use a generator'", ')');
201 do [\'pa', \&generator] or die;
203 do [\'pa', \&generator_with_state,
204 ["ss('And generators which take state');\n",
205 "pass('And return multiple lines');\n",