summaryrefslogtreecommitdiffstats
path: root/t/030_lifecycle_singleton.t
blob: 9da7b83149c443c8b4e7cc1fcc2a2f96f859a3fe (plain) (blame)
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
use v6;
use Test;

use Bread::Board;

# PERL6: doing anything at all with the type object for a role with required
# methods is broken
#sub does_ok(Mu $var, Mu $type, $msg = ("The object does '" ~ $type.perl ~ "'")) {
sub does_ok(Mu $var, Mu $type, $msg = ("The object does [some role]")) {
    ok($var.does($type), $msg);
}

class Needle { }
class Mexican::Black::Tar { }
class Addict {
    has ($.needle, $.spoon, $.stash);
}

my $s = Bread::Board::ConstructorInjection.new(
    lifecycle    => Singleton,
    name         => 'William',
    class        => Addict,
    dependencies => {
        needle => Bread::Board::ConstructorInjection.new(
            name  => 'spike',
            class => Needle,
        ),
        spoon => Bread::Board::Literal.new(
            name  => 'works',
            value => 'Spoon!',
        ),
    },
    parameters => {
        stash => { isa => Mexican::Black::Tar },
    },
);

isa_ok($s, Bread::Board::ConstructorInjection);
does_ok($s, Bread::Board::Service);

does_ok($s, Bread::Board::Singleton);
is($s.lifecycle.^name, 'Singleton');

ok(!$s.has_instance);
my $i = $s.get(stash => Mexican::Black::Tar.new);
ok($s.has_instance);

isa_ok($i, Addict);
isa_ok($i.needle, Needle);
is($i.spoon, 'Spoon!');
isa_ok($i.stash, Mexican::Black::Tar);

{
    my $i2 = $s.get(stash => Mexican::Black::Tar.new);
    is($i, $i2);
}

$s.flush_instance;

{
    my $i2 = $s.get(stash => Mexican::Black::Tar.new);
    isnt($i, $i2);

    {
        my $i2a = $s.get(stash => Mexican::Black::Tar.new);
        is($i2, $i2a);
    }
}

done;

# vim:ft=perl6:foldmethod=manual