Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added basic config tests
  • Loading branch information
kraih committed Feb 6, 2012
1 parent 7717a23 commit c7f2a5d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
20 changes: 11 additions & 9 deletions lib/Mojo.pm
Expand Up @@ -38,26 +38,28 @@ sub new {

sub build_tx { Mojo::Transaction::HTTP->new }

sub config {
my $self = shift;
sub config { shift->_dict(config => @_) }

# "D’oh."
sub handler { croak 'Method "handler" not implemented in subclass' }

sub _dict {
my ($self, $name) = (shift, shift);

# Hash
$self->{config} ||= {};
return $self->{config} unless @_;
$self->{$name} ||= {};
return $self->{$name} unless @_;

# Get
return $self->{config}->{$_[0]} unless @_ > 1 || ref $_[0];
return $self->{$name}->{$_[0]} unless @_ > 1 || ref $_[0];

# Set
my $values = ref $_[0] ? $_[0] : {@_};
$self->{config} = {%{$self->{config}}, %$values};
$self->{$name} = {%{$self->{$name}}, %$values};

return $self;
}

# "D’oh."
sub handler { croak 'Method "handler" not implemented in subclass' }

1;
__END__
Expand Down
17 changes: 1 addition & 16 deletions lib/Mojolicious.pm
Expand Up @@ -110,22 +110,7 @@ sub build_tx {
return $tx;
}

sub defaults {
my $self = shift;

# Hash
$self->{defaults} ||= {};
return $self->{defaults} unless @_;

# Get
return $self->{defaults}->{$_[0]} unless @_ > 1 || ref $_[0];

# Set
my $values = ref $_[0] ? $_[0] : {@_};
$self->{defaults} = {%{$self->{defaults}}, %$values};

return $self;
}
sub defaults { shift->_dict(defaults => @_) }

sub dispatch {
my ($self, $c) = @_;
Expand Down
13 changes: 12 additions & 1 deletion t/mojo/app.t
Expand Up @@ -6,7 +6,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 44;
use Test::More tests => 50;

# "I was so bored I cut the pony tail off the guy in front of us.
# Look at me, I'm a grad student.
Expand All @@ -25,6 +25,17 @@ my $logger = Mojo::Log->new;
my $app = Mojo->new({log => $logger});
is $app->log, $logger, 'right logger';

# Config
is $app->config('foo'), undef, 'no value';
is_deeply $app->config(foo => 'bar')->config, {foo => 'bar'}, 'right value';
is $app->config('foo'), 'bar', 'right value';
delete $app->config->{foo};
is $app->config('foo'), undef, 'no value';
$app->config(foo => 'bar', baz => 'yada');
is_deeply $app->config, {foo => 'bar', baz => 'yada'}, 'right value';
$app->config({test => 23});
is $app->config->{test}, 23, 'right value';

# Fresh application
$app = Mojolicious->new;
my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton)->app($app);
Expand Down

0 comments on commit c7f2a5d

Please sign in to comment.