xref: /openbsd-src/gnu/usr.bin/perl/cpan/Win32API-File/t/tie.t (revision b8851fcc53cbe24fd20b090f26dd149e353f6174)
1b39c5158Smillert#!perl
2b39c5158Smillert# vim:syntax=perl:
3b39c5158Smillert
4b39c5158SmillertBEGIN {
5b39c5158Smillert    $|= 1;
6b39c5158Smillert
7*b8851fccSafresh1    use Test::More;
8*b8851fccSafresh1
9b39c5158Smillert    # when building perl, skip this test if Win32API::File isn't being built
10b39c5158Smillert    if ( $ENV{PERL_CORE} ) {
11b39c5158Smillert        require Config;
12b39c5158Smillert        if ( $Config::Config{extensions} !~ m:(?<!\S)Win32API/File(?!\S): ) {
13*b8851fccSafresh1            plan skip_all => 'Skip Win32API::File extension not built';
14*b8851fccSafresh1            exit;
15b39c5158Smillert        }
16b39c5158Smillert    }
17b39c5158Smillert
18*b8851fccSafresh1    plan tests => 10;
19b39c5158Smillert}
20b39c5158Smillert
21b39c5158Smillertuse strict;
22b39c5158Smillertuse warnings;
23b39c5158Smillertuse Win32API::File qw(:ALL);
24b39c5158Smillertuse IO::File;
25b39c5158Smillert
26*b8851fccSafresh1my $filename = 'foo.txt';
27*b8851fccSafresh1ok(! -e $filename || unlink($filename), "unlinked $filename (if it existed)");
28b39c5158Smillert
29*b8851fccSafresh1my $fh = Win32API::File->new("+> $filename")
30b39c5158Smillert    or die fileLastError();
31b39c5158Smillert
32b39c5158Smillertmy $tell = tell $fh;
33*b8851fccSafresh1is(0+$tell, 0, "tell \$fh == '$tell'");
34b39c5158Smillert
35b39c5158Smillertmy $text = "some text\n";
36b39c5158Smillert
37*b8851fccSafresh1ok(print($fh $text), "printed 'some text\\n'");
38b39c5158Smillert
39b39c5158Smillert$tell = tell $fh;
40*b8851fccSafresh1my $len = length($text) + 1; # + 1 for cr
41*b8851fccSafresh1is($tell, $len, "after printing 'some text\\n', tell is: '$tell'");
42b39c5158Smillert
43*b8851fccSafresh1my $seek = seek($fh, 0, 0);
44*b8851fccSafresh1is(0+$seek, 0, "seek is: '$seek'");
45b39c5158Smillert
46*b8851fccSafresh1my $eof = eof $fh;
47*b8851fccSafresh1ok(! $eof, 'not eof');
48b39c5158Smillert
49b39c5158Smillertmy $readline = <$fh>;
50b39c5158Smillert
51b39c5158Smillertmy $pretty_readline = $readline;
52b39c5158Smillert$pretty_readline =~ s/\r/\\r/g;  $pretty_readline =~ s/\n/\\n/g;
53*b8851fccSafresh1is($pretty_readline, "some text\\r\\n", "read line is '$pretty_readline'");
54b39c5158Smillert
55*b8851fccSafresh1$eof = eof $fh;
56*b8851fccSafresh1ok($eof, 'reached eof');
57b39c5158Smillert
58*b8851fccSafresh1ok(close($fh), 'closed filehandle');
59b39c5158Smillert
60b39c5158Smillert# Test out binmode (should be only LF with print, no CR).
61b39c5158Smillert
62*b8851fccSafresh1$fh = Win32API::File->new("+> $filename")
63b39c5158Smillert    or die fileLastError();
64b39c5158Smillertbinmode $fh;
65b39c5158Smillertprint $fh "hello there\n";
66b39c5158Smillertseek $fh, 0, 0;
67b39c5158Smillert
68*b8851fccSafresh1$readline = <$fh>;
69*b8851fccSafresh1is($readline, "hello there\n", "binmode worked (no CR)");
70b39c5158Smillert
71b39c5158Smillertclose $fh;
72b39c5158Smillert
73*b8851fccSafresh1unlink $filename;
74