5 use IPC::Open3 1.0103 qw(open3);
6 use Test::More tests => 62;
11 my $pid = open3($w, $r, undef, $^X, "-e", $args{prog});
14 while(<$r>) { $output .= $_; }
19 my $Is_VMS = $^O eq 'VMS';
21 use Carp qw(carp cluck croak confess);
24 # This test must be run at BEGIN time, because code later in this file
25 # sets CORE::GLOBAL::caller
26 ok !exists $CORE::GLOBAL::{caller},
27 "Loading doesn't create CORE::GLOBAL::caller";
31 my $str = Carp::longmess("foo");
34 "foo at t/Carp.t line 31.\n",
35 "we don't overshoot the top stack frame",
40 local $SIG{__WARN__} = sub {
41 like $_[0], qr/ok (\d+)\n at.+\b(?i:carp\.t) line \d+\.$/, 'ok 2\n';
48 local $SIG{__WARN__} = sub {
49 like $_[0], qr/(\d+) at.+\b(?i:carp\.t) line \d+\.$/, 'carp 3';
56 local $SIG{__WARN__} = sub {
58 qr/^(\d+) at.+\b(?i:carp\.t) line \d+\.\n\tmain::sub_4\(\) called at.+\b(?i:carp\.t) line \d+$/,
68 local $SIG{__DIE__} = sub {
70 qr/^(\d+) at.+\b(?i:carp\.t) line \d+\.\n\teval \Q{...}\E called at.+\b(?i:carp\.t) line \d+$/,
78 local $SIG{__DIE__} = sub {
80 qr/^(\d+) at.+\b(?i:carp\.t) line \d+\.\n\teval \Q{...}\E called at.+\b(?i:carp\.t) line \d+\n\tmain::sub_6\(\) called at.+\b(?i:carp\.t) line \d+$/,
91 # test for caller_info API
92 my $eval = "use Carp; return Carp::caller_info(0);";
93 my %info = eval($eval);
94 is( $info{sub_name}, "eval '$eval'", 'caller_info API' );
96 # test for '...::CARP_NOT used only once' warning from Carp
100 local $SIG{__WARN__} = sub {
101 if ( defined $^S ) { warn $_[0] }
102 else { $warning = $_[0] }
109 eval { Carp::croak() };
112 ok !$warning, q/'...::CARP_NOT used only once' warning from Carp/;
114 # Test the location of error messages.
115 like( XA::short(), qr/^Error at XC/, "Short messages skip carped package" );
118 local @XC::ISA = "XD";
119 like( XA::short(), qr/^Error at XB/, "Short messages skip inheritance" );
123 local @XD::ISA = "XC";
124 like( XA::short(), qr/^Error at XB/, "Short messages skip inheritance" );
128 local @XD::ISA = "XB";
129 local @XB::ISA = "XC";
130 like( XA::short(), qr/^Error at XA/, "Inheritance is transitive" );
134 local @XB::ISA = "XD";
135 local @XC::ISA = "XB";
136 like( XA::short(), qr/^Error at XA/, "Inheritance is transitive" );
140 local @XC::CARP_NOT = "XD";
141 like( XA::short(), qr/^Error at XB/, "Short messages see \@CARP_NOT" );
145 local @XD::CARP_NOT = "XC";
146 like( XA::short(), qr/^Error at XB/, "Short messages see \@CARP_NOT" );
150 local @XD::CARP_NOT = "XB";
151 local @XB::CARP_NOT = "XC";
152 like( XA::short(), qr/^Error at XA/, "\@CARP_NOT is transitive" );
156 local @XB::CARP_NOT = "XD";
157 local @XC::CARP_NOT = "XB";
158 like( XA::short(), qr/^Error at XA/, "\@CARP_NOT is transitive" );
162 local @XD::ISA = "XC";
163 local @XD::CARP_NOT = "XB";
164 like( XA::short(), qr/^Error at XC/, "\@CARP_NOT overrides inheritance" );
168 local @XD::ISA = "XB";
169 local @XD::CARP_NOT = "XC";
170 like( XA::short(), qr/^Error at XB/, "\@CARP_NOT overrides inheritance" );
175 local $Carp::Internal{XC} = 1;
176 like( XA::short(), qr/^Error at XB/, "Short doesn't report Internal" );
180 local $Carp::Internal{XD} = 1;
181 like( XA::long(), qr/^Error at XC/, "Long doesn't report Internal" );
184 # %Carp::CarpInternal
186 local $Carp::CarpInternal{XD} = 1;
188 XA::short(), qr/^Error at XB/,
189 "Short doesn't report calls to CarpInternal"
194 local $Carp::CarpInternal{XD} = 1;
195 like( XA::long(), qr/^Error at XC/, "Long doesn't report CarpInternal" );
198 # tests for global variables
205 qr/t at \S*(?i:carp.t) line \d+\./,
206 qr/t at \S*(?i:carp.t) line \d+\.\n\s*main::x\('t'\) called at \S*(?i:carp.t) line \d+/
210 for my $re (@$aref) {
211 local $Carp::Verbose = $i++;
212 local $SIG{__WARN__} = sub {
213 like $_[0], $re, 'Verbose';
225 my $txt = "Carp::cluck($test_num)";
226 local $Carp::MaxEvalLen = $_;
227 local $SIG{__WARN__} = sub {
228 "@_" =~ /'(.+?)(?:\n|')/s;
230 length( $_ ? substr( $txt, 0, $_ ) : substr( $txt, 0 ) ),
241 my $arg = 'testtest';
242 local $Carp::MaxArgLen = $_;
243 local $SIG{__WARN__} = sub {
246 length( $_ ? substr( $arg, 0, $_ ) : substr( $arg, 0 ) ),
259 qr/1234 at \S*(?i:carp.t) line \d+\.\n\s*main::w\(1, 2, 3, 4\) called at \S*(?i:carp.t) line \d+/,
260 qr/1234 at \S*(?i:carp.t) line \d+\.\n\s*main::w\(1, 2, \.\.\.\) called at \S*(?i:carp.t) line \d+/,
264 local $Carp::MaxArgNums = $i++;
265 local $SIG{__WARN__} = sub {
266 like "@_", $_, 'MaxArgNums';
278 qr/1 at \S*(?i:carp.t) line \d+\.\n\s*main::w\(1\) called at \S*(?i:carp.t) line \d+/,
279 qr/1 at \S*(?i:carp.t) line \d+\.$/,
283 local $Carp::CarpLevel = $i++;
284 local $SIG{__WARN__} = sub {
285 like "@_", $_, 'CarpLevel';
295 skip "IPC::Open3::open3 needs porting", 2 if $Is_VMS;
297 # Check that croak() and confess() don't clobber $!
299 prog => 'use Carp; $@=q{Phooey}; $!=42; croak(q{Dead})',
303 is( $? >> 8, 42, 'croak() doesn\'t clobber $!' );
306 prog => 'use Carp; $@=q{Phooey}; $!=42; confess(q{Dead})',
310 is( $? >> 8, 42, 'confess() doesn\'t clobber $!' );
313 # undef used to be incorrectly reported as the string "undef"
316 local $SIG{__WARN__} = sub {
318 qr/^Bang! at.+\b(?i:carp\.t) line \d+\.\n\tmain::cluck_undef\(0, 'undef', 2, undef, 4\) called at.+\b(?i:carp\.t) line \d+$/,
319 "cluck doesn't quote undef";
326 cluck_undef( 0, "undef", 2, undef, 4 );
328 # check that Carp respects CORE::GLOBAL::caller override after Carp
330 for my $bodge_job ( 2, 1, 0 ) { SKIP: {
331 skip "can't safely detect incomplete caller override on perl $]", 6
332 if $bodge_job && !Carp::CALLER_OVERRIDE_CHECK_OK;
333 print '# ', ( $bodge_job ? 'Not ' : '' ),
334 "setting \@DB::args in caller override\n";
335 if ( $bodge_job == 1 ) {
337 print "# required B\n";
340 local *CORE::GLOBAL::caller = sub {
341 local *__ANON__ = "fakecaller";
342 my @c = CORE::caller(@_);
344 $accum .= "@c[0..3]\n";
345 if ( !$bodge_job && CORE::caller() eq 'DB' ) {
348 return CORE::caller( ( $_[0] || 0 ) + 1 );
351 return CORE::caller( ( $_[0] || 0 ) + 1 );
354 eval "scalar caller()";
355 like( $accum, qr/main::fakecaller/,
356 "test CORE::GLOBAL::caller override in eval" );
358 my $got = XA::long(42);
359 like( $accum, qr/main::fakecaller/,
360 "test CORE::GLOBAL::caller override in Carp" );
362 my $where = $bodge_job == 1 ? ' in &main::__ANON__' : '';
365 ? "\Q** Incomplete caller override detected$where; \@DB::args were not set **\E"
369 my $previous_package = $package;
372 qr/${package}::long\($warning\) called at $previous_package line \d+/,
373 "Correct arguments for $package" );
375 my $arg = $bodge_job ? $warning : 42;
377 $got, qr!XA::long\($arg\) called at.+\b(?i:carp\.t) line \d+!,
378 'Correct arguments for XA'
383 skip "can't safely detect incomplete caller override on perl $]", 1
384 unless Carp::CALLER_OVERRIDE_CHECK_OK;
386 no warnings 'redefine';
387 sub CORE::GLOBAL::caller {
390 return CORE::caller($height);
394 my $got = XA::long(42);
398 qr!XA::long\(\Q** Incomplete caller override detected; \E\@DB::args\Q were not set **\E\) called at.+\b(?i:carp\.t) line \d+!,
399 'Correct arguments for XA'
403 # UTF8-flagged strings should not cause Carp to try to load modules (even
404 # implicitly via utf8_heavy.pl) after a syntax error [perl #82854].
407 skip "IPC::Open3::open3 needs porting", 1 if $Is_VMS;
411 use utf8; use strict; use Carp;
412 BEGIN { $SIG{__DIE__} = sub { Carp::croak qq(aaaaa$_[0]) } }
418 'Carp can handle UTF8-flagged strings after a syntax error',
424 eval { croak 'heek' };
425 $@ =~ s/\n.*//; # just check first line
426 is $@, "heek at ".__FILE__." line ".(__LINE__-2).", <DATA> line 2.\n",
427 'last handle line num is mentioned';
431 skip "IPC::Open3::open3 needs porting", 1 if $Is_VMS;
435 open FH, q-Makefile.PL-;
436 <FH>; # set PL_last_in_gv
437 BEGIN { *CORE::GLOBAL::die = sub { die Carp::longmess(@_) } };
443 'Carp::longmess works inside CORE::GLOBAL::die',
448 package Foo::No::CARP_NOT;
449 eval { Carp::croak(1) };
451 [ keys %Foo::No::CARP_NOT:: ],
453 "Carp doesn't create CARP_NOT or ISA in the caller if they don't exist"
456 package Foo::No::Autovivify;
458 eval { Carp::croak(1) };
460 !defined *{$Foo::No::Autovivify::{CARP_NOT}}{ARRAY},
461 "Carp doesn't autovivify the CARP_NOT or ISA arrays if the globs exists but they lack the ARRAY slot"
504 eval { Carp::croak("Error") };
509 eval { Carp::confess("Error") };
513 # Put new tests at "new tests go here"