Commit | Line | Data |
d7c04559 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
d7c04559 |
7 | |
8 | use Scalar::Util qw(blessed); |
9 | |
7ff56534 |
10 | |
d7c04559 |
11 | =pod |
12 | |
13 | This test can be used as a basis for the runtime role composition. |
d03bd989 |
14 | Apparently it is not as simple as just making an anon class. One of |
d7c04559 |
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 | |
b805c70c |
18 | =cut |
19 | |
d7c04559 |
20 | { |
21 | package Bark; |
22 | use Moose::Role; |
23 | |
24 | sub talk { 'woof' } |
25 | |
26 | package Sleeper; |
27 | use Moose::Role; |
28 | |
29 | sub sleep { 'snore' } |
30 | sub talk { 'zzz' } |
31 | |
32 | package My::Class; |
33 | use Moose; |
34 | |
35 | sub sleep { 'nite-nite' } |
36 | } |
37 | |
38 | my $obj = My::Class->new; |
d03bd989 |
39 | isa_ok($obj, 'My::Class'); |
40 | |
d71ba374 |
41 | my $obj2 = My::Class->new; |
d03bd989 |
42 | isa_ok($obj2, 'My::Class'); |
d7c04559 |
43 | |
44 | { |
d71ba374 |
45 | ok(!$obj->can( 'talk' ), "... the role is not composed yet"); |
d03bd989 |
46 | |
d7c04559 |
47 | ok(!$obj->does('Bark'), '... we do not do any roles yet'); |
d03bd989 |
48 | |
d7c04559 |
49 | Bark->meta->apply($obj); |
50 | |
51 | ok($obj->does('Bark'), '... we now do the Bark role'); |
d03bd989 |
52 | ok(!My::Class->does('Bark'), '... the class does not do the Bark role'); |
d7c04559 |
53 | |
54 | isa_ok($obj, 'My::Class'); |
b805c70c |
55 | isnt(blessed($obj), 'My::Class', '... but it is no longer blessed into My::Class'); |
d7c04559 |
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"); |
d03bd989 |
59 | |
d7c04559 |
60 | is($obj->talk, 'woof', '... got the right return value for the newly composed method'); |
61 | } |
62 | |
63 | { |
01e8a20b |
64 | ok(!$obj2->does('Sleeper'), '... we do not do any roles yet'); |
d03bd989 |
65 | |
01e8a20b |
66 | Sleeper->meta->apply($obj2); |
d03bd989 |
67 | |
01e8a20b |
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'); |
d71ba374 |
70 | } |
71 | |
72 | { |
d7c04559 |
73 | is($obj->sleep, 'nite-nite', '... the original method responds as expected'); |
74 | |
b805c70c |
75 | ok(!$obj->does('Sleeper'), '... we do not do the Sleeper role'); |
d7c04559 |
76 | |
77 | Sleeper->meta->apply($obj); |
78 | |
79 | ok($obj->does('Bark'), '... we still do the Bark role'); |
d03bd989 |
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 | |
01e8a20b |
84 | isnt(blessed($obj), blessed($obj2), '... they still don\'t share the same anon-class/role thing'); |
d03bd989 |
85 | |
d7c04559 |
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'); |
d03bd989 |
91 | is($obj->talk, 'zzz', '... got the right return value for the newly composed method'); |
d7c04559 |
92 | } |
d71ba374 |
93 | |
94 | { |
01e8a20b |
95 | ok(!$obj2->does('Bark'), '... we do not do Bark yet'); |
d03bd989 |
96 | |
01e8a20b |
97 | Bark->meta->apply($obj2); |
d03bd989 |
98 | |
01e8a20b |
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'); |
d71ba374 |
101 | } |
102 | |
11e1ad08 |
103 | # test that anon classes are equivalent after role composition in the same order |
104 | { |
105 | foreach ($obj, $obj2) { |
106 | $_ = My::Class->new; |
107 | Bark->meta->apply($_); |
108 | Sleeper->meta->apply($_); |
109 | } |
110 | is(blessed($obj), blessed($obj2), '... they now share the same anon-class/role thing'); |
111 | } |
112 | |
a28e50e4 |
113 | done_testing; |