]> git.donarmstrong.com Git - perltidy.git/blob - t/testwide.t
New upstream version 20220613
[perltidy.git] / t / testwide.t
1 use strict;
2 use utf8;
3 use Test;
4 use Carp;
5 use FindBin;
6 BEGIN { unshift @INC, "./" }
7 BEGIN { plan tests => 3 }
8 use Perl::Tidy;
9
10 my $source = <<'EOM';
11 %pangrams=("Plain","ASCII",
12 "Zwölf große Boxkämpfer jagen Vik quer über den Sylter.","DE",
13 "Jeż wlókł gęś. Uf! Bądź choć przy nim, stań!","PL",
14 "Любя, съешь щипцы, — вздохнёт мэр, — кайф жгуч.","RU");
15 EOM
16
17 my $expected_output = <<'EOM';
18 %pangrams = (
19              "Plain",                                                  "ASCII",
20              "Zwölf große Boxkämpfer jagen Vik quer über den Sylter.", "DE",
21              "Jeż wlókł gęś. Uf! Bądź choć przy nim, stań!",           "PL",
22              "Любя, съешь щипцы, — вздохнёт мэр, — кайф жгуч.",        "RU"
23             );
24 EOM
25
26 my $perltidyrc = <<'EOM';
27 -gnu -enc=utf8
28 EOM
29
30 my $output;
31
32 # The source is in character mode here, so perltidy will not decode.
33 # So here we do not need to set -eos or -neos
34 Perl::Tidy::perltidy(
35     source      => \$source,
36     destination => \$output,
37     perltidyrc  => \$perltidyrc,
38     argv        => '-nsyn',
39 );
40
41 ok( $output, $expected_output );
42
43 Perl::Tidy::perltidy(
44     source      => $FindBin::Bin . '/testwide.pl.src',
45     destination => \$output,
46     perltidyrc  => \$perltidyrc,
47     argv        => '-nsyn',
48 );
49
50 # We have to be careful here ...  In this test we are comparing $output to a
51 # source string which is in character mode (since it is in this file declared
52 # with 'use utf8'). We need to compare strings which have the same storage
53 # mode.
54
55 # The internal storage mode of $output was character mode (decoded) for
56 # vesions prior to 20220217.02, but is byte mode (encoded) for the latest
57 # version of perltidy.
58
59 # The following statement will decode $output if it is stored in byte mode,
60 # and leave it unchanged (and return an error) otherwise.  So this will work
61 # with all version of perltidy.  See https://perldoc.perl.org/utf8
62 utf8::decode($output);
63
64 ok( $output, $expected_output );
65
66 # Test writing encoded output to stdout with the -st flag
67 # References: RT #133166, RT #133171, git #35
68 $output = "";
69 do {
70
71     # Send STDOUT to a temporary file
72     use File::Temp ();
73     my $fh      = new File::Temp();
74     my $tmpfile = $fh->filename;
75
76     # Note that we are not specifying an encoding here. Perltidy should do that.
77     local *STDOUT;
78     open STDOUT, '>', $tmpfile or die "Can't open tmpfile: $!";
79
80     Perl::Tidy::perltidy(
81         source => \$source,
82         ##destination => ... we are using -st, so no destination is specified
83         perltidyrc => \$perltidyrc,
84         argv       => '-nsyn -st',    # added -st
85     );
86     close STDOUT;
87
88     # Read the temporary file back in. Note that here we need to specify
89     # the encoding.
90     open TMP, '<', $tmpfile;
91     binmode TMP, ":raw:encoding(UTF-8)";
92     while ( my $line = <TMP> ) { $output .= $line }
93 };
94
95 ok( $output, $expected_output );
96