Regenerate test files
[gitmo/Mouse.git] / t / 050_metaclasses / 012_moose_exporter.t
CommitLineData
41888e7d 1#!/usr/bin/perl
fde8e43f 2# This is automatically generated by author/import-moose-test.pl.
3# DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4use t::lib::MooseCompat;
41888e7d 5
6use strict;
7use warnings;
8
9use Test::More;
10use Test::Exception;
41888e7d 11
fde8e43f 12use Test::Requires {
13 'Test::Output' => '0.01', # skip all if not installed
14};
41888e7d 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{
fde8e43f 34 package MooseX::Empty;
41888e7d 35
36 use Mouse ();
37 Mouse::Exporter->setup_import_methods( also => 'Mouse' );
38}
39
40{
fde8e43f 41 package WantsMoose;
41888e7d 42
fde8e43f 43 MooseX::Empty->import();
41888e7d 44
45 sub foo { 1 }
46
fde8e43f 47 ::can_ok( 'WantsMoose', 'has' );
48 ::can_ok( 'WantsMoose', 'with' );
49 ::can_ok( 'WantsMoose', 'foo' );
41888e7d 50
fde8e43f 51 MooseX::Empty->unimport();
41888e7d 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).
fde8e43f 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' );
41888e7d 63
64 # This makes sure that Mouse->init_meta() happens properly
fde8e43f 65 isa_ok( WantsMoose->meta(), 'Mouse::Meta::Class' );
66 isa_ok( WantsMoose->new(), 'Mouse::Object' );
41888e7d 67
68}
69
70{
fde8e43f 71 package MooseX::Sugar;
41888e7d 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
fde8e43f 89 MooseX::Sugar->import();
41888e7d 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
fde8e43f 100 MooseX::Sugar->unimport();
41888e7d 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{
fde8e43f 111 package MooseX::MoreSugar;
41888e7d 112
113 use Mouse ();
114
115 sub wrapped2 {
fde8e43f 116 my $caller = shift->name;
41888e7d 117 return $caller . ' called wrapped2';
118 }
119
120 sub as_is1 {
121 return 'as_is1';
122 }
123
124 Mouse::Exporter->setup_import_methods(
fde8e43f 125 with_meta => ['wrapped2'],
126 as_is => ['as_is1'],
127 also => 'MooseX::Sugar',
41888e7d 128 );
129}
130
131{
132 package WantsMoreSugar;
133
fde8e43f 134 MooseX::MoreSugar->import();
41888e7d 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
fde8e43f 151 MooseX::MoreSugar->unimport();
41888e7d 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{
fde8e43f 199 package MooseX::CircularAlso;
41888e7d 200
201 use Mouse ();
202
203 ::dies_ok(
204 sub {
205 Mouse::Exporter->setup_import_methods(
fde8e43f 206 also => [ 'Mouse', 'MooseX::CircularAlso' ],
41888e7d 207 );
208 },
209 'a circular reference in also dies with an error'
210 );
211
212 ::like(
213 $@,
fde8e43f 214 qr/\QCircular reference in 'also' parameter to Mouse::Exporter between MooseX::CircularAlso and MooseX::CircularAlso/,
41888e7d 215 'got the expected error from circular reference in also'
216 );
217}
218
219{
fde8e43f 220 package MooseX::NoAlso;
41888e7d 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{
fde8e43f 241 package MooseX::NotExporter;
41888e7d 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{
fde8e43f 262 package MooseX::OverridingSugar;
41888e7d 263
264 use Mouse ();
265
266 sub has {
fde8e43f 267 my $caller = shift->name;
41888e7d 268 return $caller . ' called has';
269 }
270
271 Mouse::Exporter->setup_import_methods(
fde8e43f 272 with_meta => ['has'],
273 also => 'Mouse',
41888e7d 274 );
275}
276
277{
278 package WantsOverridingSugar;
279
fde8e43f 280 MooseX::OverridingSugar->import();
41888e7d 281
282 ::can_ok( 'WantsOverridingSugar', 'has' );
283 ::can_ok( 'WantsOverridingSugar', 'with' );
284 ::is( has('foo'), 'WantsOverridingSugar called has',
fde8e43f 285 'has from MooseX::OverridingSugar is called, not has from Mouse' );
41888e7d 286
fde8e43f 287 MooseX::OverridingSugar->unimport();
41888e7d 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'],
fde8e43f 303 with_meta => ['does_not_exist'],
41888e7d 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 ();
fde8e43f 321 use Mouse::Deprecated -api_version => '0.88';
41888e7d 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}
fde8e43f 394
395done_testing;