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