]> git.donarmstrong.com Git - perltidy.git/blob - t/testwide-tidy.t.SKIP
New upstream version 20220217
[perltidy.git] / t / testwide-tidy.t.SKIP
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 test_all {
27     my $test_file = "$Bin/testwide-tidy.pl.src";
28     my $tidy_file = "$Bin/testwide-tidy.pl.srctdy";
29     my $tidy_str  = slurp_raw($tidy_file);
30     test_file2file( $test_file, $tidy_str );
31     test_scalar2scalar( $test_file, $tidy_str );
32     test_scalararray2scalararray( $test_file, $tidy_str );
33 }
34
35 sub test_file2file {
36     my $test_file = shift;
37     my $tidy_str  = shift;
38     my $tidy_hex  = unpack( 'H*', $tidy_str );
39
40     my $tmp_file = File::Temp->new( TMPDIR => 1 );
41
42     my $source      = $test_file;
43     my $destination = $tmp_file->filename();
44
45     note("Testing file2file: '$source' => '$destination'\n");
46
47     my $tidyresult = Perl::Tidy::perltidy(
48         argv        => '-utf8',
49         source      => $source,
50         destination => $destination
51     );
52     ok( !$tidyresult, 'perltidy' );
53
54     my $destination_str = slurp_raw($destination);
55     my $destination_hex = unpack( 'H*', $destination_str );
56
57     note("Comparing contents:\n  $tidy_hex\n  $destination_hex\n");
58     ok($tidy_hex eq $destination_hex, 'file content compare');
59
60 }
61
62 sub test_scalar2scalar {
63     my $test_file = shift;
64     my $tidy_str  = shift;
65     my $tidy_hex  = unpack( 'H*', $tidy_str );
66
67     my $source = slurp_raw($test_file);
68     my $destination;
69
70     note("Testing scalar2scalar\n");
71
72     my $tidyresult = Perl::Tidy::perltidy(
73         argv        => '-utf8 -eos',
74         source      => \$source,
75         destination => \$destination
76     );
77     ok( !$tidyresult, 'perltidy' );
78
79     my $destination_hex = unpack( 'H*', $destination );
80
81     note("Comparing contents:\n  $tidy_hex\n  $destination_hex\n");
82     ok($tidy_hex eq $destination_hex, 'scalar content compare');
83
84 }
85
86 sub test_scalararray2scalararray {
87     my $test_file = shift;
88     my $tidy_str  = shift;
89     my $tidy_hex  = unpack( 'H*', $tidy_str );
90
91     my $source      = [ lines_raw($test_file) ];
92     my $destination = [];
93
94     note("Testing scalararray2scalararray\n");
95
96     my $tidyresult = Perl::Tidy::perltidy(
97         argv        => '-utf8 -eos',
98         source      => $source,
99         destination => $destination
100     );
101     ok( !$tidyresult, 'perltidy' );
102
103     my $destination_str = join( '', @$destination );
104     my $destination_hex = unpack( 'H*', $destination_str );
105
106     note("Comparing contents:\n  $tidy_hex\n  $destination_hex\n");
107     ok($tidy_hex eq $destination_hex, 'scalararray content compare');
108 }
109
110 sub slurp_raw {
111     my $filename = shift;
112
113     open( TMP, '<', $filename );
114     binmode( TMP, ':raw' );
115     local $/;
116     my $contents = <TMP>;
117     close(TMP);
118
119     return $contents;
120 }
121
122 sub lines_raw {
123     my $filename = shift;
124
125     open( TMP, '<', $filename );
126     binmode( TMP, ':raw' );
127     my @contents = <TMP>;
128     close(TMP);
129
130     return @contents;
131 }