From: Robert 'phaylon' Sedlacek Date: Thu, 19 Feb 2009 00:55:39 +0000 (+0000) Subject: added tests for composite role application X-Git-Tag: 0.05~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-Role-Parameterized.git;a=commitdiff_plain;h=d41dc048336438dcd3e2cbbc305d0d8d0973ea8b added tests for composite role application --- diff --git a/t/150-composite-role-application.t b/t/150-composite-role-application.t new file mode 100644 index 0000000..05df310 --- /dev/null +++ b/t/150-composite-role-application.t @@ -0,0 +1,126 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use Test::More tests => 9; + +do { + package MyCompositeRoleA; + use MooseX::Role::Parameterized; + + parameter attribute => ( + isa => 'Str', + required => 1, + ); + + role { + my $p = shift; + + has $p->attribute => ( + is => 'Int', + ); + }; +}; + +do { + package MyCompositeRoleB; + use MooseX::Role::Parameterized; + + parameter accessor => ( + isa => 'Str', + required => 1, + ); + + role { + my $p = shift; + + has $p->accessor => ( + is => 'rw', + isa => 'Int', + ); + }; +}; + +do { + package MyDoubleConsumer; + use Moose; + with MyCompositeRoleA => { attribute => 'foo' }, + MyCompositeRoleB => { accessor => 'bar' }; +}; + +local $@; +eval { + ok(MyDoubleConsumer->can('foo'), 'first role in composite applied successfully'); + ok(MyDoubleConsumer->can('bar'), 'second role in composite applied successfully'); +}; +ok !$@, 'testing composite roles lived'; + +do { + package MyExtendingRole; + use MooseX::Role::Parameterized; + + parameter foo => ( + isa => 'Int', + ); + + role { + my $p = shift; + + with 'MyCompositeRoleA', { attribute => 'bar' }; + + has foo => ( + is => 'rw', + default => sub { $p->foo }, + ); + }; +}; + +do { + package MyExtendedConsumer; + use Moose; + with MyCompositeRoleA => { attribute => 'bar' }, + MyExtendingRole => { foo => 23 }; +}; + +local $@; +eval { + ok(MyExtendedConsumer->can('bar'), 'role composed through other role applied successfully'); + is(MyExtendedConsumer->new->foo, 23, 'role composing other role applied successfully'); +}; +ok !$@, 'testing role through application through other role lived'; + +do { + package MyRoleProxy; + use MooseX::Role::Parameterized; + + parameter rolename => (isa => "Str"); + parameter roleparams => (isa => "HashRef"); + + role { + my $p = shift; + + with $p->rolename, $p->roleparams; + }; +}; + +do { + package MyProxyConsumer; + use Moose; + with( + MyRoleProxy => { + rolename => 'MyCompositeRoleA', + roleparams => { attribute => 'baz' }, + }, + MyCompositeRoleB => { + accessor => 'qux', + }, + ); +}; + +local $@; +eval { + ok(MyProxyConsumer->can('baz'), 'proxied role got applied successfully'); + ok(MyProxyConsumer->can('qux'), 'other role besides proxied one got applied successfully'); +}; +ok !$@, 'testing proxied roles lived'; +