e981b951aab3a9f09f54903def2bd3ecb11fb41b
[p5sagit/p5-mst-13.2.git] / t / lib / io_linenum.t
1 #!./perl
2
3 # test added 29th April 1999 by Paul Johnson (pjcj@transeda.com)
4 # updated    28th May   1999 by Paul Johnson
5
6 use strict;
7
8 my $File;
9
10 BEGIN
11 {
12   $File = __FILE__;
13   if (-d 't')
14   {
15     chdir 't';
16     $File =~ s/^t\W+//;                                 # Remove first directory
17   }
18   unshift @INC, '../lib' if -d '../lib';
19 }
20
21 use Test;
22
23 BEGIN { plan tests => 12 }
24
25 use IO::File;
26
27 sub lineno
28 {
29   my ($f) = @_;
30   my $l;
31   $l .= "$. ";
32   $l .= $f->input_line_number;
33   $l .= " $.";                     # check $. before and after input_line_number
34   $l;
35 }
36
37 my $t;
38
39 open (F, $File) or die $!;
40 my $io = IO::File->new($File) or die $!;
41
42 <F> for (1 .. 10);
43 ok(lineno($io), "10 0 10");
44
45 $io->getline for (1 .. 5);
46 ok(lineno($io), "5 5 5");
47
48 <F>;
49 ok(lineno($io), "11 5 11");
50
51 $io->getline;
52 ok(lineno($io), "6 6 6");
53
54 $t = tell F;                                        # tell F; provokes a warning
55 ok(lineno($io), "11 6 11");
56
57 <F>;
58 ok(lineno($io), "12 6 12");
59
60 select F;
61 ok(lineno($io), "12 6 12");
62
63 <F> for (1 .. 10);
64 ok(lineno($io), "22 6 22");
65
66 $io->getline for (1 .. 5);
67 ok(lineno($io), "11 11 11");
68
69 $t = tell F;
70 # We used to have problems here before local $. worked.
71 # input_line_number() used to use select and tell.  When we did the
72 # same, that mechanism broke.  It should work now.
73 ok(lineno($io), "22 11 22");
74
75 {
76   local $.;
77   $io->getline for (1 .. 5);
78   ok(lineno($io), "16 16 16");
79 }
80
81 ok(lineno($io), "22 16 22");