/
bom.t
51 lines (40 loc) 路 1.44 KB
/
bom.t
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
use Test;
plan 10;
{
my $starts_with_bom = Buf.new(0xEF, 0xBB, 0xBF, 0x70, 0x69, 0x76, 0x6F);
my $decoded = $starts_with_bom.decode('utf-8');
is $decoded.chars, 4, '.chars reflects stripping of UTF-8 BOM (BOM + 4 chars)';
is $decoded, 'pivo', 'got correct string with UTF-8 BOM stripped';
}
{
my $only_bom = Buf.new(0xEF, 0xBB, 0xBF);
my $decoded = $only_bom.decode('utf-8');
is $decoded.chars, 0, 'when we decode only BOM, get empty string';
}
{
my $temp-file = "bom-test-1-$*PID";
given open($temp-file, :w) {
.write(Buf.new(0xEF, 0xBB, 0xBF, 0x70, 0x69, 0x76, 0x6F));
.close;
}
my $slurped = slurp($temp-file);
is $slurped.chars, 4, 'BOM stripped when reading from file';
is $slurped, 'pivo', 'Got correct BOM-stripped string reading form file';
my @lines = $temp-file.IO.lines;
is @lines.elems, 1, 'lines sanity with BOM stripping';
is @lines[0].chars, 4, 'BOM stripped first line has correct chars';
is @lines[0], 'pivo', 'BOM stripped first line is correct';
LEAVE unlink $temp-file;
}
{
my $temp-file = "bom-test-2-$*PID";
given open($temp-file, :w) {
.write(Buf.new(0xEF, 0xBB, 0xBF));
.close;
}
my $slurped = slurp($temp-file);
is $slurped.chars, 0, 'BOM-only file slurps to empty string';
my @lines = $temp-file.IO.lines;
is @lines.elems, 0, 'BOM-only file reports 0 lines like empty file';
LEAVE unlink $temp-file;
}