Make tests pass with Carp 1.25
[gitmo/Package-DeprecationManager.git] / t / basic.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Fatal;
6
7 use Test::Requires {
8     'Test::Output' => '0.16',
9 };
10
11 {
12     like(
13         exception {
14             eval 'package Foo; use Package::DeprecationManager;';
15             die $@ if $@;
16         },
17         qr/^\QYou must provide a hash reference -deprecations parameter when importing Package::DeprecationManager/,
18         'must provide a set of deprecations when using Package::DeprecationManager'
19     );
20 }
21
22 {
23     package Foo;
24
25     use Package::DeprecationManager -deprecations => {
26         'Foo::foo'  => '0.02',
27         'Foo::bar'  => '0.03',
28         'Foo::baz'  => '1.21',
29         'not a sub' => '1.23',
30     };
31
32     sub foo {
33         deprecated('foo is deprecated');
34     }
35
36     sub bar {
37         deprecated('bar is deprecated');
38     }
39
40     sub baz {
41         deprecated();
42     }
43
44     sub quux {
45         if ( $_[0] > 5 ) {
46             deprecated(
47                 message => 'quux > 5 has been deprecated',
48                 feature => 'not a sub',
49             );
50         }
51     }
52
53     sub varies {
54         deprecated("The varies sub varies: $_[0]");
55     }
56
57 }
58
59 {
60     package Bar;
61
62     Foo->import();
63
64     ::stderr_like{ Foo::foo() }
65         qr/\Qfoo is deprecated/,
66         'deprecation warning for foo';
67
68     ::stderr_like{ Foo::bar() }
69         qr/\Qbar is deprecated/,
70         'deprecation warning for bar';
71
72     ::stderr_like{ Foo::baz() }
73         qr/\QFoo::baz has been deprecated since version 1.21/,
74         'deprecation warning for baz, and message is generated by Package::DeprecationManager';
75
76     ::stderr_is{ Foo::foo() } q{}, 'no warning on second call to foo';
77
78     ::stderr_is{ Foo::bar() } q{}, 'no warning on second call to bar';
79
80     ::stderr_is{ Foo::baz() } q{}, 'no warning on second call to baz';
81
82     ::stderr_like{ Foo::varies(1) }
83         qr/\QThe varies sub varies: 1/,
84         'warning for varies sub';
85
86     ::stderr_like{ Foo::varies(2) }
87         qr/\QThe varies sub varies: 2/,
88         'warning for varies sub with different error';
89
90     ::stderr_is{ Foo::varies(1) }
91         q{},
92         'no warning for varies sub with same message as first call';
93 }
94
95 {
96     package Baz;
97
98     Foo->import( -api_version => '0.01' );
99
100     ::stderr_is{ Foo::foo() }
101         q{},
102         'no warning for foo with api_version = 0.01';
103
104     ::stderr_is{ Foo::bar() }
105         q{},
106         'no warning for bar with api_version = 0.01';
107
108     ::stderr_is{ Foo::baz() }
109         q{},
110         'no warning for baz with api_version = 0.01';
111 }
112
113 {
114     package Quux;
115
116     Foo->import( -api_version => '1.17' );
117
118     ::stderr_like{ Foo::foo() }
119         qr/\Qfoo is deprecated/,
120         'deprecation warning for foo with api_version = 1.17';
121
122     ::stderr_like{ Foo::bar() }
123         qr/\Qbar is deprecated/,
124         'deprecation warning for bar with api_version = 1.17';
125
126     ::stderr_is{ Foo::baz() }
127         q{},
128         'no warning for baz with api_version = 1.17';
129 }
130
131 {
132     package Another;
133
134     Foo->import();
135
136     ::stderr_is{ Foo::quux(1) }
137         q{},
138         'no warning for quux(1)';
139
140     ::stderr_like{ Foo::quux(10) }
141         qr/\Qquux > 5 has been deprecated/,
142         'got a warning for quux(10)';
143 }
144
145
146 {
147     package Dep;
148
149     use Package::DeprecationManager -deprecations => {
150         'Dep::foo' => '1.00',
151         },
152         -ignore => [ 'My::Package1', 'My::Package2' ];
153
154     sub foo {
155         deprecated('foo is deprecated');
156     }
157 }
158
159 {
160     package Dep2;
161
162     use Package::DeprecationManager -deprecations => {
163         'Dep2::bar' => '1.00',
164         },
165         -ignore => [ qr/My::Package[12]/ ];
166
167     sub bar {
168         deprecated('bar is deprecated');
169     }
170 }
171
172 {
173     package My::Package1;
174
175     sub foo { Dep::foo() }
176     sub bar { Dep2::bar() }
177 }
178
179 {
180     package My::Package2;
181
182     sub foo { My::Package1::foo() }
183     sub bar { My::Package1::bar() }
184 }
185
186 {
187     package My::Baz;
188
189     ::stderr_like{ My::Package2::foo() }
190         qr/^foo is deprecated at t.basic\.t line \d+\.?\s+My::Baz/,
191         'deprecation warning for call to My::Package2::foo() and mentions My::Baz but not My::Package[12]';
192
193     ::stderr_is{ My::Package2::foo() }
194         q{},
195         'no deprecation warning for second call to My::Package2::foo()';
196
197     ::stderr_is{ My::Package1::foo() }
198         q{},
199         'no deprecation warning for call to My::Package1::foo()';
200
201     ::stderr_like{ My::Package2::bar() }
202         qr/^bar is deprecated at t.basic\.t line \d+\.?\s+My::Baz/,
203         'deprecation warning for call to My::Package2::foo() and mentions My::Baz but not My::Package[12]';
204
205     ::stderr_is{ My::Package2::bar() }
206         q{},
207         'no deprecation warning for second call to My::Package2::bar()';
208 }
209
210 {
211     package My::Quux;
212
213     ::stderr_like{ My::Package1::foo() }
214         qr/^foo is deprecated at t.basic\.t line \d+\.?\s+My::Quux/,
215         'deprecation warning for call to My::Package1::foo() and mentions My::Quux but not My::Package[12]';
216
217     ::stderr_is{ My::Package1::foo() }
218         q{},
219         'no deprecation warning for second call to My::Package1::foo()';
220 }
221
222 done_testing();