Skip to content

Commit

Permalink
more Mojo::URL tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 4, 2012
1 parent 04d7d53 commit d194544
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@ This file documents the revision history for Perl extension Mojolicious.

2.75 2012-04-05
- Improved documentation.
- Improved tests.
- Fixed small bug in Mojo::Parameters->params.

2.74 2012-04-04
Expand Down
13 changes: 5 additions & 8 deletions lib/Mojo/Asset/File.pm
Expand Up @@ -55,11 +55,13 @@ sub DESTROY {

sub add_chunk {
my ($self, $chunk) = @_;

my $handle = $self->handle;
$handle->sysseek(0, SEEK_END);
$chunk //= '';
croak "Can't write to asset: $!"
unless defined $handle->syswrite($chunk, length $chunk);

return $self;
}

Expand All @@ -82,10 +84,7 @@ sub contains {

# Moving window search
while ($offset <= $end) {
if (defined $range) {
$pattern_size = $end + 1 - $offset;
return -1 if $pattern_size <= 0;
}
return -1 if defined $range && ($pattern_size = $end + 1 - $offset) <= 0;
$read = $handle->sysread(my $buffer, $pattern_size);
$offset += $read;
$window .= $buffer;
Expand Down Expand Up @@ -138,14 +137,12 @@ sub move_to {
}

sub size {
my $self = shift;
return 0 unless defined(my $file = $self->path);
return 0 unless defined(my $file = shift->path);
return -s $file;
}

sub slurp {
my $self = shift;
my $handle = $self->handle;
my $handle = shift->handle;
$handle->sysseek(0, SEEK_SET);
my $content = '';
while ($handle->sysread(my $buffer, 131072)) { $content .= $buffer }
Expand Down
14 changes: 7 additions & 7 deletions lib/Mojo/Asset/Memory.pm
Expand Up @@ -15,23 +15,23 @@ sub new { shift->SUPER::new(@_, content => '') }

sub add_chunk {
my ($self, $chunk) = @_;

$self->{content} .= $chunk if defined $chunk;
return $self
if !$self->auto_upgrade || $self->size <= $self->max_memory_size;
$self->emit(upgrade => my $file = Mojo::Asset::File->new);
my $file = Mojo::Asset::File->new;
$self->emit(upgrade => $file);

return $file->add_chunk($self->slurp);
}

sub contains {
my $self = shift;

my $self = shift;
my $start = $self->start_range;
my $pos = index $self->{content}, shift, $start;
my $pos = index $self->{content}, shift, $start;
$pos -= $start if $start && $pos >= 0;
my $end = $self->end_range;

return -1 if $end && $pos >= $end;
return $pos;
return $end && $pos >= $end ? -1 : $pos;
}

sub get_chunk {
Expand Down
12 changes: 11 additions & 1 deletion t/mojo/url.t
Expand Up @@ -2,7 +2,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 387;
use Test::More tests => 394;

# "I don't want you driving around in a car you built yourself.
# You can sit there complaining, or you can knit me some seat belts."
Expand Down Expand Up @@ -335,6 +335,16 @@ is $url->ihost, 'xn--bcher-kva.xn--bcher-kva.ch',
is $url->port, 3000, 'right port';
is $url->path, '/foo', 'right path';
is "$url", 'http://xn--bcher-kva.xn--bcher-kva.ch:3000/foo', 'right format';
$url = Mojo::URL->new('http://bücher.bücher.bücher.ch:3000/foo');
ok $url->is_abs, 'is absolute';
is $url->scheme, 'http', 'right scheme';
is $url->host, 'bücher.bücher.bücher.ch', 'right host';
is $url->ihost, 'xn--bcher-kva.xn--bcher-kva.xn--bcher-kva.ch',
'right internationalized host';
is $url->port, 3000, 'right port';
is $url->path, '/foo', 'right path';
is "$url", 'http://xn--bcher-kva.xn--bcher-kva.xn--bcher-kva.ch:3000/foo',
'right format';

# IDNA (snowman)
$url = Mojo::URL->new('http://☃.net/');
Expand Down

0 comments on commit d194544

Please sign in to comment.