From: Kenneth Olwing Date: Tue, 25 Jan 2022 11:30:32 +0000 (+0100) Subject: New test files for various UTF-8 scenarios requiring the -eos flag X-Git-Tag: 20211029.06~16^2 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=79370aeb7c76dce9cc47269492fdf0c205be4825;p=perltidy.git New test files for various UTF-8 scenarios requiring the -eos flag --- diff --git a/t/testwide-passthrough.pl.src b/t/testwide-passthrough.pl.src new file mode 100644 index 00000000..626de457 --- /dev/null +++ b/t/testwide-passthrough.pl.src @@ -0,0 +1,5 @@ +# nothing to tidy or run +"Plain"; +"Zwölf große Boxkämpfer jagen Vik quer über den Sylter."; +"Jeż wlókł gęś. Uf! Bądź choć przy nim, stań!"; +"Любя, съешь щипцы, — вздохнёт мэр, — кайф жгуч."; diff --git a/t/testwide-passthrough.t b/t/testwide-passthrough.t new file mode 100644 index 00000000..d48944b8 --- /dev/null +++ b/t/testwide-passthrough.t @@ -0,0 +1,125 @@ +use strict; +use warnings; +use utf8; + +use FindBin qw($Bin); +use File::Temp qw(tempfile); +use Test::More; + +BEGIN {unshift @INC, "./"} +use Perl::Tidy; + +# The test file has no tidying needs but is UTF-8 encoded, so all passes +# through perltidy should read/write identical contents (previously only +# file test behaved correctly) + +plan(tests => 6); + +test_all(); + +sub test_all +{ + my $test_file = "$Bin/testwide-passthrough.pl.src"; + test_file2file($test_file); + test_scalar2scalar($test_file); + test_scalararray2scalararray($test_file); +} + +sub test_file2file +{ + my $test_file = shift; + + my $tmp_file = File::Temp->new( TMPDIR => 1 ); + + my $source = $test_file; + my $destination = $tmp_file->filename(); + + note("Testing file2file: '$source' => '$destination'\n"); + + my $tidyresult = Perl::Tidy::perltidy + ( + argv => '-utf8', + source => $source, + destination => $destination + ); + ok(!$tidyresult, 'perltidy'); + + my $source_hex = unpack('H*', slurp_raw($source)); + my $destination_hex = unpack('H*', slurp_raw($destination)); + + note("Comparing contents:\n $source_hex\n $destination_hex\n"); + ok($source_hex eq $destination_hex, 'file content compare'); +} + +sub test_scalar2scalar +{ + my $testfile = shift; + + my $source = slurp_raw($testfile); + my $destination; + + note("Testing scalar2scalar\n"); + + my $tidyresult = Perl::Tidy::perltidy + ( + argv => '-utf8 -eos', + source => \$source, + destination => \$destination + ); + ok(!$tidyresult, 'perltidy'); + + my $source_hex = unpack('H*', $source); + my $destination_hex = unpack('H*', $destination); + + note("Comparing contents:\n $source_hex\n $destination_hex\n"); + ok($source_hex eq $destination_hex, 'scalar content compare'); +} + +sub test_scalararray2scalararray +{ + my $testfile = shift; + + my $source = [ lines_raw($testfile) ]; + my $destination = []; + + note("Testing scalararray2scalararray\n"); + + my $tidyresult = Perl::Tidy::perltidy + ( + argv => '-utf8 -eos', + source => $source, + destination => $destination + ); + ok(!$tidyresult, 'perltidy'); + + my $source_hex = join('', map { unpack('H*', $_) } @$source ); + my $destination_hex = join('', map { unpack('H*', $_) } @$destination); + + note("Comparing contents:\n $source_hex\n $destination_hex\n"); + ok($source_hex eq $destination_hex, 'scalararray content compare'); +} + +sub slurp_raw +{ + my $filename = shift; + + open(TMP, '<', $filename); + binmode(TMP, ':raw'); + local $/; + my $contents = ; + close(TMP); + + return $contents; +} + +sub lines_raw +{ + my $filename = shift; + + open(TMP, '<', $filename); + binmode(TMP, ':raw'); + my @contents = ; + close(TMP); + + return @contents; +} diff --git a/t/testwide-tidy.pl.src b/t/testwide-tidy.pl.src new file mode 100644 index 00000000..30bc8c15 --- /dev/null +++ b/t/testwide-tidy.pl.src @@ -0,0 +1,8 @@ +# really simple +@a= +( +"Plain", +"Zwölf große Boxkämpfer jagen Vik quer über den Sylter.", +"Jeż wlókł gęś. Uf! Bądź choć przy nim, stań!", +"Любя, съешь щипцы, — вздохнёт мэр, — кайф жгуч.", +); diff --git a/t/testwide-tidy.pl.srctdy b/t/testwide-tidy.pl.srctdy new file mode 100644 index 00000000..bd478cc9 --- /dev/null +++ b/t/testwide-tidy.pl.srctdy @@ -0,0 +1,7 @@ +# really simple +@a = ( + "Plain", + "Zwölf große Boxkämpfer jagen Vik quer über den Sylter.", + "Jeż wlókł gęś. Uf! Bądź choć przy nim, stań!", + "Любя, съешь щипцы, — вздохнёт мэр, — кайф жгуч.", +); diff --git a/t/testwide-tidy.t b/t/testwide-tidy.t new file mode 100644 index 00000000..dd2b4de8 --- /dev/null +++ b/t/testwide-tidy.t @@ -0,0 +1,125 @@ +use strict; +use warnings; +use utf8; + +use FindBin qw($Bin); +use File::Temp qw(tempfile); +use Test::More; + +BEGIN {unshift @INC, "./"} +use Perl::Tidy; + +# The test file is UTF-8 encoded + +plan(tests => 6); + +test_all(); + +sub test_all +{ + my $test_file = "$Bin/testwide-tidy.pl.src"; + my $tidy_file = "$Bin/testwide-tidy.pl.srctdy"; + my $tidy_hex = unpack('H*', slurp_raw($tidy_file)); + test_file2file($test_file, $tidy_hex); + test_scalar2scalar($test_file, $tidy_hex); + test_scalararray2scalararray($test_file, $tidy_hex); +} + +sub test_file2file +{ + my $test_file = shift; + my $tidy_hex = shift; + + my $tmp_file = File::Temp->new( TMPDIR => 1 ); + + my $source = $test_file; + my $destination = $tmp_file->filename(); + + note("Testing file2file: '$source' => '$destination'\n"); + + my $tidyresult = Perl::Tidy::perltidy + ( + argv => '-utf8', + source => $source, + destination => $destination + ); + ok(!$tidyresult, 'perltidy'); + + my $destination_hex = unpack('H*', slurp_raw($destination)); + + note("Comparing contents:\n $tidy_hex\n $destination_hex\n"); + ok($tidy_hex eq $destination_hex, 'file content compare'); +} + +sub test_scalar2scalar +{ + my $test_file = shift; + my $tidy_hex = shift; + + my $source = slurp_raw($test_file); + my $destination; + + note("Testing scalar2scalar\n"); + + my $tidyresult = Perl::Tidy::perltidy + ( + argv => '-utf8 -eos', + source => \$source, + destination => \$destination + ); + ok(!$tidyresult, 'perltidy'); + + my $destination_hex = unpack('H*', $destination); + + note("Comparing contents:\n $tidy_hex\n $destination_hex\n"); + ok($tidy_hex eq $destination_hex, 'scalar content compare'); +} + +sub test_scalararray2scalararray +{ + my $test_file = shift; + my $tidy_hex = shift; + + my $source = [ lines_raw($test_file) ]; + my $destination = []; + + note("Testing scalararray2scalararray\n"); + + my $tidyresult = Perl::Tidy::perltidy + ( + argv => '-utf8 -eos', + source => $source, + destination => $destination + ); + ok(!$tidyresult, 'perltidy'); + + my $destination_hex = join('', map { unpack('H*', $_) } @$destination); + + note("Comparing contents:\n $tidy_hex\n $destination_hex\n"); + ok($tidy_hex eq $destination_hex, 'scalararray content compare'); +} + +sub slurp_raw +{ + my $filename = shift; + + open(TMP, '<', $filename); + binmode(TMP, ':raw'); + local $/; + my $contents = ; + close(TMP); + + return $contents; +} + +sub lines_raw +{ + my $filename = shift; + + open(TMP, '<', $filename); + binmode(TMP, ':raw'); + my @contents = ; + close(TMP); + + return @contents; +}