1#!/usr/bin/perl -w
2use strict;
3
4use Test::More tests => 19;
5
6use constant NO_SUCH_FILE => "this_file_had_better_not_exist";
7
8my $line;
9
10eval {
11	use autodie ':io';
12	$line = __LINE__; open(my $fh, '<', NO_SUCH_FILE);
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");
26is($@->line, $line, "Correct line");
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');
31is($@->context, 'scalar', 'Open called in scalar/void context');
32is($@->return,undef,'Open should return undef on failure');
33
34# Testing of caller info with a real subroutine.
35
36my $line2;
37
38sub xyzzy {
39    use autodie ':io';
40    $line2 = __LINE__; open(my $fh, '<', NO_SUCH_FILE);
41    return;
42};
43
44eval { xyzzy(); };
45
46isa_ok($@, 'autodie::exception');
47is($@->caller, __PACKAGE__."::xyzzy", "Subroutine caller test");
48is($@->line, $line2, "Subroutine line test");
49