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