1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
#!/usr/bin/perl -w
use strict;
use warnings;
use autodie::hints;
use Test::More;
use constant PERL510 => ( $] >= 5.010 );
BEGIN {
if (not PERL510) {
plan skip_all => "Only subroutine hints supported in 5.8.x";
}
else {
plan 'no_plan';
}
}
use FindBin;
use lib "$FindBin::Bin/lib";
use Hints_pod_examples qw(
undef_scalar false_scalar zero_scalar empty_list default_list
empty_or_false_list undef_n_error_list foo re_fail bar
think_positive my_system
);
use autodie qw( !
undef_scalar false_scalar zero_scalar empty_list default_list
empty_or_false_list undef_n_error_list foo re_fail bar
think_positive my_system
);
my %scalar_tests = (
# Test code # Exception expected?
'undef_scalar()' => 1,
'undef_scalar(1)', => 0,
'undef_scalar(0)', => 0,
'undef_scalar("")', => 0,
'false_scalar(0)', => 1,
'false_scalar()', => 1,
'false_scalar(undef)', => 1,
'false_scalar("")', => 1,
'false_scalar(1)', => 0,
'false_scalar("1")', => 0,
'zero_scalar("0")', => 1,
'zero_scalar(0)', => 1,
'zero_scalar(1)', => 0,
'zero_scalar(undef)', => 0,
'zero_scalar("")', => 0,
'foo(0)', => 1,
'foo(undef)', => 0,
'foo(1)', => 0,
'bar(0)', => 1,
'bar(undef)', => 0,
'bar(1)', => 0,
're_fail(-1)', => 0,
're_fail("FAIL")', => 1,
're_fail("_FAIL")', => 1,
're_fail("_fail")', => 0,
're_fail("fail")', => 0,
'think_positive(-1)' => 1,
'think_positive(-2)' => 1,
'think_positive(0)' => 0,
'think_positive(1)' => 0,
'think_positive(2)' => 0,
'my_system(1)' => 1,
'my_system(2)' => 1,
'my_system(0)' => 0,
);
my %list_tests = (
'empty_list()', => 1,
'empty_list(())', => 1,
'empty_list([])', => 0,
'empty_list(0)', => 0,
'empty_list("")', => 0,
'empty_list(undef)', => 0,
'default_list()', => 1,
'default_list(0)', => 0,
'default_list("")', => 0,
'default_list(undef)', => 1,
'default_list(1)', => 0,
'default_list("str")', => 0,
'default_list(1, 2)', => 0,
'empty_or_false_list()', => 1,
'empty_or_false_list(())', => 1,
'empty_or_false_list(0)', => 1,
'empty_or_false_list(undef)',=> 1,
'empty_or_false_list("")', => 1,
'empty_or_false_list("0")', => 1,
'empty_or_false_list(1,2)', => 0,
'empty_or_false_list("a")', => 0,
'undef_n_error_list(undef, 1)' => 1,
'undef_n_error_list(undef, "a")' => 1,
'undef_n_error_list()' => 0,
'undef_n_error_list(0, 1)' => 0,
'undef_n_error_list("", 1)' => 0,
'undef_n_error_list(1)' => 0,
'foo(0)', => 1,
'foo(undef)', => 0,
'foo(1)', => 0,
'bar(0)', => 1,
'bar(undef)', => 0,
'bar(1)', => 0,
're_fail(-1)', => 1,
're_fail("FAIL")', => 0,
're_fail("_FAIL")', => 0,
're_fail("_fail")', => 0,
're_fail("fail")', => 0,
'think_positive(-1)' => 1,
'think_positive(-2)' => 1,
'think_positive(0)' => 0,
'think_positive(1)' => 0,
'think_positive(2)' => 0,
'my_system(1)' => 1,
'my_system(2)' => 1,
'my_system(0)' => 0,
);
# On Perl 5.8, autodie doesn't correctly propagate into string evals.
# The following snippet forces the use of autodie inside the eval if
# we really really have to. For 5.10+, we don't want to include this
# fix, because the tests will act as a canary if we screw up string
# eval propagation.
my $perl58_fix = (
PERL510 ?
q{} :
q{use autodie qw(
undef_scalar false_scalar zero_scalar empty_list default_list
empty_or_false_list undef_n_error_list foo re_fail bar
think_positive my_system bizarro_system
);}
);
# Some of the tests provide different hints for scalar or list context
while (my ($test, $exception_expected) = each %scalar_tests) {
eval "
$perl58_fix
my \$scalar = $test;
";
if ($exception_expected) {
isnt("$@", "", "scalar test - $test");
}
else {
is($@, "", "scalar test - $test");
}
}
while (my ($test, $exception_expected) = each %list_tests) {
eval "
$perl58_fix
my \@array = $test;
";
if ($exception_expected) {
isnt("$@", "", "array test - $test");
}
else {
is($@, "", "array test - $test");
}
}
1;
|