8 use Scalar::Util qw(blessed);
13 This test can be used as a basis for the runtime role composition.
14 Apparently it is not as simple as just making an anon class. One of
15 the problems is the way that anon classes are DESTROY-ed, which is
16 not very compatible with how instances are dealt with.
35 sub sleep { 'nite-nite' }
38 my $obj = My::Class->new;
39 isa_ok($obj, 'My::Class');
41 my $obj2 = My::Class->new;
42 isa_ok($obj2, 'My::Class');
45 ok(!$obj->can( 'talk' ), "... the role is not composed yet");
47 ok(!$obj->does('Bark'), '... we do not do any roles yet');
49 Bark->meta->apply($obj);
51 ok($obj->does('Bark'), '... we now do the Bark role');
52 ok(!My::Class->does('Bark'), '... the class does not do the Bark role');
54 isa_ok($obj, 'My::Class');
55 isnt(blessed($obj), 'My::Class', '... but it is no longer blessed into My::Class');
57 ok(!My::Class->can('talk'), "... the role is not composed at the class level");
58 ok($obj->can('talk'), "... the role is now composed at the object level");
60 is($obj->talk, 'woof', '... got the right return value for the newly composed method');
64 ok(!$obj2->does('Sleeper'), '... we do not do any roles yet');
66 Sleeper->meta->apply($obj2);
68 ok($obj2->does('Sleeper'), '... we now do the Sleeper role');
69 isnt(blessed($obj), blessed($obj2), '... they DO NOT share the same anon-class/role thing');
73 is($obj->sleep, 'nite-nite', '... the original method responds as expected');
75 ok(!$obj->does('Sleeper'), '... we do not do the Sleeper role');
77 Sleeper->meta->apply($obj);
79 ok($obj->does('Bark'), '... we still do the Bark role');
80 ok($obj->does('Sleeper'), '... we now do the Sleeper role too');
82 ok(!My::Class->does('Sleeper'), '... the class does not do the Sleeper role');
84 isnt(blessed($obj), blessed($obj2), '... they still don\'t share the same anon-class/role thing');
86 isa_ok($obj, 'My::Class');
88 is(My::Class->sleep, 'nite-nite', '... the original method still responds as expected');
90 is($obj->sleep, 'snore', '... got the right return value for the newly composed method');
91 is($obj->talk, 'zzz', '... got the right return value for the newly composed method');
95 ok(!$obj2->does('Bark'), '... we do not do Bark yet');
97 Bark->meta->apply($obj2);
99 ok($obj2->does('Bark'), '... we now do the Bark role');
100 isnt(blessed($obj), blessed($obj2), '... they still don\'t share the same anon-class/role thing');
103 # test that anon classes are equivalent after role composition in the same order
105 foreach ($obj, $obj2) {
107 Bark->meta->apply($_);
108 Sleeper->meta->apply($_);
110 is(blessed($obj), blessed($obj2), '... they now share the same anon-class/role thing');