]> git.donarmstrong.com Git - perltidy.git/blob - t/test-eol.t
New upstream version 20190601
[perltidy.git] / t / test-eol.t
1 use strict;
2 use File::Temp;
3 use Test;
4 use Carp;
5 BEGIN {plan tests => 4}
6 use Perl::Tidy;
7
8
9 #----------------------------------------------------------------------
10 ## test string->string
11 #----------------------------------------------------------------------
12 my $source_template = <<'EOM';
13 %height=("letter",27.9, "legal",35.6, "arche",121.9, "archd",91.4, "archc",61,
14  "archb",45.7, "archa",30.5, "flsa",33, "flse",33, "halfletter",21.6,
15  "11x17",43.2, "ledger",27.9);
16 %width=("letter",21.6, "legal",21.6, "arche",91.4, "archd",61, "archc",45.7,
17  "archb",30.5, "archa",22.9, "flsa",21.6, "flse",21.6, "halfletter",14,
18  "11x17",27.9, "ledger",43.2);
19 EOM
20
21 my $perltidyrc;
22
23 my $expected_output_template=<<'EOM';
24 %height = (
25            "letter",     27.9, "legal", 35.6, "arche",  121.9,
26            "archd",      91.4, "archc", 61,   "archb",  45.7,
27            "archa",      30.5, "flsa",  33,   "flse",   33,
28            "halfletter", 21.6, "11x17", 43.2, "ledger", 27.9
29           );
30 %width = (
31           "letter",     21.6, "legal", 21.6, "arche",  91.4,
32           "archd",      61,   "archc", 45.7, "archb",  30.5,
33           "archa",      22.9, "flsa",  21.6, "flse",   21.6,
34           "halfletter", 14,   "11x17", 27.9, "ledger", 43.2
35          );
36 EOM
37
38 my $source;
39 my $output;
40 my $expected_output;
41
42 my $CR = chr(015);
43 my $LF = chr(012);
44
45 $perltidyrc = <<'EOM';
46 -gnu
47 --output-line-ending="unix"     # use *nix LF EOLs
48 EOM
49
50 $source = $source_template;
51 $source =~ s/\n/$CR$LF/gmsx;
52 $expected_output = $expected_output_template;
53 $expected_output =~ s/\n/$LF/gmsx;
54
55 # my ($source_fh, $source_filename) = File::Temp::tempfile(); close $source_filename;
56 my ($output_fh, $output_filename) = File::Temp::tempfile(); close $output_filename;
57
58 # print STDERR "# source_filename = ", $source_filename, "\n";
59 # print STDERR "# output_filename = ", $output_filename, "\n";
60
61 # open $source_fh, ">", $source_filename;
62 # binmode $source_fh, ":raw";
63 # print $source_fh $source;
64 # close $source_fh;
65
66 # in-memory output (non-UTF8)
67
68 Perl::Tidy::perltidy(
69     source      => \$source,
70     destination => \$output,
71     perltidyrc  => \$perltidyrc,
72     argv        => '',
73 );
74
75 ok($output, $expected_output, "in-memory EOLs (non-UTF8)");
76
77 # file output (non-UTF8)
78 Perl::Tidy::perltidy(
79     source      => \$source,
80     destination => $output_filename,
81     perltidyrc  => \$perltidyrc,
82     argv        => '',
83 );
84
85 {# slurp entire file
86     local $/ = undef;
87     open $output_fh, "<", $output_filename;
88     binmode $output_fh, ":raw";
89     $output = <$output_fh>;
90 }
91
92 ok($output, $expected_output, "output file EOLs (non-UTF8)");
93
94 $perltidyrc = <<'EOM';
95 -gnu
96 --character-encoding="utf8"     # treat files as UTF-8 (decode and encode)
97 --output-line-ending="unix"     # use *nix LF EOLs
98 EOM
99
100 # in-memory (UTF8)
101 $source = $source_template;
102 $source =~ s/\n/$CR$LF/gmsx;
103 $expected_output = $expected_output_template;
104 $expected_output =~ s/\n/$LF/gmsx;
105
106 Perl::Tidy::perltidy(
107     source      => \$source,
108     destination => \$output,
109     perltidyrc  => \$perltidyrc,
110     argv        => '',
111 );
112
113 ok($output, $expected_output, "in-memory EOLs (UTF8)");
114
115 # file output (UTF8)
116
117 Perl::Tidy::perltidy(
118     source      => \$source,
119     destination => $output_filename,
120     perltidyrc  => \$perltidyrc,
121     argv        => '',
122 );
123
124 {# slurp entire file
125     local $/ = undef;
126     open $output_fh, "<", $output_filename;
127     binmode $output_fh, ":raw";
128     $output = <$output_fh>;
129 }
130
131 ok($output, $expected_output, "output file EOLs (UTF8)");