xref: /openbsd/gnu/usr.bin/perl/dist/Tie-File/t/21_win32.t (revision d89ec533)
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6#
7# Formerly, on a Win32 system, Tie::File would create files with
8# \n-terminated records instead of \r\n-terminated.  The tests never
9# picked this up because they were using $/ everywhere, and $/ is \n
10# on windows systems.
11#
12# These tests (Win32 only) make sure that the file had \r\n as it should.
13
14my $file = "tf21-$$.txt";
15
16unless ($^O =~ /^(MSWin32|dos)$/) {
17  print "1..0\n";
18  exit;
19}
20
21
22print "1..3\n";
23
24my $N = 1;
25use Tie::File;
26print "ok $N\n"; $N++;
27
28my @a;
29my $o = tie @a, 'Tie::File', $file, autodefer => 0;
30print $o ? "ok $N\n" : "not ok $N\n";
31$N++;
32
33my $n;
34
35# (3) Make sure that on Win32 systems, the file is written with \r\n by default
36@a = qw(fish dog carrot);
37undef $o;
38untie @a;
39open F, '<', $file or die "Couldn't open file $file: $!";
40binmode F;
41my $a = do {local $/ ; <F> };
42my $x = "fish\r\ndog\r\ncarrot\r\n" ;
43if ($a eq $x) {
44  print "ok $N\n";
45} else {
46  ctrlfix(my $msg = "expected <$x>, got <$a>");
47  print "not ok $N # $msg\n";
48}
49
50close F;
51
52sub ctrlfix {
53  for (@_) {
54    s/\n/\\n/g;
55    s/\r/\\r/g;
56  }
57}
58
59
60
61END {
62  undef $o;
63  untie @a;
64  1 while unlink $file;
65}
66
67