08011494e1c0e496ac51b96ba92630da631d519b
[gitmo/Mouse.git] / t-failing / 050_metaclasses / 030_metarole_combination.t
1 use strict;
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;
5 use warnings;
6 use Test::More;
7 $TODO = q{Mouse is not yet completed};
8
9 our @applications;
10
11 {
12     package CustomApplication;
13     use Mouse::Role;
14
15     after apply_methods => sub {
16         my ( $self, $role, $other ) = @_;
17         $self->apply_custom( $role, $other );
18     };
19
20     sub apply_custom {
21         shift;
22         push @applications, [@_];
23     }
24 }
25
26 {
27     package CustomApplication::ToClass;
28     use Mouse::Role;
29
30     with 'CustomApplication';
31 }
32
33 {
34     package CustomApplication::ToRole;
35     use Mouse::Role;
36
37     with 'CustomApplication';
38 }
39
40 {
41     package CustomApplication::ToInstance;
42     use Mouse::Role;
43
44     with 'CustomApplication';
45 }
46
47 {
48     package CustomApplication::Composite;
49     use Mouse::Role;
50
51     with 'CustomApplication';
52
53     around apply_custom => sub {
54         my ( $next, $self, $composite, $other ) = @_;
55         for my $role ( @{ $composite->get_roles } ) {
56             $self->$next( $role, $other );
57         }
58     };
59 }
60
61 {
62     package CustomApplication::Composite::ToClass;
63     use Mouse::Role;
64
65     with 'CustomApplication::Composite';
66 }
67
68 {
69     package CustomApplication::Composite::ToRole;
70     use Mouse::Role;
71
72     with 'CustomApplication::Composite';
73 }
74
75 {
76     package CustomApplication::Composite::ToInstance;
77     use Mouse::Role;
78
79     with 'CustomApplication::Composite';
80 }
81
82 {
83     package Role::Composite;
84     use Mouse::Role;
85
86     around apply_params => sub {
87         my ( $next, $self, @args ) = @_;
88         return Mouse::Util::MetaRole::apply_metaroles(
89             for            => $self->$next(@args),
90             role_metaroles => {
91                 application_to_class =>
92                     ['CustomApplication::Composite::ToClass'],
93                 application_to_role =>
94                     ['CustomApplication::Composite::ToRole'],
95                 application_to_instance =>
96                     ['CustomApplication::Composite::ToInstance'],
97             },
98         );
99     };
100 }
101
102 {
103     package Role::WithCustomApplication;
104     use Mouse::Role;
105
106     around composition_class_roles => sub {
107         my ($orig, $self) = @_;
108         return $self->$orig, 'Role::Composite';
109     };
110 }
111
112 {
113     package CustomRole;
114     Mouse::Exporter->setup_import_methods(
115         also => 'Mouse::Role',
116     );
117
118     sub init_meta {
119         my ( $self, %options ) = @_;
120         return Mouse::Util::MetaRole::apply_metaroles(
121             for            => Mouse::Role->init_meta(%options),
122             role_metaroles => {
123                 role => ['Role::WithCustomApplication'],
124                 application_to_class =>
125                     ['CustomApplication::ToClass'],
126                 application_to_role => ['CustomApplication::ToRole'],
127                 application_to_instance =>
128                     ['CustomApplication::ToInstance'],
129             },
130         );
131     }
132 }
133
134 {
135     package My::Role::Normal;
136     use Mouse::Role;
137 }
138
139 {
140     package My::Role::Special;
141     CustomRole->import;
142 }
143
144 ok( My::Role::Normal->meta->isa('Mouse::Meta::Role'), "sanity check" );
145 ok( My::Role::Special->meta->isa('Mouse::Meta::Role'),
146     "using custom application roles does not change the role metaobject's class"
147 );
148 ok( My::Role::Special->meta->meta->does_role('Role::WithCustomApplication'),
149     "the role's metaobject has custom applications" );
150 is_deeply( [My::Role::Special->meta->composition_class_roles],
151     ['Role::Composite'],
152     "the role knows about the specified composition class" );
153
154 {
155     package Foo;
156     use Mouse;
157
158     local @applications;
159     with 'My::Role::Special';
160
161     ::is( @applications, 1, 'one role application' );
162     ::is( $applications[0]->[0]->name, 'My::Role::Special',
163         "the application's first role was My::Role::Special'" );
164     ::is( $applications[0]->[1]->name, 'Foo',
165         "the application provided an additional role" );
166 }
167
168 {
169     package Bar;
170     use Mouse::Role;
171
172     local @applications;
173     with 'My::Role::Special';
174
175     ::is( @applications,               1 );
176     ::is( $applications[0]->[0]->name, 'My::Role::Special' );
177     ::is( $applications[0]->[1]->name, 'Bar' );
178 }
179
180 {
181     package Baz;
182     use Mouse;
183
184     my $i = Baz->new;
185     local @applications;
186     My::Role::Special->meta->apply($i);
187
188     ::is( @applications,               1 );
189     ::is( $applications[0]->[0]->name, 'My::Role::Special' );
190     ::ok( $applications[0]->[1]->is_anon_class );
191     ::ok( $applications[0]->[1]->name->isa('Baz') );
192 }
193
194 {
195     package Corge;
196     use Mouse;
197
198     local @applications;
199     with 'My::Role::Normal', 'My::Role::Special';
200
201     ::is( @applications,               2 );
202     ::is( $applications[0]->[0]->name, 'My::Role::Normal' );
203     ::is( $applications[0]->[1]->name, 'Corge' );
204     ::is( $applications[1]->[0]->name, 'My::Role::Special' );
205     ::is( $applications[1]->[1]->name, 'Corge' );
206 }
207
208 {
209     package Thud;
210     use Mouse::Role;
211
212     local @applications;
213     with 'My::Role::Normal', 'My::Role::Special';
214
215     ::is( @applications,               2 );
216     ::is( $applications[0]->[0]->name, 'My::Role::Normal' );
217     ::is( $applications[0]->[1]->name, 'Thud' );
218     ::is( $applications[1]->[0]->name, 'My::Role::Special' );
219     ::is( $applications[1]->[1]->name, 'Thud' );
220 }
221
222 {
223     package Garply;
224     use Mouse;
225
226     my $i = Garply->new;
227     local @applications;
228     Mouse::Meta::Role->combine(
229         [ 'My::Role::Normal'  => undef ],
230         [ 'My::Role::Special' => undef ],
231     )->apply($i);
232
233     ::is( @applications,               2 );
234     ::is( $applications[0]->[0]->name, 'My::Role::Normal' );
235     ::ok( $applications[0]->[1]->is_anon_class );
236     ::ok( $applications[0]->[1]->name->isa('Garply') );
237     ::is( $applications[1]->[0]->name, 'My::Role::Special' );
238     ::ok( $applications[1]->[1]->is_anon_class );
239     ::ok( $applications[1]->[1]->name->isa('Garply') );
240 }
241
242 done_testing;