Merge branch 'master' into topic/strict_export_list
[gitmo/Moose.git] / t / 050_metaclasses / 012_moose_exporter.t
CommitLineData
4403da90 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
3d659f7f 6use Test::More;
4403da90 7use Test::Exception;
3d659f7f 8BEGIN {
9 eval "use Test::Output;";
10 plan skip_all => "Test::Output is required for this test" if $@;
addd05f6 11 plan tests => 65;
3d659f7f 12}
b30e7781 13
14
15{
16 package HasOwnImmutable;
17
18 use Moose;
19
20 no Moose;
21
8b23f9f8 22 ::stderr_is( sub { eval q[sub make_immutable { return 'foo' }] },
b30e7781 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}
2c9c8797 31
4403da90 32{
33 package MooseX::Empty;
34
35 use Moose ();
aedcb7d9 36 Moose::Exporter->setup_import_methods( also => 'Moose' );
4403da90 37}
38
39{
40 package WantsMoose;
41
2c9c8797 42 MooseX::Empty->import();
4403da90 43
44 sub foo { 1 }
45
2c9c8797 46 ::can_ok( 'WantsMoose', 'has' );
47 ::can_ok( 'WantsMoose', 'with' );
48 ::can_ok( 'WantsMoose', 'foo' );
4403da90 49
2c9c8797 50 MooseX::Empty->unimport();
4403da90 51}
52
53{
2c9c8797 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).
4403da90 59 ok( ! WantsMoose->can('has'), 'WantsMoose::has() has been cleaned' );
60 ok( ! WantsMoose->can('with'), 'WantsMoose::with() has been cleaned' );
61 can_ok( 'WantsMoose', 'foo' );
42c391b1 62
63 # This makes sure that Moose->init_meta() happens properly
64 isa_ok( WantsMoose->meta(), 'Moose::Meta::Class' );
65 isa_ok( WantsMoose->new(), 'Moose::Object' );
66
4403da90 67}
68
69{
70 package MooseX::Sugar;
71
72 use Moose ();
73
74 sub wrapped1 {
0661fc1a 75 my $meta = shift;
76 return $meta->name . ' called wrapped1';
4403da90 77 }
78
aedcb7d9 79 Moose::Exporter->setup_import_methods(
0661fc1a 80 with_meta => ['wrapped1'],
81 also => 'Moose',
2c9c8797 82 );
4403da90 83}
84
85{
86 package WantsSugar;
87
2c9c8797 88 MooseX::Sugar->import();
4403da90 89
90 sub foo { 1 }
91
2c9c8797 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' );
4403da90 98
2c9c8797 99 MooseX::Sugar->unimport();
4403da90 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 MooseX::MoreSugar;
111
112 use Moose ();
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
aedcb7d9 123 Moose::Exporter->setup_import_methods(
2c9c8797 124 with_caller => ['wrapped2'],
125 as_is => ['as_is1'],
126 also => 'MooseX::Sugar',
127 );
4403da90 128}
129
130{
131 package WantsMoreSugar;
132
2c9c8797 133 MooseX::MoreSugar->import();
4403da90 134
135 sub foo { 1 }
136
2c9c8797 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 MooseX::MoreSugar->unimport();
4403da90 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{
085fba61 163 package My::Metaclass;
164 use Moose;
165 BEGIN { extends 'Moose::Meta::Class' }
166
167 package My::Object;
168 use Moose;
169 BEGIN { extends 'Moose::Object' }
170
171 package HasInitMeta;
172
173 use Moose ();
174
175 sub init_meta {
176 shift;
177 return Moose->init_meta( @_,
178 metaclass => 'My::Metaclass',
179 base_class => 'My::Object',
180 );
181 }
182
aedcb7d9 183 Moose::Exporter->setup_import_methods( also => 'Moose' );
085fba61 184}
185
186{
187 package NewMeta;
188
2c9c8797 189 HasInitMeta->import();
085fba61 190}
191
192{
193 isa_ok( NewMeta->meta(), 'My::Metaclass' );
194 isa_ok( NewMeta->new(), 'My::Object' );
195}
196
197{
4403da90 198 package MooseX::CircularAlso;
199
200 use Moose ();
201
202 ::dies_ok(
203 sub {
aedcb7d9 204 Moose::Exporter->setup_import_methods(
4403da90 205 also => [ 'Moose', 'MooseX::CircularAlso' ],
206 );
207 },
208 'a circular reference in also dies with an error'
209 );
210
211 ::like(
212 $@,
c480e8ae 213 qr/\QCircular reference in also parameter to Moose::Exporter between MooseX::CircularAlso and MooseX::CircularAlso/,
4403da90 214 'got the expected error from circular reference in also'
215 );
216}
217
218{
ba1a3c2f 219 package MooseX::NoAlso;
4403da90 220
221 use Moose ();
222
223 ::dies_ok(
224 sub {
aedcb7d9 225 Moose::Exporter->setup_import_methods(
4403da90 226 also => [ 'NoSuchThing' ],
227 );
228 },
229 'a package which does not use Moose::Exporter in also dies with an error'
230 );
231
232 ::like(
233 $@,
ba1a3c2f 234 qr/\QPackage in also (NoSuchThing) does not seem to use Moose::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 MooseX::NotExporter;
241
242 use Moose ();
243
244 ::dies_ok(
245 sub {
246 Moose::Exporter->setup_import_methods(
247 also => [ 'Moose::Meta::Method' ],
248 );
249 },
250 'a package which does not use Moose::Exporter in also dies with an error'
251 );
252
253 ::like(
254 $@,
255 qr/\QPackage in also (Moose::Meta::Method) does not seem to use Moose::Exporter at /,
4403da90 256 'got the expected error from a reference in also to a package which does not use Moose::Exporter'
257 );
258}
ae8817b6 259
260{
261 package MooseX::OverridingSugar;
262
263 use Moose ();
264
265 sub has {
266 my $caller = shift;
267 return $caller . ' called has';
268 }
269
270 Moose::Exporter->setup_import_methods(
271 with_caller => ['has'],
272 also => 'Moose',
273 );
274}
275
276{
277 package WantsOverridingSugar;
278
279 MooseX::OverridingSugar->import();
280
281 ::can_ok( 'WantsOverridingSugar', 'has' );
282 ::can_ok( 'WantsOverridingSugar', 'with' );
283 ::is( has('foo'), 'WantsOverridingSugar called has',
284 'has from MooseX::OverridingSugar is called, not has from Moose' );
285
286 MooseX::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
e6a5040f 294{
295 package NonExistentExport;
296
297 use Moose ();
298
299 ::stderr_like {
300 Moose::Exporter->setup_import_methods(
301 also => ['Moose'],
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");
addd05f6 315
0661fc1a 316 package AllOptions;
317 use Moose ();
318 use Moose::Exporter;
319
320 Moose::Exporter->setup_import_methods(
321 also => ['Moose'],
322 with_meta => [ 'with_meta1', 'with_meta2' ],
323 with_caller => [ 'with_caller1', 'with_caller2' ],
324 as_is => ['as_is1'],
325 );
326
327 sub with_caller1 {
328 return @_;
329 }
330
331 sub with_caller2 (&) {
332 return @_;
333 }
334
335 sub as_is1 {2}
336
337 sub with_meta1 {
338 return @_;
339 }
340
341 sub with_meta2 (&) {
342 return @_;
343 }
344}
345
346{
347 package UseAllOptions;
348
349 AllOptions->import();
350}
351
352{
353 can_ok( 'UseAllOptions', $_ )
354 for qw( with_meta1 with_meta2 with_caller1 with_caller2 as_is1 );
355
356 {
357 my ( $caller, $arg1 ) = UseAllOptions::with_caller1(42);
358 is( $caller, 'UseAllOptions', 'with_caller wrapped sub gets the right caller' );
359 is( $arg1, 42, 'with_caller wrapped sub returns argument it was passed' );
360 }
361
362 {
363 my ( $meta, $arg1 ) = UseAllOptions::with_meta1(42);
364 isa_ok( $meta, 'Moose::Meta::Class', 'with_meta first argument' );
365 is( $arg1, 42, 'with_meta1 returns argument it was passed' );
366 }
367
368 is(
5a204c51 369 prototype( UseAllOptions->can('with_caller2') ),
370 prototype( AllOptions->can('with_caller2') ),
371 'using correct prototype on with_meta function'
372 );
373
374 is(
0661fc1a 375 prototype( UseAllOptions->can('with_meta2') ),
376 prototype( AllOptions->can('with_meta2') ),
377 'using correct prototype on with_meta function'
378 );
379
380 {
381 package UseAllOptions;
382 AllOptions->unimport();
383 }
384
385 ok( ! UseAllOptions->can($_), "UseAllOptions::$_ has been unimported" )
386 for qw( with_meta1 with_meta2 with_caller1 with_caller2 as_is1 );
e6a5040f 387}