Asset-File
view release on metacpan or search on metacpan
use Test::More;
use File::Basename 'dirname';
use File::Spec::Functions qw(catdir catfile);
use Asset::File;
# File asset
my $file = Asset::File->new;
is $file->size, 0, 'file is empty';
is $file->mtime, (stat $file->handle)[9], 'right mtime';
is $file->slurp, '', 'file is empty';
$file->add_chunk('abc');
is $file->contains('abc'), 0, '"abc" at position 0';
is $file->contains('bc'), 1, '"bc" at position 1';
is $file->contains('db'), -1, 'does not contain "db"';
is $file->size, 3, 'right size';
is $file->mtime, (stat $file->handle)[9], 'right mtime';
# Cleanup
my $path = $file->path;
ok -e $path, 'temporary file exists';
undef $file;
ok !-e $path, 'temporary file has been cleaned up';
# Open existing and upgrade to rw
Asset::File->new({path => $path, cleanup => 0})->add_chunk('foo');
my $stat = [(stat($path))];
$file = Asset::File->new(path => $path);
is_deeply [(stat($path))], $stat, "opening didn't affect stat";
ok eval { $file->add_chunk('bar') }, "didn't die";
is $file->get_chunk, 'foobar', 'right content';
undef $file;
ok -e $path, 'file exists';
unlink $path;
# Empty file asset
$file = Asset::File->new;
is $file->contains('a'), -1, 'does not contain "a"';
# File asset range support (a[bcdefabc])
$file = Asset::File->new();
$file->add_chunk('abcdefabc');
$file->start_range(1);
ok $file->is_range, 'has range';
is $file->contains('bcdef'), 0, '"bcdef" at position 0';
is $file->contains('cdef'), 1, '"cdef" at position 1';
is $file->contains('abc'), 5, '"abc" at position 5';
is $file->contains('db'), -1, 'does not contain "db"';
# File asset write range support (a[bcdefabc])
$file = Asset::File->new();
$file->start_range(10);
$file->add_chunk('abcdefabc');
$file->start_range(0);
is $file->contains('bcdef'), 11, '"bcdef" at position 11';
is $file->contains('cdef'), 12, '"cdef" at position 12';
is $file->contains('abc'), 10, '"abc" at position 10';
is $file->contains('db'), -1, 'does not contain "db"';
# File asset range support (ab[cdefghi]jk)
$file = Asset::File->new();
$file->add_chunk('abcdefghijk');
$file->start_range(2);
$file->end_range(8);
ok $file->is_range, 'has range';
is $file->contains('cdefghi'), 0, '"cdefghi" at position 0';
is $file->contains('fghi'), 3, '"fghi" at position 3';
is $file->contains('f'), 3, '"f" at position 3';
is $file->contains('hi'), 5, '"hi" at position 5';
is $file->contains('db'), -1, 'does not contain "db"';
is $file->get_chunk(0), 'cdefghi', 'chunk from position 0';
is $file->get_chunk(1), 'defghi', 'chunk from position 1';
is $file->get_chunk(5), 'hi', 'chunk from position 5';
is $file->get_chunk(0, 2), 'cd', 'chunk from position 0 (2 bytes)';
is $file->get_chunk(1, 3), 'def', 'chunk from position 1 (3 bytes)';
is $file->get_chunk(5, 1), 'h', 'chunk from position 5 (1 byte)';
is $file->get_chunk(5, 3), 'hi', 'chunk from position 5 (2 byte)';
# Huge file asset
$file = Asset::File->new;
ok !$file->is_range, 'no range';
$file->add_chunk('a' x 131072);
$file->add_chunk('b');
$file->add_chunk('c' x 131072);
$file->add_chunk('ddd');
is $file->contains('a'), 0, '"a" at position 0';
is $file->contains('b'), 131072, '"b" at position 131072';
is $file->contains('c'), 131073, '"c" at position 131073';
is $file->contains('abc'), 131071, '"abc" at position 131071';
is $file->contains('ccdd'), 262143, '"ccdd" at position 262143';
is $file->contains('dd'), 262145, '"dd" at position 262145';
is $file->contains('ddd'), 262145, '"ddd" at position 262145';
is $file->contains('e'), -1, 'does not contain "e"';
is $file->contains('a' x 131072), 0, '"a" x 131072 at position 0';
( run in 2.565 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )