This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
diagnostics.t: Restore test name removed by f0e510f
[perl5.git] / lib / diagnostics.t
CommitLineData
00680da2 1#!./perl -w
f5ad5489
GS
2
3BEGIN {
7b903762
RGS
4 chdir '..' if -d '../pod' && -d '../t';
5 @INC = 'lib';
f63ff350 6 require './t/test.pl';
245e6c67 7 plan(18);
f5ad5489
GS
8}
9
00680da2
FC
10BEGIN {
11 my $w;
12 $SIG{__WARN__} = sub { $w = shift };
13 use_ok('diagnostics');
14 is $w, undef, 'no warnings when loading diagnostics.pm';
15}
f5ad5489 16
d23f0205 17require base;
f5ad5489 18
d23f0205
MS
19eval {
20 'base'->import(qw(I::do::not::exist));
21};
f5ad5489 22
d142a076
FC
23like( $@, qr/^Base class package "I::do::not::exist" is empty/,
24 'diagnostics not tripped up by "use base qw(Dont::Exist)"');
8b56d6ff 25
2dde0467 26open *whatever, ">", \my $warning
8b56d6ff 27 or die "Couldn't redirect STDERR to var: $!";
2dde0467
FC
28my $old_stderr = *STDERR{IO};
29*STDERR = *whatever{IO};
30
31# Test for %.0f patterns in perldiag, added in 5.11.0
8b56d6ff
FC
32warn('gmtime(nan) too large');
33like $warning, qr/\(W overflow\) You called/, '%0.f patterns';
4b056c06
FC
34
35# L<foo/bar> links
36seek STDERR, 0,0;
37$warning = '';
38warn("accept() on closed socket spanner");
39like $warning, qr/"accept" in perlfunc/, 'L<foo/bar> links';
40
41# L<foo|bar/baz> links
42seek STDERR, 0,0;
43$warning = '';
44warn
45 'Lexing code attempted to stuff non-Latin-1 character into Latin-1 input';
c58550c3 46like $warning, qr/using lex_stuff_pvn or similar/, 'L<foo|bar/baz>';
f4739a71
FC
47
48# Multiple messages with the same description
49seek STDERR, 0,0;
50$warning = '';
b6a06234 51warn 'Code point 0xBEE5 is not Unicode, may not be portable';
989843c8 52like $warning, qr/S utf8/,
f4739a71
FC
53 'Message sharing its description with the following message';
54
c0d3a21f
MH
55# Periods at end of entries in perldiag.pod get matched correctly
56seek STDERR, 0,0;
57$warning = '';
58warn "Execution of -e aborted due to compilation errors.\n";
59like $warning, qr/The final summary message/, 'Periods at end of line';
e958e573 60
e958e573
MH
61# Test for %d/%u
62seek STDERR, 0,0;
63$warning = '';
64warn "Bad arg length for us, is 4, should be 42";
65like $warning, qr/In C parlance/, '%u works';
524e9188 66
b6a06234
FC
67# Test for %X
68seek STDERR, 0,0;
69$warning = '';
70warn "Unicode surrogate U+C0FFEE is illegal in UTF-8";
71like $warning, qr/You had a UTF-16 surrogate/, '%X';
72
524e9188
MH
73# Strip S<>
74seek STDERR, 0,0;
75$warning = '';
76warn "syntax error";
77like $warning, qr/cybernetic version of 20 questions/s, 'strip S<>';
2dde0467 78
33327ed8
FC
79# Errors ending with dots
80seek STDERR, 0,0;
81$warning = '';
0a437bc9
FC
82warn "I had compilation errors.\n";
83like $warning, qr/final summary message/, 'dotty errors';
84
85# Multiline errors
86seek STDERR, 0,0;
87$warning = '';
88warn "Attempt to reload weapon aborted.\nCompilation failed in require";
89like $warning,
90 qr/You tried to load a file.*Perl could not compile/s,
91 'multiline errors';
33327ed8 92
2dde0467
FC
93*STDERR = $old_stderr;
94
95# These tests use a panic under the hope that the description is not likely
96# to change.
97@runperl_args = (
98 switches => [ '-Ilib', '-Mdiagnostics' ],
99 stderr => 1,
100 nolib => 1, # -I../lib would go outside the build dir
101);
102$subs =
103 "sub foo{bar()}sub bar{baz()}sub baz{die q _panic: gremlins_}foo()";
104is runperl(@runperl_args, prog => $subs),
105 << 'EOT', 'internal error with backtrace';
106panic: gremlins at -e line 1 (#1)
107 (P) An internal error.
108
109Uncaught exception from user code:
110 panic: gremlins at -e line 1.
111 main::baz() called at -e line 1
112 main::bar() called at -e line 1
113 main::foo() called at -e line 1
114EOT
115is runperl(@runperl_args, prog => $subs =~ s/panic\K/k/r),
116 << 'EOU', 'user error with backtrace';
117Uncaught exception from user code:
118 panick: gremlins at -e line 1.
119 main::baz() called at -e line 1
120 main::bar() called at -e line 1
121 main::foo() called at -e line 1
122EOU
123is runperl(@runperl_args, prog => 'die q _panic: gremlins_'),
124 << 'EOV', 'no backtrace from top-level internal error';
125panic: gremlins at -e line 1 (#1)
126 (P) An internal error.
127
128Uncaught exception from user code:
129 panic: gremlins at -e line 1.
130EOV
131is runperl(@runperl_args, prog => 'die q _panick: gremlins_'),
132 << 'EOW', 'no backtrace from top-level user error';
133Uncaught exception from user code:
134 panick: gremlins at -e line 1.
135EOW
245e6c67
FC
136like runperl(
137 @runperl_args,
138 prog => $subs =~
f5aea3e0
NC
139 s[q _panic: gremlins_]
140 [qq _Attempt to reload foo aborted.\\nCompilation failed in require_]r,
245e6c67
FC
141 ),
142 qr/Uncaught exception from user code:
143 Attempt to reload foo aborted\.
144 Compilation failed in require at -e line \d+\.
145 main::baz\(\) called at -e line \d+
146 main::bar\(\) called at -e line \d+
147 main::foo\(\) called at -e line \d+
148/, 'backtrace from multiline error';