From 8ad579f26a1c74d5685491df55b57e694e54e087 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 20 Feb 2011 02:04:52 -0600 Subject: initial implementation --- t/01-basic.t | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ t/02-deps.t | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 t/01-basic.t create mode 100644 t/02-deps.t (limited to 't') diff --git a/t/01-basic.t b/t/01-basic.t new file mode 100644 index 0000000..a679a47 --- /dev/null +++ b/t/01-basic.t @@ -0,0 +1,79 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +{ + package Baz; + use Moose; +} + +{ + package Foo; + use Moose; + use MooseX::Bread::Board; + + has foo => ( + is => 'ro', + isa => 'Str', + default => 'FOO', + ); + + has bar => ( + is => 'ro', + isa => 'Str', + value => 'BAR', + ); + + has baz => ( + is => 'ro', + isa => 'Baz', + class => 'Baz', + ); + + my $i = 0; + has quux => ( + is => 'ro', + isa => 'Str', + block => sub { 'QUUX' . $i++ }, + ); +} + +{ + my $foo = Foo->new; + ok($foo->has_service($_), "has service $_") + for qw(bar baz quux); + ok(!$foo->has_service('foo'), "doesn't have service foo"); + isa_ok($foo->get_service('bar'), 'MooseX::Bread::Board::Literal'); + isa_ok($foo->get_service('baz'), 'MooseX::Bread::Board::ConstructorInjection'); + isa_ok($foo->get_service('quux'), 'MooseX::Bread::Board::BlockInjection'); +} + +{ + my $foo = Foo->new; + is($foo->foo, 'FOO', "normal attrs work"); + is($foo->bar, 'BAR', "literals work"); + isa_ok($foo->baz, 'Baz'); + isnt($foo->baz, $foo->baz, "new instance each time"); + is($foo->quux, 'QUUX0', "block injections work"); + is($foo->quux, 'QUUX1', "and they are run on each access"); +} + +{ + my $baz = Baz->new; + my $foo = Foo->new( + foo => 'OOF', + bar => 'RAB', + baz => $baz, + quux => 'XUUQ', + ); + is($foo->foo, 'OOF', "normal attrs work from constructor"); + is($foo->bar, 'RAB', "constructor overrides literals"); + isa_ok($foo->baz, 'Baz'); + is($foo->baz, $baz, "constructor overrides constructor injections"); + is($foo->baz, $foo->baz, "and returns the same thing each time"); + is($foo->quux, 'XUUQ', "constructor overrides block injections"); + is($foo->quux, 'XUUQ', "and returns the same thing each time"); +} + +done_testing; diff --git a/t/02-deps.t b/t/02-deps.t new file mode 100644 index 0000000..73b3d4a --- /dev/null +++ b/t/02-deps.t @@ -0,0 +1,45 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +{ + package Baz; + use Moose; + + has bar => ( + is => 'ro', + isa => 'Str', + required => 1, + ); +} + +{ + package Foo; + use Moose; + use MooseX::Bread::Board; + + my $i = 0; + has bar => ( + is => 'ro', + isa => 'Str', + block => sub { $i++ }, + ); + + has baz => ( + is => 'ro', + isa => 'Baz', + class => 'Baz', + dependencies => ['bar'], + ); +} + +{ + my $foo = Foo->new; + my $baz = $foo->baz; + is($baz->bar, '0', "deps resolved correctly"); + is($baz->bar, '0', "doesn't re-resolve, since Baz is a normal class"); + is($foo->baz->bar, '1', "re-resolves since the baz attr isn't a singleton"); +} + +done_testing; -- cgit v1.2.3-54-g00ecf