]> git.donarmstrong.com Git - perltidy.git/blob - t/testwide-passthrough.t
643023d393ee12464d6a70e9c28bd3896a2750c4
[perltidy.git] / t / testwide-passthrough.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 has no tidying needs but is UTF-8 encoded, so all passes
21 # through perltidy should read/write identical contents (previously only
22 # file test behaved correctly)
23
24 plan( tests => 6 );
25
26 test_all();
27
28 sub test_all {
29     my $test_file = "$Bin/testwide-passthrough.pl.src";
30     test_file2file($test_file);
31     test_scalar2scalar($test_file);
32     test_scalararray2scalararray($test_file);
33 }
34
35 sub test_file2file {
36     my $test_file = shift;
37
38     my $tmp_file = File::Temp->new( TMPDIR => 1 );
39
40     my $source      = $test_file;
41     my $destination = $tmp_file->filename();
42
43     note("Testing file2file: '$source' => '$destination'\n");
44
45     my $tidyresult = Perl::Tidy::perltidy(
46         argv        => '-utf8 -npro',
47         source      => $source,
48         destination => $destination
49     );
50     ok( !$tidyresult, 'perltidy' );
51
52     my $source_str      = slurp_raw($source);
53     my $destination_str = slurp_raw($destination);
54
55     my $source_hex      = unpack( 'H*', $source_str );
56     my $destination_hex = unpack( 'H*', $destination_str );
57     note("Comparing contents:\n  $source_hex\n  $destination_hex\n");
58
59     ok( $source_hex eq $destination_hex, 'file content compare' );
60 }
61
62 sub test_scalar2scalar {
63     my $testfile = shift;
64
65     my $source = slurp_raw($testfile);
66     my $destination;
67
68     note("Testing scalar2scalar\n");
69
70     my $tidyresult = Perl::Tidy::perltidy(
71         argv        => '-utf8 -eos -npro',
72         source      => \$source,
73         destination => \$destination
74     );
75     ok( !$tidyresult, 'perltidy' );
76
77     my $source_hex      = unpack( 'H*', $source );
78     my $destination_hex = unpack( 'H*', $destination );
79
80     note("Comparing contents:\n  $source_hex\n  $destination_hex\n");
81     ok( $source_hex eq $destination_hex, 'scalar content compare' );
82 }
83
84 sub test_scalararray2scalararray {
85     my $testfile = shift;
86
87     my $source      = [ lines_raw($testfile) ];
88     my $destination = [];
89
90     note("Testing scalararray2scalararray\n");
91
92     my $tidyresult = Perl::Tidy::perltidy(
93         argv        => '-utf8 -eos -npro',
94         source      => $source,
95         destination => $destination
96     );
97     ok( !$tidyresult, 'perltidy' );
98
99     my $source_str      = join( "", @$source );
100     my $destination_str = join( "", @$destination );
101
102     my $source_hex      = unpack( 'H*', $source_str );
103     my $destination_hex = unpack( 'H*', $destination_str );
104
105     note("Comparing contents:\n  $source_hex\n  $destination_hex\n");
106     ok( $source_hex eq $destination_hex, 'scalararray content compare' );
107 }
108
109 sub slurp_raw {
110     my $filename = shift;
111
112     open( TMP, '<', $filename );
113     binmode( TMP, ':raw' );
114     local $/;
115     my $contents = <TMP>;
116     close(TMP);
117
118     return $contents;
119 }
120
121 sub lines_raw {
122     my $filename = shift;
123
124     open( TMP, '<', $filename );
125     binmode( TMP, ':raw' );
126     my @contents = <TMP>;
127     close(TMP);
128
129     return @contents;
130 }