File: 29-encoding.t

package info (click to toggle)
libfile-loadlines-perl 1.047-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 204 kB
  • sloc: perl: 545; makefile: 2
file content (59 lines) | stat: -rw-r--r-- 1,513 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#! perl

# Test non Latin filenames from file.

use strict;
use warnings;
use Test::More tests => 4;
use utf8;
use Encode qw(encode_utf8 encode);
use File::LoadLines;
binmode( STDERR, ':utf8' );

# The file.
my $filename = "testň.dat";
# And its contents.
my $reftext = "Hi There!";

-d "t" && chdir "t";

# We explicitly create the file, since this name is not portable and
# treated badly by archivers and unpackers.

if ( $^O =~ /mswin/i ) {
    require Win32API::File;
    my $fn = encode('UTF-16LE', "$filename").chr(0).chr(0);

    # Sometimes CREATE_ALWAYS barfs.
    Win32API::File::DeleteFileW($fn);

    # Create the file.
    my $fh = Win32API::File::CreateFileW
      ( $fn, Win32API::File::GENERIC_WRITE(), 0, [],
	Win32API::File::CREATE_ALWAYS(), 0, []);
    die("$filename: $^E (Win32)\n") if $^E;

    # Get handle and store contents.
    Win32API::File::OsFHandleOpen( 'FILE', $fh, "w")
	or die("$filename: $!\n");
    print FILE ( $reftext, "\n" );
    close(FILE);
}
else {
    open( my $fh, '>', encode_utf8($filename) );
    die("$filename: $!\n") unless $fh;
    print $fh ( $reftext, "\n" );
    close($fh);
}

my $options = {};
my @lines = loadlines( "testW.dat", $options );
is( $options->{encoding}, "UTF-8", "returned encoding" );
is( $lines[0], $filename, "correct data" );

$options = {};
@lines = loadlines( encode_utf8($lines[0]), $options );
is( $options->{encoding}, "ASCII", "returned encoding 2" );
is( $lines[0], $reftext, "correct data 2" );

unlink(encode_utf8($filename));