10 'Test::Output' => '0.01', # skip all if not installed
14 package HasOwnImmutable;
20 ::stderr_is( sub { eval q[sub make_immutable { return 'foo' }] },
22 'no warning when defining our own make_immutable sub' );
26 is( HasOwnImmutable->make_immutable(), 'foo',
27 'HasOwnImmutable->make_immutable does not get overwritten' );
31 package MooseX::Empty;
34 Moose::Exporter->setup_import_methods( also => 'Moose' );
40 MooseX::Empty->import();
44 ::can_ok( 'WantsMoose', 'has' );
45 ::can_ok( 'WantsMoose', 'with' );
46 ::can_ok( 'WantsMoose', 'foo' );
48 MooseX::Empty->unimport();
52 # Note: it's important that these methods be out of scope _now_,
53 # after unimport was called. We tried a
54 # namespace::clean(0.08)-based solution, but had to abandon it
55 # because it cleans the namespace _later_ (when the file scope
57 ok( ! WantsMoose->can('has'), 'WantsMoose::has() has been cleaned' );
58 ok( ! WantsMoose->can('with'), 'WantsMoose::with() has been cleaned' );
59 can_ok( 'WantsMoose', 'foo' );
61 # This makes sure that Moose->init_meta() happens properly
62 isa_ok( WantsMoose->meta(), 'Moose::Meta::Class' );
63 isa_ok( WantsMoose->new(), 'Moose::Object' );
68 package MooseX::Sugar;
74 return $meta->name . ' called wrapped1';
77 Moose::Exporter->setup_import_methods(
78 with_meta => ['wrapped1'],
86 MooseX::Sugar->import();
90 ::can_ok( 'WantsSugar', 'has' );
91 ::can_ok( 'WantsSugar', 'with' );
92 ::can_ok( 'WantsSugar', 'wrapped1' );
93 ::can_ok( 'WantsSugar', 'foo' );
94 ::is( wrapped1(), 'WantsSugar called wrapped1',
95 'wrapped1 identifies the caller correctly' );
97 MooseX::Sugar->unimport();
101 ok( ! WantsSugar->can('has'), 'WantsSugar::has() has been cleaned' );
102 ok( ! WantsSugar->can('with'), 'WantsSugar::with() has been cleaned' );
103 ok( ! WantsSugar->can('wrapped1'), 'WantsSugar::wrapped1() has been cleaned' );
104 can_ok( 'WantsSugar', 'foo' );
108 package MooseX::MoreSugar;
113 my $caller = shift->name;
114 return $caller . ' called wrapped2';
121 Moose::Exporter->setup_import_methods(
122 with_meta => ['wrapped2'],
124 also => 'MooseX::Sugar',
129 package WantsMoreSugar;
131 MooseX::MoreSugar->import();
135 ::can_ok( 'WantsMoreSugar', 'has' );
136 ::can_ok( 'WantsMoreSugar', 'with' );
137 ::can_ok( 'WantsMoreSugar', 'wrapped1' );
138 ::can_ok( 'WantsMoreSugar', 'wrapped2' );
139 ::can_ok( 'WantsMoreSugar', 'as_is1' );
140 ::can_ok( 'WantsMoreSugar', 'foo' );
141 ::is( wrapped1(), 'WantsMoreSugar called wrapped1',
142 'wrapped1 identifies the caller correctly' );
143 ::is( wrapped2(), 'WantsMoreSugar called wrapped2',
144 'wrapped2 identifies the caller correctly' );
145 ::is( as_is1(), 'as_is1',
146 'as_is1 works as expected' );
148 MooseX::MoreSugar->unimport();
152 ok( ! WantsMoreSugar->can('has'), 'WantsMoreSugar::has() has been cleaned' );
153 ok( ! WantsMoreSugar->can('with'), 'WantsMoreSugar::with() has been cleaned' );
154 ok( ! WantsMoreSugar->can('wrapped1'), 'WantsMoreSugar::wrapped1() has been cleaned' );
155 ok( ! WantsMoreSugar->can('wrapped2'), 'WantsMoreSugar::wrapped2() has been cleaned' );
156 ok( ! WantsMoreSugar->can('as_is1'), 'WantsMoreSugar::as_is1() has been cleaned' );
157 can_ok( 'WantsMoreSugar', 'foo' );
161 package My::Metaclass;
163 BEGIN { extends 'Moose::Meta::Class' }
167 BEGIN { extends 'Moose::Object' }
175 return Moose->init_meta( @_,
176 metaclass => 'My::Metaclass',
177 base_class => 'My::Object',
181 Moose::Exporter->setup_import_methods( also => 'Moose' );
187 HasInitMeta->import();
191 isa_ok( NewMeta->meta(), 'My::Metaclass' );
192 isa_ok( NewMeta->new(), 'My::Object' );
196 package MooseX::CircularAlso;
201 ::exception{ Moose::Exporter->setup_import_methods(
202 also => [ 'Moose', 'MooseX::CircularAlso' ],
205 qr/\QCircular reference in 'also' parameter to Moose::Exporter between MooseX::CircularAlso and MooseX::CircularAlso/,
206 'a circular reference in also dies with an error'
211 package MooseX::NoAlso;
216 ::exception{ Moose::Exporter->setup_import_methods(
217 also => ['NoSuchThing'],
220 qr/\QPackage in also (NoSuchThing) does not seem to use Moose::Exporter (is it loaded?) at /,
221 'a package which does not use Moose::Exporter in also dies with an error'
226 package MooseX::NotExporter;
231 ::exception{ Moose::Exporter->setup_import_methods(
232 also => ['Moose::Meta::Method'],
235 qr/\QPackage in also (Moose::Meta::Method) does not seem to use Moose::Exporter at /,
236 'a package which does not use Moose::Exporter in also dies with an error'
241 package MooseX::OverridingSugar;
246 my $caller = shift->name;
247 return $caller . ' called has';
250 Moose::Exporter->setup_import_methods(
251 with_meta => ['has'],
257 package WantsOverridingSugar;
259 MooseX::OverridingSugar->import();
261 ::can_ok( 'WantsOverridingSugar', 'has' );
262 ::can_ok( 'WantsOverridingSugar', 'with' );
263 ::is( has('foo'), 'WantsOverridingSugar called has',
264 'has from MooseX::OverridingSugar is called, not has from Moose' );
266 MooseX::OverridingSugar->unimport();
270 ok( ! WantsOverridingSugar->can('has'), 'WantsSugar::has() has been cleaned' );
271 ok( ! WantsOverridingSugar->can('with'), 'WantsSugar::with() has been cleaned' );
275 package MooseX::OverridingSugar::PassThru;
278 my $caller = shift->name;
279 return $caller . ' called with';
282 Moose::Exporter->setup_import_methods(
283 with_meta => ['with'],
284 also => 'MooseX::OverridingSugar',
290 package WantsOverridingSugar::PassThru;
292 MooseX::OverridingSugar::PassThru->import();
294 ::can_ok( 'WantsOverridingSugar::PassThru', 'has' );
295 ::can_ok( 'WantsOverridingSugar::PassThru', 'with' );
298 'WantsOverridingSugar::PassThru called has',
299 'has from MooseX::OverridingSugar is called, not has from Moose'
304 'WantsOverridingSugar::PassThru called with',
305 'with from MooseX::OverridingSugar::PassThru is called, not has from Moose'
309 MooseX::OverridingSugar::PassThru->unimport();
313 ok( ! WantsOverridingSugar::PassThru->can('has'), 'WantsOverridingSugar::PassThru::has() has been cleaned' );
314 ok( ! WantsOverridingSugar::PassThru->can('with'), 'WantsOverridingSugar::PassThru::with() has been cleaned' );
319 package NonExistentExport;
324 Moose::Exporter->setup_import_methods(
326 with_meta => ['does_not_exist'],
328 } qr/^Trying to export undefined sub NonExistentExport::does_not_exist/,
329 "warns when a non-existent method is requested to be exported";
333 package WantsNonExistentExport;
335 NonExistentExport->import;
337 ::ok(!__PACKAGE__->can('does_not_exist'),
338 "undefined subs do not get exported");
344 use Moose::Deprecated -api_version => '0.88';
347 Moose::Exporter->setup_import_methods(
349 with_meta => [ 'with_meta1', 'with_meta2' ],
350 with_caller => [ 'with_caller1', 'with_caller2' ],
358 sub with_caller2 (&) {
374 package UseAllOptions;
376 AllOptions->import();
380 can_ok( 'UseAllOptions', $_ )
381 for qw( with_meta1 with_meta2 with_caller1 with_caller2 as_is1 );
384 my ( $caller, $arg1 ) = UseAllOptions::with_caller1(42);
385 is( $caller, 'UseAllOptions', 'with_caller wrapped sub gets the right caller' );
386 is( $arg1, 42, 'with_caller wrapped sub returns argument it was passed' );
390 my ( $meta, $arg1 ) = UseAllOptions::with_meta1(42);
391 isa_ok( $meta, 'Moose::Meta::Class', 'with_meta first argument' );
392 is( $arg1, 42, 'with_meta1 returns argument it was passed' );
396 prototype( UseAllOptions->can('with_caller2') ),
397 prototype( AllOptions->can('with_caller2') ),
398 'using correct prototype on with_meta function'
402 prototype( UseAllOptions->can('with_meta2') ),
403 prototype( AllOptions->can('with_meta2') ),
404 'using correct prototype on with_meta function'
409 package UseAllOptions;
410 AllOptions->unimport();
414 ok( ! UseAllOptions->can($_), "UseAllOptions::$_ has been unimported" )
415 for qw( with_meta1 with_meta2 with_caller1 with_caller2 as_is1 );
419 package InitMetaError;
422 Moose::Exporter->setup_import_methods(also => ['Moose']);
426 Moose->init_meta(%options, metaclass => 'Not::Loaded');
431 package InitMetaError::Role;
434 Moose::Exporter->setup_import_methods(also => ['Moose::Role']);
438 Moose::Role->init_meta(%options, metaclass => 'Not::Loaded');
443 package WantsInvalidMetaclass;
445 ::exception { InitMetaError->import },
446 qr/The Metaclass Not::Loaded must be loaded\. \(Perhaps you forgot to 'use Not::Loaded'\?\)/,
447 "error when wanting a nonexistent metaclass"
452 package WantsInvalidMetaclass::Role;
454 ::exception { InitMetaError::Role->import },
455 qr/The Metaclass Not::Loaded must be loaded\. \(Perhaps you forgot to 'use Not::Loaded'\?\)/,
456 "error when wanting a nonexistent metaclass"