X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/5675696b3881ef5bfde3012a829ca51ab1d42333..941a98a017bebaf9418c346af5601481227ecf3c:/t/op/incfilter.t diff --git a/t/op/incfilter.t b/t/op/incfilter.t index 4dbf7e9..97ce37a 100644 --- a/t/op/incfilter.t +++ b/t/op/incfilter.t @@ -14,7 +14,7 @@ BEGIN { use strict; use Filter::Util::Call; -plan(tests => 19); +plan(tests => 128); unshift @INC, sub { no warnings 'uninitialized'; @@ -56,11 +56,11 @@ do [sub { open $fh, "<", \'fail("File handles and filters work from \@INC");'; -do [$fh, sub {s/fail/pass/}] or die; +do [$fh, sub {s/fail/pass/; return;}] or die; open $fh, "<", \'fail("File handles and filters with state work from \@INC");'; -do [$fh, sub {s/$_[1]/pass/}, 'fail'] or die; +do [$fh, sub {s/$_[1]/pass/; return;}, 'fail'] or die; print "# 2 tests with pipes from subprocesses.\n"; @@ -70,7 +70,7 @@ do $fh or die; open $fh, 'echo fail|' or die $!; -do [$fh, sub {s/$_[1]/pass/}, 'fail'] or die; +do [$fh, sub {s/$_[1]/pass/; return;}, 'fail'] or die; sub rot13_filter { filter_add(sub { @@ -92,19 +92,17 @@ ORTVA {ebg13_svygre}; pass("This will rot13'ed twice"); EOC -do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/;}] or die; +do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die; my $count = 32; sub prepend_rot13_filter { filter_add(sub { - my $previous = defined $_ ? $_ : ''; + my $previous = $_; # Filters should append to any existing data in $_ # But (logically) shouldn't filter it twice. my $test = "fzrt!"; $_ = $test; my $status = filter_read(); - # Sadly, doing this inside the source filter causes an - # infinte loop my $got = substr $_, 0, length $test, ''; is $got, $test, "Upstream didn't alter existing data"; tr/A-Za-z/N-ZA-Mn-za-m/; @@ -119,4 +117,59 @@ ORTVA {cercraq_ebg13_svygre}; pass("This will rot13'ed twice"); EOC -do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/;}] or die; +do [$fh, sub {tr/A-Za-z/N-ZA-Mn-za-m/; return;}] or die; + +# This generates a heck of a lot of oks, but I think it's necessary. +my $amount = 1; +sub prepend_block_counting_filter { + filter_add(sub { + my $output = $_; + my $count = 256; + while (--$count) { + $_ = ''; + my $status = filter_read($amount); + cmp_ok (length $_, '<=', $amount, "block mode works?"); + $output .= $_; + if ($status <= 0 or /\n/s) { + $_ = $output; + return $status; + } + } + die "Looping infinitely"; + + }) +} + +open $fh, "<", \<<'EOC'; +BEGIN {prepend_block_counting_filter}; +pass("one by one"); +pass("and again"); +EOC + +do [$fh, sub {return;}] or die; + +open $fh, "<", \<<'EOC'; +BEGIN {prepend_block_counting_filter}; +pas("SSS make s fast SSS"); +EOC + +do [$fh, sub {s/s/ss/gs; s/([\nS])/$1$1$1/gs; return;}] or die; + +sub prepend_line_counting_filter { + filter_add(sub { + my $output = $_; + $_ = ''; + my $status = filter_read(); + my $newlines = tr/\n//; + cmp_ok ($newlines, '<=', 1, "1 line at most?"); + $_ = $output . $_ if defined $output; + return $status; + }) +} + +open $fh, "<", \<<'EOC'; +BEGIN {prepend_line_counting_filter}; +pass("You should see this line thrice"); +EOC + +do [$fh, sub {$_ .= $_ . $_; return;}] or die;