From 18e0c21c891deec7847d2cb43fc4e48d85baa219 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 27 Dec 2012 04:57:50 -0600 Subject: start working on this --- t/001_constructor_injection.t | 94 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 t/001_constructor_injection.t (limited to 't') diff --git a/t/001_constructor_injection.t b/t/001_constructor_injection.t new file mode 100644 index 0000000..331deee --- /dev/null +++ b/t/001_constructor_injection.t @@ -0,0 +1,94 @@ +use v6; +use Test; + +use Bread::Board; + +sub does_ok(Mu $var, Mu $type, $msg = ("The object does '" ~ $type.perl ~ "'")) { + ok($var.does($type), $msg); +} + +class Needle { } +class Mexican::Black::Tar { } +class Addict { + has $.needle; + has $.spoon; + has $.stash; + has $.overdose; + + method shoot_up_good (Addict $class: *%args) { + $class.new(|%args, overdose => 1); + } +} + +{ + my $s = Bread::Board::ConstructorInjection.new( + name => 'William', + class => Addict, + dependencies => { + needle => Bread::Board::ConstructorInjection.new( + name => 'spike', + class => Needle, + ), + spoon => Bread::Board::Literal.new( + name => 'works', + value => 'Spoon!', + ), + }, + parameters => { + stash => { isa => Mexican::Black::Tar }, + }, + ); + + isa_ok($s, Bread::Board::ConstructorInjection); + does_ok($s, Bread::Board::Service); + + { + my $i = $s.get(stash => Mexican::Black::Tar.new); + + isa_ok($i, Addict); + isa_ok($i.needle, Needle); + is($i.spoon, 'Spoon!'); + isa_ok($i.stash, Mexican::Black::Tar); + ok(!$i.overdose); + + my $i2 = $s.get(stash => Mexican::Black::Tar.new); + isnt($i, $i2); + } + + # $s.constructor_name('shoot_up_good'); + $s.constructor_name = 'shoot_up_good'; + + { + my $i = $s.get(stash => Mexican::Black::Tar.new); + isa_ok($i, Addict); + ok($i.overdose, 'Alternate constructor called'); + } + + is($s.name, 'William'); + is($s.class.perl, Addict.perl); + + my $deps = $s.dependencies; + is_deeply([$deps.keys.sort], [qw/needle spoon/]); + + my $needle = $s.get_dependency('needle'); + isa_ok($needle, Bread::Board::Dependency); + isa_ok($needle.service, Bread::Board::ConstructorInjection); + is($needle.service.name, 'spike'); + is($needle.service.class.perl, Needle.perl); + + my $spoon = $s.get_dependency('spoon'); + isa_ok($spoon, Bread::Board::Dependency); + isa_ok($spoon.service, Bread::Board::Literal); + is($spoon.service.name, 'works'); + is($spoon.service.value, 'Spoon!'); + + my $params = $s.parameters; + is_deeply([$params.keys.sort], [qw/stash/]); + is_deeply($params., { isa => Mexican::Black::Tar }); + + dies_ok { $s.get }; + dies_ok { $s.get(stash => []) }; + dies_ok { $s.get(stash => Mexican::Black::Tar.new, foo => 10) }; +} + +done; -- cgit v1.2.3-54-g00ecf