Log-Mini/t/02_Logger.t

124 lines
2.5 KiB
Perl
Raw Normal View History

2019-02-21 22:30:49 +03:00
use lib 'lib';
use strict;
use warnings;
use Test::More;
use Test::Fatal;
2023-03-17 18:29:38 +03:00
2019-02-21 22:30:49 +03:00
subtest 'creates correct object' => sub {
isa_ok(LoggerTest->new, 'LoggerTest');
};
subtest 'has default log level' => sub {
my $logger = LoggerTest->new;
is $logger->level, 'error';
};
subtest 'sets log level' => sub {
my $logger = LoggerTest->new;
$logger->set_level('debug');
is $logger->level, 'debug';
};
subtest 'not throws when known log level' => sub {
my $log = LoggerTest->new;
2023-03-17 18:29:38 +03:00
for my $level (qw/error warn debug info trace/) {
2019-02-21 22:30:49 +03:00
ok !exception { $log->set_level($level) };
}
};
subtest 'throws exception when invalid log level' => sub {
my $log = LoggerTest->new;
ok exception { $log->set_level('unknown') };
};
subtest 'prints formatted line' => sub {
my $output = [];
2023-03-17 18:29:38 +03:00
my $log = _build_logger(output => $output);
2019-02-21 22:30:49 +03:00
2023-03-17 18:29:38 +03:00
for my $level (qw/error warn debug info trace/) {
$log->set_level($level);
2019-02-21 22:30:49 +03:00
$log->$level('message');
2023-03-17 18:29:38 +03:00
like $output->[-1], qr/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d.\d{3} \[$level\] message/;
2019-02-21 22:30:49 +03:00
}
};
2023-03-17 18:29:38 +03:00
subtest 'log all suitable levels' => sub {
2019-02-21 22:30:49 +03:00
my $output = [];
2023-03-17 18:29:38 +03:00
my $log = _build_logger(output => $output);
2019-02-21 22:30:49 +03:00
my $levels = {
error => [qw/error/],
warn => [qw/error warn/],
info => [qw/error warn info/],
debug => [qw/error warn info debug/],
trace => [qw/error warn info debug trace/],
};
for my $level (keys %$levels) {
$log->set_level($level);
for my $test_level (@{$levels->{$level}}) {
$log->$test_level('message');
ok $output->[-1];
}
}
};
subtest 'not logs when level is lower' => sub {
my $output = [];
2023-03-17 18:29:38 +03:00
my $log = _build_logger(output => $output);
2019-02-21 22:30:49 +03:00
my $levels = {
2023-03-17 18:29:38 +03:00
error => [qw/warn info debug trace/],
warn => [qw/info debug trace/],
info => [qw/debug trace/],
debug => [qw/trace/],
2019-02-21 22:30:49 +03:00
};
for my $level (keys %$levels) {
$log->set_level($level);
for my $test_level (@{$levels->{$level}}) {
$log->$test_level('message');
ok !$output->[-1], "not log '$test_level' when '$level'";
}
}
};
2023-03-17 18:29:38 +03:00
sub _build_logger
{
2019-02-21 22:30:49 +03:00
my $logger = LoggerTest->new(@_);
$logger->set_level('debug');
return $logger;
}
done_testing;
package LoggerTest;
2023-03-17 18:29:38 +03:00
use base 'Log::Mini::Logger::Base';
2019-02-21 22:30:49 +03:00
2023-03-17 18:29:38 +03:00
sub new
{
2019-02-21 22:30:49 +03:00
my $self = shift->SUPER::new(@_);
my (%params) = @_;
$self->{output} = $params{output};
return $self;
}
2023-03-17 18:29:38 +03:00
sub _print
{
2019-02-21 22:30:49 +03:00
my $self = shift;
push @{$self->{output}}, @_;
}