Commit | Line | Data |
4403da90 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
3d659f7f |
6 | use Test::More; |
4403da90 |
7 | use Test::Exception; |
3d659f7f |
8 | BEGIN { |
9 | eval "use Test::Output;"; |
10 | plan skip_all => "Test::Output is required for this test" if $@; |
3d659f7f |
11 | } |
b30e7781 |
12 | |
13 | |
14 | { |
15 | package HasOwnImmutable; |
16 | |
17 | use Moose; |
18 | |
19 | no Moose; |
20 | |
8b23f9f8 |
21 | ::stderr_is( sub { eval q[sub make_immutable { return 'foo' }] }, |
b30e7781 |
22 | '', |
23 | 'no warning when defining our own make_immutable sub' ); |
24 | } |
25 | |
26 | { |
27 | is( HasOwnImmutable->make_immutable(), 'foo', |
28 | 'HasOwnImmutable->make_immutable does not get overwritten' ); |
29 | } |
2c9c8797 |
30 | |
4403da90 |
31 | { |
32 | package MooseX::Empty; |
33 | |
34 | use Moose (); |
aedcb7d9 |
35 | Moose::Exporter->setup_import_methods( also => 'Moose' ); |
4403da90 |
36 | } |
37 | |
38 | { |
39 | package WantsMoose; |
40 | |
2c9c8797 |
41 | MooseX::Empty->import(); |
4403da90 |
42 | |
43 | sub foo { 1 } |
44 | |
2c9c8797 |
45 | ::can_ok( 'WantsMoose', 'has' ); |
46 | ::can_ok( 'WantsMoose', 'with' ); |
47 | ::can_ok( 'WantsMoose', 'foo' ); |
4403da90 |
48 | |
2c9c8797 |
49 | MooseX::Empty->unimport(); |
4403da90 |
50 | } |
51 | |
52 | { |
2c9c8797 |
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 |
57 | # ends). |
4403da90 |
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' ); |
42c391b1 |
61 | |
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' ); |
65 | |
4403da90 |
66 | } |
67 | |
68 | { |
69 | package MooseX::Sugar; |
70 | |
71 | use Moose (); |
72 | |
73 | sub wrapped1 { |
0661fc1a |
74 | my $meta = shift; |
75 | return $meta->name . ' called wrapped1'; |
4403da90 |
76 | } |
77 | |
aedcb7d9 |
78 | Moose::Exporter->setup_import_methods( |
0661fc1a |
79 | with_meta => ['wrapped1'], |
80 | also => 'Moose', |
2c9c8797 |
81 | ); |
4403da90 |
82 | } |
83 | |
84 | { |
85 | package WantsSugar; |
86 | |
2c9c8797 |
87 | MooseX::Sugar->import(); |
4403da90 |
88 | |
89 | sub foo { 1 } |
90 | |
2c9c8797 |
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' ); |
4403da90 |
97 | |
2c9c8797 |
98 | MooseX::Sugar->unimport(); |
4403da90 |
99 | } |
100 | |
101 | { |
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' ); |
106 | } |
107 | |
108 | { |
109 | package MooseX::MoreSugar; |
110 | |
111 | use Moose (); |
112 | |
113 | sub wrapped2 { |
3b400403 |
114 | my $caller = shift->name; |
4403da90 |
115 | return $caller . ' called wrapped2'; |
116 | } |
117 | |
118 | sub as_is1 { |
119 | return 'as_is1'; |
120 | } |
121 | |
aedcb7d9 |
122 | Moose::Exporter->setup_import_methods( |
3b400403 |
123 | with_meta => ['wrapped2'], |
124 | as_is => ['as_is1'], |
125 | also => 'MooseX::Sugar', |
2c9c8797 |
126 | ); |
4403da90 |
127 | } |
128 | |
129 | { |
130 | package WantsMoreSugar; |
131 | |
2c9c8797 |
132 | MooseX::MoreSugar->import(); |
4403da90 |
133 | |
134 | sub foo { 1 } |
135 | |
2c9c8797 |
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' ); |
148 | |
149 | MooseX::MoreSugar->unimport(); |
4403da90 |
150 | } |
151 | |
152 | { |
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' ); |
159 | } |
160 | |
161 | { |
085fba61 |
162 | package My::Metaclass; |
163 | use Moose; |
164 | BEGIN { extends 'Moose::Meta::Class' } |
165 | |
166 | package My::Object; |
167 | use Moose; |
168 | BEGIN { extends 'Moose::Object' } |
169 | |
170 | package HasInitMeta; |
171 | |
172 | use Moose (); |
173 | |
174 | sub init_meta { |
175 | shift; |
176 | return Moose->init_meta( @_, |
177 | metaclass => 'My::Metaclass', |
178 | base_class => 'My::Object', |
179 | ); |
180 | } |
181 | |
aedcb7d9 |
182 | Moose::Exporter->setup_import_methods( also => 'Moose' ); |
085fba61 |
183 | } |
184 | |
185 | { |
186 | package NewMeta; |
187 | |
2c9c8797 |
188 | HasInitMeta->import(); |
085fba61 |
189 | } |
190 | |
191 | { |
192 | isa_ok( NewMeta->meta(), 'My::Metaclass' ); |
193 | isa_ok( NewMeta->new(), 'My::Object' ); |
194 | } |
195 | |
196 | { |
4403da90 |
197 | package MooseX::CircularAlso; |
198 | |
199 | use Moose (); |
200 | |
201 | ::dies_ok( |
202 | sub { |
aedcb7d9 |
203 | Moose::Exporter->setup_import_methods( |
4403da90 |
204 | also => [ 'Moose', 'MooseX::CircularAlso' ], |
205 | ); |
206 | }, |
207 | 'a circular reference in also dies with an error' |
208 | ); |
209 | |
210 | ::like( |
211 | $@, |
b822369d |
212 | qr/\QCircular reference in 'also' parameter to Moose::Exporter between MooseX::CircularAlso and MooseX::CircularAlso/, |
4403da90 |
213 | 'got the expected error from circular reference in also' |
214 | ); |
215 | } |
216 | |
217 | { |
ba1a3c2f |
218 | package MooseX::NoAlso; |
4403da90 |
219 | |
220 | use Moose (); |
221 | |
222 | ::dies_ok( |
223 | sub { |
aedcb7d9 |
224 | Moose::Exporter->setup_import_methods( |
4403da90 |
225 | also => [ 'NoSuchThing' ], |
226 | ); |
227 | }, |
228 | 'a package which does not use Moose::Exporter in also dies with an error' |
229 | ); |
230 | |
231 | ::like( |
232 | $@, |
ba1a3c2f |
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' |
235 | ); |
236 | } |
237 | |
238 | { |
239 | package MooseX::NotExporter; |
240 | |
241 | use Moose (); |
242 | |
243 | ::dies_ok( |
244 | sub { |
245 | Moose::Exporter->setup_import_methods( |
246 | also => [ 'Moose::Meta::Method' ], |
247 | ); |
248 | }, |
249 | 'a package which does not use Moose::Exporter in also dies with an error' |
250 | ); |
251 | |
252 | ::like( |
253 | $@, |
254 | qr/\QPackage in also (Moose::Meta::Method) does not seem to use Moose::Exporter at /, |
4403da90 |
255 | 'got the expected error from a reference in also to a package which does not use Moose::Exporter' |
256 | ); |
257 | } |
ae8817b6 |
258 | |
259 | { |
260 | package MooseX::OverridingSugar; |
261 | |
262 | use Moose (); |
263 | |
264 | sub has { |
3b400403 |
265 | my $caller = shift->name; |
ae8817b6 |
266 | return $caller . ' called has'; |
267 | } |
268 | |
269 | Moose::Exporter->setup_import_methods( |
3b400403 |
270 | with_meta => ['has'], |
271 | also => 'Moose', |
ae8817b6 |
272 | ); |
273 | } |
274 | |
275 | { |
276 | package WantsOverridingSugar; |
277 | |
278 | MooseX::OverridingSugar->import(); |
279 | |
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' ); |
284 | |
285 | MooseX::OverridingSugar->unimport(); |
286 | } |
287 | |
288 | { |
289 | ok( ! WantsSugar->can('has'), 'WantsSugar::has() has been cleaned' ); |
290 | ok( ! WantsSugar->can('with'), 'WantsSugar::with() has been cleaned' ); |
291 | } |
292 | |
e6a5040f |
293 | { |
294 | package NonExistentExport; |
295 | |
296 | use Moose (); |
297 | |
298 | ::stderr_like { |
299 | Moose::Exporter->setup_import_methods( |
300 | also => ['Moose'], |
3b400403 |
301 | with_meta => ['does_not_exist'], |
e6a5040f |
302 | ); |
303 | } qr/^Trying to export undefined sub NonExistentExport::does_not_exist/, |
304 | "warns when a non-existent method is requested to be exported"; |
305 | } |
306 | |
307 | { |
308 | package WantsNonExistentExport; |
309 | |
310 | NonExistentExport->import; |
311 | |
312 | ::ok(!__PACKAGE__->can('does_not_exist'), |
313 | "undefined subs do not get exported"); |
a584a0c8 |
314 | } |
addd05f6 |
315 | |
a584a0c8 |
316 | { |
0661fc1a |
317 | package AllOptions; |
318 | use Moose (); |
3b400403 |
319 | use Moose::Deprecated -api_version => '0.88'; |
0661fc1a |
320 | use Moose::Exporter; |
321 | |
322 | Moose::Exporter->setup_import_methods( |
323 | also => ['Moose'], |
324 | with_meta => [ 'with_meta1', 'with_meta2' ], |
325 | with_caller => [ 'with_caller1', 'with_caller2' ], |
326 | as_is => ['as_is1'], |
327 | ); |
328 | |
329 | sub with_caller1 { |
330 | return @_; |
331 | } |
332 | |
333 | sub with_caller2 (&) { |
334 | return @_; |
335 | } |
336 | |
337 | sub as_is1 {2} |
338 | |
339 | sub with_meta1 { |
340 | return @_; |
341 | } |
342 | |
343 | sub with_meta2 (&) { |
344 | return @_; |
345 | } |
346 | } |
347 | |
348 | { |
349 | package UseAllOptions; |
350 | |
351 | AllOptions->import(); |
352 | } |
353 | |
354 | { |
355 | can_ok( 'UseAllOptions', $_ ) |
356 | for qw( with_meta1 with_meta2 with_caller1 with_caller2 as_is1 ); |
357 | |
358 | { |
359 | my ( $caller, $arg1 ) = UseAllOptions::with_caller1(42); |
360 | is( $caller, 'UseAllOptions', 'with_caller wrapped sub gets the right caller' ); |
361 | is( $arg1, 42, 'with_caller wrapped sub returns argument it was passed' ); |
362 | } |
363 | |
364 | { |
365 | my ( $meta, $arg1 ) = UseAllOptions::with_meta1(42); |
366 | isa_ok( $meta, 'Moose::Meta::Class', 'with_meta first argument' ); |
367 | is( $arg1, 42, 'with_meta1 returns argument it was passed' ); |
368 | } |
369 | |
370 | is( |
5a204c51 |
371 | prototype( UseAllOptions->can('with_caller2') ), |
372 | prototype( AllOptions->can('with_caller2') ), |
373 | 'using correct prototype on with_meta function' |
374 | ); |
375 | |
376 | is( |
0661fc1a |
377 | prototype( UseAllOptions->can('with_meta2') ), |
378 | prototype( AllOptions->can('with_meta2') ), |
379 | 'using correct prototype on with_meta function' |
380 | ); |
a584a0c8 |
381 | } |
0661fc1a |
382 | |
a584a0c8 |
383 | { |
384 | package UseAllOptions; |
385 | AllOptions->unimport(); |
386 | } |
0661fc1a |
387 | |
a584a0c8 |
388 | { |
0661fc1a |
389 | ok( ! UseAllOptions->can($_), "UseAllOptions::$_ has been unimported" ) |
390 | for qw( with_meta1 with_meta2 with_caller1 with_caller2 as_is1 ); |
e6a5040f |
391 | } |
a28e50e4 |
392 | |
393 | done_testing; |