9 eval "use Test::Output;";
10 plan skip_all => "Test::Output is required for this test" if $@;
15 package HasOwnImmutable;
21 ::stderr_is( sub { eval q[sub make_immutable { return 'foo' }] },
23 'no warning when defining our own make_immutable sub' );
27 is( HasOwnImmutable->make_immutable(), 'foo',
28 'HasOwnImmutable->make_immutable does not get overwritten' );
32 package MooseX::Empty;
35 Moose::Exporter->setup_import_methods( also => 'Moose' );
41 MooseX::Empty->import();
45 ::can_ok( 'WantsMoose', 'has' );
46 ::can_ok( 'WantsMoose', 'with' );
47 ::can_ok( 'WantsMoose', 'foo' );
49 MooseX::Empty->unimport();
53 # Note: it's important that these methods be out of scope _now_,
54 # after unimport was called. We tried a
55 # namespace::clean(0.08)-based solution, but had to abandon it
56 # because it cleans the namespace _later_ (when the file scope
58 ok( ! WantsMoose->can('has'), 'WantsMoose::has() has been cleaned' );
59 ok( ! WantsMoose->can('with'), 'WantsMoose::with() has been cleaned' );
60 can_ok( 'WantsMoose', 'foo' );
62 # This makes sure that Moose->init_meta() happens properly
63 isa_ok( WantsMoose->meta(), 'Moose::Meta::Class' );
64 isa_ok( WantsMoose->new(), 'Moose::Object' );
69 package MooseX::Sugar;
75 return $meta->name . ' called wrapped1';
78 Moose::Exporter->setup_import_methods(
79 with_meta => ['wrapped1'],
87 MooseX::Sugar->import();
91 ::can_ok( 'WantsSugar', 'has' );
92 ::can_ok( 'WantsSugar', 'with' );
93 ::can_ok( 'WantsSugar', 'wrapped1' );
94 ::can_ok( 'WantsSugar', 'foo' );
95 ::is( wrapped1(), 'WantsSugar called wrapped1',
96 'wrapped1 identifies the caller correctly' );
98 MooseX::Sugar->unimport();
102 ok( ! WantsSugar->can('has'), 'WantsSugar::has() has been cleaned' );
103 ok( ! WantsSugar->can('with'), 'WantsSugar::with() has been cleaned' );
104 ok( ! WantsSugar->can('wrapped1'), 'WantsSugar::wrapped1() has been cleaned' );
105 can_ok( 'WantsSugar', 'foo' );
109 package MooseX::MoreSugar;
115 return $caller . ' called wrapped2';
122 Moose::Exporter->setup_import_methods(
123 with_caller => ['wrapped2'],
125 also => 'MooseX::Sugar',
130 package WantsMoreSugar;
132 MooseX::MoreSugar->import();
136 ::can_ok( 'WantsMoreSugar', 'has' );
137 ::can_ok( 'WantsMoreSugar', 'with' );
138 ::can_ok( 'WantsMoreSugar', 'wrapped1' );
139 ::can_ok( 'WantsMoreSugar', 'wrapped2' );
140 ::can_ok( 'WantsMoreSugar', 'as_is1' );
141 ::can_ok( 'WantsMoreSugar', 'foo' );
142 ::is( wrapped1(), 'WantsMoreSugar called wrapped1',
143 'wrapped1 identifies the caller correctly' );
144 ::is( wrapped2(), 'WantsMoreSugar called wrapped2',
145 'wrapped2 identifies the caller correctly' );
146 ::is( as_is1(), 'as_is1',
147 'as_is1 works as expected' );
149 MooseX::MoreSugar->unimport();
153 ok( ! WantsMoreSugar->can('has'), 'WantsMoreSugar::has() has been cleaned' );
154 ok( ! WantsMoreSugar->can('with'), 'WantsMoreSugar::with() has been cleaned' );
155 ok( ! WantsMoreSugar->can('wrapped1'), 'WantsMoreSugar::wrapped1() has been cleaned' );
156 ok( ! WantsMoreSugar->can('wrapped2'), 'WantsMoreSugar::wrapped2() has been cleaned' );
157 ok( ! WantsMoreSugar->can('as_is1'), 'WantsMoreSugar::as_is1() has been cleaned' );
158 can_ok( 'WantsMoreSugar', 'foo' );
162 package My::Metaclass;
164 BEGIN { extends 'Moose::Meta::Class' }
168 BEGIN { extends 'Moose::Object' }
176 return Moose->init_meta( @_,
177 metaclass => 'My::Metaclass',
178 base_class => 'My::Object',
182 Moose::Exporter->setup_import_methods( also => 'Moose' );
188 HasInitMeta->import();
192 isa_ok( NewMeta->meta(), 'My::Metaclass' );
193 isa_ok( NewMeta->new(), 'My::Object' );
197 package MooseX::CircularAlso;
203 Moose::Exporter->setup_import_methods(
204 also => [ 'Moose', 'MooseX::CircularAlso' ],
207 'a circular reference in also dies with an error'
212 qr/\QCircular reference in 'also' parameter to Moose::Exporter between MooseX::CircularAlso and MooseX::CircularAlso/,
213 'got the expected error from circular reference in also'
218 package MooseX::NoAlso;
224 Moose::Exporter->setup_import_methods(
225 also => [ 'NoSuchThing' ],
228 'a package which does not use Moose::Exporter in also dies with an error'
233 qr/\QPackage in also (NoSuchThing) does not seem to use Moose::Exporter (is it loaded?) at /,
234 'got the expected error from a reference in also to a package which is not loaded'
239 package MooseX::NotExporter;
245 Moose::Exporter->setup_import_methods(
246 also => [ 'Moose::Meta::Method' ],
249 'a package which does not use Moose::Exporter in also dies with an error'
254 qr/\QPackage in also (Moose::Meta::Method) does not seem to use Moose::Exporter at /,
255 'got the expected error from a reference in also to a package which does not use Moose::Exporter'
260 package MooseX::OverridingSugar;
266 return $caller . ' called has';
269 Moose::Exporter->setup_import_methods(
270 with_caller => ['has'],
276 package WantsOverridingSugar;
278 MooseX::OverridingSugar->import();
280 ::can_ok( 'WantsOverridingSugar', 'has' );
281 ::can_ok( 'WantsOverridingSugar', 'with' );
282 ::is( has('foo'), 'WantsOverridingSugar called has',
283 'has from MooseX::OverridingSugar is called, not has from Moose' );
285 MooseX::OverridingSugar->unimport();
289 ok( ! WantsSugar->can('has'), 'WantsSugar::has() has been cleaned' );
290 ok( ! WantsSugar->can('with'), 'WantsSugar::with() has been cleaned' );
294 package NonExistentExport;
299 Moose::Exporter->setup_import_methods(
301 with_caller => ['does_not_exist'],
303 } qr/^Trying to export undefined sub NonExistentExport::does_not_exist/,
304 "warns when a non-existent method is requested to be exported";
308 package WantsNonExistentExport;
310 NonExistentExport->import;
312 ::ok(!__PACKAGE__->can('does_not_exist'),
313 "undefined subs do not get exported");
321 Moose::Exporter->setup_import_methods(
323 with_meta => [ 'with_meta1', 'with_meta2' ],
324 with_caller => [ 'with_caller1', 'with_caller2' ],
332 sub with_caller2 (&) {
348 package UseAllOptions;
350 AllOptions->import();
354 can_ok( 'UseAllOptions', $_ )
355 for qw( with_meta1 with_meta2 with_caller1 with_caller2 as_is1 );
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' );
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' );
370 prototype( UseAllOptions->can('with_caller2') ),
371 prototype( AllOptions->can('with_caller2') ),
372 'using correct prototype on with_meta function'
376 prototype( UseAllOptions->can('with_meta2') ),
377 prototype( AllOptions->can('with_meta2') ),
378 'using correct prototype on with_meta function'
383 package UseAllOptions;
384 AllOptions->unimport();
388 ok( ! UseAllOptions->can($_), "UseAllOptions::$_ has been unimported" )
389 for qw( with_meta1 with_meta2 with_caller1 with_caller2 as_is1 );