« lib/Perl/ToPerl6/TransformerParameter/Behavior/Integer.pm
↑ Summary
lib/Perl/ToPerl6/Transformer/Variables/RewriteSigils.pm »
| 1 | package Test::Perl::ToPerl6::Transformer; | ||
| 2 | |||
| 3 | use 5.006001; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Carp qw< croak confess >; | ||
| 9 | use English qw< -no_match_vars >; | ||
| 10 | use List::Util qw< min >; | ||
| 11 | use List::MoreUtils qw< all none >; | ||
| 12 | use Readonly; | ||
| 13 | |||
| 14 | use Test::Builder qw<>; | ||
| 15 | use Test::More; | ||
| 16 | |||
| 17 | use Perl::ToPerl6::Transformation; | ||
| 18 | use Perl::ToPerl6::TestUtils qw< | ||
| 19 | ptransform_with_transformations | ||
| 20 | ftransform_with_transformations | ||
| 21 | subtests_in_tree | ||
| 22 | >; | ||
| 23 | |||
| 24 | #----------------------------------------------------------------------------- | ||
| 25 | |||
| 26 | use Exporter 'import'; | ||
| 27 | |||
| 28 | Readonly::Array our @EXPORT_OK => qw< all_transformers_ok transform_ok >; | ||
| 29 | Readonly::Hash our %EXPORT_TAGS => (all => \@EXPORT_OK); | ||
| 30 | |||
| 31 | #----------------------------------------------------------------------------- | ||
| 32 | |||
| 33 | Perl::ToPerl6::Transformation::set_format( "%m at line %l, column %c. (%r)\n" ); | ||
| 34 | Perl::ToPerl6::TestUtils::block_perlmogrifyrc(); | ||
| 35 | |||
| 36 | #----------------------------------------------------------------------------- | ||
| 37 | |||
| 38 | my $TEST = Test::Builder->new(); | ||
| 39 | |||
| 40 | #----------------------------------------------------------------------------- | ||
| 41 | |||
| 42 | sub transform_ok { | ||
| 43 | my ($transformer, $fh) = @_; | ||
| 44 | my $subtests_with_extras = subtests_in_tree( 't', 'include extras' ); | ||
| 45 | |||
| 46 | my $subtests = []; | ||
| 47 | my $in_expected; | ||
| 48 | for my $line (<$fh>) { | ||
| 49 | chomp $line; | ||
| 50 | if ( $line =~ /^## name: (.+)/ ) { | ||
| 51 | $in_expected = undef; | ||
| 52 | push @{ $subtests }, { | ||
| 53 | name => $1, | ||
| 54 | failures => 0, | ||
| 55 | lineno => 1, | ||
| 56 | parms => {}, | ||
| 57 | original => [], | ||
| 58 | expected => [], | ||
| 59 | } | ||
| 60 | } | ||
| 61 | elsif ( $line eq '##-->' ) { $in_expected = 1 } | ||
| 62 | elsif ( $in_expected ) { push @{ $subtests->[-1]{expected} }, $line } | ||
| 63 | else { | ||
| 64 | unless ( $subtests and @{ $subtests } ) { | ||
| 65 | $TEST->ok( 0, 'Test formatted correctly' ); | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | push @{ $subtests->[-1]{original} }, $line; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | $TEST->note("Running tests for transformer: $transformer"); | ||
| 73 | |||
| 74 | my ($full_transformer_name, $method) = | ||
| 75 | ("Perl::ToPerl6::Transformer::$transformer", 'transform'); | ||
| 76 | my $can_ok_label = qq{Class '$full_transformer_name' has method '$method'}; | ||
| 77 | $TEST->ok( $full_transformer_name->can($method), $can_ok_label ); | ||
| 78 | |||
| 79 | for my $subtest ( @{ $subtests } ) { | ||
| 80 | my $todo = $subtest->{TODO}; | ||
| 81 | if ($todo) { $TEST->todo_start( $todo ); } | ||
| 82 | |||
| 83 | my ($error, @transformations) = _run_subtest($transformer, $subtest); | ||
| 84 | $TEST->ok( !$error, _create_test_name($transformer, $subtest) ); | ||
| 85 | # my ($ok, @diag)= | ||
| 86 | # _evaluate_test_results($subtest, $error, \@transformations); | ||
| 87 | # | ||
| 88 | # if (@diag) { $TEST->diag(@diag); } | ||
| 89 | # if ($todo) { $TEST->todo_end(); } | ||
| 90 | } | ||
| 91 | |||
| 92 | return; | ||
| 93 | } | ||
| 94 | |||
| 95 | #----------------------------------------------------------------------------- | ||
| 96 | |||
| 97 | sub all_transformers_ok { | ||
| 98 | my (%args) = @_; | ||
| 99 | my $wanted_transformers = $args{-transformers}; | ||
| 100 | my $test_dir = $args{'-test-directory'} || 't'; | ||
| 101 | |||
| 102 | my $subtests_with_extras = subtests_in_tree( $test_dir, 'include extras' ); | ||
| 103 | |||
| 104 | if ($wanted_transformers) { | ||
| 105 | _validate_wanted_transformer_names($wanted_transformers, $subtests_with_extras); | ||
| 106 | _filter_unwanted_subtests($wanted_transformers, $subtests_with_extras); | ||
| 107 | } | ||
| 108 | |||
| 109 | # $TEST->plan( tests => _compute_test_count($subtests_with_extras) ); | ||
| 110 | $TEST->plan( tests => 1 ); | ||
| 111 | my $transformers_to_test = join q{, }, keys %{$subtests_with_extras}; | ||
| 112 | $TEST->note("Running tests for transformers: $transformers_to_test"); | ||
| 113 | |||
| 114 | for my $transformer ( sort keys %{$subtests_with_extras} ) { | ||
| 115 | |||
| 116 | my ($full_transformer_name, $method) = ("Perl::ToPerl6::Transformer::$transformer", 'transform'); | ||
| 117 | my $can_ok_label = qq{Class '$full_transformer_name' has method '$method'}; | ||
| 118 | $TEST->ok( $full_transformer_name->can($method), $can_ok_label ); | ||
| 119 | |||
| 120 | for my $subtest ( @{ $subtests_with_extras->{$transformer}{subtests} } ) { | ||
| 121 | my $todo = $subtest->{TODO}; | ||
| 122 | if ($todo) { $TEST->todo_start( $todo ); } | ||
| 123 | |||
| 124 | my ($error, @transformations) = _run_subtest($transformer, $subtest); | ||
| 125 | # my ($ok, @diag)= _evaluate_test_results($subtest, $error, \@transformations); | ||
| 126 | # $TEST->ok( $ok, _create_test_name($transformer, $subtest) ); | ||
| 127 | # | ||
| 128 | # if (@diag) { $TEST->diag(@diag); } | ||
| 129 | # if ($todo) { $TEST->todo_end(); } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | return; | ||
| 134 | } | ||
| 135 | |||
| 136 | #----------------------------------------------------------------------------- | ||
| 137 | |||
| 138 | sub _validate_wanted_transformer_names { | ||
| 139 | my ($wanted_transformers, $subtests_with_extras) = @_; | ||
| 140 | return 1 if not $wanted_transformers; | ||
| 141 | my @all_testable_transformers = keys %{ $subtests_with_extras }; | ||
| 142 | my @wanted_transformers = @{ $wanted_transformers }; | ||
| 143 | |||
| 144 | |||
| 145 | my @invalid = grep {my $p = $_; none { $_ =~ $p } @all_testable_transformers} @wanted_transformers; | ||
| 146 | croak( q{No tests found for transformers matching: } . join q{, }, @invalid ) if @invalid; | ||
| 147 | return 1; | ||
| 148 | } | ||
| 149 | |||
| 150 | #----------------------------------------------------------------------------- | ||
| 151 | |||
| 152 | sub _filter_unwanted_subtests { | ||
| 153 | my ($wanted_transformers, $subtests_with_extras) = @_; | ||
| 154 | return 1 if not $wanted_transformers; | ||
| 155 | my @all_testable_transformers = keys %{ $subtests_with_extras }; | ||
| 156 | my @wanted_transformers = @{ $wanted_transformers }; | ||
| 157 | |||
| 158 | for my $p (@all_testable_transformers) { | ||
| 159 | if (none {$p =~ m/$_/xism} @wanted_transformers) { | ||
| 160 | delete $subtests_with_extras->{$p}; # side-effects! | ||
| 161 | } | ||
| 162 | } | ||
| 163 | return 1; | ||
| 164 | } | ||
| 165 | |||
| 166 | #----------------------------------------------------------------------------- | ||
| 167 | |||
| 168 | sub __markup_array { | ||
| 169 | my ($lines, $error_line, $offset) = @_; | ||
| 170 | my @out; | ||
| 171 | for my $i ( 0 .. $#{$lines} ) { | ||
| 172 | push @out, ">$lines->[$i]<"; | ||
| 173 | if ( $offset and $i == $error_line ) { | ||
| 174 | push @out, ( '-' x $offset ) . '^'; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | @out; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub __results_string { | ||
| 181 | my ($subtest, $error_line) = @_; | ||
| 182 | |||
| 183 | my $offset; | ||
| 184 | if ( $error_line ) { | ||
| 185 | # Sigh, for the moment just walk the strings. ^ should work... | ||
| 186 | # | ||
| 187 | for ( 0 .. length($subtest->{expected}[$error_line]) ) { | ||
| 188 | next if substr($subtest->{expected}[$error_line], $_, 1 ) eq | ||
| 189 | substr($subtest->{got}[$error_line], $_, 1 ); | ||
| 190 | $offset = $_ + 1; | ||
| 191 | last; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | join( "\n", __markup_array( $subtest->{original}, $error_line ), | ||
| 196 | '====??====>', | ||
| 197 | __markup_array( $subtest->{expected}, $error_line, $offset ), | ||
| 198 | '====!!====>', | ||
| 199 | __markup_array( $subtest->{got}, $error_line, $offset ) | ||
| 200 | ); | ||
| 201 | } | ||
| 202 | |||
| 203 | sub __is_deeply { | ||
| 204 | my ($transformer, $subtest) = @_; | ||
| 205 | |||
| 206 | my $error_line = 0; | ||
| 207 | my $last_line = min( $#{ $subtest->{expected} }, | ||
| 208 | $#{ $subtest->{got} } ); | ||
| 209 | my $first_different_line = 0; | ||
| 210 | for my $idx ( 0 .. $last_line ) { | ||
| 211 | next if $subtest->{expected}[$idx] eq | ||
| 212 | $subtest->{got}[$idx]; | ||
| 213 | $first_different_line = $idx; | ||
| 214 | $TEST->diag( | ||
| 215 | "Output begins to differ at line " . | ||
| 216 | ( $first_different_line + 1 ) . | ||
| 217 | ":\n" . | ||
| 218 | __results_string($subtest, $first_different_line) | ||
| 219 | ); | ||
| 220 | $error_line = $first_different_line; | ||
| 221 | last; | ||
| 222 | } | ||
| 223 | |||
| 224 | if ( $#{ $subtest->{expected} } > $#{ $subtest->{got} } ) { | ||
| 225 | warn "Error was [$@]\n" if $@; | ||
| 226 | $TEST->diag( | ||
| 227 | "Transformed file missing lines from original:\n". | ||
| 228 | __results_string($subtest) | ||
| 229 | ); | ||
| 230 | $error_line = $#{ $subtest->{got} }; | ||
| 231 | } | ||
| 232 | elsif ( $#{ $subtest->{expected} } < $#{ $subtest->{got} } ) { | ||
| 233 | $TEST->diag( | ||
| 234 | "Transformed file has more lines than original:\n". | ||
| 235 | __results_string($subtest) | ||
| 236 | ); | ||
| 237 | $error_line = $#{ $subtest->{expected} }; | ||
| 238 | } | ||
| 239 | $subtest->{lineno} = $error_line + 1; | ||
| 240 | return $error_line; | ||
| 241 | } | ||
| 242 | |||
| 243 | #----------------------------------------------------------------------------- | ||
| 244 | |||
| 245 | sub _run_subtest { | ||
| 246 | my ($transformer, $subtest) = @_; | ||
| 247 | |||
| 248 | my $document; | ||
| 249 | my @transformations; | ||
| 250 | my $error; | ||
| 251 | if ( $subtest->{filename} ) { | ||
| 252 | eval { | ||
| 253 | @transformations = | ||
| 254 | ftransform_with_transformations( | ||
| 255 | $transformer, | ||
| 256 | \$subtest->{code}, | ||
| 257 | $subtest->{filename}, | ||
| 258 | $subtest->{parms}, | ||
| 259 | ); | ||
| 260 | 1; | ||
| 261 | } or do { | ||
| 262 | $error = $EVAL_ERROR || 'An unknown problem occurred.'; | ||
| 263 | }; | ||
| 264 | } | ||
| 265 | else { | ||
| 266 | eval { | ||
| 267 | ($document, @transformations) = | ||
| 268 | ptransform_with_transformations( | ||
| 269 | $transformer, | ||
| 270 | $subtest->{original}, | ||
| 271 | $subtest->{parms}, | ||
| 272 | ); | ||
| 273 | 1; | ||
| 274 | } or do { | ||
| 275 | $error = $EVAL_ERROR || 'An unknown problem occurred.'; | ||
| 276 | }; | ||
| 277 | } | ||
| 278 | if ( $document ) { | ||
| 279 | $subtest->{got} = [split /\n/,$document]; | ||
| 280 | } | ||
| 281 | else { | ||
| 282 | die "*** caught error $error!\n"; | ||
| 283 | } | ||
| 284 | my $num_errors = __is_deeply($transformer, $subtest); | ||
| 285 | return $num_errors; | ||
| 286 | } | ||
| 287 | |||
| 288 | #----------------------------------------------------------------------------- | ||
| 289 | |||
| 290 | sub _evaluate_test_results { | ||
| 291 | my ($subtest, $error, $transformations) = @_; | ||
| 292 | |||
| 293 | if ($subtest->{error}) { | ||
| 294 | return _evaluate_error_case($subtest, $error); | ||
| 295 | } | ||
| 296 | elsif ($error) { | ||
| 297 | confess $error; | ||
| 298 | } | ||
| 299 | else { | ||
| 300 | return _evaluate_transformation_case($subtest, $transformations); | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | #----------------------------------------------------------------------------- | ||
| 305 | |||
| 306 | sub _evaluate_transformation_case { | ||
| 307 | my ($subtest, $transformations) = @_; | ||
| 308 | my ($ok, @diagnostics); | ||
| 309 | |||
| 310 | my @transformations = @{$transformations}; | ||
| 311 | my $have = scalar @transformations; | ||
| 312 | my $want = _compute_wanted_transformation_count($subtest); | ||
| 313 | if ( not $ok = $have == $want ) { | ||
| 314 | my $msg = qq(Expected $want transformations, got $have. ); | ||
| 315 | if (@transformations) { $msg .= q(Found transformations follow...); } | ||
| 316 | push @diagnostics, $msg . "\n"; | ||
| 317 | push @diagnostics, map { qq(Found transformation: $_) } @transformations; | ||
| 318 | } | ||
| 319 | |||
| 320 | return ($ok, @diagnostics) | ||
| 321 | } | ||
| 322 | |||
| 323 | #----------------------------------------------------------------------------- | ||
| 324 | |||
| 325 | sub _evaluate_error_case { | ||
| 326 | my ($subtest, $error) = @_; | ||
| 327 | my ($ok, @diagnostics); | ||
| 328 | |||
| 329 | if ( 'Regexp' eq ref $subtest->{error} ) { | ||
| 330 | $ok = $error =~ $subtest->{error} | ||
| 331 | or push @diagnostics, qq(Error message '$error' doesn't match $subtest->{error}.); | ||
| 332 | } | ||
| 333 | else { | ||
| 334 | $ok = $subtest->{error} | ||
| 335 | or push @diagnostics, q(Didn't get an error message when we expected one.); | ||
| 336 | } | ||
| 337 | |||
| 338 | return ($ok, @diagnostics); | ||
| 339 | } | ||
| 340 | |||
| 341 | #----------------------------------------------------------------------------- | ||
| 342 | |||
| 343 | sub _compute_test_count { | ||
| 344 | my ($subtests_with_extras) = @_; | ||
| 345 | |||
| 346 | # one can_ok() for each transformer | ||
| 347 | my $ntransformers = scalar keys %{ $subtests_with_extras }; | ||
| 348 | |||
| 349 | my $nsubtests = 0; | ||
| 350 | for my $subtest_with_extras ( values %{$subtests_with_extras} ) { | ||
| 351 | # one [pf]transform() test per subtest | ||
| 352 | $nsubtests += @{ $subtest_with_extras->{subtests} }; | ||
| 353 | } | ||
| 354 | |||
| 355 | return $nsubtests + $ntransformers; | ||
| 356 | } | ||
| 357 | |||
| 358 | #----------------------------------------------------------------------------- | ||
| 359 | |||
| 360 | sub _compute_wanted_transformation_count { | ||
| 361 | my ($subtest) = @_; | ||
| 362 | |||
| 363 | # If any optional modules are NOT available, then there should be no transformations. | ||
| 364 | return 0 if not _all_optional_modules_are_available($subtest); | ||
| 365 | return $subtest->{failures}; | ||
| 366 | } | ||
| 367 | |||
| 368 | #----------------------------------------------------------------------------- | ||
| 369 | |||
| 370 | sub _all_optional_modules_are_available { | ||
| 371 | my ($subtest) = @_; | ||
| 372 | my $optional_modules = $subtest->{optional_modules} or return 1; | ||
| 373 | return all {eval "require $_;" or 0;} split m/,\s*/xms, $optional_modules; | ||
| 374 | } | ||
| 375 | |||
| 376 | #----------------------------------------------------------------------------- | ||
| 377 | |||
| 378 | sub _create_test_name { | ||
| 379 | my ($transformer, $subtest) = @_; | ||
| 380 | return join ' - ', $transformer, "line $subtest->{lineno}", $subtest->{name}; | ||
| 381 | } | ||
| 382 | |||
| 383 | #----------------------------------------------------------------------------- | ||
| 384 | 1; | ||
| 385 | |||
| 386 | __END__ |