Add another MOOSE_TEST_MD option, MooseX
[gitmo/Moose.git] / t / roles / run_time_role_composition.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use Scalar::Util qw(blessed);
9
10
11 =pod
12
13 This test can be used as a basis for the runtime role composition.
14 Apparently it is not as simple as just making an anon class. One of
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 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;
39 isa_ok($obj, 'My::Class');
40
41 my $obj2 = My::Class->new;
42 isa_ok($obj2, 'My::Class');
43
44 {
45     ok(!$obj->can( 'talk' ), "... the role is not composed yet");
46
47     ok(!$obj->does('Bark'), '... we do not do any roles yet');
48
49     Bark->meta->apply($obj);
50
51     ok($obj->does('Bark'), '... we now do the Bark role');
52     ok(!My::Class->does('Bark'), '... the class does not do the Bark role');
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");
59
60     is($obj->talk, 'woof', '... got the right return value for the newly composed method');
61 }
62
63 {
64     ok(!$obj2->does('Sleeper'), '... we do not do any roles yet');
65
66     Sleeper->meta->apply($obj2);
67
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');
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');
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
84     isnt(blessed($obj), blessed($obj2), '... they still don\'t share the same anon-class/role thing');
85
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');
91     is($obj->talk, 'zzz', '... got the right return value for the newly composed method');
92 }
93
94 {
95     ok(!$obj2->does('Bark'), '... we do not do Bark yet');
96
97     Bark->meta->apply($obj2);
98
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');
101 }
102
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
113 done_testing;