File: 060_extend_w_sugar.t

package info (click to toggle)
libbread-board-perl 0.29-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 620 kB
  • sloc: perl: 5,280; makefile: 13
file content (105 lines) | stat: -rw-r--r-- 2,668 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::Moose;

use Bread::Board;

{
    package FileLogger;
    use Moose;
    has 'log_file' => (is => 'ro', required => 1);

    package MyApplication;
    use Moose;
    has 'logger' => (is => 'ro', isa => 'FileLogger', required => 1);
}

{
    package My::App;
    use Moose;
    use Bread::Board;

    extends 'Bread::Board::Container';

    has 'log_file_name' => (
        is      => 'ro',
        isa     => 'Str',
        default => 'logfile.log',
    );

    sub BUILD {
        my $self = shift;

        container $self => as {

            service 'log_file' => $self->log_file_name;

            service 'logger' => (
                class        => 'FileLogger',
                lifecycle    => 'Singleton',
                dependencies => {
                    log_file => depends_on('log_file'),
                }
            );

            service 'application' => (
                class        => 'MyApplication',
                dependencies => {
                    logger => depends_on('logger'),
                }
            );

        };
    }
}

my $c1 = My::App->new( name => 'MyApp1' );
isa_ok($c1, 'My::App');
isa_ok($c1, 'Bread::Board::Container');

my $c2 = My::App->new( name => 'MyApp2', log_file_name => 'another_logfile.log' );
isa_ok($c2, 'My::App');
isa_ok($c2, 'Bread::Board::Container');

# test the first one

my $logger1 = $c1->resolve( service => 'logger' );
isa_ok($logger1, 'FileLogger');

is($logger1->log_file, 'logfile.log', '... got the right logfile dep');

is($c1->fetch('logger/log_file')->service, $c1->fetch('log_file'), '... got the right value');
is($c1->fetch('logger/log_file')->get, 'logfile.log', '... got the right value');

my $app1 = $c1->resolve( service => 'application' );
isa_ok($app1, 'MyApplication');

isa_ok($app1->logger, 'FileLogger');
is($app1->logger, $logger1, '... got the right logger (singleton)');

# test the second one

my $logger2 = $c2->resolve( service => 'logger' );
isa_ok($logger2, 'FileLogger');

is($logger2->log_file, 'another_logfile.log', '... got the right logfile dep');

is($c2->fetch('logger/log_file')->service, $c2->fetch('log_file'), '... got the right value');
is($c2->fetch('logger/log_file')->get, 'another_logfile.log', '... got the right value');

my $app2 = $c2->resolve( service => 'application' );
isa_ok($app2, 'MyApplication');

isa_ok($app2->logger, 'FileLogger');
is($app2->logger, $logger2, '... got the right logger (singleton)');

# make sure they share nothing

isnt( $logger1, $logger2, '... these are not the same' );
isnt( $app1, $app2, '... these are not the same' );

done_testing;