From 8e670dd6512860312aa9ee3dd59c71fba7f182a5 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 27 Dec 2012 05:28:07 -0600 Subject: block services --- t/003_block_injection.t | 83 +++++++++++++++++++++++++++++++++++++ t/004_block_injection_w_out_class.t | 25 +++++++++++ 2 files changed, 108 insertions(+) create mode 100644 t/003_block_injection.t create mode 100644 t/004_block_injection_w_out_class.t (limited to 't') diff --git a/t/003_block_injection.t b/t/003_block_injection.t new file mode 100644 index 0000000..ec12ec0 --- /dev/null +++ b/t/003_block_injection.t @@ -0,0 +1,83 @@ +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; + + method shoot_up_good (Addict $class: *%args) { + $class.new(|%args, overdose => 1); + } +} + +{ + my $s = Bread::Board::BlockInjection.new( + name => 'William', + class => Addict, + block => -> $s { $s.class.new(|$s.params) }, + 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::BlockInjection); + 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); + + my $i2 = $s.get(stash => Mexican::Black::Tar.new); + isnt($i, $i2); + } + + 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; diff --git a/t/004_block_injection_w_out_class.t b/t/004_block_injection_w_out_class.t new file mode 100644 index 0000000..3ff775e --- /dev/null +++ b/t/004_block_injection_w_out_class.t @@ -0,0 +1,25 @@ +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); +} + +my $s = Bread::Board::BlockInjection.new( + name => 'NoClass', + block => -> $s { return { foo => $s.param('foo') } }, + dependencies => { + foo => Bread::Board::Literal.new(name => 'foo', value => 'FOO'); + }, +); + +isa_ok($s, Bread::Board::BlockInjection); +does_ok($s, Bread::Board::Service); + +my $x = $s.get; +isa_ok($x, Hash); +is_deeply($x, { foo => 'FOO' }); + +done; -- cgit v1.2.3-54-g00ecf