Log-Mini/t/03_LoggerFILE.t
Denis Fedoseev 0bd73580aa Checking in changes prior to tagging of version 0.1.1.
Changelog diff is:

diff --git a/Changes b/Changes
index b732829..0a53712 100644
--- a/Changes
+++ b/Changes
@@ -2,6 +2,9 @@ Revision history for Perl extension Log-Mini

 {{$NEXT}}

+0.1.1 2019-03-19T20:44:04Z
+    - Error in the constructor was fixed. 0.1.0 removed from CPAN.
+
 0.1.0 2019-03-19T12:08:23Z
     - Supports formatted with sprintf messages
     - log method was added
2019-03-19 23:44:10 +03:00

87 lines
No EOL
1.8 KiB
Perl

use lib 'lib';
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use File::Temp;
use Log::Mini::LoggerFILE;
subtest 'creates correct object' => sub {
isa_ok(Log::Mini::LoggerFILE->new, 'Log::Mini::LoggerFILE');
};
subtest 'prints to file' => sub {
for my $level (qw/error warn debug/) {
my $file = File::Temp->new;
my $log = _build_logger(file => $file->filename);
$log->$level('message');
undef $log;
my $content = _slurp($file);
ok $content;
}
};
subtest 'prints to file synced' => sub {
my $file = File::Temp->new;
my $log = _build_logger(file => $file->filename, synced => 1);
for my $level (qw/error warn debug/) {
$log->$level('message');
my $content = _slurp($file);
ok $content;
}
};
subtest 'prints to stderr with \n' => sub {
for my $level (qw/error warn debug/) {
my $file = File::Temp->new;
my $log = _build_logger(file => $file->filename);
$log->$level('message');
undef $log;
my $content = _slurp($file);
like $content, qr/\n$/;
}
};
subtest 'prints sprintf formatted line' => sub {
for my $level (qw/error warn debug/) {
my $file = File::Temp->new;
my $log = _build_logger(file => $file->filename);
$log->$level('message %s', 'formatted');
undef $log;
my $content = _slurp($file);
like $content,
qr/^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d.\d{3} \[$level\] message formatted$/;
}
};
sub _slurp {
my $file = shift;
my $content = do { local $/; open my $fh, '<', $file->filename or die $!; <$fh> };
return $content;
}
sub _build_logger {
my $logger = Log::Mini::LoggerFILE->new(@_);
$logger->set_level('debug');
return $logger;
}
done_testing;