From 64b0df203441b83a6601371bfadb1aac634e5d20 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 16 Apr 2011 15:37:47 -0500 Subject: fix this test up a bit --- t/50-infer.t | 145 +++++++++++++++++++++++------------------------------------ 1 file changed, 55 insertions(+), 90 deletions(-) (limited to 't') diff --git a/t/50-infer.t b/t/50-infer.t index 88fcb68..bae915d 100644 --- a/t/50-infer.t +++ b/t/50-infer.t @@ -7,6 +7,12 @@ use Test::Fatal; { package Foo; use Moose; + + has data => ( + is => 'ro', + isa => 'Str', + default => 'FOO', + ); } { @@ -29,6 +35,11 @@ use Test::Fatal; isa => 'Bar', required => 1, ); + + has thing => ( + is => 'ro', + isa => 'Str', + ); } { @@ -36,17 +47,6 @@ use Test::Fatal; use Moose; use Bread::Board::Declare; - has foo => ( - is => 'ro', - isa => 'Foo', - block => sub { Foo->new }, - ); - - has bar => ( - is => 'ro', - isa => 'Bar', - ); - has baz => ( is => 'ro', isa => 'Baz', @@ -58,6 +58,11 @@ use Test::Fatal; isa_ok($c->baz, 'Baz'); isa_ok($c->baz->foo, 'Foo'); isa_ok($c->baz->bar, 'Bar'); + + is($c->baz->thing, undef, "right thing"); + is($c->baz->foo->data, 'FOO', "right data"); + + isa_ok($c->resolve(type => 'Baz'), 'Baz'); } { @@ -66,9 +71,7 @@ use Test::Fatal; extends 'Baz'; - has thing => ( - is => 'ro', - isa => 'Str', + has '+thing' => ( required => 1, ); } @@ -78,16 +81,6 @@ use Test::Fatal; use Moose; use Bread::Board::Declare; - has foo => ( - is => 'ro', - isa => 'Foo', - ); - - has bar => ( - is => 'ro', - isa => 'Bar', - ); - has baz => ( is => 'ro', isa => 'Baz2', @@ -107,22 +100,18 @@ use Test::Fatal; use Moose; use Bread::Board::Declare; - has foo => ( - is => 'ro', - isa => 'Foo', - ); - - has bar => ( - is => 'ro', - isa => 'Bar', - ); - has thing => ( is => 'ro', isa => 'Str', value => 'THING', ); + has foo => ( + is => 'ro', + isa => 'Foo', + dependencies => { data => 'thing' }, + ); + has baz => ( is => 'ro', isa => 'Baz2', @@ -135,89 +124,65 @@ use Test::Fatal; isa_ok($c->baz, 'Baz2'); isa_ok($c->baz->foo, 'Foo'); isa_ok($c->baz->bar, 'Bar'); + is($c->baz->foo->data, 'THING', + "inference finds services in the container"); is($c->baz->thing, 'THING', "partial dependency specification works"); } { - package My::Container4; + package Quux; use Moose; - use Bread::Board::Declare; - has foo => ( - is => 'ro', - isa => 'Foo', + has baz => ( + is => 'ro', + isa => 'Baz', + required => 1, ); - has other_foo => ( + has foo => ( is => 'ro', isa => 'Foo', ); - - has bar => ( - is => 'ro', - isa => 'Bar', - ); - - has baz => ( - is => 'ro', - isa => 'Baz', - ); -} - -{ - like( - exception { My::Container4->new }, - qr/^You have already declared a typemap for type Foo/, - "correct error when inferring is ambiguous" - ); -} - -{ - package Foo2; - use Moose; - - extends 'Foo'; - - has bar => ( - is => 'ro', - isa => 'Bar', - required => 1, - ); } { - package My::Container5; + package My::Container4; use Moose; use Bread::Board::Declare; - has foo => ( - is => 'ro', - isa => 'Foo2', + has data => ( + is => 'ro', + isa => 'Str', + value => 'DATA', ); - has other_foo => ( - is => 'ro', - isa => 'Foo2', - typemap => 0, + has quux => ( + is => 'ro', + isa => 'Quux', ); - has bar => ( - is => 'ro', - isa => 'Bar', + has foo => ( + is => 'ro', + isa => 'Foo', + dependencies => ['data'], ); - has baz => ( - is => 'ro', - isa => 'Baz', + has quux2 => ( + is => 'ro', + isa => 'Quux', + dependencies => ['foo'], ); } { - my $c = My::Container5->new; - isa_ok($c->baz, 'Baz'); - isa_ok($c->baz->foo, 'Foo'); - isa_ok($c->baz->bar, 'Bar'); - isa_ok($c->other_foo->bar, 'Bar'); + my $c = My::Container4->new; + isa_ok($c->quux, 'Quux'); + isa_ok($c->quux->baz, 'Baz'); + isa_ok($c->quux->baz->foo, 'Foo'); + isa_ok($c->quux->baz->bar, 'Bar'); + + is($c->quux->foo, undef, "non-required attrs are not inferred"); + is($c->quux2->foo->data, 'DATA', "but can be explicitly specified"); } done_testing; -- cgit v1.2.3-54-g00ecf