xref: /openbsd/gnu/usr.bin/perl/cpan/autodie/t/hints.t (revision b39c5158)
1*b39c5158Smillert#!/usr/bin/perl -w
2*b39c5158Smillertuse strict;
3*b39c5158Smillertuse warnings;
4*b39c5158Smillertuse autodie::hints;
5*b39c5158Smillert
6*b39c5158Smillertuse FindBin;
7*b39c5158Smillertuse lib "$FindBin::Bin/lib";
8*b39c5158Smillert
9*b39c5158Smillertuse File::Copy qw(copy move cp mv);
10*b39c5158Smillert
11*b39c5158Smillertuse Test::More 'no_plan';
12*b39c5158Smillert
13*b39c5158Smillertuse constant NO_SUCH_FILE  => "this_file_had_better_not_exist";
14*b39c5158Smillertuse constant NO_SUCH_FILE2 => "this_file_had_better_not_exist_xyzzy";
15*b39c5158Smillert
16*b39c5158Smillertuse constant PERL510  => ( $] >= 5.0100 );
17*b39c5158Smillertuse constant PERL5101 => ( $] >= 5.0101 );
18*b39c5158Smillertuse constant PERL5102 => ( $] >= 5.0102 );
19*b39c5158Smillert
20*b39c5158Smillert# File::Copy states that all subroutines return '0' on failure.
21*b39c5158Smillert# However both Windows and VMS may return other false values
22*b39c5158Smillert# (notably empty-string) on failure.  This constant indicates
23*b39c5158Smillert# whether we should skip some tests because the return values
24*b39c5158Smillert# from File::Copy may not be what's in the documentation.
25*b39c5158Smillert
26*b39c5158Smillertuse constant WEIRDO_FILE_COPY =>
27*b39c5158Smillert    ( ! PERL5102 and ( $^O eq "MSWin32" or $^O eq "VMS" ));
28*b39c5158Smillert
29*b39c5158Smillertuse Hints_test qw(
30*b39c5158Smillert    fail_on_empty fail_on_false fail_on_undef
31*b39c5158Smillert);
32*b39c5158Smillert
33*b39c5158Smillertuse autodie qw(fail_on_empty fail_on_false fail_on_undef);
34*b39c5158Smillert
35*b39c5158Smillertdiag("Sub::Identify ", exists( $INC{'Sub/Identify.pm'} ) ? "is" : "is not",
36*b39c5158Smillert     " loaded") if (! $ENV{PERL_CORE});
37*b39c5158Smillert
38*b39c5158Smillertmy $hints = "autodie::hints";
39*b39c5158Smillert
40*b39c5158Smillert# Basic hinting tests
41*b39c5158Smillert
42*b39c5158Smillertis( $hints->sub_fullname(\&copy), 'File::Copy::copy' , "Id: copy" );
43*b39c5158Smillertis(
44*b39c5158Smillert    $hints->sub_fullname(\&cp),
45*b39c5158Smillert    PERL5101 ? 'File::Copy::cp' : 'File::Copy::copy' , "Id: cp"
46*b39c5158Smillert);
47*b39c5158Smillert
48*b39c5158Smillertis( $hints->sub_fullname(\&move), 'File::Copy::move' , "Id: move" );
49*b39c5158Smillertis( $hints->sub_fullname(\&mv),
50*b39c5158Smillert    PERL5101 ? 'File::Copy::mv' : 'File::Copy::move' , "Id: mv"
51*b39c5158Smillert);
52*b39c5158Smillert
53*b39c5158Smillertif (PERL510) {
54*b39c5158Smillert    ok( $hints->get_hints_for(\&copy)->{scalar}->(0) ,
55*b39c5158Smillert        "copy() hints should fail on 0 for scalars."
56*b39c5158Smillert    );
57*b39c5158Smillert    ok( $hints->get_hints_for(\&copy)->{list}->(0) ,
58*b39c5158Smillert        "copy() hints should fail on 0 for lists."
59*b39c5158Smillert    );
60*b39c5158Smillert}
61*b39c5158Smillert
62*b39c5158Smillert# Scalar context test
63*b39c5158Smillert
64*b39c5158Smillerteval {
65*b39c5158Smillert    use autodie qw(copy);
66*b39c5158Smillert
67*b39c5158Smillert    my $scalar_context = copy(NO_SUCH_FILE, NO_SUCH_FILE2);
68*b39c5158Smillert};
69*b39c5158Smillert
70*b39c5158Smillertisnt("$@", "", "Copying in scalar context should throw an error.");
71*b39c5158Smillertisa_ok($@, "autodie::exception");
72*b39c5158Smillert
73*b39c5158Smillertis($@->function, "File::Copy::copy", "Function should be original name");
74*b39c5158Smillert
75*b39c5158SmillertSKIP: {
76*b39c5158Smillert    skip("File::Copy is weird on Win32/VMS before 5.10.1", 1)
77*b39c5158Smillert        if WEIRDO_FILE_COPY;
78*b39c5158Smillert
79*b39c5158Smillert    is($@->return, 0, "File::Copy returns zero on failure");
80*b39c5158Smillert}
81*b39c5158Smillert
82*b39c5158Smillertis($@->context, "scalar", "File::Copy called in scalar context");
83*b39c5158Smillert
84*b39c5158Smillert# List context test.
85*b39c5158Smillert
86*b39c5158Smillerteval {
87*b39c5158Smillert    use autodie qw(copy);
88*b39c5158Smillert
89*b39c5158Smillert    my @list_context = copy(NO_SUCH_FILE, NO_SUCH_FILE2);
90*b39c5158Smillert};
91*b39c5158Smillert
92*b39c5158Smillertisnt("$@", "", "Copying in list context should throw an error.");
93*b39c5158Smillertisa_ok($@, "autodie::exception");
94*b39c5158Smillert
95*b39c5158Smillertis($@->function, "File::Copy::copy", "Function should be original name");
96*b39c5158Smillert
97*b39c5158SmillertSKIP: {
98*b39c5158Smillert    skip("File::Copy is weird on Win32/VMS before 5.10.1", 1)
99*b39c5158Smillert        if WEIRDO_FILE_COPY;
100*b39c5158Smillert
101*b39c5158Smillert    is_deeply($@->return, [0], "File::Copy returns zero on failure");
102*b39c5158Smillert}
103*b39c5158Smillertis($@->context, "list", "File::Copy called in list context");
104*b39c5158Smillert
105*b39c5158Smillert# Tests on loaded funcs.
106*b39c5158Smillert
107*b39c5158Smillertmy %tests = (
108*b39c5158Smillert
109*b39c5158Smillert    # Test code             # Exception expected?
110*b39c5158Smillert
111*b39c5158Smillert    'fail_on_empty()'       => 1,
112*b39c5158Smillert    'fail_on_empty(0)'      => 0,
113*b39c5158Smillert    'fail_on_empty(undef)'  => 0,
114*b39c5158Smillert    'fail_on_empty(1)'      => 0,
115*b39c5158Smillert
116*b39c5158Smillert    'fail_on_false()'       => 1,
117*b39c5158Smillert    'fail_on_false(0)'      => 1,
118*b39c5158Smillert    'fail_on_false(undef)'  => 1,
119*b39c5158Smillert    'fail_on_false(1)'      => 0,
120*b39c5158Smillert
121*b39c5158Smillert    'fail_on_undef()'       => 1,
122*b39c5158Smillert    'fail_on_undef(0)'      => 0,
123*b39c5158Smillert    'fail_on_undef(undef)'  => 1,
124*b39c5158Smillert    'fail_on_undef(1)'      => 0,
125*b39c5158Smillert
126*b39c5158Smillert);
127*b39c5158Smillert
128*b39c5158Smillert# On Perl 5.8, autodie doesn't correctly propagate into string evals.
129*b39c5158Smillert# The following snippet forces the use of autodie inside the eval if
130*b39c5158Smillert# we really really have to.  For 5.10+, we don't want to include this
131*b39c5158Smillert# fix, because the tests will act as a canary if we screw up string
132*b39c5158Smillert# eval propagation.
133*b39c5158Smillert
134*b39c5158Smillertmy $perl58_fix = (
135*b39c5158Smillert    $] >= 5.010 ?
136*b39c5158Smillert    "" :
137*b39c5158Smillert    "use autodie qw(fail_on_empty fail_on_false fail_on_undef); "
138*b39c5158Smillert);
139*b39c5158Smillert
140*b39c5158Smillertwhile (my ($test, $exception_expected) = each %tests) {
141*b39c5158Smillert    eval "
142*b39c5158Smillert        $perl58_fix
143*b39c5158Smillert        my \@array = $test;
144*b39c5158Smillert    ";
145*b39c5158Smillert
146*b39c5158Smillert
147*b39c5158Smillert    if ($exception_expected) {
148*b39c5158Smillert        isnt("$@", "", $test);
149*b39c5158Smillert    }
150*b39c5158Smillert    else {
151*b39c5158Smillert        is($@, "", $test);
152*b39c5158Smillert    }
153*b39c5158Smillert}
154*b39c5158Smillert
155*b39c5158Smillert1;
156