Import t/050_metaclass from Moose
[gitmo/Mouse.git] / t / 050_metaclasses / failing / 012_moose_exporter.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8 BEGIN {
9     eval "use Test::Output;";
10     plan skip_all => "Test::Output is required for this test" if $@;
11     plan tests => 65;
12 }
13
14
15 {
16     package HasOwnImmutable;
17
18     use Mouse;
19
20     no Mouse;
21
22     ::stderr_is( sub { eval q[sub make_immutable { return 'foo' }] },
23                   '',
24                   'no warning when defining our own make_immutable sub' );
25 }
26
27 {
28     is( HasOwnImmutable->make_immutable(), 'foo',
29         'HasOwnImmutable->make_immutable does not get overwritten' );
30 }
31
32 {
33     package MouseX::Empty;
34
35     use Mouse ();
36     Mouse::Exporter->setup_import_methods( also => 'Mouse' );
37 }
38
39 {
40     package WantsMouse;
41
42     MouseX::Empty->import();
43
44     sub foo { 1 }
45
46     ::can_ok( 'WantsMouse', 'has' );
47     ::can_ok( 'WantsMouse', 'with' );
48     ::can_ok( 'WantsMouse', 'foo' );
49
50     MouseX::Empty->unimport();
51 }
52
53 {
54     # Note: it's important that these methods be out of scope _now_,
55     # after unimport was called. We tried a
56     # namespace::clean(0.08)-based solution, but had to abandon it
57     # because it cleans the namespace _later_ (when the file scope
58     # ends).
59     ok( ! WantsMouse->can('has'),  'WantsMouse::has() has been cleaned' );
60     ok( ! WantsMouse->can('with'), 'WantsMouse::with() has been cleaned' );
61     can_ok( 'WantsMouse', 'foo' );
62
63     # This makes sure that Mouse->init_meta() happens properly
64     isa_ok( WantsMouse->meta(), 'Mouse::Meta::Class' );
65     isa_ok( WantsMouse->new(), 'Mouse::Object' );
66
67 }
68
69 {
70     package MouseX::Sugar;
71
72     use Mouse ();
73
74     sub wrapped1 {
75         my $meta = shift;
76         return $meta->name . ' called wrapped1';
77     }
78
79     Mouse::Exporter->setup_import_methods(
80         with_meta => ['wrapped1'],
81         also      => 'Mouse',
82     );
83 }
84
85 {
86     package WantsSugar;
87
88     MouseX::Sugar->import();
89
90     sub foo { 1 }
91
92     ::can_ok( 'WantsSugar', 'has' );
93     ::can_ok( 'WantsSugar', 'with' );
94     ::can_ok( 'WantsSugar', 'wrapped1' );
95     ::can_ok( 'WantsSugar', 'foo' );
96     ::is( wrapped1(), 'WantsSugar called wrapped1',
97           'wrapped1 identifies the caller correctly' );
98
99     MouseX::Sugar->unimport();
100 }
101
102 {
103     ok( ! WantsSugar->can('has'),  'WantsSugar::has() has been cleaned' );
104     ok( ! WantsSugar->can('with'), 'WantsSugar::with() has been cleaned' );
105     ok( ! WantsSugar->can('wrapped1'), 'WantsSugar::wrapped1() has been cleaned' );
106     can_ok( 'WantsSugar', 'foo' );
107 }
108
109 {
110     package MouseX::MoreSugar;
111
112     use Mouse ();
113
114     sub wrapped2 {
115         my $caller = shift;
116         return $caller . ' called wrapped2';
117     }
118
119     sub as_is1 {
120         return 'as_is1';
121     }
122
123     Mouse::Exporter->setup_import_methods(
124         with_caller => ['wrapped2'],
125         as_is       => ['as_is1'],
126         also        => 'MouseX::Sugar',
127     );
128 }
129
130 {
131     package WantsMoreSugar;
132
133     MouseX::MoreSugar->import();
134
135     sub foo { 1 }
136
137     ::can_ok( 'WantsMoreSugar', 'has' );
138     ::can_ok( 'WantsMoreSugar', 'with' );
139     ::can_ok( 'WantsMoreSugar', 'wrapped1' );
140     ::can_ok( 'WantsMoreSugar', 'wrapped2' );
141     ::can_ok( 'WantsMoreSugar', 'as_is1' );
142     ::can_ok( 'WantsMoreSugar', 'foo' );
143     ::is( wrapped1(), 'WantsMoreSugar called wrapped1',
144           'wrapped1 identifies the caller correctly' );
145     ::is( wrapped2(), 'WantsMoreSugar called wrapped2',
146           'wrapped2 identifies the caller correctly' );
147     ::is( as_is1(), 'as_is1',
148           'as_is1 works as expected' );
149
150     MouseX::MoreSugar->unimport();
151 }
152
153 {
154     ok( ! WantsMoreSugar->can('has'),  'WantsMoreSugar::has() has been cleaned' );
155     ok( ! WantsMoreSugar->can('with'), 'WantsMoreSugar::with() has been cleaned' );
156     ok( ! WantsMoreSugar->can('wrapped1'), 'WantsMoreSugar::wrapped1() has been cleaned' );
157     ok( ! WantsMoreSugar->can('wrapped2'), 'WantsMoreSugar::wrapped2() has been cleaned' );
158     ok( ! WantsMoreSugar->can('as_is1'), 'WantsMoreSugar::as_is1() has been cleaned' );
159     can_ok( 'WantsMoreSugar', 'foo' );
160 }
161
162 {
163     package My::Metaclass;
164     use Mouse;
165     BEGIN { extends 'Mouse::Meta::Class' }
166
167     package My::Object;
168     use Mouse;
169     BEGIN { extends 'Mouse::Object' }
170
171     package HasInitMeta;
172
173     use Mouse ();
174
175     sub init_meta {
176         shift;
177         return Mouse->init_meta( @_,
178                                  metaclass  => 'My::Metaclass',
179                                  base_class => 'My::Object',
180                                );
181     }
182
183     Mouse::Exporter->setup_import_methods( also => 'Mouse' );
184 }
185
186 {
187     package NewMeta;
188
189     HasInitMeta->import();
190 }
191
192 {
193     isa_ok( NewMeta->meta(), 'My::Metaclass' );
194     isa_ok( NewMeta->new(), 'My::Object' );
195 }
196
197 {
198     package MouseX::CircularAlso;
199
200     use Mouse ();
201
202     ::dies_ok(
203         sub {
204             Mouse::Exporter->setup_import_methods(
205                 also => [ 'Mouse', 'MouseX::CircularAlso' ],
206             );
207         },
208         'a circular reference in also dies with an error'
209     );
210
211     ::like(
212         $@,
213         qr/\QCircular reference in 'also' parameter to Mouse::Exporter between MouseX::CircularAlso and MouseX::CircularAlso/,
214         'got the expected error from circular reference in also'
215     );
216 }
217
218 {
219     package MouseX::NoAlso;
220
221     use Mouse ();
222
223     ::dies_ok(
224         sub {
225             Mouse::Exporter->setup_import_methods(
226                 also => [ 'NoSuchThing' ],
227             );
228         },
229         'a package which does not use Mouse::Exporter in also dies with an error'
230     );
231
232     ::like(
233         $@,
234         qr/\QPackage in also (NoSuchThing) does not seem to use Mouse::Exporter (is it loaded?) at /,
235         'got the expected error from a reference in also to a package which is not loaded'
236     );
237 }
238
239 {
240     package MouseX::NotExporter;
241
242     use Mouse ();
243
244     ::dies_ok(
245         sub {
246             Mouse::Exporter->setup_import_methods(
247                 also => [ 'Mouse::Meta::Method' ],
248             );
249         },
250         'a package which does not use Mouse::Exporter in also dies with an error'
251     );
252
253     ::like(
254         $@,
255         qr/\QPackage in also (Mouse::Meta::Method) does not seem to use Mouse::Exporter at /,
256         'got the expected error from a reference in also to a package which does not use Mouse::Exporter'
257     );
258 }
259
260 {
261     package MouseX::OverridingSugar;
262
263     use Mouse ();
264
265     sub has {
266         my $caller = shift;
267         return $caller . ' called has';
268     }
269
270     Mouse::Exporter->setup_import_methods(
271         with_caller => ['has'],
272         also        => 'Mouse',
273     );
274 }
275
276 {
277     package WantsOverridingSugar;
278
279     MouseX::OverridingSugar->import();
280
281     ::can_ok( 'WantsOverridingSugar', 'has' );
282     ::can_ok( 'WantsOverridingSugar', 'with' );
283     ::is( has('foo'), 'WantsOverridingSugar called has',
284           'has from MouseX::OverridingSugar is called, not has from Mouse' );
285
286     MouseX::OverridingSugar->unimport();
287 }
288
289 {
290     ok( ! WantsSugar->can('has'),  'WantsSugar::has() has been cleaned' );
291     ok( ! WantsSugar->can('with'), 'WantsSugar::with() has been cleaned' );
292 }
293
294 {
295     package NonExistentExport;
296
297     use Mouse ();
298
299     ::stderr_like {
300         Mouse::Exporter->setup_import_methods(
301             also => ['Mouse'],
302             with_caller => ['does_not_exist'],
303         );
304     } qr/^Trying to export undefined sub NonExistentExport::does_not_exist/,
305       "warns when a non-existent method is requested to be exported";
306 }
307
308 {
309     package WantsNonExistentExport;
310
311     NonExistentExport->import;
312
313     ::ok(!__PACKAGE__->can('does_not_exist'),
314          "undefined subs do not get exported");
315 }
316
317 {
318     package AllOptions;
319     use Mouse ();
320     use Mouse::Exporter;
321
322     Mouse::Exporter->setup_import_methods(
323         also        => ['Mouse'],
324         with_meta   => [ 'with_meta1', 'with_meta2' ],
325         with_caller => [ 'with_caller1', 'with_caller2' ],
326         as_is       => ['as_is1'],
327     );
328
329     sub with_caller1 {
330         return @_;
331     }
332
333     sub with_caller2 (&) {
334         return @_;
335     }
336
337     sub as_is1 {2}
338
339     sub with_meta1 {
340         return @_;
341     }
342
343     sub with_meta2 (&) {
344         return @_;
345     }
346 }
347
348 {
349     package UseAllOptions;
350
351     AllOptions->import();
352 }
353
354 {
355     can_ok( 'UseAllOptions', $_ )
356         for qw( with_meta1 with_meta2 with_caller1 with_caller2 as_is1 );
357
358     {
359         my ( $caller, $arg1 ) = UseAllOptions::with_caller1(42);
360         is( $caller, 'UseAllOptions', 'with_caller wrapped sub gets the right caller' );
361         is( $arg1, 42, 'with_caller wrapped sub returns argument it was passed' );
362     }
363
364     {
365         my ( $meta, $arg1 ) = UseAllOptions::with_meta1(42);
366         isa_ok( $meta, 'Mouse::Meta::Class', 'with_meta first argument' );
367         is( $arg1, 42, 'with_meta1 returns argument it was passed' );
368     }
369
370     is(
371         prototype( UseAllOptions->can('with_caller2') ),
372         prototype( AllOptions->can('with_caller2') ),
373         'using correct prototype on with_meta function'
374     );
375
376     is(
377         prototype( UseAllOptions->can('with_meta2') ),
378         prototype( AllOptions->can('with_meta2') ),
379         'using correct prototype on with_meta function'
380     );
381 }
382
383 {
384     package UseAllOptions;
385     AllOptions->unimport();
386 }
387
388 {
389     ok( ! UseAllOptions->can($_), "UseAllOptions::$_ has been unimported" )
390         for qw( with_meta1 with_meta2 with_caller1 with_caller2 as_is1 );
391 }