Skip to content

Commit

Permalink
Add tests for UTF-8 BOM stripping.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthn committed Oct 6, 2015
1 parent e284033 commit e2a621c
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions S16-io/bom.t
@@ -0,0 +1,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;
}

0 comments on commit e2a621c

Please sign in to comment.