20 #------------------------------
23 # ...as a scalar-array:
27 "Found a rather large ",
28 "mouse in his stew\n Said the waiter, \"Don't shout,\n",
33 $DATA_S = join '', @DATA_SA;
35 @DATA_LA = lines($DATA_S);
38 # ...as a scalar-array:
41 " will be wanting one ",
45 $ADATA_S = join '', @ADATA_SA;
50 $FDATA_S = $DATA_S . $ADATA_S;
52 @FDATA_LA = lines($FDATA_S);
61 my $BUF = ''; # buffer
65 #------------------------------
67 #------------------------------
73 #------------------------------
75 #------------------------------
83 #------------------------------
84 # test_print HANDLE, TEST
85 #------------------------------
86 # Test printing to handle.
90 my ($self, $GH, $all) = @_;
94 $M = "PRINT: able to print to $GH";
95 $GH->print($ADATA_SA[0]);
96 $GH->print(@ADATA_SA[1..2]);
100 #------------------------------
102 #------------------------------
107 my ($self, $GH) = @_;
111 $M = "GETC: seek(0,0) and getc()";
113 for (0..2) { $c[$_] = $GH->getc };
114 $T->ok((($c[0] eq 'A') &&
116 ($c[2] eq 'd')), $M);
119 #------------------------------
120 # test_getline HANDLE
121 #------------------------------
122 # Test getline() and getlines().
126 my ($self, $GH) = @_;
129 $M = "GETLINE/SEEK3: seek(3,START) and getline() gets part of 1st line";
131 my $got = $GH->getline;
132 my $want = "iner while dining at Crewe\n";
133 $T->ok(($got eq $want), $M,
138 $M = "GETLINE/NEXT: next getline() gets subsequent line";
140 $T->ok(($_ eq "Found a rather large mouse in his stew\n"), $M,
143 $M = "GETLINE/EOF: repeated getline() finds end of stream";
145 for (1..6) { $last = $GH->getline }
147 Last => (defined($last) ? $last : 'undef'));
149 $M = "GETLINE/GETLINES: seek(0,0) and getlines() slurps in string";
151 my @got = $GH->getlines;
152 my $gots = join '', @got;
153 $T->ok(($gots eq $FDATA_S), $M,
159 #------------------------------
161 #------------------------------
166 my ($self, $GH) = @_;
169 $M = "READ/FIRST10: reading first 10 bytes with seek(0,START) + read(10)";
172 $T->ok(($BUF eq "A diner wh"), $M);
174 $M = "READ/NEXT10: reading next 10 bytes with read(10)";
176 $T->ok(($BUF eq "ile dining"), $M);
178 $M = "READ/TELL20: tell() the current location as 20";
179 $T->ok(($GH->tell == 20), $M);
181 $M = "READ/SLURP: seek(0,START) + read(1000) reads in whole handle";
183 $GH->read($BUF,1000);
184 $T->ok(($BUF eq $FDATA_S), $M);
187 #------------------------------
189 #------------------------------
190 # Test seeks other than (0,0).
194 my ($self, $GH) = @_;
197 $M = "SEEK/SET: seek(2,SET) + read(5) returns 'diner'";
200 $T->ok_eq($BUF, 'diner',
203 $M = "SEEK/END: seek(-6,END) + read(3) returns 'too'";
206 $T->ok_eq($BUF, 'too',
209 $M = "SEEK/CUR: seek(-7,CUR) + read(7) returns 'one too'";
212 $T->ok_eq($BUF, 'one too',
216 #------------------------------
218 #------------------------------
219 # Test tiehandle getline() interface.
224 my ($tieclass, @tieargs) = @{$p{TieArgs}};
230 $M = "TIE/TIE: able to tie";
231 tie(*OUT, $tieclass, @tieargs);
233 TieClass => $tieclass,
234 TieArgs => \@tieargs);
236 $M = "TIE/PRINT: printing data";
238 print OUT $ADATA_SA[0];
239 print OUT @ADATA_SA[1..2];
242 $M = "TIE/GETLINE: seek(0,0) and scalar <> get expected lines";
243 tied(*OUT)->seek(0,0); # rewind
244 @lines = (); push @lines, $_ while <OUT>; # get lines one at a time
245 $nmatched = 0; # total up matches...
246 for (0..$#lines) { ++$nmatched if ($lines[$_] eq $FDATA_LA[$_]) };
247 $T->ok(($nmatched == int(@FDATA_LA)), $M,
250 Lines=> "0..$#lines",
252 FDatl=> int(@FDATA_LA),
255 $M = "TIE/GETLINES: seek(0,0) and array <> slurps in lines";
256 tied(*OUT)->seek(0,0); # rewind
257 @lines = <OUT>; # get lines all at once
258 $nmatched = 0; # total up matches...
259 for (0..$#lines) { ++$nmatched if ($lines[$_] eq $FDATA_LA[$_]) };
260 $T->ok(($nmatched == int(@FDATA_LA)), $M,
263 Lines=> "0..$#lines",
266 # $M = "TIE/TELL: telling data";
267 # my $tell_oo = tied(*OUT)->tell;
268 # my $tell_tie = tell OUT;
269 # $T->ok(($tell_oo == $tell_tie), $M,
271 # Gotl => $tell_tie);
275 #------------------------------
277 #------------------------------
285 sub test_recordsep_count {
286 my ($self, $seps) = @_;
288 $count += 3 if ($seps =~ /undef/) ;
289 $count += 3 if ($seps =~ /empty/) ;
290 $count += 2 if ($seps =~ /custom/) ;
291 $count += 11 if ($seps =~ /newline/);
295 my ($self, $seps, $opener) = @_;
297 my @lines = ("par 1, line 1\n",
308 my $all = join('', @lines);
310 ### Slurp everything:
311 if ($seps =~ /undef/) {
312 $GH = &$opener(\@lines);
314 $T->ok_eq($GH->getline, $all,
315 "RECORDSEP undef: getline slurps everything");
318 ### Read a little, slurp the rest:
319 if ($seps =~ /undef/) {
320 $GH = &$opener(\@lines);
321 $T->ok_eq($GH->getline, $lines[0],
322 "RECORDSEP undef: get first line");
324 $T->ok_eq($GH->getline, join('', @lines[1..$#lines]),
325 "RECORDSEP undef: slurp the rest");
328 ### Read paragraph by paragraph:
329 if ($seps =~ /empty/) {
330 $GH = &$opener(\@lines);
332 $T->ok_eq($GH->getline, join('', @lines[0..2]),
333 "RECORDSEP empty: first par");
334 $T->ok_eq($GH->getline, join('', @lines[6..7]),
335 "RECORDSEP empty: second par");
336 $T->ok_eq($GH->getline, join('', @lines[8..10]),
337 "RECORDSEP empty: third par");
340 ### Read record by record:
341 if ($seps =~ /custom/) {
342 $GH = &$opener(\@lines);
344 $T->ok_eq($GH->getline, "par 1,",
345 "RECORDSEP custom: first rec");
346 $T->ok_eq($GH->getline, " line 1\npar 1,",
347 "RECORDSEP custom: second rec");
350 ### Read line by line:
351 if ($seps =~ /newline/) {
352 $GH = &$opener(\@lines);
355 $T->ok_eq($GH->getline, $lines[$i],
356 "RECORDSEP newline: rec $i");
362 #------------------------------