6 use Test::More tests => 45;
12 package HasOwnImmutable;
18 ::stderr_is( sub { eval q[sub make_immutable { return 'foo' }] },
20 'no warning when defining our own make_immutable sub' );
24 is( HasOwnImmutable->make_immutable(), 'foo',
25 'HasOwnImmutable->make_immutable does not get overwritten' );
29 package MooseX::Empty;
32 Moose::Exporter->setup_import_methods( also => 'Moose' );
38 MooseX::Empty->import();
42 ::can_ok( 'WantsMoose', 'has' );
43 ::can_ok( 'WantsMoose', 'with' );
44 ::can_ok( 'WantsMoose', 'foo' );
46 MooseX::Empty->unimport();
50 # Note: it's important that these methods be out of scope _now_,
51 # after unimport was called. We tried a
52 # namespace::clean(0.08)-based solution, but had to abandon it
53 # because it cleans the namespace _later_ (when the file scope
55 ok( ! WantsMoose->can('has'), 'WantsMoose::has() has been cleaned' );
56 ok( ! WantsMoose->can('with'), 'WantsMoose::with() has been cleaned' );
57 can_ok( 'WantsMoose', 'foo' );
59 # This makes sure that Moose->init_meta() happens properly
60 isa_ok( WantsMoose->meta(), 'Moose::Meta::Class' );
61 isa_ok( WantsMoose->new(), 'Moose::Object' );
66 package MooseX::Sugar;
72 return $caller . ' called wrapped1';
75 Moose::Exporter->setup_import_methods(
76 with_caller => ['wrapped1'],
84 MooseX::Sugar->import();
88 ::can_ok( 'WantsSugar', 'has' );
89 ::can_ok( 'WantsSugar', 'with' );
90 ::can_ok( 'WantsSugar', 'wrapped1' );
91 ::can_ok( 'WantsSugar', 'foo' );
92 ::is( wrapped1(), 'WantsSugar called wrapped1',
93 'wrapped1 identifies the caller correctly' );
95 MooseX::Sugar->unimport();
99 ok( ! WantsSugar->can('has'), 'WantsSugar::has() has been cleaned' );
100 ok( ! WantsSugar->can('with'), 'WantsSugar::with() has been cleaned' );
101 ok( ! WantsSugar->can('wrapped1'), 'WantsSugar::wrapped1() has been cleaned' );
102 can_ok( 'WantsSugar', 'foo' );
106 package MooseX::MoreSugar;
112 return $caller . ' called wrapped2';
119 Moose::Exporter->setup_import_methods(
120 with_caller => ['wrapped2'],
122 also => 'MooseX::Sugar',
127 package WantsMoreSugar;
129 MooseX::MoreSugar->import();
133 ::can_ok( 'WantsMoreSugar', 'has' );
134 ::can_ok( 'WantsMoreSugar', 'with' );
135 ::can_ok( 'WantsMoreSugar', 'wrapped1' );
136 ::can_ok( 'WantsMoreSugar', 'wrapped2' );
137 ::can_ok( 'WantsMoreSugar', 'as_is1' );
138 ::can_ok( 'WantsMoreSugar', 'foo' );
139 ::is( wrapped1(), 'WantsMoreSugar called wrapped1',
140 'wrapped1 identifies the caller correctly' );
141 ::is( wrapped2(), 'WantsMoreSugar called wrapped2',
142 'wrapped2 identifies the caller correctly' );
143 ::is( as_is1(), 'as_is1',
144 'as_is1 works as expected' );
146 MooseX::MoreSugar->unimport();
150 ok( ! WantsMoreSugar->can('has'), 'WantsMoreSugar::has() has been cleaned' );
151 ok( ! WantsMoreSugar->can('with'), 'WantsMoreSugar::with() has been cleaned' );
152 ok( ! WantsMoreSugar->can('wrapped1'), 'WantsMoreSugar::wrapped1() has been cleaned' );
153 ok( ! WantsMoreSugar->can('wrapped2'), 'WantsMoreSugar::wrapped2() has been cleaned' );
154 ok( ! WantsMoreSugar->can('as_is1'), 'WantsMoreSugar::as_is1() has been cleaned' );
155 can_ok( 'WantsMoreSugar', 'foo' );
159 package My::Metaclass;
161 BEGIN { extends 'Moose::Meta::Class' }
165 BEGIN { extends 'Moose::Object' }
173 return Moose->init_meta( @_,
174 metaclass => 'My::Metaclass',
175 base_class => 'My::Object',
179 Moose::Exporter->setup_import_methods( also => 'Moose' );
185 HasInitMeta->import();
189 isa_ok( NewMeta->meta(), 'My::Metaclass' );
190 isa_ok( NewMeta->new(), 'My::Object' );
194 package MooseX::CircularAlso;
200 Moose::Exporter->setup_import_methods(
201 also => [ 'Moose', 'MooseX::CircularAlso' ],
204 'a circular reference in also dies with an error'
209 qr/\QCircular reference in also parameter to Moose::Exporter between MooseX::CircularAlso and MooseX::CircularAlso/,
210 'got the expected error from circular reference in also'
215 package MooseX::CircularAlso;
221 Moose::Exporter->setup_import_methods(
222 also => [ 'NoSuchThing' ],
225 'a package which does not use Moose::Exporter in also dies with an error'
230 qr/\QPackage in also (NoSuchThing) does not seem to use Moose::Exporter/,
231 'got the expected error from a reference in also to a package which does not use Moose::Exporter'
236 package MooseX::OverridingSugar;
242 return $caller . ' called has';
245 Moose::Exporter->setup_import_methods(
246 with_caller => ['has'],
252 package WantsOverridingSugar;
254 MooseX::OverridingSugar->import();
256 ::can_ok( 'WantsOverridingSugar', 'has' );
257 ::can_ok( 'WantsOverridingSugar', 'with' );
258 ::is( has('foo'), 'WantsOverridingSugar called has',
259 'has from MooseX::OverridingSugar is called, not has from Moose' );
261 MooseX::OverridingSugar->unimport();
265 ok( ! WantsSugar->can('has'), 'WantsSugar::has() has been cleaned' );
266 ok( ! WantsSugar->can('with'), 'WantsSugar::with() has been cleaned' );