Add IO::Scalar fail case to the tests.
[p5sagit/p5-mst-13.2.git] / t / op / tie.t
CommitLineData
49d42823 1#!./perl
2
96e82bbb 3# Add new tests to the end with format:
3ca7705e 4# ########
5#
6# # test description
7# Test code
8# EXPECT
9# Warn or die msgs (if any) at - line 1234
96e82bbb 10#
49d42823 11
12chdir 't' if -d 't';
20822f61 13@INC = '../lib';
49d42823 14$ENV{PERL5LIB} = "../lib";
15
16$|=1;
17
18undef $/;
f0faabb7 19@prgs = split /^########\n/m, <DATA>;
49d42823 20
3ca7705e 21require './test.pl';
22plan(tests => scalar @prgs);
49d42823 23for (@prgs){
f0faabb7 24 ++$i;
25 my($prog,$expected) = split(/\nEXPECT\n/, $_, 2);
26 print("not ok $i # bad test format\n"), next
27 unless defined $expected;
3ca7705e 28 my ($testname) = $prog =~ /^# (.*)\n/m;
f0faabb7 29 $testname ||= '';
49d42823 30 $results =~ s/\n+$//;
31 $expected =~ s/\n+$//;
3ca7705e 32
33 fresh_perl_is($prog, $expected, {}, $testname);
49d42823 34}
35
36__END__
37
38# standard behaviour, without any extra references
39use Tie::Hash ;
40tie %h, Tie::StdHash;
41untie %h;
42EXPECT
43########
44
a29a5827 45# standard behaviour, without any extra references
46use Tie::Hash ;
47{package Tie::HashUntie;
48 use base 'Tie::StdHash';
49 sub UNTIE
50 {
51 warn "Untied\n";
52 }
53}
54tie %h, Tie::HashUntie;
55untie %h;
56EXPECT
57Untied
58########
59
49d42823 60# standard behaviour, with 1 extra reference
61use Tie::Hash ;
62$a = tie %h, Tie::StdHash;
63untie %h;
64EXPECT
65########
66
67# standard behaviour, with 1 extra reference via tied
68use Tie::Hash ;
69tie %h, Tie::StdHash;
70$a = tied %h;
71untie %h;
72EXPECT
73########
74
75# standard behaviour, with 1 extra reference which is destroyed
76use Tie::Hash ;
77$a = tie %h, Tie::StdHash;
78$a = 0 ;
79untie %h;
80EXPECT
81########
82
83# standard behaviour, with 1 extra reference via tied which is destroyed
84use Tie::Hash ;
85tie %h, Tie::StdHash;
86$a = tied %h;
87$a = 0 ;
88untie %h;
89EXPECT
90########
91
92# strict behaviour, without any extra references
4438c4b7 93use warnings 'untie';
49d42823 94use Tie::Hash ;
95tie %h, Tie::StdHash;
96untie %h;
97EXPECT
98########
99
100# strict behaviour, with 1 extra references generating an error
4438c4b7 101use warnings 'untie';
49d42823 102use Tie::Hash ;
103$a = tie %h, Tie::StdHash;
104untie %h;
105EXPECT
3ca7705e 106untie attempted while 1 inner references still exist at - line 6.
49d42823 107########
108
109# strict behaviour, with 1 extra references via tied generating an error
4438c4b7 110use warnings 'untie';
49d42823 111use Tie::Hash ;
112tie %h, Tie::StdHash;
113$a = tied %h;
114untie %h;
115EXPECT
3ca7705e 116untie attempted while 1 inner references still exist at - line 7.
49d42823 117########
118
119# strict behaviour, with 1 extra references which are destroyed
4438c4b7 120use warnings 'untie';
49d42823 121use Tie::Hash ;
122$a = tie %h, Tie::StdHash;
123$a = 0 ;
124untie %h;
125EXPECT
126########
127
128# strict behaviour, with extra 1 references via tied which are destroyed
4438c4b7 129use warnings 'untie';
49d42823 130use Tie::Hash ;
131tie %h, Tie::StdHash;
132$a = tied %h;
133$a = 0 ;
134untie %h;
135EXPECT
136########
137
6751fbc4 138# strict error behaviour, with 2 extra references
4438c4b7 139use warnings 'untie';
49d42823 140use Tie::Hash ;
141$a = tie %h, Tie::StdHash;
142$b = tied %h ;
143untie %h;
144EXPECT
3ca7705e 145untie attempted while 2 inner references still exist at - line 7.
49d42823 146########
147
148# strict behaviour, check scope of strictness.
4438c4b7 149no warnings 'untie';
49d42823 150use Tie::Hash ;
151$A = tie %H, Tie::StdHash;
152$C = $B = tied %H ;
153{
4438c4b7 154 use warnings 'untie';
49d42823 155 use Tie::Hash ;
156 tie %h, Tie::StdHash;
157 untie %h;
158}
159untie %H;
160EXPECT
33c27489 161########
3ca7705e 162
ae21d580 163# Forbidden aggregate self-ties
33c27489 164sub Self::TIEHASH { bless $_[1], $_[0] }
ae21d580 165{
f0faabb7 166 my %c;
ae21d580 167 tie %c, 'Self', \%c;
168}
169EXPECT
3ca7705e 170Self-ties of arrays and hashes are not supported at - line 6.
ae21d580 171########
3ca7705e 172
ae21d580 173# Allowed scalar self-ties
f0faabb7 174my $destroyed = 0;
ae21d580 175sub Self::TIESCALAR { bless $_[1], $_[0] }
f0faabb7 176sub Self::DESTROY { $destroyed = 1; }
33c27489 177{
ae21d580 178 my $c = 42;
ae21d580 179 tie $c, 'Self', \$c;
33c27489 180}
f0faabb7 181die "self-tied scalar not DESTROYd" unless $destroyed == 1;
182EXPECT
183########
3ca7705e 184
f0faabb7 185# Allowed glob self-ties
186my $destroyed = 0;
6751fbc4 187my $printed = 0;
f0faabb7 188sub Self2::TIEHANDLE { bless $_[1], $_[0] }
189sub Self2::DESTROY { $destroyed = 1; }
6751fbc4 190sub Self2::PRINT { $printed = 1; }
f0faabb7 191{
192 use Symbol;
193 my $c = gensym;
194 tie *$c, 'Self2', $c;
6751fbc4 195 print $c 'Hello';
f0faabb7 196}
6751fbc4 197die "self-tied glob not PRINTed" unless $printed == 1;
f0faabb7 198die "self-tied glob not DESTROYd" unless $destroyed == 1;
199EXPECT
200########
3ca7705e 201
f0faabb7 202# Allowed IO self-ties
203my $destroyed = 0;
204sub Self3::TIEHANDLE { bless $_[1], $_[0] }
205sub Self3::DESTROY { $destroyed = 1; }
206{
207 use Symbol 'geniosym';
208 my $c = geniosym;
209 tie *$c, 'Self3', $c;
210}
211die "self-tied IO not DESTROYd" unless $destroyed == 1;
33c27489 212EXPECT
7bb043c3 213########
3ca7705e 214
7bb043c3 215# Interaction of tie and vec
216
217my ($a, $b);
218use Tie::Scalar;
219tie $a,Tie::StdScalar or die;
220vec($b,1,1)=1;
221$a = $b;
222vec($a,1,1)=0;
223vec($b,1,1)=0;
224die unless $a eq $b;
225EXPECT
83f527ec 226########
3ca7705e 227
0b2c215a 228# correct unlocalisation of tied hashes (patch #16431)
229use Tie::Hash ;
230tie %tied, Tie::StdHash;
96e82bbb 231{ local $hash{'foo'} } warn "plain hash bad unlocalize" if exists $hash{'foo'};
232{ local $tied{'foo'} } warn "tied hash bad unlocalize" if exists $tied{'foo'};
233{ local $ENV{'foo'} } warn "%ENV bad unlocalize" if exists $ENV{'foo'};
0b2c215a 234EXPECT
3ca7705e 235########
0b2c215a 236
3ca7705e 237# An attempt at lvalueable barewords broke this
238tie FH, 'main';
239EXPECT
240Can't modify constant item in tie at - line 3, near "'main';"
241Execution of - aborted due to compilation errors.