Commit | Line | Data |
d7c04559 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
bfe41369 |
6 | use Test::More tests => 28; |
d7c04559 |
7 | |
8 | use Scalar::Util qw(blessed); |
9 | |
7ff56534 |
10 | |
d7c04559 |
11 | |
12 | =pod |
13 | |
14 | This test can be used as a basis for the runtime role composition. |
15 | Apparently it is not as simple as just making an anon class. One of |
16 | the problems is the way that anon classes are DESTROY-ed, which is |
17 | not very compatible with how instances are dealt with. |
18 | |
b805c70c |
19 | =cut |
20 | |
d7c04559 |
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; |
d71ba374 |
40 | isa_ok($obj, 'My::Class'); |
41 | |
42 | my $obj2 = My::Class->new; |
43 | isa_ok($obj2, 'My::Class'); |
d7c04559 |
44 | |
45 | { |
d71ba374 |
46 | ok(!$obj->can( 'talk' ), "... the role is not composed yet"); |
d7c04559 |
47 | |
48 | ok(!$obj->does('Bark'), '... we do not do any roles yet'); |
49 | |
50 | Bark->meta->apply($obj); |
51 | |
52 | ok($obj->does('Bark'), '... we now do the Bark role'); |
53 | ok(!My::Class->does('Bark'), '... the class does not do the Bark role'); |
54 | |
55 | isa_ok($obj, 'My::Class'); |
b805c70c |
56 | isnt(blessed($obj), 'My::Class', '... but it is no longer blessed into My::Class'); |
d7c04559 |
57 | |
58 | ok(!My::Class->can('talk'), "... the role is not composed at the class level"); |
59 | ok($obj->can('talk'), "... the role is now composed at the object level"); |
60 | |
61 | is($obj->talk, 'woof', '... got the right return value for the newly composed method'); |
62 | } |
63 | |
64 | { |
d71ba374 |
65 | ok(!$obj2->does('Bark'), '... we do not do any roles yet'); |
66 | |
67 | Bark->meta->apply($obj2); |
68 | |
69 | ok($obj2->does('Bark'), '... we now do the Bark role'); |
70 | is(blessed($obj), blessed($obj2), '... they share the same anon-class/role thing'); |
71 | } |
72 | |
73 | { |
d7c04559 |
74 | is($obj->sleep, 'nite-nite', '... the original method responds as expected'); |
75 | |
b805c70c |
76 | ok(!$obj->does('Sleeper'), '... we do not do the Sleeper role'); |
d7c04559 |
77 | |
78 | Sleeper->meta->apply($obj); |
79 | |
80 | ok($obj->does('Bark'), '... we still do the Bark role'); |
81 | ok($obj->does('Sleeper'), '... we now do the Sleeper role too'); |
82 | |
d71ba374 |
83 | ok(!My::Class->does('Sleeper'), '... the class does not do the Sleeper role'); |
84 | |
85 | isnt(blessed($obj), blessed($obj2), '... they no longer share the same anon-class/role thing'); |
d7c04559 |
86 | |
87 | isa_ok($obj, 'My::Class'); |
88 | |
89 | is(My::Class->sleep, 'nite-nite', '... the original method still responds as expected'); |
90 | |
91 | is($obj->sleep, 'snore', '... got the right return value for the newly composed method'); |
92 | is($obj->talk, 'zzz', '... got the right return value for the newly composed method'); |
93 | } |
d71ba374 |
94 | |
95 | { |
96 | ok(!$obj2->does('Sleeper'), '... we do not do any roles yet'); |
97 | |
98 | Sleeper->meta->apply($obj2); |
99 | |
100 | ok($obj2->does('Sleeper'), '... we now do the Bark role'); |
101 | is(blessed($obj), blessed($obj2), '... they share the same anon-class/role thing again'); |
102 | } |
103 | |
bfe41369 |
104 | SKIP: |
105 | { |
106 | eval 'use Test::Output;'; |
107 | skip 'This test requires Test::Output', 1 |
108 | if $@; |
109 | |
110 | my $obj = My::Class->new; |
111 | |
112 | stderr_is( |
113 | sub { |
114 | for ( 1 .. 200 ) { |
115 | Sleeper->meta->apply($obj); |
116 | } |
117 | }, |
118 | q{}, |
119 | 'No warnings when re-applying a role to an object 200 times' |
120 | ); |
121 | } |