| 1 |
#!perl |
| 2 |
# Copyright 2022 Jeffrey Kegler |
| 3 |
# This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 4 |
# redistribute it and/or modify it under the terms of the GNU Lesser |
| 5 |
# General Public License as published by the Free Software Foundation, |
| 6 |
# either version 3 of the License, or (at your option) any later version. |
| 7 |
# |
| 8 |
# Marpa::R2 is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
# Lesser General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU Lesser |
| 14 |
# General Public License along with Marpa::R2. If not, see |
| 15 |
# http://www.gnu.org/licenses/. |
| 16 |
|
| 17 |
use 5.010001; |
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
|
| 21 |
use English qw( -no_match_vars ); |
| 22 |
use Fatal qw(open close); |
| 23 |
use Text::Diff; |
| 24 |
use Getopt::Long qw(GetOptions); |
| 25 |
use List::Util; |
| 26 |
use Test::More 0.94; |
| 27 |
use Carp; |
| 28 |
|
| 29 |
use Perl::Tidy; |
| 30 |
use Text::Wrap; |
| 31 |
|
| 32 |
use lib 'inc'; |
| 33 |
use Marpa::R2::Display; |
| 34 |
|
| 35 |
my $warnings = 0; |
| 36 |
my $options_result = GetOptions( 'warnings' => \$warnings ); |
| 37 |
|
| 38 |
Marpa::R2::exception("$PROGRAM_NAME options parsing failed") |
| 39 |
if not $options_result; |
| 40 |
|
| 41 |
my %exclude = map { ( $_, 1 ) } qw(); |
| 42 |
my @additional_files = qw(); |
| 43 |
|
| 44 |
my @test_files = @ARGV; |
| 45 |
my $debug_mode = scalar @test_files; |
| 46 |
if ( not $debug_mode ) { |
| 47 |
|
| 48 |
for my $additional_file (@additional_files) { |
| 49 |
Test::More::diag("Adding $additional_file"); |
| 50 |
push @test_files, $additional_file; |
| 51 |
} |
| 52 |
|
| 53 |
open my $manifest, '<', 'MANIFEST' |
| 54 |
or Marpa::R2::exception("Cannot open MANIFEST: $ERRNO"); |
| 55 |
FILE: while ( my $file = <$manifest> ) { |
| 56 |
chomp $file; |
| 57 |
$file =~ s/\s*[#].*\z//xms; |
| 58 |
next FILE if $file =~ m( [/] old_pod [/] )xms; |
| 59 |
next FILE if $file =~ m( html [/] etc [/] drafts [/] )xms; |
| 60 |
my ($ext) = $file =~ / [.] ([^.]+) \z /xms; |
| 61 |
next FILE if not defined $ext; |
| 62 |
$ext = lc $ext; |
| 63 |
next FILE |
| 64 |
if $ext ne 'pod' |
| 65 |
and $ext ne 'pl' |
| 66 |
and $ext ne 'pm' |
| 67 |
and $ext ne 't'; |
| 68 |
push @test_files, $file; |
| 69 |
} # FILE |
| 70 |
close $manifest; |
| 71 |
|
| 72 |
my %file_seen = (); |
| 73 |
FILE: for my $test_file (@test_files) { |
| 74 |
|
| 75 |
next FILE if $exclude{$test_file}; |
| 76 |
next FILE if -d $test_file; |
| 77 |
if ( $file_seen{$test_file}++ ) { |
| 78 |
Test::More::diag("Duplicate file: $test_file"); |
| 79 |
} |
| 80 |
} ## end for my $test_file (@test_files) |
| 81 |
@test_files = keys %file_seen; |
| 82 |
|
| 83 |
} ## end if ( not $debug_mode ) |
| 84 |
|
| 85 |
my $error_file; |
| 86 |
## no critic (InputOutput::RequireBriefOpen) |
| 87 |
if ($debug_mode) { |
| 88 |
open $error_file, '>&STDOUT' |
| 89 |
or Marpa::R2::exception("Cannot dup STDOUT: $ERRNO"); |
| 90 |
} |
| 91 |
else { |
| 92 |
open $error_file, '>', 'author.t/display.errs' |
| 93 |
or Marpa::R2::exception("Cannot open display.errs: $ERRNO"); |
| 94 |
} |
| 95 |
## use critic |
| 96 |
|
| 97 |
my $display_data = Marpa::R2::Display->new(); |
| 98 |
|
| 99 |
FILE: for my $file (@test_files) { |
| 100 |
if ( not -f $file ) { |
| 101 |
Test::More::fail(qq{"$file" is not a file}); |
| 102 |
next FILE; |
| 103 |
} |
| 104 |
$display_data->read($file); |
| 105 |
|
| 106 |
} ## end for my $file (@test_files) |
| 107 |
|
| 108 |
my @formatting_instructions = qw(perltidy |
| 109 |
remove-display-indent |
| 110 |
remove-blank-last-line inline |
| 111 |
partial flatten normalize-whitespace); |
| 112 |
|
| 113 |
sub format_display { |
| 114 |
my ( $text, $instructions, $is_copy ) = @_; |
| 115 |
my $result = ${$text}; |
| 116 |
|
| 117 |
if ( $instructions->{'remove-display-indent'} and $is_copy ) { |
| 118 |
my ($first_line_spaces) = ( $result =~ /^ (\s+) \S/xms ); |
| 119 |
$first_line_spaces = quotemeta $first_line_spaces; |
| 120 |
$result =~ s/^$first_line_spaces//gxms; |
| 121 |
} |
| 122 |
if ( $instructions->{'inline'} ) { |
| 123 |
my $min_indent = 99_999_999; |
| 124 |
my @text = grep {/ [^ ] /xms} split /\n/xms, $result; |
| 125 |
for my $line (@text) { |
| 126 |
my ($s) = ( $line =~ / \A ([ ]* ) /xms ); |
| 127 |
my $indent = length $s; |
| 128 |
$min_indent > $indent and $min_indent = $indent; |
| 129 |
} |
| 130 |
$result = join "\n", map { substr $_, $min_indent } @text; |
| 131 |
my $tidied; |
| 132 |
|
| 133 |
# perltidy options chosen to make it as likely |
| 134 |
# as possible that code which differs |
| 135 |
# only in whitespace |
| 136 |
# will end up the same. |
| 137 |
Perl::Tidy::perltidy( |
| 138 |
source => \$result, |
| 139 |
destination => \$tidied, |
| 140 |
perltidyrc => \'-sbt=0 -iob -dcsc -sil=0', |
| 141 |
); |
| 142 |
$result = $tidied; |
| 143 |
} ## end if ( $instructions->{'inline'} ) |
| 144 |
if ( $instructions->{'remove-blank-last-line'} ) { |
| 145 |
$result =~ s/^[ \t]*\n\z//xms; |
| 146 |
} |
| 147 |
if ( $instructions->{'flatten'} ) { |
| 148 |
$result =~ s/[\n\r]/ /gxms; |
| 149 |
} |
| 150 |
if ( $instructions->{'normalize-whitespace'} ) { |
| 151 |
$result =~ s/^\s+//gxms; |
| 152 |
$result =~ s/\s+$//gxms; |
| 153 |
$result =~ s/[ \f\t]+/ /gxms; |
| 154 |
$result =~ s/\n+/\n/gxms; |
| 155 |
} ## end if ( $instructions->{'normalize-whitespace'} ) |
| 156 |
if ( defined( my $tidy_options = $instructions->{'perltidy'} ) ) { |
| 157 |
my $tidied; |
| 158 |
Perl::Tidy::perltidy( |
| 159 |
source => \$result, |
| 160 |
destination => \$tidied, |
| 161 |
perltidyrc => \$tidy_options |
| 162 |
); |
| 163 |
$result = $tidied; |
| 164 |
} ## end if ( defined( my $tidy_options = $instructions->{'perltidy'...})) |
| 165 |
return \$result; |
| 166 |
} ## end sub format_display |
| 167 |
|
| 168 |
# reformat two display according to the instructions in the |
| 169 |
# second, and compare. |
| 170 |
sub compare { |
| 171 |
my ( $original, $copy ) = @_; |
| 172 |
my $formatted_original = |
| 173 |
format_display( \$original->{content}, $copy, 0 ); |
| 174 |
my $formatted_copy = format_display( \$copy->{content}, $copy, 1 ); |
| 175 |
if ( $copy->{partial} ) { |
| 176 |
return 1 if -1 != index ${$formatted_original}, ${$formatted_copy}; |
| 177 |
Test::More::diag( |
| 178 |
"Partial: ", |
| 179 |
$original->{filename}, |
| 180 |
' vs. ', |
| 181 |
$copy->{filename}, |
| 182 |
"\n", |
| 183 |
( Text::Diff::diff $formatted_original, |
| 184 |
$formatted_copy, |
| 185 |
{ STYLE => 'Table' } |
| 186 |
) |
| 187 |
# Text::Wrap::wrap( q{ }, q{ }, ${$formatted_copy} ), |
| 188 |
# "\nOriginal:\n", |
| 189 |
# Text::Wrap::wrap( q{ }, q{ }, ${$formatted_original} ) |
| 190 |
); |
| 191 |
return 0; |
| 192 |
} ## end if ( $copy->{partial} ) |
| 193 |
return 1 if ${$formatted_original} eq ${$formatted_copy}; |
| 194 |
Test::More::diag( |
| 195 |
'Differences: ', |
| 196 |
$original->{filename}, |
| 197 |
' vs. ', |
| 198 |
$copy->{filename}, |
| 199 |
"\n", |
| 200 |
( Text::Diff::diff $formatted_original, |
| 201 |
$formatted_copy, |
| 202 |
{ STYLE => 'Table' } |
| 203 |
) |
| 204 |
); |
| 205 |
return 0; |
| 206 |
} ## end sub compare |
| 207 |
|
| 208 |
my $tests_run = 0; |
| 209 |
my $displays_by_name = $display_data->{displays}; |
| 210 |
DISPLAY_NAME: for my $display_name ( keys %{$displays_by_name} ) { |
| 211 |
|
| 212 |
my $displays = $displays_by_name->{$display_name}; |
| 213 |
if ( scalar @{$displays} <= 1 ) { |
| 214 |
Test::More::fail( |
| 215 |
qq{Display "$display_name" has only one instance, in file } |
| 216 |
. $displays->[0]->{filename} ); |
| 217 |
$tests_run++; |
| 218 |
} ## end if ( scalar @{$displays} <= 1 ) |
| 219 |
|
| 220 |
# find the "original" |
| 221 |
my $original_ix; |
| 222 |
DISPLAY: for my $display_ix ( 0 .. $#{$displays} ) { |
| 223 |
if (not grep { $_ ~~ \@formatting_instructions } |
| 224 |
keys %{ $displays->[$display_ix] } |
| 225 |
) |
| 226 |
{ |
| 227 |
$original_ix = $display_ix; |
| 228 |
} ## end if ( not grep { $_ ~~ \@formatting_instructions } keys...) |
| 229 |
} ## end for my $display_ix ( 0 .. $#{$displays} ) |
| 230 |
|
| 231 |
# Warn if there wasn't a clear original? |
| 232 |
$original_ix //= 0; # default to the first |
| 233 |
|
| 234 |
DISPLAY: for my $copy_ix ( 0 .. $#{$displays} ) { |
| 235 |
next DISPLAY if $copy_ix == $original_ix; |
| 236 |
Test::More::ok compare( $displays->[$original_ix], |
| 237 |
$displays->[$copy_ix] ), "$display_name, copy $copy_ix"; |
| 238 |
$tests_run++; |
| 239 |
} ## end for my $copy_ix ( 0 .. $#{$displays} ) |
| 240 |
|
| 241 |
} ## end for my $display_name ( keys %{$displays_by_name} ) |
| 242 |
|
| 243 |
my $verbatim_by_file = $display_data->{verbatim_lines}; |
| 244 |
VERBATIM_FILE: for my $verbatim_file ( keys %{$verbatim_by_file} ) { |
| 245 |
my @unchecked = (); |
| 246 |
my $verbatim_lines = $verbatim_by_file->{$verbatim_file}; |
| 247 |
for my $verbatim_line_number ( 1 .. $#{$verbatim_lines} ) { |
| 248 |
my $verbatim_line = $verbatim_lines->[$verbatim_line_number]; |
| 249 |
if ($verbatim_line) { |
| 250 |
push @unchecked, "$verbatim_line_number: $verbatim_line"; |
| 251 |
} |
| 252 |
} ## end for my $verbatim_line_number ( 1 .. $#{$verbatim_lines...}) |
| 253 |
next VERBATIM_FILE if not @unchecked; |
| 254 |
Test::More::fail( qq{Verbatim line(s) not checked in "$verbatim_file": } |
| 255 |
. ( scalar @unchecked ) |
| 256 |
. " lines\n" |
| 257 |
. ( join "\n", @unchecked ) ); |
| 258 |
$tests_run++; |
| 259 |
} ## end for my $verbatim_file ( keys %{$verbatim_by_file} ) |
| 260 |
|
| 261 |
Test::More::done_testing($tests_run); |
| 262 |
|
| 263 |
__END__ |
| 264 |
|
| 265 |
# vim: set expandtab shiftwidth=4: |