Skip to content

Commit

Permalink
better tests for asset classes (closes #453)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 13, 2013
1 parent 4d24b67 commit 1771bc8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 35 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

3.86 2013-02-14
- Improved documentation.
- Improved tests. (jberger, sri)

3.85 2013-02-13
- Deprecated Mojo::UserAgent->build_form_tx in favor of
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojo/Asset.pm
Expand Up @@ -83,9 +83,10 @@ subclass.
=head2 get_chunk
my $bytes = $asset->get_chunk($offset);
my $bytes = $asset->get_chunk($offset, $max);
Get chunk of data starting from a specific position. Meant to be overloaded
in a subclass.
Get chunk of data starting from a specific position, defaults to a maximum
chunk size of C<131072> bytes. Meant to be overloaded in a subclass.
=head2 is_file
Expand Down
11 changes: 7 additions & 4 deletions lib/Mojo/Asset/File.pm
Expand Up @@ -92,7 +92,8 @@ sub contains {
}

sub get_chunk {
my ($self, $offset) = @_;
my ($self, $offset, $max) = @_;
$max //= 131072;

$offset += $self->start_range;
my $handle = $self->handle;
Expand All @@ -102,9 +103,9 @@ sub get_chunk {
if (defined(my $end = $self->end_range)) {
my $chunk = $end + 1 - $offset;
return '' if $chunk <= 0;
$handle->sysread($buffer, $chunk > 131072 ? 131072 : $chunk);
$handle->sysread($buffer, $chunk > $max ? $max : $chunk);
}
else { $handle->sysread($buffer, 131072) }
else { $handle->sysread($buffer, $max) }

return $buffer;
}
Expand Down Expand Up @@ -221,8 +222,10 @@ Check if asset contains a specific string.
=head2 get_chunk
my $bytes = $file->get_chunk($offset);
my $bytes = $file->get_chunk($offset, $max);
Get chunk of data starting from a specific position.
Get chunk of data starting from a specific position, defaults to a maximum
chunk size of C<131072> bytes.
=head2 is_file
Expand Down
12 changes: 7 additions & 5 deletions lib/Mojo/Asset/Memory.pm
Expand Up @@ -32,15 +32,15 @@ sub contains {
}

sub get_chunk {
my ($self, $offset) = @_;
my ($self, $offset, $max) = @_;
$max //= 131072;

$offset += $self->start_range;
my $size = 131072;
if (my $end = $self->end_range) {
$size = $end + 1 - $offset if ($offset + $size) > $end;
$max = $end + 1 - $offset if ($offset + $max) > $end;
}

return substr shift->{content}, $offset, $size;
return substr shift->{content}, $offset, $max;
}

sub move_to {
Expand Down Expand Up @@ -139,8 +139,10 @@ Check if asset contains a specific string.
=head2 get_chunk
my $bytes = $mem->get_chunk($offset);
my $bytes = $mem->get_chunk($offset, $max);
Get chunk of data starting from a specific position.
Get chunk of data starting from a specific position, defaults to a maximum
chunk size of C<131072> bytes.
=head2 move_to
Expand Down
50 changes: 26 additions & 24 deletions t/mojo/asset.t
Expand Up @@ -56,35 +56,37 @@ is $mem->contains('db'), -1, 'does not contain "db"';
$file = Mojo::Asset::File->new(start_range => 2, end_range => 8);
ok $file->is_range, 'has range';
$file->add_chunk('abcdefghijk');
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 $mem->contains('ij'), -1, 'does not contain "ij"';
is $file->contains('db'), -1, 'does not contain "db"';
my $chunk = $file->get_chunk(0);
is $chunk, 'cdefghi', 'chunk from position 0';
$chunk = $file->get_chunk(1);
is $chunk, 'defghi', 'chunk from position 1';
$chunk = $file->get_chunk(5);
is $chunk, 'hi', 'chunk from position 5';
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 $mem->contains('ij'), -1, 'does not contain "ij"';
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)';

# Memory asset range support (ab[cdefghi]jk)
$mem = Mojo::Asset::Memory->new(start_range => 2, end_range => 8);
ok $mem->is_range, 'has range';
$mem->add_chunk('abcdefghijk');
is $mem->contains('cdefghi'), 0, '"cdefghi" at position 0';
is $mem->contains('fghi'), 3, '"fghi" at position 3';
is $mem->contains('f'), 3, '"f" at position 3';
is $mem->contains('hi'), 5, '"hi" at position 5';
is $mem->contains('ij'), -1, 'does not contain "ij"';
is $mem->contains('db'), -1, 'does not contain "db"';
$chunk = $mem->get_chunk(0);
is $chunk, 'cdefghi', 'chunk from position 0';
$chunk = $mem->get_chunk(1);
is $chunk, 'defghi', 'chunk from position 1';
$chunk = $mem->get_chunk(5);
is $chunk, 'hi', 'chunk from position 5';
is $mem->contains('cdefghi'), 0, '"cdefghi" at position 0';
is $mem->contains('fghi'), 3, '"fghi" at position 3';
is $mem->contains('f'), 3, '"f" at position 3';
is $mem->contains('hi'), 5, '"hi" at position 5';
is $mem->contains('ij'), -1, 'does not contain "ij"';
is $mem->contains('db'), -1, 'does not contain "db"';
is $mem->get_chunk(0), 'cdefghi', 'chunk from position 0';
is $mem->get_chunk(1), 'defghi', 'chunk from position 1';
is $mem->get_chunk(5), 'hi', 'chunk from position 5';
is $mem->get_chunk(0, 2), 'cd', 'chunk from position 0 (2 bytes)';
is $mem->get_chunk(1, 3), 'def', 'chunk from position 1 (3 bytes)';
is $mem->get_chunk(5, 1), 'h', 'chunk from position 5 (1 byte)';
is $mem->get_chunk(5, 3), 'hi', 'chunk from position 5 (2 byte)';

# Huge file asset
$file = Mojo::Asset::File->new;
Expand Down

0 comments on commit 1771bc8

Please sign in to comment.