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
|
#!/usr/bin/perl
use strict;
use warnings;
use lib 't/lib';
use Test::Framework;
use File::BOM qw(
open_bom
decode_from_bom
get_encoding_from_filehandle
);
use File::Temp qw( tmpnam );
use Test::Exception ( tests => 10 );
use Test::More;
my $absent = tmpnam();
throws_ok { open_bom(my $fh, $absent) }
qr/^Couldn't read/,
"open_bom on non-existant file fails";
throws_ok { open_bom(my $fh, ">new_file.txt") }
qr(^Couldn't read),
"Attempt to open_bom for writing fails";
throws_ok { open_bom(my $fh, "| cat") }
qr(^Couldn't read),
"Attempt to open_bom as pipe fails";
throws_ok { decode_from_bom(undef) }
qr/^No string/,
"decode_from_bom with no string fails";
{
# The following tests are known to produce warnings
local $SIG{__WARN__} = sub {};
my $tmpfile = tmpnam();
open WRITER, '>', $tmpfile or die "Couldn't write to '$tmpfile': $!";
# _get_encoding_* functions don't qualify refs as they are not public
# Therefore _get_encoding_seekable(WRITER) will not work
throws_ok { File::BOM::_get_encoding_seekable(\*WRITER) }
qr/^Couldn't read from handle/,
"_get_encoding_seekable on unreadable handle fails";
throws_ok { File::BOM::_get_encoding_unseekable(\*WRITER) }
qr/^Couldn't read byte/,
"_get_encoding_unseekable() on unreadable handle fails";
close WRITER;
unlink $tmpfile;
SKIP:
{
skip "mkfifo not supported on this platform", 3
unless $fifo_supported;
skip "mkfifo tests skipped on cygwin, set TEST_FIFO to enable them", 3
if $^O eq 'cygwin' && !$ENV{'TEST_FIFO'};
my($pid, $fifo);
($pid, $fifo) = write_fifo('');
open(STREAM, '<:bytes', $fifo) or die "Couldn't read fifo '$fifo': $!";
throws_ok { File::BOM::_get_encoding_seekable(\*STREAM) }
qr/^Couldn't reset read position/,
"_get_encoding_seekable on unseekable handle fails";
throws_ok { get_encoding_from_filehandle(STREAM) }
qr/^Unseekable handle/,
"get_encoding_from_filehandle on unseekable handle fails";
close STREAM; waitpid($pid, 0); unlink $fifo;
($pid, $fifo) = write_fifo('');
lives_ok { my($enc) = open_bom(my $fh, $fifo) }
"(\$enc) = open_bom(FH, \$fifo) lives";
waitpid($pid, 0); unlink $fifo;
}
throws_ok { open_bom(my $fh, 't/data/no_bom.txt', 'invalid') }
qr/^Couldn't set binmode of handle opened on/,
"open_bom with invalid default encoding fails";
}
__END__
vim:ft=perl
|