File: memory.t

package info (click to toggle)
liblog-any-perl 1.717-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 448 kB
  • sloc: perl: 1,499; makefile: 11
file content (114 lines) | stat: -rw-r--r-- 3,700 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use strict;
use warnings;
use Test::More tests => 37;
use Log::Any;
use Log::Any::Adapter::Util qw(cmp_deeply);

BEGIN { 
    $Log::Any::OverrideDefaultProxyClass = 'Log::Any::Proxy::Test';
}

{

    package Foo;
    use Log::Any qw($log);

    sub log_debug {
        my ( $class, $text ) = @_;
        $log->debug($text) if $log->is_debug();
    }
}
{

    package Bar;
    use Log::Any qw($log);

    sub log_info {
        my ( $class, $text ) = @_;
        $log->info($text) if $log->is_info();
    }
}

require Log::Any::Adapter;

$Baz::log = Log::Any->get_logger( category => 'Baz' );
my $main_log = Log::Any->get_logger();
is( $main_log->adapter, Log::Any->get_logger()->adapter, "memoization - no cat" );
is( $main_log->adapter, Log::Any->get_logger( category => 'main' )->adapter,
    "memoization - cat" );

my $memclass  = 'Log::Any::Adapter::Test';
my $nullclass = 'Log::Any::Adapter::Null';

isa_ok( $Foo::log->adapter, $nullclass, 'Foo::log before set' );
isa_ok( $Bar::log->adapter, $nullclass, 'Bar::log before set' );
isa_ok( $Baz::log->adapter, $nullclass, 'Baz::log before set' );
isa_ok( $main_log->adapter, $nullclass, 'main_log before set' );
ok( !Log::Any->has_consumer, 'no consumer yet' );

my $entry = Log::Any::Adapter->set( { category => qr/Foo|Bar/ }, "+$memclass" );

isa_ok( $Foo::log->adapter, $memclass,  'Foo::log after first set' );
isa_ok( $Bar::log->adapter, $memclass,  'Bar::log after first set' );
isa_ok( $Baz::log->adapter, $nullclass, 'Baz::log after first set' );
isa_ok( $main_log->adapter, $nullclass, 'main_log after first set' );
ok( Log::Any->has_consumer, 'consumer active' );

my $entry2 =
  Log::Any::Adapter->set( { category => qr/Baz|main/ }, "+$memclass" );

isa_ok( $Foo::log->adapter, $memclass, 'Foo::log after second set' );
isa_ok( $Bar::log->adapter, $memclass, 'Bar::log after second set' );
isa_ok( $Baz::log->adapter, $memclass, 'Baz::log after second set' );
isa_ok( $main_log->adapter, $memclass, 'main_log after second set' );
ok( Log::Any->has_consumer, 'consumer active' );

ok( $Foo::log ne $Bar::log, 'Foo::log and Bar::log are different' );
is( $main_log->adapter, Log::Any->get_logger()->adapter, "memoization - no cat" );
is( $main_log->adapter, Log::Any->get_logger( category => 'main' )->adapter,
    "memoization - cat" );

cmp_deeply( $Foo::log->msgs, [], 'Foo::log has empty buffer' );
cmp_deeply( $Bar::log->msgs, [], 'Bar::log has empty buffer' );
cmp_deeply( $main_log->msgs, [], 'Bar::log has empty buffer' );
ok( $Foo::log ne $Bar::log, 'Foo::log and Bar::log are different objects' );

Foo->log_debug('for foo');
Bar->log_info('for bar');
$main_log->error('for main');

$Foo::log->category_contains_ok(
    Foo => qr/for foo/,
    'Foo log appeared in memory'
);

$Bar::log->category_contains_ok(
    Bar => qr/for bar/,
    'Bar log appeared in memory'
);

$main_log->category_contains_ok(
    main => qr/for main/,
    'main log appeared in memory'
);

Log::Any::Adapter->remove($entry);

isa_ok( $Foo::log->adapter, $nullclass, 'Foo::log' );
isa_ok( $Bar::log->adapter, $nullclass, 'Bar::log' );
isa_ok( $Baz::log->adapter, $memclass,  'Baz::log' );
isa_ok( $main_log->adapter, $memclass,  'main_log' );

Log::Any::Adapter->remove($entry2);

isa_ok( $Foo::log->adapter, $nullclass, 'Foo::log' );
isa_ok( $Bar::log->adapter, $nullclass, 'Bar::log' );
isa_ok( $Baz::log->adapter, $nullclass, 'Baz::log' );
isa_ok( $main_log->adapter, $nullclass, 'main_log' );

{
    Log::Any::Adapter->set( { category => 'Foo', lexically => \my $lex },
        "+$memclass" );
    isa_ok( $Foo::log->adapter, $memclass, 'Foo::log in lexical scope' );
}
isa_ok( $Foo::log->adapter, $nullclass, 'Foo::log outside lexical scope' );