This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get t/uni/cache.t working under minitest
[perl5.git] / t / op / tiehandle.t
CommitLineData
94c40caf 1#!./perl -w
1d603a67
GB
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
1d603a67
GB
6}
7
8my @expect;
9my $data = "";
10my @data = ();
1d603a67 11
94c40caf 12require './test.pl';
0861ec18 13plan(tests => 67);
1d603a67
GB
14
15sub compare {
3a28f3fb
MS
16 local $Level = $Level + 1;
17
1d603a67 18 return unless @expect;
94c40caf 19 return ::fail() unless(@_ == @expect);
1d603a67 20
94c40caf 21 for my $i (0..$#_) {
1d603a67 22 next if $_[$i] eq $expect[$i];
94c40caf 23 return ::fail();
1d603a67
GB
24 }
25
94c40caf 26 ::pass();
1d603a67
GB
27}
28
94c40caf
JH
29
30package Implement;
31
1d603a67 32sub TIEHANDLE {
94c40caf 33 ::compare(TIEHANDLE => @_);
1d603a67
GB
34 my ($class,@val) = @_;
35 return bless \@val,$class;
36}
37
38sub PRINT {
94c40caf 39 ::compare(PRINT => @_);
1d603a67
GB
40 1;
41}
42
43sub PRINTF {
94c40caf 44 ::compare(PRINTF => @_);
1d603a67
GB
45 2;
46}
47
48sub READLINE {
94c40caf 49 ::compare(READLINE => @_);
1d603a67
GB
50 wantarray ? @data : shift @data;
51}
52
53sub GETC {
94c40caf 54 ::compare(GETC => @_);
1d603a67
GB
55 substr($data,0,1);
56}
57
58sub READ {
94c40caf 59 ::compare(READ => @_);
1d603a67
GB
60 substr($_[1],$_[3] || 0) = substr($data,0,$_[2]);
61 3;
62}
63
32e65323
CS
64sub EOF {
65 ::compare(EOF => @_);
66 @data ? '' : 1;
67}
68
1d603a67 69sub WRITE {
94c40caf 70 ::compare(WRITE => @_);
1d603a67 71 $data = substr($_[1],$_[3] || 0, $_[2]);
145d37e2 72 length($data);
1d603a67
GB
73}
74
75sub CLOSE {
94c40caf 76 ::compare(CLOSE => @_);
1d603a67
GB
77 5;
78}
79
80package main;
81
82use Symbol;
83
1d603a67
GB
84my $fh = gensym;
85
86@expect = (TIEHANDLE => 'Implement');
87my $ob = tie *$fh,'Implement';
94c40caf
JH
88is(ref($ob), 'Implement');
89is(tied(*$fh), $ob);
1d603a67
GB
90
91@expect = (PRINT => $ob,"some","text");
92$r = print $fh @expect[2,3];
94c40caf 93is($r, 1);
1d603a67
GB
94
95@expect = (PRINTF => $ob,"%s","text");
96$r = printf $fh @expect[2,3];
94c40caf 97is($r, 2);
1d603a67 98
32e65323
CS
99@data = ("the line\n");
100@expect = (EOF => $ob, 1);
101is(eof($fh), '');
102
103$text = $data[0];
1d603a67
GB
104@expect = (READLINE => $ob);
105$ln = <$fh>;
94c40caf 106is($ln, $text);
1d603a67 107
32e65323
CS
108@expect = (EOF => $ob, 0);
109is(eof, 1);
110
1d603a67
GB
111@expect = ();
112@in = @data = qw(a line at a time);
113@line = <$fh>;
114@expect = @in;
94c40caf 115compare(@line);
1d603a67
GB
116
117@expect = (GETC => $ob);
118$data = "abc";
119$ch = getc $fh;
94c40caf 120is($ch, "a");
1d603a67
GB
121
122$buf = "xyz";
123@expect = (READ => $ob, $buf, 3);
124$data = "abc";
125$r = read $fh,$buf,3;
94c40caf
JH
126is($r, 3);
127is($buf, "abc");
1d603a67
GB
128
129
130$buf = "xyzasd";
131@expect = (READ => $ob, $buf, 3,3);
132$data = "abc";
133$r = sysread $fh,$buf,3,3;
94c40caf
JH
134is($r, 3);
135is($buf, "xyzabc");
1d603a67
GB
136
137$buf = "qwerty";
138@expect = (WRITE => $ob, $buf, 4,1);
139$data = "";
140$r = syswrite $fh,$buf,4,1;
94c40caf
JH
141is($r, 4);
142is($data, "wert");
1d603a67 143
145d37e2
GA
144$buf = "qwerty";
145@expect = (WRITE => $ob, $buf, 4);
146$data = "";
147$r = syswrite $fh,$buf,4;
94c40caf
JH
148is($r, 4);
149is($data, "qwer");
145d37e2
GA
150
151$buf = "qwerty";
152@expect = (WRITE => $ob, $buf, 6);
153$data = "";
154$r = syswrite $fh,$buf;
94c40caf
JH
155is($r, 6);
156is($data, "qwerty");
145d37e2 157
1d603a67
GB
158@expect = (CLOSE => $ob);
159$r = close $fh;
94c40caf 160is($r, 5);
01bb7c6d
DC
161
162# Does aliasing work with tied FHs?
163*ALIAS = *$fh;
164@expect = (PRINT => $ob,"some","text");
165$r = print ALIAS @expect[2,3];
94c40caf 166is($r, 1);
01bb7c6d
DC
167
168{
169 use warnings;
170 # Special case of aliasing STDERR, which used
171 # to dump core when warnings were enabled
87582a92 172 local *STDERR = *$fh;
01bb7c6d
DC
173 @expect = (PRINT => $ob,"some","text");
174 $r = print STDERR @expect[2,3];
94c40caf 175 is($r, 1);
01bb7c6d 176}
df646e84
JH
177
178{
3a28f3fb
MS
179 package Bar::Say;
180 use feature 'say';
181 use base qw(Implement);
182
183 my $ors;
184 sub PRINT {
185 $ors = $\;
186 my $self = shift;
187 return $self->SUPER::PRINT(@_);
188 }
189
190 my $fh = Symbol::gensym;
191 @expect = (TIEHANDLE => 'Bar::Say');
192 ::ok( my $obj = tie *$fh, 'Bar::Say' );
193
194 local $\ = 'something';
195 @expect = (PRINT => $obj, "stuff", "and", "things");
196 ::ok( print $fh @expect[2..4] );
197 ::is( $ors, 'something' );
198
199 ::ok( say $fh @expect[2..4] );
200 ::is( $ors, "\n", 'say sets $\ to \n in PRINT' );
201 ::is( $\, "something", " and it's localized" );
0861ec18
RS
202
203 local $\;
204 ::ok( say $fh @expect[2..4] );
205 ::is( $ors, "\n", 'say sets $\ to \n in PRINT' );
206 ::is( $\, undef, " and it's localized, even for undef \$\\" );
3a28f3fb
MS
207}
208
209{
df646e84
JH
210 # Test for change #11536
211 package Foo;
212 use strict;
213 sub TIEHANDLE { bless {} }
214 my $cnt = 'a';
215 sub READ {
216 $_[1] = $cnt++;
217 1;
218 }
219 sub do_read {
220 my $fh = shift;
221 read $fh, my $buff, 1;
94c40caf 222 ::pass();
df646e84
JH
223 }
224 $|=1;
225 tie *STDIN, 'Foo';
226 read STDIN, my $buff, 1;
94c40caf 227 ::pass();
df646e84
JH
228 do_read(\*STDIN);
229 untie *STDIN;
230}
231
4ba0502e
JH
232
233{
234 # test for change 11639: Can't localize *FH, then tie it
235 {
236 local *foo;
237 tie %foo, 'Blah';
238 }
239 ok(!tied %foo);
240
241 {
242 local *bar;
243 tie @bar, 'Blah';
244 }
245 ok(!tied @bar);
246
247 {
248 local *BAZ;
249 tie *BAZ, 'Blah';
250 }
251 ok(!tied *BAZ);
252
253 package Blah;
254
255 sub TIEHANDLE {bless {}}
256 sub TIEHASH {bless {}}
257 sub TIEARRAY {bless {}}
258}
259
87582a92
AT
260{
261 # warnings should pass to the PRINT method of tied STDERR
262 my @received;
263
264 local *STDERR = *$fh;
94c40caf 265 no warnings 'redefine';
87582a92
AT
266 local *Implement::PRINT = sub { @received = @_ };
267
268 $r = warn("some", "text", "\n");
269 @expect = (PRINT => $ob,"sometext\n");
270
94c40caf 271 compare(PRINT => @received);
7ff03255
SG
272
273 use warnings;
274 print undef;
275
94c40caf 276 like($received[1], qr/Use of uninitialized value/);
87582a92
AT
277}
278
0b7c7b4f
HS
279{
280 # [ID 20020713.001] chomp($data=<tied_fh>)
281 local *TEST;
282 tie *TEST, 'CHOMP';
283 my $data;
284 chomp($data = <TEST>);
94c40caf 285 is($data, 'foobar');
0b7c7b4f
HS
286
287 package CHOMP;
288 sub TIEHANDLE { bless {}, $_[0] }
289 sub READLINE { "foobar\n" }
290}
94c40caf 291
32e65323
CS
292{
293 # make sure the new eof() features work with @ARGV magic
294 local *ARGV;
295 @ARGV = ('haha');
296
297 @expect = (TIEHANDLE => 'Implement');
298 $ob = tie *ARGV, 'Implement';
299 is(ref($ob), 'Implement');
300 is(tied(*ARGV), $ob);
301
302 @data = ("stuff\n");
303 @expect = (EOF => $ob, 1);
304 is(eof(ARGV), '');
305 @expect = (EOF => $ob, 2);
306 is(eof(), '');
307 shift @data;
308 @expect = (EOF => $ob, 0);
309 is(eof, 1);
310}