source: trunk/essentials/dev-lang/perl/t/op/caller.t

Last change on this file was 3181, checked in by bird, 18 years ago

perl 5.8.8

File size: 3.1 KB
Line 
1#!./perl
2# Tests for caller()
3
4BEGIN {
5 chdir 't' if -d 't';
6 @INC = '../lib';
7 require './test.pl';
8 plan( tests => 31 );
9}
10
11my @c;
12
13print "# Tests with caller(0)\n";
14
15@c = caller(0);
16ok( (!@c), "caller(0) in main program" );
17
18eval { @c = caller(0) };
19is( $c[3], "(eval)", "subroutine name in an eval {}" );
20ok( !$c[4], "hasargs false in an eval {}" );
21
22eval q{ @c = (Caller(0))[3] };
23is( $c[3], "(eval)", "subroutine name in an eval ''" );
24ok( !$c[4], "hasargs false in an eval ''" );
25
26sub { @c = caller(0) } -> ();
27is( $c[3], "main::__ANON__", "anonymous subroutine name" );
28ok( $c[4], "hasargs true with anon sub" );
29
30# Bug 20020517.003, used to dump core
31sub foo { @c = caller(0) }
32my $fooref = delete $::{foo};
33$fooref -> ();
34is( $c[3], "(unknown)", "unknown subroutine name" );
35ok( $c[4], "hasargs true with unknown sub" );
36
37print "# Tests with caller(1)\n";
38
39sub f { @c = caller(1) }
40
41sub callf { f(); }
42callf();
43is( $c[3], "main::callf", "subroutine name" );
44ok( $c[4], "hasargs true with callf()" );
45&callf;
46ok( !$c[4], "hasargs false with &callf" );
47
48eval { f() };
49is( $c[3], "(eval)", "subroutine name in an eval {}" );
50ok( !$c[4], "hasargs false in an eval {}" );
51
52eval q{ f() };
53is( $c[3], "(eval)", "subroutine name in an eval ''" );
54ok( !$c[4], "hasargs false in an eval ''" );
55
56sub { f() } -> ();
57is( $c[3], "main::__ANON__", "anonymous subroutine name" );
58ok( $c[4], "hasargs true with anon sub" );
59
60sub foo2 { f() }
61my $fooref2 = delete $::{foo2};
62$fooref2 -> ();
63is( $c[3], "(unknown)", "unknown subroutine name" );
64ok( $c[4], "hasargs true with unknown sub" );
65
66# See if caller() returns the correct warning mask
67
68sub testwarn {
69 my $w = shift;
70 is( (caller(0))[9], $w, "warnings");
71}
72
73# NB : extend the warning mask values below when new warnings are added
74{
75 no warnings;
76 BEGIN { is( ${^WARNING_BITS}, "\0" x 12, 'warning bits' ) }
77 testwarn("\0" x 12);
78 use warnings;
79 BEGIN { is( ${^WARNING_BITS}, "UUUUUUUUUUU\25", 'warning bits' ) }
80 BEGIN { testwarn("UUUUUUUUUUU\25"); }
81 # run-time :
82 # the warning mask has been extended by warnings::register
83 testwarn("UUUUUUUUUUUU");
84 use warnings::register;
85 BEGIN { is( ${^WARNING_BITS}, "UUUUUUUUUUUU", 'warning bits' ) }
86 testwarn("UUUUUUUUUUUU");
87}
88
89
90# The next two cases test for a bug where caller ignored evals if
91# the DB::sub glob existed but &DB::sub did not (for example, if
92# $^P had been set but no debugger has been loaded). The tests
93# thus assume that there is no &DB::sub: if there is one, they
94# should both pass no matter whether or not this bug has been
95# fixed.
96
97my $debugger_test = q<
98 my @stackinfo = caller(0);
99 return scalar @stackinfo;
100>;
101
102sub pb { return (caller(0))[3] }
103
104my $i = eval $debugger_test;
105is( $i, 10, "do not skip over eval (and caller returns 10 elements)" );
106
107is( eval 'pb()', 'main::pb', "actually return the right function name" );
108
109my $saved_perldb = $^P;
110$^P = 16;
111$^P = $saved_perldb;
112
113$i = eval $debugger_test;
114is( $i, 10, 'do not skip over eval even if $^P had been on at some point' );
115is( eval 'pb()', 'main::pb', 'actually return the right function name even if $^P had been on at some point' );
116
Note: See TracBrowser for help on using the repository browser.