Merge of autodie 1.999 into blead.
[p5sagit/p5-mst-13.2.git] / t / lib / autodie / basic_exceptions.t
CommitLineData
0b09a93a 1#!/usr/bin/perl -w
2use strict;
3
db4e6d09 4use Test::More tests => 17;
0b09a93a 5
6use constant NO_SUCH_FILE => "this_file_had_better_not_exist";
7
db4e6d09 8my $line;
9
0b09a93a 10eval {
11 use autodie ':io';
db4e6d09 12 $line = __LINE__; open(my $fh, '<', NO_SUCH_FILE);
0b09a93a 13};
14
15like($@, qr/Can't open '\w+' for reading: /, "Prety printed open msg");
16like($@, qr{\Q$0\E}, "Our file mention in error message");
17
18like($@, qr{for reading: '.+'}, "Error should be in single-quotes");
19like($@->errno,qr/./, "Errno should not be empty");
20
21like($@, qr{\n$}, "Errors should end with a newline");
22is($@->file, $0, "Correct file");
23is($@->function, 'CORE::open', "Correct dying sub");
24is($@->package, __PACKAGE__, "Correct package");
25is($@->caller,__PACKAGE__."::__ANON__", "Correct caller");
db4e6d09 26is($@->line, $line, "Correct line");
0b09a93a 27is($@->args->[1], '<', 'Correct mode arg');
28is($@->args->[2], NO_SUCH_FILE, 'Correct filename arg');
29ok($@->matches('open'), 'Looks like an error from open');
30ok($@->matches(':io'), 'Looks like an error from :io');
db4e6d09 31
32# Testing of caller info with a real subroutine.
33
34my $line2;
35
36sub xyzzy {
37 use autodie ':io';
38 $line2 = __LINE__; open(my $fh, '<', NO_SUCH_FILE);
39 return;
40};
41
42eval { xyzzy(); };
43
44isa_ok($@, 'autodie::exception');
45is($@->caller, __PACKAGE__."::xyzzy", "Subroutine caller test");
46is($@->line, $line2, "Subroutine line test");