File: 110_clone_w_singleton.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 (66 lines) | stat: -rw-r--r-- 1,917 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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Scalar::Util qw(refaddr);

use Bread::Board;

{
    package Test::Class;
    use Moose;
    has 'dep' => ( is => 'ro', isa => 'Int' );
}

my $board = Bread::Board::Container->new( name => 'app' );
isa_ok($board, 'Bread::Board::Container');

$board->add_service(
    Bread::Board::ConstructorInjection->new(
        lifecycle => 'Singleton',
        name      => 'test',
        class     => 'Test::Class',
        dependencies => {
            dep => Bread::Board::Dependency->new(service_path => '/dep'),
        },
    )
);
ok($board->has_service('test'), '... got the test service');
isa_ok($board->get_service('test'), 'Bread::Board::ConstructorInjection');

$board->add_service(
    Bread::Board::Literal->new(name => 'dep', value => 1)
);
ok($board->has_service('dep'), '... got the dep service');
isa_ok($board->get_service('dep'), 'Bread::Board::Literal');

## check the singleton-ness

is($board->fetch('/test')->get, $board->fetch('/test')->get, '... got the singleton');

# clone ...

my $board2 = $board->clone;
isa_ok($board2, 'Bread::Board::Container');
isnt($board, $board2, '... they are not the same instance');

ok($board2->has_service('test'), '... got the test service');
isa_ok($board2->get_service('test'), 'Bread::Board::ConstructorInjection');

ok($board2->has_service('dep'), '... got the dep service');
isa_ok($board2->get_service('dep'), 'Bread::Board::Literal');

isnt($board->get_service('test'), $board2->get_service('test'), '... not the same test services');
isnt($board->get_service('dep'), $board2->get_service('dep'), '... not the same dep services');

## check the singleton-ness

is($board2->fetch('/test')->get, $board2->fetch('/test')->get, '... got the singleton');

## check the singleton-less-ness

isnt($board->fetch('/test')->get, $board2->fetch('/test')->get, '... singleton are not shared');

done_testing;