Move logic into Moose::Meta::Role for deprecation check of excludes and alias. Add...
[gitmo/Moose.git] / t / 010_basics / 030_deprecations.t
CommitLineData
34d6d196 1use strict;
2use warnings;
3
b558f8a6 4use Test::Exception;
34d6d196 5use Test::More;
6
7use Test::Requires {
8 'Test::Output' => '0.01',
9};
10
efa806d8 11# All tests are wrapped with lives_and because the stderr output tests will
996509b8 12# otherwise eat exceptions, and the test just dies silently.
efa806d8 13
34d6d196 14{
15 package Role;
16
17 use Moose::Role;
18
19 sub thing { }
20}
21
22{
23 package Foo;
24
25 use Moose;
26
efa806d8 27 ::lives_and(
28 sub {
29 ::stderr_like{ has foo => (
30 traits => ['String'],
31 is => 'ro',
32 isa => 'Str',
33 );
34 }
cab2e1d0 35 qr{\QAllowing a native trait to automatically supply a default is deprecated. You can avoid this warning by supply a default, builder, or making the attribute required at t/010_basics/030_deprecations.t line},
efa806d8 36 'Not providing a default for native String trait warns';
37
38 ::stderr_is{ has bar => (
39 traits => ['Bool'],
40 isa => 'Bool',
41 default => q{},
42 );
43 } q{}, 'No warning when _default_is is set';
44
45 ::stderr_like{ Foo->new->bar }
46 qr{\QThe bar method in the Foo class was automatically created by the native delegation trait for the bar attribute. This "default is" feature is deprecated. Explicitly set "is" or define accessor names to avoid this at t/010_basics/030_deprecations.t line},
47 'calling a reader on a method created by a _default_is warns';
48
49 ::stderr_like{ with 'Role' =>
50 { excludes => ['thing'], alias => { thing => 'thing2' } };
51 }
4c52e860 52 qr/\QThe alias and excludes options for role application have been renamed -alias and -excludes (Foo is consuming Role - do you need to upgrade Foo?)/,
efa806d8 53 'passing excludes or alias with a leading dash warns';
54 ::ok(
55 !Foo->meta->has_method('thing'),
56 'thing method is excluded from role application'
57 );
58 ::ok(
59 Foo->meta->has_method('thing2'),
60 'thing2 method is created as alias in role application'
61 );
4c52e860 62 }
63 );
64}
34d6d196 65
3cf2f9ec 66{
67 package Pack1;
68
69 use Moose;
70
efa806d8 71 ::lives_and(
72 sub {
73 ::stderr_is{ has foo => (
74 traits => ['String'],
75 is => 'ro',
76 isa => 'Str',
77 builder => '_build_foo',
78 );
79 } q{},
80 'Providing a builder for a String trait avoids default default warning';
81
82 has bar => (
b558f8a6 83 traits => ['String'],
efa806d8 84 reader => '_bar',
b558f8a6 85 isa => 'Str',
efa806d8 86 default => q{},
87 );
88
89 ::ok(
90 !Pack1->can('bar'),
91 'no default is assigned when reader is provided'
b558f8a6 92 );
3cf2f9ec 93
efa806d8 94 ::stderr_is{ Pack1->new->_bar } q{},
95 'Providing a reader for a String trait avoids default is warning';
96 }
97 );
98
99 sub _build_foo { q{} }
3cf2f9ec 100}
101
102{
103 package Pack2;
104
105 use Moose;
106
efa806d8 107 ::lives_and(
108 sub {
109 ::stderr_is{ has foo => (
110 traits => ['String'],
111 is => 'ro',
112 isa => 'Str',
113 required => 1,
114 );
115 } q{},
116 'Making a String trait required avoids default default warning';
117
118 has bar => (
119 traits => ['String'],
120 writer => '_bar',
121 isa => 'Str',
122 default => q{},
123 );
124
125 ::ok(
126 !Pack2->can('bar'),
127 'no default is assigned when writer is provided'
128 );
129
130 ::stderr_is{ Pack2->new( foo => 'x' )->_bar('x') }
131 q{},
132 'Providing a writer for a String trait avoids default is warning';
133 }
3cf2f9ec 134}
135
136{
137 package Pack3;
138
139 use Moose;
140
efa806d8 141 ::lives_and(
142 sub {
143 ::stderr_is{ has foo => (
144 traits => ['String'],
145 is => 'ro',
146 isa => 'Str',
147 lazy_build => 1,
148 );
149 } q{},
150 'Making a String trait lazy_build avoids default default warning';
151
152 has bar => (
153 traits => ['String'],
154 accessor => '_bar',
155 isa => 'Str',
156 default => q{},
157 );
158
159 ::ok(
160 !Pack3->can('bar'),
161 'no default is assigned when accessor is provided'
b558f8a6 162 );
b558f8a6 163
efa806d8 164 ::stderr_is{ Pack3->new->_bar }
165 q{},
166 'Providing a accessor for a String trait avoids default is warning';
167 }
4c52e860 168<<<<<<< HEAD
169=======
170 qr/\QThe alias and excludes options for role application have been renamed -alias and -excludes (Foo is consuming Role - do you need to upgrade Foo?)/,
171 'passing excludes or alias with a leading dash warns';
172 ::ok(
173 !Foo->meta->has_method('thing'),
174 'thing method is excluded from role application'
175 );
176 ::ok(
177 Foo->meta->has_method('thing2'),
178 'thing2 method is created as alias in role application'
179>>>>>>> Move logic into Moose::Meta::Role for deprecation check of excludes and alias. Add friendlier wording to the warning. Update the test to check for new wording
efa806d8 180 );
181
182 sub _build_foo { q{} }
3cf2f9ec 183}
184
34d6d196 185done_testing;
186