Commit | Line | Data |
67199842 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6475f69d |
6 | use Test::More; |
67199842 |
7 | |
8 | use Scalar::Util qw(blessed); |
9 | |
10 | |
67199842 |
11 | =pod |
12 | |
13 | This test can be used as a basis for the runtime role composition. |
6cfa1e5e |
14 | Apparently it is not as simple as just making an anon class. One of |
67199842 |
15 | the problems is the way that anon classes are DESTROY-ed, which is |
16 | not very compatible with how instances are dealt with. |
17 | |
18 | =cut |
19 | |
20 | { |
21 | package Bark; |
22 | use Mouse::Role; |
23 | |
24 | sub talk { 'woof' } |
25 | |
26 | package Sleeper; |
27 | use Mouse::Role; |
28 | |
29 | sub sleep { 'snore' } |
30 | sub talk { 'zzz' } |
31 | |
32 | package My::Class; |
33 | use Mouse; |
34 | |
35 | sub sleep { 'nite-nite' } |
36 | } |
37 | |
38 | my $obj = My::Class->new; |
6cfa1e5e |
39 | isa_ok($obj, 'My::Class'); |
40 | |
67199842 |
41 | my $obj2 = My::Class->new; |
6cfa1e5e |
42 | isa_ok($obj2, 'My::Class'); |
67199842 |
43 | |
44 | { |
45 | ok(!$obj->can( 'talk' ), "... the role is not composed yet"); |
6cfa1e5e |
46 | |
67199842 |
47 | ok(!$obj->does('Bark'), '... we do not do any roles yet'); |
6cfa1e5e |
48 | |
67199842 |
49 | Bark->meta->apply($obj); |
50 | |
51 | ok($obj->does('Bark'), '... we now do the Bark role'); |
6cfa1e5e |
52 | ok(!My::Class->does('Bark'), '... the class does not do the Bark role'); |
67199842 |
53 | |
54 | isa_ok($obj, 'My::Class'); |
55 | isnt(blessed($obj), 'My::Class', '... but it is no longer blessed into My::Class'); |
56 | |
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"); |
6cfa1e5e |
59 | |
67199842 |
60 | is($obj->talk, 'woof', '... got the right return value for the newly composed method'); |
61 | } |
62 | |
63 | { |
9864f0e4 |
64 | ok(!$obj2->does('Bark'), '... we do not do any roles yet'); |
6cfa1e5e |
65 | |
9864f0e4 |
66 | Bark->meta->apply($obj2); |
6cfa1e5e |
67 | |
9864f0e4 |
68 | ok($obj2->does('Bark'), '... we now do the Bark role'); |
69 | is(blessed($obj), blessed($obj2), '... they share the same anon-class/role thing'); |
67199842 |
70 | } |
71 | |
72 | { |
73 | is($obj->sleep, 'nite-nite', '... the original method responds as expected'); |
74 | |
75 | ok(!$obj->does('Sleeper'), '... we do not do the Sleeper role'); |
76 | |
77 | Sleeper->meta->apply($obj); |
78 | |
79 | ok($obj->does('Bark'), '... we still do the Bark role'); |
6cfa1e5e |
80 | ok($obj->does('Sleeper'), '... we now do the Sleeper role too'); |
81 | |
82 | ok(!My::Class->does('Sleeper'), '... the class does not do the Sleeper role'); |
83 | |
9864f0e4 |
84 | isnt(blessed($obj), blessed($obj2), '... they no longer share the same anon-class/role thing'); |
6cfa1e5e |
85 | |
67199842 |
86 | isa_ok($obj, 'My::Class'); |
87 | |
88 | is(My::Class->sleep, 'nite-nite', '... the original method still responds as expected'); |
89 | |
90 | is($obj->sleep, 'snore', '... got the right return value for the newly composed method'); |
6cfa1e5e |
91 | is($obj->talk, 'zzz', '... got the right return value for the newly composed method'); |
67199842 |
92 | } |
93 | |
94 | { |
9864f0e4 |
95 | ok(!$obj2->does('Sleeper'), '... we do not do any roles yet'); |
6cfa1e5e |
96 | |
9864f0e4 |
97 | Sleeper->meta->apply($obj2); |
fde8e43f |
98 | |
9864f0e4 |
99 | ok($obj2->does('Sleeper'), '... we now do the Bark role'); |
100 | is(blessed($obj), blessed($obj2), '... they share the same anon-class/role thing again'); |
67199842 |
101 | } |
102 | |
6475f69d |
103 | done_testing; |