X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F030_roles%2F010_run_time_role_composition.t;h=a1dfd7c62bb311bb33b47d56fbe5b63d58c692d5;hb=f0b2e5673e864903e74a429565d0c57b69a60b95;hp=1a86a1888e5e4400e4c60618068e8ddd01be4a55;hpb=7ff5653479c2bfc0794635f7fbade9bfe7bb2381;p=gitmo%2FMoose.git diff --git a/t/030_roles/010_run_time_role_composition.t b/t/030_roles/010_run_time_role_composition.t index 1a86a18..a1dfd7c 100644 --- a/t/030_roles/010_run_time_role_composition.t +++ b/t/030_roles/010_run_time_role_composition.t @@ -3,16 +3,15 @@ use strict; use warnings; -use Test::More tests => 27; +use Test::More; use Scalar::Util qw(blessed); - =pod This test can be used as a basis for the runtime role composition. -Apparently it is not as simple as just making an anon class. One of +Apparently it is not as simple as just making an anon class. One of the problems is the way that anon classes are DESTROY-ed, which is not very compatible with how instances are dealt with. @@ -37,37 +36,37 @@ not very compatible with how instances are dealt with. } my $obj = My::Class->new; -isa_ok($obj, 'My::Class'); - +isa_ok($obj, 'My::Class'); + my $obj2 = My::Class->new; -isa_ok($obj2, 'My::Class'); +isa_ok($obj2, 'My::Class'); { ok(!$obj->can( 'talk' ), "... the role is not composed yet"); - + ok(!$obj->does('Bark'), '... we do not do any roles yet'); - + Bark->meta->apply($obj); ok($obj->does('Bark'), '... we now do the Bark role'); - ok(!My::Class->does('Bark'), '... the class does not do the Bark role'); + ok(!My::Class->does('Bark'), '... the class does not do the Bark role'); isa_ok($obj, 'My::Class'); isnt(blessed($obj), 'My::Class', '... but it is no longer blessed into My::Class'); ok(!My::Class->can('talk'), "... the role is not composed at the class level"); ok($obj->can('talk'), "... the role is now composed at the object level"); - + is($obj->talk, 'woof', '... got the right return value for the newly composed method'); } { - ok(!$obj2->does('Bark'), '... we do not do any roles yet'); - - Bark->meta->apply($obj2); - - ok($obj2->does('Bark'), '... we now do the Bark role'); - is(blessed($obj), blessed($obj2), '... they share the same anon-class/role thing'); + ok(!$obj2->does('Sleeper'), '... we do not do any roles yet'); + + Sleeper->meta->apply($obj2); + + ok($obj2->does('Sleeper'), '... we now do the Sleeper role'); + isnt(blessed($obj), blessed($obj2), '... they DO NOT share the same anon-class/role thing'); } { @@ -78,29 +77,37 @@ isa_ok($obj2, 'My::Class'); Sleeper->meta->apply($obj); ok($obj->does('Bark'), '... we still do the Bark role'); - ok($obj->does('Sleeper'), '... we now do the Sleeper role too'); - - ok(!My::Class->does('Sleeper'), '... the class does not do the Sleeper role'); - - isnt(blessed($obj), blessed($obj2), '... they no longer share the same anon-class/role thing'); - + ok($obj->does('Sleeper'), '... we now do the Sleeper role too'); + + ok(!My::Class->does('Sleeper'), '... the class does not do the Sleeper role'); + + isnt(blessed($obj), blessed($obj2), '... they still don\'t share the same anon-class/role thing'); + isa_ok($obj, 'My::Class'); is(My::Class->sleep, 'nite-nite', '... the original method still responds as expected'); is($obj->sleep, 'snore', '... got the right return value for the newly composed method'); - is($obj->talk, 'zzz', '... got the right return value for the newly composed method'); + is($obj->talk, 'zzz', '... got the right return value for the newly composed method'); } { - ok(!$obj2->does('Sleeper'), '... we do not do any roles yet'); - - Sleeper->meta->apply($obj2); - - ok($obj2->does('Sleeper'), '... we now do the Bark role'); - is(blessed($obj), blessed($obj2), '... they share the same anon-class/role thing again'); -} + ok(!$obj2->does('Bark'), '... we do not do Bark yet'); + Bark->meta->apply($obj2); + ok($obj2->does('Bark'), '... we now do the Bark role'); + isnt(blessed($obj), blessed($obj2), '... they still don\'t share the same anon-class/role thing'); +} +# test that anon classes are equivalent after role composition in the same order +{ + foreach ($obj, $obj2) { + $_ = My::Class->new; + Bark->meta->apply($_); + Sleeper->meta->apply($_); + } + is(blessed($obj), blessed($obj2), '... they now share the same anon-class/role thing'); +} +done_testing;