X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/d3db65ffa00c802f9536308be8dd6c439a0d94d8..bd1869dca4d5c5a05da6fe5ec612a3f53b18c6ea:/t/io/crlf.t diff --git a/t/io/crlf.t b/t/io/crlf.t index 858df17..7eb9a78 100644 --- a/t/io/crlf.t +++ b/t/io/crlf.t @@ -9,34 +9,74 @@ use Config; require "test.pl"; -my $file = "crlf$$.dat"; -END { - unlink($file); -} +my $file = tempfile(); + +if (find PerlIO::Layer 'perlio') { + plan(tests => 16); + ok(open(FOO,">:crlf",$file)); + ok(print FOO 'a'.((('a' x 14).qq{\n}) x 2000) || close(FOO)); + ok(open(FOO,"<:crlf",$file)); + + my $text; + { local $/; $text = } + is(count_chars($text, "\015\012"), 0); + is(count_chars($text, "\n"), 2000); + + binmode(FOO); + seek(FOO,0,0); + { local $/; $text = } + is(count_chars($text, "\015\012"), 2000); -if ($Config{useperlio}) { - plan(tests => 6); - ok(open(FOO,">:crlf",$file)); - ok(print FOO 'a'.((('a' x 14).qq{\n}) x 2000) || close(FOO)); - ok(open(FOO,"<:crlf",$file)); - my $seen = 0; - while () - { - $seen++ if (/\r/); - } - is($seen,0); - binmode(FOO); - seek(FOO,0,0); - $seen = 0; - while () - { - $seen++ if (/\r/); - } - is($seen,2000); - ok(close(FOO)); + SKIP: + { + skip_if_miniperl("miniperl can't rely on loading PerlIO::scalar"); + skip("no PerlIO::scalar") unless $Config{extensions} =~ m!\bPerlIO/scalar\b!; + require PerlIO::scalar; + my $fcontents = join "", map {"$_\015\012"} "a".."zzz"; + open my $fh, "<:crlf", \$fcontents; + local $/ = "xxx"; + local $_ = <$fh>; + my $pos = tell $fh; # pos must be behind "xxx", before "\nxxy\n" + seek $fh, $pos, 0; + $/ = "\n"; + $s = <$fh>.<$fh>; + is($s, "\nxxy\n"); + } + + ok(close(FOO)); + + # binmode :crlf should not cumulate. + # Try it first once and then twice so that even UNIXy boxes + # get to exercise this, for DOSish boxes even once is enough. + # Try also pushing :utf8 first so that there are other layers + # in between (this should not matter: CRLF layers still should + # not accumulate). + for my $utf8 ('', ':utf8') { + for my $binmode (1..2) { + open(FOO, ">$file"); + # require PerlIO; print PerlIO::get_layers(FOO), "\n"; + binmode(FOO, "$utf8:crlf") for 1..$binmode; + # require PerlIO; print PerlIO::get_layers(FOO), "\n"; + print FOO "Hello\n"; + close FOO; + open(FOO, "<$file"); + binmode(FOO); + my $foo = scalar ; + close FOO; + print join(" ", "#", map { sprintf("%02x", $_) } unpack("C*", $foo)), + "\n"; + like($foo, qr/\x0d\x0a$/); + unlike($foo, qr/\x0d\x0d/); + } + } } else { - skip_all("No perlio, so no :crlf"); + skip_all("No perlio, so no :crlf"); } - +sub count_chars { + my($text, $chars) = @_; + my $seen = 0; + $seen++ while $text =~ /$chars/g; + return $seen; +}