File: 1-basic.t

package info (click to toggle)
libperlio-eol-perl 0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 148 kB
  • sloc: ansic: 169; perl: 41; makefile: 3
file content (98 lines) | stat: -rw-r--r-- 2,495 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use strict;
use Test::More tests => 23;
use File::Spec ();
use File::Temp 0.19 ();

BEGIN { use_ok('PerlIO::eol', qw( eol_is_mixed CR LF CRLF NATIVE )) }

my ($CR, $LF, $CRLF) = (CR, LF, CRLF);

is( eol_is_mixed("."), 0 );
is( eol_is_mixed(".$CRLF."), 0 );
is( eol_is_mixed(".$CR.$LF."), 3 );
is( eol_is_mixed(".$CRLF.$CR"), 4 );

$/ = undef;

sub is_hex ($$;$) {
    @_ = (
        join(' ', unpack '(H2)*', $_[0]),
        join(' ', unpack '(H2)*', $_[1]),
        $_[2],
    );
    goto &is;
}

my $directory = File::Temp->newdir;
my $read_fn = File::Spec->catfile($directory, "read");
my $write_fn = File::Spec->catfile($directory, "write");

{
    open my $w, ">:raw", $read_fn or die "can't create testfile: $!";
    print $w "...$CRLF$LF$CR...";
}

{
    ok(open(my $r, "<:raw:eol(CR)", $read_fn), "open for read");
    is_hex(<$r>, "...$CR$CR$CR...", "read");
}

{
    ok(open(my $r, "<:raw:eol(LF)", $read_fn), "open for read");
    is_hex(<$r>, "...$LF$LF$LF...", "read");
}

{
    ok(open(my $r, "<:raw:eol(CRLF)", $read_fn), "open for read");
    is_hex(<$r>, "...$CRLF$CRLF$CRLF...", "read");
}

{
    local $@;
    ok(open(my $r, "<:raw:eol(CR!)", $read_fn), "open for read");
    is(eval { <$r> }, undef, 'mixed encoding');
    like($@, qr/Mixed newlines/, 'raises exception');
}

{
    ok(open(my $r, "<:raw:eol(CRLF?)", $read_fn), "open for read");
    my $warning;
    local $SIG{__WARN__} = sub { $warning = $_[0] };
    is_hex(<$r>, "...$CRLF$CRLF$CRLF...", "read");
    like($warning, qr/Mixed newlines found in "\Q$read_fn\E"/, 'raises exception');
}

{
    local $@;
    open my $w, ">:raw:eol(LF!)", $write_fn or die "can't create testfile: $!";
    eval { print $w "...$CRLF$LF$CR..." };
    like($@, qr/Mixed newlines found in "\Q$write_fn\E"/, 'raises exception');
}

TODO: {
    local $@;
    local $TODO = 'Trailing CR in mixed encodings';
    open my $w, ">:raw:eol(LF!)", $write_fn or die "can't create testfile: $!";
    eval { print $w "...$CRLF$CR" };
    like($@, qr/Mixed newlines found in "\Q$write_fn\E"/, 'raises exception');
}

{
    ok(open(my $w, ">:raw:eol(CrLf-lf)", $write_fn), "open for write");
    print $w "...$CR$LF...";
}

{
    open my $r, "<:raw", $write_fn or die "can't read testfile: $!";
    is_hex(<$r>, "...$LF...", "write");
}

{
    ok(open(my $w, ">:raw:eol(LF-Native)", $write_fn), "open for write");
    print $w "...$CR";
}

{
    open my $r, "<", $write_fn or die "can't read testfile: $!";
    is_hex(<$r>, "...\n", "write");
}