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