improve alias/excludes warning
[gitmo/Moose.git] / t / 010_basics / 030_deprecations.t
1 use strict;
2 use warnings;
3
4 use Test::Exception;
5 use Test::More;
6
7 use Test::Requires {
8     'Test::Output' => '0.01',
9 };
10
11 # All tests are wrapped with lives_and because the stderr output tests will
12 # otherwise eat exceptions, and the test just dies silently.
13
14 {
15     package Role;
16
17     use Moose::Role;
18
19     sub thing { }
20 }
21
22 {
23     package Foo;
24
25     use Moose;
26
27     ::lives_and(
28         sub {
29             ::stderr_like{ has foo => (
30                     traits => ['String'],
31                     is     => 'ro',
32                     isa    => 'Str',
33                 );
34                 }
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},
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                 }
52                 qr/\QThe alias and excludes options for role application have been renamed -alias and -excludes (applying Role to Foo - do you need to upgrade Foo?)/,
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             );
62         } );
63     }
64
65 {
66     package Pack1;
67
68     use Moose;
69
70     ::lives_and(
71         sub {
72             ::stderr_is{ has foo => (
73                     traits  => ['String'],
74                     is      => 'ro',
75                     isa     => 'Str',
76                     builder => '_build_foo',
77                 );
78                 } q{},
79                 'Providing a builder for a String trait avoids default default warning';
80
81             has bar => (
82                 traits  => ['String'],
83                 reader  => '_bar',
84                 isa     => 'Str',
85                 default => q{},
86             );
87
88             ::ok(
89                 !Pack1->can('bar'),
90                 'no default is assigned when reader is provided'
91             );
92
93             ::stderr_is{ Pack1->new->_bar } q{},
94                 'Providing a reader for a String trait avoids default is warning';
95         }
96     );
97
98     sub _build_foo { q{} }
99 }
100
101 {
102     package Pack2;
103
104     use Moose;
105
106     ::lives_and(
107         sub {
108             ::stderr_is{ has foo => (
109                     traits   => ['String'],
110                     is       => 'ro',
111                     isa      => 'Str',
112                     required => 1,
113                 );
114                 } q{},
115                 'Making a String trait required avoids default default warning';
116
117             has bar => (
118                 traits  => ['String'],
119                 writer  => '_bar',
120                 isa     => 'Str',
121                 default => q{},
122             );
123
124             ::ok(
125                 !Pack2->can('bar'),
126                 'no default is assigned when writer is provided'
127             );
128
129             ::stderr_is{ Pack2->new( foo => 'x' )->_bar('x') }
130                 q{},
131                 'Providing a writer for a String trait avoids default is warning';
132         }
133 }
134
135 {
136     package Pack3;
137
138     use Moose;
139
140     ::lives_and(
141         sub {
142             ::stderr_is{ has foo => (
143                     traits     => ['String'],
144                     is         => 'ro',
145                     isa        => 'Str',
146                     lazy_build => 1,
147                 );
148                 } q{},
149                 'Making a String trait lazy_build avoids default default warning';
150
151             has bar => (
152                 traits   => ['String'],
153                 accessor => '_bar',
154                 isa      => 'Str',
155                 default  => q{},
156             );
157
158             ::ok(
159                 !Pack3->can('bar'),
160                 'no default is assigned when accessor is provided'
161             );
162
163             ::stderr_is{ Pack3->new->_bar }
164                 q{},
165                 'Providing a accessor for a String trait avoids default is warning';
166         }
167     );
168
169     sub _build_foo { q{} }
170 }
171
172 done_testing;
173