summaryrefslogtreecommitdiffstats
path: root/t/01-basic.t
blob: e97810145a84f832cbc478d13632e56fbadc6712 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Moose;

{
    package Baz;
    use Moose;
}

my $i;
{
    package Foo;
    use Moose;
    use Bread::Board::Declare;

    has foo => (
        is      => 'ro',
        isa     => 'Str',
        default => 'FOO',
    );

    has bar => (
        is    => 'ro',
        isa   => 'Str',
        value => 'BAR',
    );

    has baz => (
        is  => 'ro',
        isa => 'Baz',
    );

    has baz2 => (
        is      => 'ro',
        isa     => 'Baz',
        service => 0,
    );

    has quux => (
        is    => 'ro',
        isa   => 'Str',
        block => sub { 'QUUX' . $i++ },
    );
}

with_immutable {
$i = 0;
{
    my $foo = Foo->new;
    isa_ok($foo, 'Bread::Board::Container');
    ok($foo->has_service($_), "has service $_")
        for qw(bar baz quux);
    ok(!$foo->has_service($_), "doesn't have service $_")
        for qw(foo baz2);
    isa_ok($foo->get_service('bar'), 'Bread::Board::Declare::Literal');
    isa_ok($foo->get_service('baz'), 'Bread::Board::Declare::ConstructorInjection');
    isa_ok($foo->get_service('quux'), 'Bread::Board::Declare::BlockInjection');
}

{
    my $foo = Foo->new;
    is($foo->foo, 'FOO', "normal attrs work");
    is($foo->bar, 'BAR', "literals work");
    isa_ok($foo->baz, 'Baz');
    isnt($foo->baz, $foo->baz, "new instance each time");
    is($foo->quux, 'QUUX0', "block injections work");
    is($foo->quux, 'QUUX1', "and they are run on each access");
}

{
    my $baz = Baz->new;
    my $foo = Foo->new(
        foo  => 'OOF',
        bar  => 'RAB',
        baz  => $baz,
        quux => 'XUUQ',
    );
    is($foo->foo, 'OOF', "normal attrs work from constructor");
    is($foo->bar, 'RAB', "constructor overrides literals");
    isa_ok($foo->baz, 'Baz');
    is($foo->baz, $baz, "constructor overrides constructor injections");
    is($foo->baz, $foo->baz, "and returns the same thing each time");
    is($foo->quux, 'XUUQ', "constructor overrides block injections");
    is($foo->quux, 'XUUQ', "and returns the same thing each time");
}
} 'Foo';

done_testing;