9 eval "use Test::Output;";
10 plan skip_all => "Test::Output is required for this test" if $@;
16 package HasOwnImmutable;
22 ::stderr_is( sub { eval q[sub make_immutable { return 'foo' }] },
24 'no warning when defining our own make_immutable sub' );
28 is( HasOwnImmutable->make_immutable(), 'foo',
29 'HasOwnImmutable->make_immutable does not get overwritten' );
33 package MooseX::Empty;
36 Moose::Exporter->setup_import_methods( also => 'Moose' );
42 MooseX::Empty->import();
46 ::can_ok( 'WantsMoose', 'has' );
47 ::can_ok( 'WantsMoose', 'with' );
48 ::can_ok( 'WantsMoose', 'foo' );
50 MooseX::Empty->unimport();
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
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' );
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' );
70 package MooseX::Sugar;
76 return $caller . ' called wrapped1';
79 Moose::Exporter->setup_import_methods(
80 with_caller => ['wrapped1'],
88 MooseX::Sugar->import();
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' );
99 MooseX::Sugar->unimport();
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' );
110 package MooseX::MoreSugar;
116 return $caller . ' called wrapped2';
123 Moose::Exporter->setup_import_methods(
124 with_caller => ['wrapped2'],
126 also => 'MooseX::Sugar',
131 package WantsMoreSugar;
133 MooseX::MoreSugar->import();
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' );
150 MooseX::MoreSugar->unimport();
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' );
163 package My::Metaclass;
165 BEGIN { extends 'Moose::Meta::Class' }
169 BEGIN { extends 'Moose::Object' }
177 return Moose->init_meta( @_,
178 metaclass => 'My::Metaclass',
179 base_class => 'My::Object',
183 Moose::Exporter->setup_import_methods( also => 'Moose' );
189 HasInitMeta->import();
193 isa_ok( NewMeta->meta(), 'My::Metaclass' );
194 isa_ok( NewMeta->new(), 'My::Object' );
198 package MooseX::CircularAlso;
204 Moose::Exporter->setup_import_methods(
205 also => [ 'Moose', 'MooseX::CircularAlso' ],
208 'a circular reference in also dies with an error'
213 qr/\QCircular reference in also parameter to Moose::Exporter between MooseX::CircularAlso and MooseX::CircularAlso/,
214 'got the expected error from circular reference in also'
219 package MooseX::NoAlso;
225 Moose::Exporter->setup_import_methods(
226 also => [ 'NoSuchThing' ],
229 'a package which does not use Moose::Exporter in also dies with an error'
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'
240 package MooseX::NotExporter;
246 Moose::Exporter->setup_import_methods(
247 also => [ 'Moose::Meta::Method' ],
250 'a package which does not use Moose::Exporter in also dies with an error'
255 qr/\QPackage in also (Moose::Meta::Method) does not seem to use Moose::Exporter at /,
256 'got the expected error from a reference in also to a package which does not use Moose::Exporter'
261 package MooseX::OverridingSugar;
267 return $caller . ' called has';
270 Moose::Exporter->setup_import_methods(
271 with_caller => ['has'],
277 package WantsOverridingSugar;
279 MooseX::OverridingSugar->import();
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' );
286 MooseX::OverridingSugar->unimport();
290 ok( ! WantsSugar->can('has'), 'WantsSugar::has() has been cleaned' );
291 ok( ! WantsSugar->can('with'), 'WantsSugar::with() has been cleaned' );
295 package NonExistentExport;
300 Moose::Exporter->setup_import_methods(
302 with_caller => ['does_not_exist'],
304 } qr/^Trying to export undefined sub NonExistentExport::does_not_exist/,
305 "warns when a non-existent method is requested to be exported";
309 package WantsNonExistentExport;
311 NonExistentExport->import;
313 ::ok(!__PACKAGE__->can('does_not_exist'),
314 "undefined subs do not get exported");