1#!/usr/bin/perl
2
3# 2003-04-09 Tels: test the offset method from 0.94
4
5use strict;
6use warnings;
7
8use Test::More;
9use File::Spec;
10
11use POSIX 'SEEK_SET';
12my $file = "tf42-$$.txt";
13
14BEGIN
15  {
16  $| = 1;
17  unshift @INC, File::Spec->catdir(File::Spec->updir, 'lib');
18  chdir 't' if -d 't';
19  print "# INC = @INC\n";
20
21  plan tests => 24;
22
23  use_ok ('Tie::File');
24  }
25
26$/ = "#";	# avoid problems with \n\r vs. \n
27
28my @a;
29my $o = tie @a, 'Tie::File', $file, autodefer => 0;
30
31is (ref($o), 'Tie::File');
32
33is ($o->offset(0), 0, 'first one always there');
34is ($o->offset(1), undef, 'no offsets yet');
35
36$a[0] = 'Bourbon';
37is ($o->offset(0), 0, 'first is ok');
38is ($o->offset(1), 8, 'and second ok');
39is ($o->offset(2), undef, 'third undef');
40
41$a[1] = 'makes';
42is ($o->offset(0), 0, 'first is ok');
43is ($o->offset(1), 8, 'and second ok');
44is ($o->offset(2), 14, 'and third ok');
45is ($o->offset(3), undef, 'fourth undef');
46
47$a[2] = 'the baby';
48is ($o->offset(0), 0, 'first is ok');
49is ($o->offset(1), 8, 'and second ok');
50is ($o->offset(2), 14, 'and third ok');
51is ($o->offset(3), 23, 'and fourth ok');
52is ($o->offset(4), undef, 'fourth undef');
53
54$a[3] = 'grin';
55is ($o->offset(0), 0, 'first is ok');
56is ($o->offset(1), 8, 'and second ok');
57is ($o->offset(2), 14, 'and third ok');
58is ($o->offset(3), 23, 'and fourth ok');
59is ($o->offset(4), 28, 'and fifth ok');
60
61$a[4] = '!';
62is ($o->offset(5), 30, 'and fifth ok');
63$a[3] = 'water';
64is ($o->offset(4), 29, 'and fourth changed ok');
65is ($o->offset(5), 31, 'and fifth ok');
66
67END {
68  undef $o;
69  untie @a;
70  1 while unlink $file;
71}
72