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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#! /usr/bin/perl
#---------------------------------------------------------------------
# 20-open.t
# Copyright 2012 Christopher J. Madsen
#
# Actually open files and check the encoding
#---------------------------------------------------------------------
use strict;
use warnings;
use Test::More 0.88;
plan tests => 107;
use IO::HTML;
use File::Temp;
use Scalar::Util 'blessed';
#---------------------------------------------------------------------
sub test
{
my ($expected, $out, $data, $name, $nextArg) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $options;
if (ref $name) {
$options = $name;
$name = $nextArg;
}
unless ($name) {
$name = 'test ' . ($expected || 'cp1252');
}
my $tmp = File::Temp->new(UNLINK => 1);
open(my $mem, '>', \(my $buf)) or die;
if ($out) {
$out = ":encoding($out)" unless $out =~ /^:/;
binmode $tmp, $out;
binmode $mem, $out;
}
print $mem $data;
print $tmp $data;
close $mem;
$tmp->close;
my ($fh, $encoding, $bom) = IO::HTML::file_and_encoding("$tmp", $options);
if ($options and $options->{encoding}) {
ok(blessed($encoding), 'returned an object');
$encoding = eval { $encoding->name };
}
is($encoding, $expected || 'cp1252', $name);
my $firstLine = <$fh>;
like($firstLine, qr/^<html/i);
close $fh;
$fh = html_file("$tmp", $options);
is(<$fh>, $firstLine);
close $fh;
# Test sniff_encoding:
undef $mem;
open($mem, '<', \$buf) or die "Can't open in-memory file: $!";
delete $options->{encoding} if $options;
($encoding, $bom) = IO::HTML::sniff_encoding($mem, undef, $options);
is($encoding, $expected);
seek $mem, 0, 0;
$options->{encoding} = 1;
($encoding, $bom) = IO::HTML::sniff_encoding($mem, undef, $options);
if (defined $expected) {
ok(blessed($encoding), 'encoding is an object');
is(eval { $encoding->name }, $expected);
} else {
is($encoding, undef);
}
} # end test
#---------------------------------------------------------------------
test 'utf-8-strict' => '' => <<'';
<html><meta charset="UTF-8">
test 'utf-8-strict' => ':utf8' => <<"";
<html><head><title>Foo\xA0Bar</title>
test undef, latin1 => <<"";
<html><head><title>Foo\xA0Bar</title>
test 'UTF-16BE' => 'UTF-16BE' => <<"";
\x{FeFF}<html><head><title>Foo\xA0Bar</title>
test 'utf-8-strict' => ':utf8' => <<"";
\x{FeFF}<html><meta charset="UTF-16">
test 'utf-8-strict' => ':utf8' => <<"";
<html><meta charset="UTF-16BE">
test 'UTF-16LE' => 'UTF-16LE' => <<"";
\x{FeFF}<html><meta charset="UTF-16">
test 'UTF-16LE' => 'UTF-16LE' => <<"", { encoding => 1 };
\x{FeFF}<html><meta charset="UTF-16">
test 'utf-8-strict' => ':utf8' => <<"", { encoding => 1, need_pragma => 0 };
<html><meta charset="UTF-16BE">
test 'utf-8-strict' => ':utf8' =>
"<html><title>Foo\xA0Bar" . ("\x{2014}" x 512) . "</title>\n",
'UTF-8 character crosses boundary';
test 'utf-8-strict' => ':utf8' =>
"<html><title>Foo Bar" . ("\x{2014}" x 512) . "</title>\n",
'UTF-8 character crosses boundary 2';
test undef, '', <<'', 'wrong pragma';
<html>
<head>
<meta http-equiv="X-Content-Type" content="text/html; charset=UTF-8" />
<title>Title</title>
test 'utf-8-strict', '', <<'', {need_pragma => 0}, 'need_pragma 0';
<html>
<head>
<meta http-equiv="X-Content-Type" content="text/html; charset=UTF-8" />
<title>Title</title>
test 'iso-8859-15', '', <<"", { encoding => 1, need_pragma => 0 };
<html>
<meta content="text/html; charset=ISO-8859-15">
<meta charset="UTF-16BE">
test 'iso-8859-15', latin1 =>
'<html>' . (' ' x 985) . qq'<head><meta charset="ISO-8859-15">\n',
'found with 985 spaces';
test undef, latin1 =>
'<html>' . (' ' x 986) . qq'<head><meta charset="ISO-8859-15">\n',
'not found with 986 spaces';
{
local $IO::HTML::bytes_to_check = 1040;
test 'iso-8859-15', latin1 =>
'<html>' . (' ' x 1001) . qq'<head><meta charset="ISO-8859-15">\n',
'found with 1001 spaces';
test undef, latin1 =>
'<html>' . (' ' x 1002) . qq'<head><meta charset="ISO-8859-15">\n',
'not found with 1002 spaces';
}
done_testing;
|