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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
#!/usr/bin/perl -w
# Test that open still honors the open pragma.
use strict;
use warnings;
use autodie;
use Fcntl;
use File::Temp;
use Test::More;
if( $] < '5.01000' ) {
plan skip_all => "autodie does not honor the open pragma before 5.10";
}
else {
plan "no_plan";
}
# Test with an open pragma on
{
use open IN => ':encoding(utf8)', OUT => ':utf8';
# Test the standard handles and all newly opened handles are utf8
my $file = File::Temp->new;
my $txt = "autodie is MËTÁŁ";
# open for writing
{
open my $fh, ">", $file;
my @layers = PerlIO::get_layers($fh);
ok( (grep { $_ eq 'utf8' } @layers), "open write honors open pragma" ) or diag join ", ", @layers;
print $fh $txt;
close $fh;
}
# open for reading, explicit
{
open my $fh, "<", $file;
my @layers = PerlIO::get_layers($fh);
ok( (grep { $_ eq 'encoding(utf8)' } @layers), "open read honors open pragma" ) or diag join ", ", @layers;
is join("\n", <$fh>), $txt;
}
# open for reading, implicit
{
open my($fh), $file;
my @layers = PerlIO::get_layers($fh);
ok( (grep { $_ eq 'encoding(utf8)' } @layers), "open implicit read honors open pragma" ) or diag join ", ", @layers;
is join("\n", <$fh>), $txt;
}
# open for read/write
{
open my $fh, "+>", $file;
my @layers = PerlIO::get_layers($fh);
ok( (grep { $_ eq 'utf8' } @layers), "open implicit read honors open pragma" ) or diag join ", ", @layers;
}
# open for append
{
open my $fh, ">>", $file;
my @layers = PerlIO::get_layers($fh);
ok( (grep { $_ eq 'utf8' } @layers), "open implicit read honors open pragma" ) or diag join ", ", @layers;
}
# raw
{
open my $fh, ">:raw", $file;
my @layers = PerlIO::get_layers($fh);
ok( !(grep { $_ eq 'utf8' } @layers), 'open pragma is not used if raw is specified' ) or diag join ", ", @layers;
}
}
# Test without open pragma
{
my $file = File::Temp->new;
open my $fh, ">", $file;
my @layers = PerlIO::get_layers($fh);
ok( grep(!/utf8/, @layers), "open pragma remains lexical" ) or diag join ", ", @layers;
}
# sysopen
{
use open IN => ':encoding(utf8)', OUT => ':utf8';
# Test the standard handles and all newly opened handles are utf8
my $file = File::Temp->new;
my $txt = "autodie is MËTÁŁ";
# open for writing only
{
sysopen my $fh, $file, O_CREAT|O_TRUNC|O_WRONLY;
my @layers = PerlIO::get_layers($fh);
ok( (grep { $_ eq 'utf8' } @layers), "open write honors open pragma" ) or diag join ", ", @layers;
print $fh $txt;
close $fh;
}
# open for reading only
{
sysopen my $fh, $file, O_RDONLY;
my @layers = PerlIO::get_layers($fh);
ok( (grep { $_ eq 'encoding(utf8)' } @layers), "open read honors open pragma" ) or diag join ", ", @layers;
is join("\n", <$fh>), $txt;
}
# open for reading and writing
{
sysopen my $fh, $file, O_RDWR;
my @layers = PerlIO::get_layers($fh);
ok( (grep { $_ eq 'utf8' } @layers), "open read/write honors open write pragma" ) or diag join ", ", @layers;
is join("\n", <$fh>), $txt;
}
}
|