]> git.donarmstrong.com Git - perltidy.git/blob - t/testwide-tidy.t
New upstream version 20230309
[perltidy.git] / t / testwide-tidy.t
1 use strict;
2 use warnings;
3 use utf8;
4
5 use FindBin qw($Bin);
6 use File::Temp qw(tempfile);
7 use Test::More;
8
9 BEGIN { unshift @INC, "./" }
10 use Perl::Tidy;
11
12 # This tests the -eos (--encode-output-strings) which was added for issue
13 # git #83 to fix an issue with tidyall.
14
15 # NOTE: to prevent automatic conversion of line endings LF to CRLF under github
16 # Actions with Windows, which would cause test failure, it is essential that
17 # there be a file 't/.gitattributes' with the line:
18 # * -text
19
20 # The test file is UTF-8 encoded
21
22 plan( tests => 6 );
23
24 test_all();
25
26 sub my_note {
27     my ($msg) = @_;
28
29     # work around problem where sub Test::More::note does not exist
30     # in older versions of perl
31     if ($] >= 5.010) {
32        note($msg);
33     }
34     return;
35 }
36
37 sub test_all {
38     my $test_file = "$Bin/testwide-tidy.pl.src";
39     my $tidy_file = "$Bin/testwide-tidy.pl.srctdy";
40     my $tidy_str  = slurp_raw($tidy_file);
41     test_file2file( $test_file, $tidy_str );
42     test_scalar2scalar( $test_file, $tidy_str );
43     test_scalararray2scalararray( $test_file, $tidy_str );
44 }
45
46 sub test_file2file {
47     my $test_file = shift;
48     my $tidy_str  = shift;
49     my $tidy_hex  = unpack( 'H*', $tidy_str );
50
51     my $tmp_file = File::Temp->new( TMPDIR => 1 );
52
53     my $source      = $test_file;
54     my $destination = $tmp_file->filename();
55
56     my_note("Testing file2file: '$source' => '$destination'\n");
57
58     my $tidyresult = Perl::Tidy::perltidy(
59         argv        => '-utf8 -npro',
60         source      => $source,
61         destination => $destination
62     );
63     ok( !$tidyresult, 'perltidy' );
64
65     my $destination_str = slurp_raw($destination);
66     my $destination_hex = unpack( 'H*', $destination_str );
67
68     my_note("Comparing contents:\n  $tidy_hex\n  $destination_hex\n");
69     ok($tidy_hex eq $destination_hex, 'file content compare');
70
71 }
72
73 sub test_scalar2scalar {
74     my $test_file = shift;
75     my $tidy_str  = shift;
76     my $tidy_hex  = unpack( 'H*', $tidy_str );
77
78     my $source = slurp_raw($test_file);
79     my $destination;
80
81     my_note("Testing scalar2scalar\n");
82
83     my $tidyresult = Perl::Tidy::perltidy(
84         argv        => '-utf8 -eos -npro',
85         source      => \$source,
86         destination => \$destination
87     );
88     ok( !$tidyresult, 'perltidy' );
89
90     my $destination_hex = unpack( 'H*', $destination );
91
92     my_note("Comparing contents:\n  $tidy_hex\n  $destination_hex\n");
93     ok($tidy_hex eq $destination_hex, 'scalar content compare');
94
95 }
96
97 sub test_scalararray2scalararray {
98     my $test_file = shift;
99     my $tidy_str  = shift;
100     my $tidy_hex  = unpack( 'H*', $tidy_str );
101
102     my $source      = [ lines_raw($test_file) ];
103     my $destination = [];
104
105     my_note("Testing scalararray2scalararray\n");
106
107     my $tidyresult = Perl::Tidy::perltidy(
108         argv        => '-utf8 -eos -npro',
109         source      => $source,
110         destination => $destination
111     );
112     ok( !$tidyresult, 'perltidy' );
113
114     my $destination_str = join( '', @$destination );
115     my $destination_hex = unpack( 'H*', $destination_str );
116
117     my_note("Comparing contents:\n  $tidy_hex\n  $destination_hex\n");
118     ok($tidy_hex eq $destination_hex, 'scalararray content compare');
119 }
120
121 sub slurp_raw {
122     my $filename = shift;
123
124     open( TMP, '<', $filename );
125     binmode( TMP, ':raw' );
126     local $/;
127     my $contents = <TMP>;
128     close(TMP);
129
130     return $contents;
131 }
132
133 sub lines_raw {
134     my $filename = shift;
135
136     open( TMP, '<', $filename );
137     binmode( TMP, ':raw' );
138     my @contents = <TMP>;
139     close(TMP);
140
141     return @contents;
142 }