Commit | Line | Data |
d7c04559 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
d71ba374 |
6 | use Test::More tests => 28; |
d7c04559 |
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 | |
b805c70c |
21 | =cut |
22 | |
d7c04559 |
23 | { |
24 | package Bark; |
25 | use Moose::Role; |
26 | |
27 | sub talk { 'woof' } |
28 | |
29 | package Sleeper; |
30 | use Moose::Role; |
31 | |
32 | sub sleep { 'snore' } |
33 | sub talk { 'zzz' } |
34 | |
35 | package My::Class; |
36 | use Moose; |
37 | |
38 | sub sleep { 'nite-nite' } |
39 | } |
40 | |
41 | my $obj = My::Class->new; |
d71ba374 |
42 | isa_ok($obj, 'My::Class'); |
43 | |
44 | my $obj2 = My::Class->new; |
45 | isa_ok($obj2, 'My::Class'); |
d7c04559 |
46 | |
47 | { |
d71ba374 |
48 | ok(!$obj->can( 'talk' ), "... the role is not composed yet"); |
d7c04559 |
49 | |
50 | ok(!$obj->does('Bark'), '... we do not do any roles yet'); |
51 | |
52 | Bark->meta->apply($obj); |
53 | |
54 | ok($obj->does('Bark'), '... we now do the Bark role'); |
55 | ok(!My::Class->does('Bark'), '... the class does not do the Bark role'); |
56 | |
57 | isa_ok($obj, 'My::Class'); |
b805c70c |
58 | isnt(blessed($obj), 'My::Class', '... but it is no longer blessed into My::Class'); |
d7c04559 |
59 | |
60 | ok(!My::Class->can('talk'), "... the role is not composed at the class level"); |
61 | ok($obj->can('talk'), "... the role is now composed at the object level"); |
62 | |
63 | is($obj->talk, 'woof', '... got the right return value for the newly composed method'); |
64 | } |
65 | |
66 | { |
d71ba374 |
67 | ok(!$obj2->does('Bark'), '... we do not do any roles yet'); |
68 | |
69 | Bark->meta->apply($obj2); |
70 | |
71 | ok($obj2->does('Bark'), '... we now do the Bark role'); |
72 | is(blessed($obj), blessed($obj2), '... they share the same anon-class/role thing'); |
73 | } |
74 | |
75 | { |
d7c04559 |
76 | is($obj->sleep, 'nite-nite', '... the original method responds as expected'); |
77 | |
b805c70c |
78 | ok(!$obj->does('Sleeper'), '... we do not do the Sleeper role'); |
d7c04559 |
79 | |
80 | Sleeper->meta->apply($obj); |
81 | |
82 | ok($obj->does('Bark'), '... we still do the Bark role'); |
83 | ok($obj->does('Sleeper'), '... we now do the Sleeper role too'); |
84 | |
d71ba374 |
85 | ok(!My::Class->does('Sleeper'), '... the class does not do the Sleeper role'); |
86 | |
87 | isnt(blessed($obj), blessed($obj2), '... they no longer share the same anon-class/role thing'); |
d7c04559 |
88 | |
89 | isa_ok($obj, 'My::Class'); |
90 | |
91 | is(My::Class->sleep, 'nite-nite', '... the original method still responds as expected'); |
92 | |
93 | is($obj->sleep, 'snore', '... got the right return value for the newly composed method'); |
94 | is($obj->talk, 'zzz', '... got the right return value for the newly composed method'); |
95 | } |
d71ba374 |
96 | |
97 | { |
98 | ok(!$obj2->does('Sleeper'), '... we do not do any roles yet'); |
99 | |
100 | Sleeper->meta->apply($obj2); |
101 | |
102 | ok($obj2->does('Sleeper'), '... we now do the Bark role'); |
103 | is(blessed($obj), blessed($obj2), '... they share the same anon-class/role thing again'); |
104 | } |
105 | |
106 | |
107 | |
108 | |