Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added Mojo::Log tests
  • Loading branch information
kraih committed Mar 31, 2012
1 parent a129eea commit b6bcc42
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 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.71 2012-03-31
- Improved documentation.
- Improved tests.

2.70 2012-03-30
- Improved speed of version command by switching to the MetaCPAN API.
Expand Down
3 changes: 0 additions & 3 deletions lib/Mojo/Loader.pm
@@ -1,9 +1,6 @@
package Mojo::Loader;
use Mojo::Base -base;

# "Don't let Krusty's death get you down, boy.
# People die all the time, just like that.
# Why, you could wake up dead tomorrow! Well, good night."
use File::Basename 'fileparse';
use File::Spec::Functions qw/catdir catfile splitdir/;
use Mojo::Command;
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Log.pm
Expand Up @@ -53,8 +53,7 @@ sub fatal { shift->log('fatal', @_) }

sub format {
my ($self, $level, @msgs) = @_;
my $msgs = join "\n", @msgs;
return '[' . localtime(time) . "] [$level] $msgs\n";
return '[' . localtime(time) . "] [$level] " . join("\n", @msgs) . "\n";
}

sub info { shift->log('info', @_) }
Expand Down
70 changes: 70 additions & 0 deletions t/mojo/log.t
@@ -0,0 +1,70 @@
use Mojo::Base -strict;

use Test::More tests => 30;

# "Don't let Krusty's death get you down, boy.
# People die all the time, just like that.
# Why, you could wake up dead tomorrow! Well, good night."
use Mojo::Log;

# Formatting
my $log = Mojo::Log->new;
like $log->format(debug => 'Test 123.'), qr/^\[.*\] \[debug\] Test 123\.\n$/,
'right format';
like $log->format(qw/debug Test 123./), qr/^\[.*\] \[debug\] Test\n123\.\n$/,
'right format';

# Events
my $messages = [];
$log->unsubscribe('message')->on(
message => sub {
my ($log, $level, @messages) = @_;
push @$messages, $level, @messages;
}
);
$log->info('Whatever.');
is_deeply $messages, [qw/info Whatever./], 'right messages';
$log->level('error')->info('Again.');
is_deeply $messages, [qw/info Whatever./], 'right messages';
$log->fatal('Test', 123);
is_deeply $messages, [qw/info Whatever. fatal Test 123/], 'right messages';

# "debug"
$log->level('debug');
ok $log->is_debug, '"debug" log level is active';
ok $log->is_info, '"info" log level is active';
ok $log->is_warn, '"warn" log level is active';
ok $log->is_error, '"error" log level is active';
ok $log->is_fatal, '"fatal" log level is active';

# "info"
$log->level('info');
ok !$log->is_debug, '"debug" log level is inactive';
ok $log->is_info, '"info" log level is active';
ok $log->is_warn, '"warn" log level is active';
ok $log->is_error, '"error" log level is active';
ok $log->is_fatal, '"fatal" log level is active';

# "warn"
$log->level('warn');
ok !$log->is_debug, '"debug" log level is inactive';
ok !$log->is_info, '"info" log level is inactive';
ok $log->is_warn, '"warn" log level is active';
ok $log->is_error, '"error" log level is active';
ok $log->is_fatal, '"fatal" log level is active';

# "error"
$log->level('error');
ok !$log->is_debug, '"debug" log level is inactive';
ok !$log->is_info, '"info" log level is inactive';
ok !$log->is_warn, '"warn" log level is inactive';
ok $log->is_error, '"error" log level is active';
ok $log->is_fatal, '"fatal" log level is active';

# "fatal"
$log->level('fatal');
ok !$log->is_debug, '"debug" log level is inactive';
ok !$log->is_info, '"info" log level is inactive';
ok !$log->is_warn, '"warn" log level is inactive';
ok !$log->is_error, '"error" log level is inactive';
ok $log->is_fatal, '"fatal" log level is active';

0 comments on commit b6bcc42

Please sign in to comment.