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