1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7use Test::Fatal;
8
9use Bread::Board;
10
11{
12    my $c = container Foo => as {
13        container Bar => as {
14            service baz => 21;
15        };
16
17        container Moo => ['Bar'] => as {
18            service kooh => (
19                block => sub {
20                    my ($s) = @_;
21                    $s->param('baz') * 2;
22                },
23                dependencies => {
24                    baz => depends_on('Bar/baz'),
25                },
26            );
27        };
28    };
29
30    container $c => as {
31        container '+Bar' => as {
32            service bif => 123;
33        };
34
35        container '+Moo' => as {
36            service boo => (
37                block => sub {
38                    my ($s) = @_;
39                    $s->param('a') + $s->param('b');
40                },
41                dependencies => {
42                    a => depends_on('kooh'),
43                    b => depends_on('Bar/bif'),
44                },
45            );
46        };
47    };
48
49    is $c->resolve(service => 'Bar/baz'), 21, 'can resolve Bar/Baz from container';
50    is $c->resolve(service => 'Bar/bif'), 123, 'can resolve Bar/bif from container';
51
52    my $p = $c->fetch('Moo')->create(Bar => $c->fetch('Bar'));
53    is $p->resolve(service => 'kooh'), 42, 'can resolve kooh from parameterized container';
54    is $p->resolve(service => 'boo'), 165, 'can resolve boo from parameterized container';
55
56    like exception {
57        container '+Foo' => as {};
58    }, qr/^Inheriting containers isn't possible outside of the context of a container/, 'exception thrown when trying to inherit +Foo outside of container context';
59
60    like exception {
61        container $c => as {
62            container '+Buf' => as {};
63        };
64    }, qr/^Could not find container or service for Buf in Foo/, 'exception thrown when trying to inherit +Buf and it does not exist';
65
66    like exception {
67        container $c => as {
68            container '+Buf' => ['Moo'] => as {};
69        };
70    }, qr/^Declaring container parameters when inheriting is not supported/, 'exception thrown when trying to declare container parameters when inheriting';
71}
72
73{
74    {
75        package Thing;
76        use Moose;
77        has bar => (is => 'ro', required => 1);
78        no Moose;
79    }
80
81    {
82        package TestThing;
83        use Moose;
84        extends 'Thing';
85        no Moose;
86    }
87
88    my $c = container Foo => as {
89        service bar => 42;
90
91        container Moo => as {
92            container Kooh => as {
93                service boo => (
94                    class => 'Thing',
95                    dependencies => {
96                        bar => '../../bar',
97                    },
98                );
99            };
100        };
101    };
102
103    isa_ok $c->resolve(service => 'Moo/Kooh/boo'), 'Thing', 'can resolve Moo/Kooh/boo and get Thing';
104    is $c->resolve(service => 'Moo/Kooh/boo')->bar, 42, '... and can call bar method on it';
105
106    container $c => as {
107        container '+Moo/Kooh' => as {
108            service '+boo' => (class => 'TestThing');
109        };
110    };
111
112    isa_ok $c->resolve(service => 'Moo/Kooh/boo'), 'TestThing', 'can resolve Moo/Kooh/boo and get TestThing';
113    is $c->resolve(service => 'Moo/Kooh/boo')->bar, 42, '... and can call bar method on it';
114}
115
116done_testing;
117