Make it easy to deprecate a feature, rather than just a whole sub/method
[gitmo/Package-DeprecationManager.git] / t / basic.t
1 use strict;
2 use warnings;
3
4 use Test::Exception;
5 use Test::More;
6 use Test::Warn;
7
8 {
9     throws_ok {
10         eval 'package Foo; use Package::DeprecationManager;';
11         die $@ if $@;
12     }
13     qr/^\QYou must provide a hash reference -deprecations parameter when importing Package::DeprecationManager/,
14         'must provide a set of deprecations when using Package::DeprecationManager';
15 }
16
17 {
18     package Foo;
19
20     use Package::DeprecationManager -deprecations => {
21         'Foo::foo'  => '0.02',
22         'Foo::bar'  => '0.03',
23         'Foo::baz'  => '1.21',
24         'not a sub' => '1.23',
25     };
26
27     sub foo {
28         deprecated('foo is deprecated');
29     }
30
31     sub bar {
32         deprecated('bar is deprecated');
33     }
34
35     sub baz {
36         deprecated();
37     }
38
39     sub quux {
40         if ( $_[0] > 5 ) {
41             deprecated(
42                 message => 'quux > 5 has been deprecated',
43                 feature => 'not a sub',
44             );
45         }
46     }
47 }
48
49 {
50     package Bar;
51
52     Foo->import();
53
54     ::warning_is{ Foo::foo() }
55         { carped => 'foo is deprecated' },
56         'deprecation warning for foo';
57
58     ::warning_is{ Foo::bar() }
59         { carped => 'bar is deprecated' },
60         'deprecation warning for bar';
61
62     ::warning_is{ Foo::baz() }
63         { carped => 'Foo::baz has been deprecated since version 1.21' },
64         'deprecation warning for baz, and message is generated by Package::DeprecationManager';
65
66     ::warning_is{ Foo::foo() } q{}, 'no warning on second call to foo';
67
68     ::warning_is{ Foo::bar() } q{}, 'no warning on second call to bar';
69
70     ::warning_is{ Foo::baz() } q{}, 'no warning on second call to baz';
71 }
72
73 {
74     package Baz;
75
76     Foo->import( -api_version => '0.01' );
77
78     ::warning_is{ Foo::foo() }
79         q{},
80         'no warning for foo with api_version = 0.01';
81
82     ::warning_is{ Foo::bar() }
83         q{},
84         'no warning for bar with api_version = 0.01';
85
86     ::warning_is{ Foo::baz() }
87         q{},
88         'no warning for baz with api_version = 0.01';
89 }
90
91 {
92     package Quux;
93
94     Foo->import( -api_version => '1.17' );
95
96     ::warning_is{ Foo::foo() }
97         { carped => 'foo is deprecated' },
98         'deprecation warning for foo with api_version = 1.17';
99
100     ::warning_is{ Foo::bar() }
101         { carped => 'bar is deprecated' },
102         'deprecation warning for bar with api_version = 1.17';
103
104     ::warning_is{ Foo::baz() }
105         q{},
106         'no warning for baz with api_version = 1.17';
107 }
108
109 {
110     package Another;
111
112     Foo->import();
113
114     ::warning_is{ Foo::quux(1) }
115         q{},
116         'no warning for quux(1)';
117
118     ::warning_is{ Foo::quux(10) }
119         { carped => 'quux > 5 has been deprecated' },
120         'got a warning for quux(10)';
121 }
122
123 done_testing();