Skip distro which requires rpm
[gitmo/Moose.git] / t / basics / deprecations.t
CommitLineData
34d6d196 1use strict;
2use warnings;
3
b10dde3a 4use Test::Fatal;
34d6d196 5use Test::More;
6
7use Test::Requires {
8 'Test::Output' => '0.01',
9};
10
53a4d826 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
b10dde3a 27 ::is( ::exception (
efa806d8 28 sub {
29 ::stderr_like{ has foo => (
30 traits => ['String'],
31 is => 'ro',
32 isa => 'Str',
33 );
34 }
e46b3371 35 qr{\QAllowing a native trait to automatically supply a default is deprecated. You can avoid this warning by supplying a default, builder, or making the attribute required at $0 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 }
e46b3371 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 $0 line},
efa806d8 47 'calling a reader on a method created by a _default_is warns';
4c52e860 48 }
b10dde3a 49 ), undef );
4c52e860 50}
34d6d196 51
3cf2f9ec 52{
53 package Pack1;
54
55 use Moose;
56
b10dde3a 57 ::is( ::exception (
efa806d8 58 sub {
59 ::stderr_is{ has foo => (
60 traits => ['String'],
61 is => 'ro',
62 isa => 'Str',
63 builder => '_build_foo',
64 );
65 } q{},
66 'Providing a builder for a String trait avoids default default warning';
67
68 has bar => (
b558f8a6 69 traits => ['String'],
efa806d8 70 reader => '_bar',
b558f8a6 71 isa => 'Str',
efa806d8 72 default => q{},
73 );
74
75 ::ok(
76 !Pack1->can('bar'),
77 'no default is assigned when reader is provided'
b558f8a6 78 );
3cf2f9ec 79
efa806d8 80 ::stderr_is{ Pack1->new->_bar } q{},
81 'Providing a reader for a String trait avoids default is warning';
82 }
b10dde3a 83 ), undef );
efa806d8 84
85 sub _build_foo { q{} }
3cf2f9ec 86}
87
88{
89 package Pack2;
90
91 use Moose;
92
b10dde3a 93 ::is( ::exception (
efa806d8 94 sub {
95 ::stderr_is{ has foo => (
96 traits => ['String'],
97 is => 'ro',
98 isa => 'Str',
99 required => 1,
100 );
101 } q{},
102 'Making a String trait required avoids default default warning';
103
104 has bar => (
105 traits => ['String'],
106 writer => '_bar',
107 isa => 'Str',
108 default => q{},
109 );
110
111 ::ok(
112 !Pack2->can('bar'),
113 'no default is assigned when writer is provided'
114 );
115
116 ::stderr_is{ Pack2->new( foo => 'x' )->_bar('x') }
117 q{},
118 'Providing a writer for a String trait avoids default is warning';
119 }
b10dde3a 120 ), undef );
3cf2f9ec 121}
122
123{
124 package Pack3;
125
126 use Moose;
127
b10dde3a 128 ::is( ::exception (
efa806d8 129 sub {
130 ::stderr_is{ has foo => (
131 traits => ['String'],
132 is => 'ro',
133 isa => 'Str',
134 lazy_build => 1,
135 );
136 } q{},
137 'Making a String trait lazy_build avoids default default warning';
138
139 has bar => (
140 traits => ['String'],
141 accessor => '_bar',
142 isa => 'Str',
143 default => q{},
144 );
145
146 ::ok(
147 !Pack3->can('bar'),
148 'no default is assigned when accessor is provided'
b558f8a6 149 );
b558f8a6 150
efa806d8 151 ::stderr_is{ Pack3->new->_bar }
152 q{},
153 'Providing a accessor for a String trait avoids default is warning';
154 }
b10dde3a 155 ), undef );
efa806d8 156
157 sub _build_foo { q{} }
3cf2f9ec 158}
159
7ec88c15 160{
161 use Moose::Util::TypeConstraints;
162
163 is(
164 exception {
165 stderr_like {
166 subtype 'Frubble', as 'Str', optimize_as sub { };
167 }
168 qr/\QProviding an optimized subroutine ref for type constraints is deprecated./,
169 'Providing an optimize_as sub is deprecated';
170 },
171 undef
172 );
173}
174
34d6d196 175done_testing;
176