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