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