From b2118130f3b95818f332816eaf97b9349e881146 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 10 Oct 2011 18:18:00 -0500 Subject: more tests --- t/subcontainers.t | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 110 insertions(+), 6 deletions(-) (limited to 't/subcontainers.t') diff --git a/t/subcontainers.t b/t/subcontainers.t index 992f873..b5921ce 100644 --- a/t/subcontainers.t +++ b/t/subcontainers.t @@ -2,6 +2,7 @@ use strict; use warnings; use Test::More; +use Test::Moose; { package SubContainer; @@ -28,15 +29,118 @@ use Test::More; } { - my $c = Container->new; - is($c->resolve(service => 'subcontainer/foo_sub'), 'FOOSUB'); - is($c->subcontainer->foo_sub, 'FOOSUB'); + package InlineSubContainers; + use Moose; + use Bread::Board::Declare; + use Bread::Board; + + has subcontainer => ( + traits => ['Container'], + is => 'ro', + isa => 'Bread::Board::Container', + default => sub { + container Foo => as { + service bar => 'BAR'; + }; + }, + ); + + has other_subcontainer => ( + traits => ['Container'], + is => 'ro', + isa => 'Bread::Board::Container', + default => sub { + container Foo => as { + service baz => ( + block => sub { + my $s = shift; + "other " . $s->param('other_bar'); + }, + dependencies => { + other_bar => '../subcontainer/bar', + }, + ); + }, + }, + ); } { - my $c = Container->new(subcontainer => SubContainer->new(foo_sub => 'SUBFOO')); - is($c->resolve(service => 'subcontainer/foo_sub'), 'SUBFOO'); - is($c->subcontainer->foo_sub, 'SUBFOO'); + package WithDeps; + use Moose; + use Bread::Board::Declare; + + has thing => ( + is => 'ro', + isa => 'Str', + value => 'THING', + ); + + has other_thing => ( + is => 'ro', + isa => 'Str', + block => sub { + my $s = shift; + $s->param('foo_sub'); + }, + dependencies => ['sub/foo_sub'], + ); + + has sub => ( + traits => ['Container'], + is => 'ro', + isa => 'SubContainer', + dependencies => { + foo_sub => 'thing', + }, + ); } +with_immutable { + { + my $c = Container->new; + is($c->resolve(service => 'subcontainer/foo_sub'), 'FOOSUB'); + isa_ok($c->subcontainer, 'SubContainer'); + is($c->subcontainer->foo_sub, 'FOOSUB'); + + my $c2 = Container->new; + isnt($c->subcontainer, $c2->subcontainer); + } + + { + my $c = Container->new(subcontainer => SubContainer->new(foo_sub => 'SUBFOO')); + is($c->resolve(service => 'subcontainer/foo_sub'), 'SUBFOO'); + isa_ok($c->subcontainer, 'SubContainer'); + is($c->subcontainer->foo_sub, 'SUBFOO'); + } + + { + my $c = InlineSubContainers->new; + + is($c->resolve(service => 'subcontainer/bar'), 'BAR'); + isa_ok($c->subcontainer, 'Bread::Board::Container'); + is($c->subcontainer->resolve(service => 'bar'), 'BAR'); + + is($c->resolve(service => 'other_subcontainer/baz'), 'other BAR'); + isa_ok($c->other_subcontainer, 'Bread::Board::Container'); + is($c->other_subcontainer->resolve(service => 'baz'), 'other BAR'); + } + + { + my $c = WithDeps->new; + is($c->resolve(service => 'sub/foo_sub'), 'THING'); + is($c->thing, 'THING'); + is($c->sub->foo_sub, 'THING'); + is($c->other_thing, 'THING'); + } + + { + my $c = WithDeps->new(thing => 'GNIHT'); + is($c->resolve(service => 'sub/foo_sub'), 'GNIHT'); + is($c->thing, 'GNIHT'); + is($c->sub->foo_sub, 'GNIHT'); + is($c->other_thing, 'GNIHT'); + } +} 'SubContainer', 'Container', 'InlineSubContainers', 'WithDeps'; + done_testing; -- cgit v1.2.3