Skip to content

Commit

Permalink
handle constructors consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 22, 2015
1 parent afbfefe commit 0e272be
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 18 deletions.
16 changes: 4 additions & 12 deletions lib/Mojo/Asset/Memory.pm
Expand Up @@ -26,7 +26,7 @@ sub contains {
my ($self, $str) = @_;

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

Expand All @@ -42,7 +42,7 @@ sub get_chunk {
$max = $end + 1 - $offset if ($offset + $max) > $end;
}

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

sub move_to {
Expand All @@ -51,11 +51,9 @@ sub move_to {
return $self;
}

sub new { shift->SUPER::new(@_, content => '') }
sub size { length(shift->{content} // '') }

sub size { length shift->{content} }

sub slurp { shift->{content} }
sub slurp { shift->{content} // '' }

1;

Expand Down Expand Up @@ -157,12 +155,6 @@ chunk size of C<131072> bytes (128KB).
Move asset data into a specific file.
=head2 new
my $mem = Mojo::Asset::Memory->new;
Construct a new L<Mojo::Asset::Memory> object.
=head2 size
my $size = $mem->size;
Expand Down
4 changes: 4 additions & 0 deletions lib/Mojo/Content/MultiPart.pm
Expand Up @@ -291,6 +291,10 @@ True.
=head2 new
my $multi = Mojo::Content::MultiPart->new;
my $multi
= Mojo::Content::MultiPart->new(parts => [Mojo::Content::Single->new]);
my $multi
= Mojo::Content::MultiPart->new({parts => [Mojo::Content::Single->new]});
Construct a new L<Mojo::Content::MultiPart> object and subscribe to L</"read">
event with default content parser.
Expand Down
2 changes: 2 additions & 0 deletions lib/Mojo/Content/Single.pm
Expand Up @@ -146,6 +146,8 @@ Get a chunk of content starting from a specific position.
=head2 new
my $single = Mojo::Content::Single->new;
my $single = Mojo::Content::Single->new(asset => Mojo::Asset::File->new);
my $single = Mojo::Content::Single->new({asset => Mojo::Asset::File->new});
Construct a new L<Mojo::Content::Single> object and subscribe to L</"read">
event with default content parser.
Expand Down
6 changes: 2 additions & 4 deletions lib/Mojo/Exception.pm
Expand Up @@ -8,10 +8,7 @@ has [qw(frames line lines_before lines_after)] => sub { [] };
has message => 'Exception!';
has 'verbose';

sub new {
my $self = shift->SUPER::new;
return @_ ? $self->_detect(@_) : $self;
}
sub new { @_ > 1 ? shift->SUPER::new->_detect(@_) : shift->SUPER::new }

sub throw { die shift->new->trace(2)->_detect(@_) }

Expand Down Expand Up @@ -174,6 +171,7 @@ following new ones.
=head2 new
my $e = Mojo::Exception->new;
my $e = Mojo::Exception->new('Oops!');
my $e = Mojo::Exception->new('Oops!', $files);
Expand Down
2 changes: 2 additions & 0 deletions lib/Mojo/Server.pm
Expand Up @@ -211,6 +211,8 @@ Load application from script.
=head2 new
my $server = Mojo::Server->new;
my $server = Mojo::Server->new(reverse_proxy => 1);
my $server = Mojo::Server->new({reverse_proxy => 1});
Construct a new L<Mojo::Server> object and subscribe to L</"request"> event
with default request handling.
Expand Down
2 changes: 2 additions & 0 deletions lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -604,6 +604,8 @@ Local interface port.
=head2 new
my $ws = Mojo::Transaction::WebSocket->new;
my $ws = Mojo::Transaction::WebSocket->new(compressed => 1);
my $ws = Mojo::Transaction::WebSocket->new({compressed => 1});
Construct a new L<Mojo::Transaction::WebSocket> object and subscribe to
L</"frame"> event with default message parser, which also handles C<PING> and
Expand Down
2 changes: 2 additions & 0 deletions lib/Mojolicious.pm
Expand Up @@ -619,6 +619,8 @@ requests indiscriminately, for a full list of available hooks see L</"HOOKS">.
=head2 new
my $app = Mojolicious->new;
my $app = Mojolicious->new(moniker => 'foo_bar');
my $app = Mojolicious->new({moniker => 'foo_bar'});
Construct a new L<Mojolicious> application and call L</"startup">. Will
automatically detect your home directory and set up logging based on your
Expand Down
1 change: 1 addition & 0 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -352,6 +352,7 @@ disabled by default.
=head2 new
my $pattern = Mojolicious::Routes::Pattern->new;
my $pattern = Mojolicious::Routes::Pattern->new('/:action');
my $pattern
= Mojolicious::Routes::Pattern->new('/:action', action => qr/\w+/);
Expand Down
6 changes: 6 additions & 0 deletions t/mojo/asset.t
Expand Up @@ -39,10 +39,16 @@ is $mem->mtime($mtime + 23)->mtime, $mtime + 23, 'right mtime';

# Empty file asset
$file = Mojo::Asset::File->new;
is $file->size, 0, 'asset is empty';
is $file->get_chunk(0), '', 'no content';
is $file->slurp, '', 'no content';
is $file->contains('a'), -1, 'does not contain "a"';

# Empty memory asset
$mem = Mojo::Asset::Memory->new;
is $mem->size, 0, 'asset is empty';
is $mem->get_chunk(0), '', 'no content';
is $mem->slurp, '', 'no content';
ok !$mem->is_range, 'no range';
is $mem->contains('a'), -1, 'does not contain "a"';

Expand Down
11 changes: 11 additions & 0 deletions t/mojo/content.t
@@ -1,6 +1,7 @@
use Mojo::Base -strict;

use Test::More;
use Mojo::Asset::Memory;
use Mojo::Content::MultiPart;
use Mojo::Content::Single;

Expand All @@ -11,13 +12,23 @@ ok !$content->body_contains('a'), 'content does not contain "a"';
ok $content->body_contains('f'), 'content contains "f"';
ok $content->body_contains('o'), 'content contains "o"';
ok $content->body_contains('foo'), 'content contains "foo"';
$content = Mojo::Content::Single->new(
asset => Mojo::Asset::Memory->new->add_chunk('bar'));
ok !$content->body_contains('foo'), 'content does not contain "foo"';
ok $content->body_contains('bar'), 'content contains "bar"';
$content = Mojo::Content::Single->new(
{asset => Mojo::Asset::Memory->new->add_chunk('foo')});
ok !$content->body_contains('bar'), 'content does not contain "bar"';
ok $content->body_contains('foo'), 'content contains "foo"';

# Multipart
$content = Mojo::Content::MultiPart->new(parts => [$content]);
ok !$content->body_contains('a'), 'content does not contain "a"';
ok $content->body_contains('f'), 'content contains "f"';
ok $content->body_contains('o'), 'content contains "o"';
ok $content->body_contains('foo'), 'content contains "foo"';
$content = Mojo::Content::MultiPart->new({parts => [$content]});
ok $content->body_contains('foo'), 'content contains "foo"';
push @{$content->parts}, Mojo::Content::Single->new;
$content->parts->[1]->asset->add_chunk('.*?foo+');
$content->parts->[1]->headers->header('X-Bender' => 'bar+');
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/daemon.t
Expand Up @@ -227,7 +227,7 @@ ok $remote_port > 0, 'has remote port';

# Pipelined
my $daemon
= Mojo::Server::Daemon->new(listen => ['http://127.0.0.1'], silent => 1);
= Mojo::Server::Daemon->new({listen => ['http://127.0.0.1'], silent => 1});
$daemon->start;
my $port = Mojo::IOLoop->acceptor($daemon->acceptors->[0])->port;
is $daemon->app->moniker, 'HelloWorld', 'right moniker';
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/websocket_frames.t
Expand Up @@ -220,7 +220,7 @@ isnt(Mojo::Transaction::WebSocket->new->build_frame(1, 0, 0, 0, 2, ''),
$bytes, 'frames are not equal');

# Compressed binary message roundtrip
$ws = Mojo::Transaction::WebSocket->new(compressed => 1);
$ws = Mojo::Transaction::WebSocket->new({compressed => 1});
$bytes = $ws->build_message({binary => 'just works'});
$frame = $ws->parse_frame(\($dummy = $bytes));
is $frame->[0], 1, 'fin flag is set';
Expand Down
2 changes: 2 additions & 0 deletions t/mojolicious/app.t
Expand Up @@ -439,6 +439,8 @@ my $app = Mojolicious->new;
is $app->log->level, 'debug', 'right log level';

# Make sure we can override attributes with constructor arguments
$app = MojoliciousTest->new(mode => 'test');
is $app->mode, 'test', 'right mode';
$app = MojoliciousTest->new({mode => 'test'});
is $app->mode, 'test', 'right mode';

Expand Down

0 comments on commit 0e272be

Please sign in to comment.