This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make utf8::encode respect magic
[perl5.git] / t / io / crlf.t
CommitLineData
d3db65ff
NIS
1#!./perl -w
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = qw(. ../lib);
e05e9c3d
NC
6 require "test.pl";
7 skip_all_without_perlio();
d3db65ff
NIS
8}
9
10use Config;
11
d3db65ff 12
62a28c97 13my $file = tempfile();
d3db65ff 14
e05e9c3d 15{
14b1a0c4
RGS
16 plan(tests => 16);
17 ok(open(FOO,">:crlf",$file));
18 ok(print FOO 'a'.((('a' x 14).qq{\n}) x 2000) || close(FOO));
19 ok(open(FOO,"<:crlf",$file));
887ede57 20
14b1a0c4
RGS
21 my $text;
22 { local $/; $text = <FOO> }
23 is(count_chars($text, "\015\012"), 0);
24 is(count_chars($text, "\n"), 2000);
887ede57 25
14b1a0c4
RGS
26 binmode(FOO);
27 seek(FOO,0,0);
28 { local $/; $text = <FOO> }
29 is(count_chars($text, "\015\012"), 2000);
887ede57 30
14b1a0c4
RGS
31 SKIP:
32 {
bd1869dc 33 skip_if_miniperl("miniperl can't rely on loading PerlIO::scalar");
14b1a0c4
RGS
34 skip("no PerlIO::scalar") unless $Config{extensions} =~ m!\bPerlIO/scalar\b!;
35 require PerlIO::scalar;
36 my $fcontents = join "", map {"$_\015\012"} "a".."zzz";
37 open my $fh, "<:crlf", \$fcontents;
38 local $/ = "xxx";
39 local $_ = <$fh>;
40 my $pos = tell $fh; # pos must be behind "xxx", before "\nxxy\n"
41 seek $fh, $pos, 0;
42 $/ = "\n";
43 $s = <$fh>.<$fh>;
bd1869dc 44 is($s, "\nxxy\n");
14b1a0c4 45 }
e949e37c 46
14b1a0c4 47 ok(close(FOO));
8229d19f 48
14b1a0c4
RGS
49 # binmode :crlf should not cumulate.
50 # Try it first once and then twice so that even UNIXy boxes
51 # get to exercise this, for DOSish boxes even once is enough.
52 # Try also pushing :utf8 first so that there are other layers
53 # in between (this should not matter: CRLF layers still should
54 # not accumulate).
55 for my $utf8 ('', ':utf8') {
56 for my $binmode (1..2) {
57 open(FOO, ">$file");
58 # require PerlIO; print PerlIO::get_layers(FOO), "\n";
59 binmode(FOO, "$utf8:crlf") for 1..$binmode;
60 # require PerlIO; print PerlIO::get_layers(FOO), "\n";
61 print FOO "Hello\n";
62 close FOO;
63 open(FOO, "<$file");
64 binmode(FOO);
65 my $foo = scalar <FOO>;
66 close FOO;
67 print join(" ", "#", map { sprintf("%02x", $_) } unpack("C*", $foo)),
68 "\n";
bd1869dc
NC
69 like($foo, qr/\x0d\x0a$/);
70 unlike($foo, qr/\x0d\x0d/);
14b1a0c4
RGS
71 }
72 }
d3db65ff 73}
d3db65ff 74
887ede57 75sub count_chars {
14b1a0c4
RGS
76 my($text, $chars) = @_;
77 my $seen = 0;
78 $seen++ while $text =~ /$chars/g;
79 return $seen;
887ede57 80}