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