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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use Bread::Board::ConstructorInjection;
use Bread::Board::Literal;
use Bread::Board::Container;
use Bread::Board::Dependency;
{
package Item;
use Moose;
has my_name => (is => 'ro');
package ListOfItems;
use Moose;
sub as_string { join ',',map {$_->my_name} @{shift->items} }
has 'items' => (is => 'ro', isa => 'ArrayRef');
}
{
my $s = Bread::Board::ConstructorInjection->new(
name => 'list_of_items',
class => 'ListOfItems',
dependencies => {
items => [
map {
Bread::Board::ConstructorInjection->new(
name => $_,
class => 'Item',
dependencies => {
my_name => Bread::Board::Literal->new(
name => 'item_name',
value => $_,
),
},
)
}
qw(one two three)
],
},
);
my $output = $s->get->as_string;
is(
$output,
'one,two,three',
'no container worked'
);
}
{
my $c = Bread::Board::Container->new(
name => 'list_container',
services => [
(map {
Bread::Board::ConstructorInjection->new(
name => "item_$_",
class => 'Item',
dependencies => {
my_name => Bread::Board::Literal->new(
name => 'item_name',
value => $_,
),
},
)
}
qw(one two three)),
Bread::Board::ConstructorInjection->new(
name => 'list_of_items',
class => 'ListOfItems',
dependencies => {
items => [ map { "item_$_" } qw(one two three) ],
},
),
],
);
my $output = $c->fetch('list_of_items')->get->as_string;
is(
$output,
'one,two,three',
'container with no ambiguous path names worked'
);
}
{
my $c = Bread::Board::Container->new(
name => 'list_container',
sub_containers => [
map { Bread::Board::Container->new(
name => "$_",
services => [
Bread::Board::ConstructorInjection->new(
name => "item",
class => 'Item',
dependencies => {
my_name => Bread::Board::Literal->new(
name => 'item_name',
value => $_,
),
},
)
],
) } qw(one two three),
],
services => [
Bread::Board::ConstructorInjection->new(
name => 'list_of_items',
class => 'ListOfItems',
dependencies => {
# all of these have a service_name of "item", the
# dependency coercion must give them distinct
# names
items => [ map { "/$_/item" } qw(one two three) ],
},
),
],
);
my $output = $c->fetch('list_of_items')->get->as_string;
is(
$output,
'one,two,three',
'multiple containers with ambiguous names worked'
);
}
done_testing;
|