autodie 2.03
[p5sagit/p5-mst-13.2.git] / lib / autodie / t / hints.t
1 #!/usr/bin/perl -w
2 use strict;
3 use warnings;
4 use autodie::hints;
5
6 use FindBin;
7 use lib "$FindBin::Bin/lib";
8
9 use File::Copy qw(copy move cp mv);
10
11 use Test::More 'no_plan';
12
13 use constant NO_SUCH_FILE  => "this_file_had_better_not_exist";
14 use constant NO_SUCH_FILE2 => "this_file_had_better_not_exist_xyzzy";
15
16 use constant PERL510  => ( $] >= 5.0100 );
17 use constant PERL5101 => ( $] >= 5.0101 );
18
19 use Hints_test qw(
20     fail_on_empty fail_on_false fail_on_undef
21 );
22
23 use autodie qw(fail_on_empty fail_on_false fail_on_undef);
24
25 diag("Sub::Identify ", exists( $INC{'Sub/Identify.pm'} ) ? "is" : "is not",
26      " loaded");
27
28 my $hints = "autodie::hints";
29
30 # Basic hinting tests
31
32 is( $hints->sub_fullname(\&copy), 'File::Copy::copy' , "Id: copy" );
33 is(
34     $hints->sub_fullname(\&cp),
35     PERL5101 ? 'File::Copy::cp' : 'File::Copy::copy' , "Id: cp"
36 );
37
38 is( $hints->sub_fullname(\&move), 'File::Copy::move' , "Id: move" );
39 is( $hints->sub_fullname(\&mv),
40     PERL5101 ? 'File::Copy::mv' : 'File::Copy::move' , "Id: mv"
41 );
42
43 if (PERL510) {
44     ok( $hints->get_hints_for(\&copy)->{scalar}->(0) ,
45         "copy() hints should fail on 0 for scalars."
46     );
47     ok( $hints->get_hints_for(\&copy)->{list}->(0) ,
48         "copy() hints should fail on 0 for lists."
49     );
50 }
51
52 # Scalar context test
53
54 eval {
55     use autodie qw(copy);
56
57     my $scalar_context = copy(NO_SUCH_FILE, NO_SUCH_FILE2);
58 };
59
60 isnt("$@", "", "Copying in scalar context should throw an error.");
61 isa_ok($@, "autodie::exception");
62
63 is($@->function, "File::Copy::copy", "Function should be original name");
64 is($@->return, 0, "File::Copy returns zero on failure");
65 is($@->context, "scalar", "File::Copy called in scalar context");
66
67 # List context test.
68
69 eval {
70     use autodie qw(copy);
71
72     my @list_context = copy(NO_SUCH_FILE, NO_SUCH_FILE2);
73 };
74
75 isnt("$@", "", "Copying in list context should throw an error.");
76 isa_ok($@, "autodie::exception");
77
78 is($@->function, "File::Copy::copy", "Function should be original name");
79 is_deeply($@->return, [0], "File::Copy returns zero on failure");
80 is($@->context, "list", "File::Copy called in list context");
81
82 # Tests on loaded funcs.
83
84 my %tests = (
85
86     # Test code             # Exception expected?
87
88     'fail_on_empty()'       => 1,
89     'fail_on_empty(0)'      => 0,
90     'fail_on_empty(undef)'  => 0,
91     'fail_on_empty(1)'      => 0,
92
93     'fail_on_false()'       => 1,
94     'fail_on_false(0)'      => 1,
95     'fail_on_false(undef)'  => 1,
96     'fail_on_false(1)'      => 0,
97
98     'fail_on_undef()'       => 1,
99     'fail_on_undef(0)'      => 0,
100     'fail_on_undef(undef)'  => 1,
101     'fail_on_undef(1)'      => 0,
102
103 );
104
105 # On Perl 5.8, autodie doesn't correctly propagate into string evals.
106 # The following snippet forces the use of autodie inside the eval if
107 # we really really have to.  For 5.10+, we don't want to include this
108 # fix, because the tests will act as a canary if we screw up string
109 # eval propagation.
110
111 my $perl58_fix = (
112     $] >= 5.010 ?
113     "" :
114     "use autodie qw(fail_on_empty fail_on_false fail_on_undef); "
115 );
116
117 while (my ($test, $exception_expected) = each %tests) {
118     eval "
119         $perl58_fix
120         my \@array = $test;
121     ";
122
123
124     if ($exception_expected) {
125         isnt("$@", "", $test);
126     }
127     else {
128         is($@, "", $test);
129     }
130 }
131
132 1;