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