Tests for deleting stash entries.
[p5sagit/p5-mst-13.2.git] / t / op / stash.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = qw(../lib);
6 }
7
8 BEGIN { require "./test.pl"; }
9
10 plan( tests => 30 );
11
12 # Used to segfault (bug #15479)
13 fresh_perl_is(
14     '%:: = ""',
15     'Odd number of elements in hash assignment at - line 1.',
16     { switches => [ '-w' ] },
17     'delete $::{STDERR} and print a warning',
18 );
19
20 # Used to segfault
21 fresh_perl_is(
22     'BEGIN { $::{"X::"} = 2 }',
23     '',
24     { switches => [ '-w' ] },
25     q(Insert a non-GV in a stash, under warnings 'once'),
26 );
27
28 ok( !defined %oedipa::maas::, q(stashes aren't defined if not used) );
29 ok( !defined %{"oedipa::maas::"}, q(- work with hard refs too) );
30
31 ok( defined %tyrone::slothrop::, q(stashes are defined if seen at compile time) );
32 ok( defined %{"tyrone::slothrop::"}, q(- work with hard refs too) );
33
34 ok( defined %bongo::shaftsbury::, q(stashes are defined if a var is seen at compile time) );
35 ok( defined %{"bongo::shaftsbury::"}, q(- work with hard refs too) );
36
37 package tyrone::slothrop;
38 $bongo::shaftsbury::scalar = 1;
39
40 package main;
41
42 # Used to warn
43 # Unbalanced string table refcount: (1) for "A::" during global destruction.
44 # for ithreads.
45 {
46     local $ENV{PERL_DESTRUCT_LEVEL} = 2;
47     fresh_perl_is(
48                   'package A; sub a { // }; %::=""',
49                   '',
50                   '',
51                   );
52 }
53
54 # now tests in eval
55
56 ok( !eval  { defined %achtfaden:: },   'works in eval{}' );
57 ok( !eval q{ defined %schoenmaker:: }, 'works in eval("")' );
58
59 # now tests with strictures
60
61 {
62     use strict;
63     ok( !defined %pig::, q(referencing a non-existent stash doesn't produce stricture errors) );
64     ok( !exists $pig::{bodine}, q(referencing a non-existent stash element doesn't produce stricture errors) );
65 }
66
67 SKIP: {
68     eval { require B; 1 } or skip "no B", 12;
69
70     *b = \&B::svref_2object;
71     my $CVf_ANON = B::CVf_ANON();
72
73     my $sub = do {
74         package one;
75         \&{"one"};
76     };
77     delete $one::{one};
78     my $gv = b($sub)->GV;
79
80     isa_ok( $gv, "B::GV", "deleted stash entry leaves CV with valid GV");
81     is( b($sub)->CvFLAGS & $CVf_ANON, $CVf_ANON, "...and CVf_ANON set");
82     is( eval { $gv->NAME }, "__ANON__", "...and an __ANON__ name");
83     is( eval { $gv->STASH->NAME }, "one", "...but leaves stash intact");
84
85     $sub = do {
86         package two;
87         \&{"two"};
88     };
89     %two:: = ();
90     $gv = b($sub)->GV;
91
92     isa_ok( $gv, "B::GV", "cleared stash leaves CV with valid GV");
93     is( b($sub)->CvFLAGS & $CVf_ANON, $CVf_ANON, "...and CVf_ANON set");
94     is( eval { $gv->NAME }, "__ANON__", "...and an __ANON__ name");
95     is( eval { $gv->STASH->NAME }, "__ANON__", "...and an __ANON__ stash");
96
97     $sub = do {
98         package three;
99         \&{"three"};
100     };
101     undef %three::;
102     $gv = b($sub)->GV;
103
104     isa_ok( $gv, "B::GV", "undefed stash leaves CV with valid GV");
105     is( b($sub)->CvFLAGS & $CVf_ANON, $CVf_ANON, "...and CVf_ANON set");
106     is( eval { $gv->NAME }, "__ANON__", "...and an __ANON__ name");
107     is( eval { $gv->STASH->NAME }, "__ANON__", "...and an __ANON__ stash");
108
109     TODO: {
110         local $TODO = "anon CVs not accounted for yet";
111
112         $sub = do {
113             package four;
114             sub { 1 };
115         };
116         %four:: = ();
117         $gv = b($sub)->GV;
118
119         isa_ok( $gv, "B::GV", "cleared stash leaves anon CV with valid GV");
120         is( eval { $gv->STASH->NAME }, "__ANON__", "...and an __ANON__ stash");
121
122         $sub = do {
123             package five;
124             sub { 1 };
125         };
126         undef %five::;
127         $gv = b($sub)->GV;
128
129         isa_ok( $gv, "B::GV", "undefed stash leaves anon CV with valid GV");
130         is( eval { $gv->STASH->NAME }, "__ANON__", "...and an __ANON__ stash");
131     }
132     
133     # [perl #58530]
134     fresh_perl_is(
135         'sub foo { 1 }; use overload q/""/ => \&foo;' .
136             'delete $main::{foo}; bless []',
137         "",
138         {},
139         "no segfault with overload/deleted stash entry [#58530]",
140     );
141 }