Commit | Line | Data |
d7c04559 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More tests => 1; |
7 | |
8 | use Scalar::Util qw(blessed); |
9 | |
10 | BEGIN { |
11 | use_ok('Moose'); |
12 | } |
13 | |
14 | =pod |
15 | |
16 | This test can be used as a basis for the runtime role composition. |
17 | Apparently it is not as simple as just making an anon class. One of |
18 | the problems is the way that anon classes are DESTROY-ed, which is |
19 | not very compatible with how instances are dealt with. |
20 | |
21 | { |
22 | package Bark; |
23 | use Moose::Role; |
24 | |
25 | sub talk { 'woof' } |
26 | |
27 | package Sleeper; |
28 | use Moose::Role; |
29 | |
30 | sub sleep { 'snore' } |
31 | sub talk { 'zzz' } |
32 | |
33 | package My::Class; |
34 | use Moose; |
35 | |
36 | sub sleep { 'nite-nite' } |
37 | } |
38 | |
39 | my $obj = My::Class->new; |
40 | ok(!$obj->can( 'talk' ), "... the role is not composed yet"); |
41 | |
42 | |
43 | { |
44 | isa_ok($obj, 'My::Class'); |
45 | |
46 | ok(!$obj->does('Bark'), '... we do not do any roles yet'); |
47 | |
48 | Bark->meta->apply($obj); |
49 | |
50 | ok($obj->does('Bark'), '... we now do the Bark role'); |
51 | ok(!My::Class->does('Bark'), '... the class does not do the Bark role'); |
52 | |
53 | isa_ok($obj, 'My::Class'); |
54 | isnt(blessed($obj), 'My::Class', '... but it is not longer blessed into My::Class'); |
55 | |
56 | ok(!My::Class->can('talk'), "... the role is not composed at the class level"); |
57 | ok($obj->can('talk'), "... the role is now composed at the object level"); |
58 | |
59 | is($obj->talk, 'woof', '... got the right return value for the newly composed method'); |
60 | } |
61 | |
62 | { |
63 | is($obj->sleep, 'nite-nite', '... the original method responds as expected'); |
64 | |
65 | ok(!$obj->does('Bark'), '... we do not do the Sleeper role'); |
66 | |
67 | Sleeper->meta->apply($obj); |
68 | |
69 | ok($obj->does('Bark'), '... we still do the Bark role'); |
70 | ok($obj->does('Sleeper'), '... we now do the Sleeper role too'); |
71 | |
72 | ok(!My::Class->does('Sleeper'), '... the class does not do the Sleeper role'); |
73 | |
74 | isa_ok($obj, 'My::Class'); |
75 | |
76 | is(My::Class->sleep, 'nite-nite', '... the original method still responds as expected'); |
77 | |
78 | is($obj->sleep, 'snore', '... got the right return value for the newly composed method'); |
79 | is($obj->talk, 'zzz', '... got the right return value for the newly composed method'); |
80 | } |
81 | |
82 | =cut |