Unconditionally depend on Test::Output; dropping Test::Warn
[gitmo/Moose.git] / t / 050_metaclasses / 012_moose_exporter.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 45;
7 use Test::Exception;
8 use Test::Output;
9
10
11 {
12     package HasOwnImmutable;
13
14     use Moose;
15
16     no Moose;
17
18     ::stderr_is( sub { eval q[sub make_immutable { return 'foo' }] },
19                   '',
20                   'no warning when defining our own make_immutable sub' );
21 }
22
23 {
24     is( HasOwnImmutable->make_immutable(), 'foo',
25         'HasOwnImmutable->make_immutable does not get overwritten' );
26 }
27
28 {
29     package MooseX::Empty;
30
31     use Moose ();
32     Moose::Exporter->setup_import_methods( also => 'Moose' );
33 }
34
35 {
36     package WantsMoose;
37
38     MooseX::Empty->import();
39
40     sub foo { 1 }
41
42     ::can_ok( 'WantsMoose', 'has' );
43     ::can_ok( 'WantsMoose', 'with' );
44     ::can_ok( 'WantsMoose', 'foo' );
45
46     MooseX::Empty->unimport();
47 }
48
49 {
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
54     # ends).
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' );
58
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' );
62
63 }
64
65 {
66     package MooseX::Sugar;
67
68     use Moose ();
69
70     sub wrapped1 {
71         my $caller = shift;
72         return $caller . ' called wrapped1';
73     }
74
75     Moose::Exporter->setup_import_methods(
76         with_caller => ['wrapped1'],
77         also        => 'Moose',
78     );
79 }
80
81 {
82     package WantsSugar;
83
84     MooseX::Sugar->import();
85
86     sub foo { 1 }
87
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' );
94
95     MooseX::Sugar->unimport();
96 }
97
98 {
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' );
103 }
104
105 {
106     package MooseX::MoreSugar;
107
108     use Moose ();
109
110     sub wrapped2 {
111         my $caller = shift;
112         return $caller . ' called wrapped2';
113     }
114
115     sub as_is1 {
116         return 'as_is1';
117     }
118
119     Moose::Exporter->setup_import_methods(
120         with_caller => ['wrapped2'],
121         as_is       => ['as_is1'],
122         also        => 'MooseX::Sugar',
123     );
124 }
125
126 {
127     package WantsMoreSugar;
128
129     MooseX::MoreSugar->import();
130
131     sub foo { 1 }
132
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' );
145
146     MooseX::MoreSugar->unimport();
147 }
148
149 {
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' );
156 }
157
158 {
159     package My::Metaclass;
160     use Moose;
161     BEGIN { extends 'Moose::Meta::Class' }
162
163     package My::Object;
164     use Moose;
165     BEGIN { extends 'Moose::Object' }
166
167     package HasInitMeta;
168
169     use Moose ();
170
171     sub init_meta {
172         shift;
173         return Moose->init_meta( @_,
174                                  metaclass  => 'My::Metaclass',
175                                  base_class => 'My::Object',
176                                );
177     }
178
179     Moose::Exporter->setup_import_methods( also => 'Moose' );
180 }
181
182 {
183     package NewMeta;
184
185     HasInitMeta->import();
186 }
187
188 {
189     isa_ok( NewMeta->meta(), 'My::Metaclass' );
190     isa_ok( NewMeta->new(), 'My::Object' );
191 }
192
193 {
194     package MooseX::CircularAlso;
195
196     use Moose ();
197
198     ::dies_ok(
199         sub {
200             Moose::Exporter->setup_import_methods(
201                 also => [ 'Moose', 'MooseX::CircularAlso' ],
202             );
203         },
204         'a circular reference in also dies with an error'
205     );
206
207     ::like(
208         $@,
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'
211     );
212 }
213
214 {
215     package MooseX::CircularAlso;
216
217     use Moose ();
218
219     ::dies_ok(
220         sub {
221             Moose::Exporter->setup_import_methods(
222                 also => [ 'NoSuchThing' ],
223             );
224         },
225         'a package which does not use Moose::Exporter in also dies with an error'
226     );
227
228     ::like(
229         $@,
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'
232     );
233 }
234
235 {
236     package MooseX::OverridingSugar;
237
238     use Moose ();
239
240     sub has {
241         my $caller = shift;
242         return $caller . ' called has';
243     }
244
245     Moose::Exporter->setup_import_methods(
246         with_caller => ['has'],
247         also        => 'Moose',
248     );
249 }
250
251 {
252     package WantsOverridingSugar;
253
254     MooseX::OverridingSugar->import();
255
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' );
260
261     MooseX::OverridingSugar->unimport();
262 }
263
264 {
265     ok( ! WantsSugar->can('has'),  'WantsSugar::has() has been cleaned' );
266     ok( ! WantsSugar->can('with'), 'WantsSugar::with() has been cleaned' );
267 }
268