| 1 |
# A package to execute the tests of the MMD-Test-Suite repository. |
| 2 |
|
| 3 |
package MmdTest; |
| 4 |
|
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
use utf8; |
| 8 |
use feature ':5.24'; |
| 9 |
|
| 10 |
use Exporter 'import'; |
| 11 |
use File::Basename; |
| 12 |
use HtmlSanitizer; |
| 13 |
use Test2::V0; |
| 14 |
use Text::Diff; |
| 15 |
|
| 16 |
our @EXPORT = qw(test_suite); |
| 17 |
|
| 18 |
sub slurp_file { |
| 19 |
my ($file_name) = @_; |
| 20 |
open my $fh, '<:encoding(utf-8)', $file_name; |
| 21 |
local $/ = undef; |
| 22 |
my $content = <$fh>; |
| 23 |
close $fh; |
| 24 |
return $content; |
| 25 |
} |
| 26 |
|
| 27 |
sub one_test { |
| 28 |
my ($pmarkdown, $md_file, $html_file) = @_; |
| 29 |
my $test_name = fileparse($md_file, '.text'); |
| 30 |
my $md = slurp_file($md_file); |
| 31 |
my $out = sanitize_html($pmarkdown->convert($md)); |
| 32 |
my $expected = sanitize_html(slurp_file($html_file)); |
| 33 |
my @diag; |
| 34 |
my $diff = diff \$out, \$expected, { FILENAME_A => 'actual', FILENAME_B => 'expected', CONTEXT => 0 }; |
| 35 |
push @diag, 'Diff:', $diff, "\n"; |
| 36 |
push @diag, 'Input markdown:', $md, "\n"; |
| 37 |
is ($out, $expected, $test_name, @diag); |
| 38 |
} |
| 39 |
|
| 40 |
sub test_suite { |
| 41 |
my ($test_dir, $pmarkdown, %opt) = @_; |
| 42 |
skip_all('MMD-Test-Suite must be checked out.') unless -d $test_dir; |
| 43 |
my $i = $opt{start_num} // 0; |
| 44 |
my %todo = map { $_ => 1 } @{$opt{todo} // []}; |
| 45 |
my %bugs = map { $_ => 1 } @{$opt{bugs} // []}; |
| 46 |
my $ext = $opt{ext} // 'html'; |
| 47 |
for my $md_file (glob "${test_dir}/*.text") { |
| 48 |
$i++; |
| 49 |
next if exists $opt{test_num} && $opt{test_num} != $i; |
| 50 |
my $html_file = $md_file =~ s/\.text$/.$ext/r; |
| 51 |
my $test = sub { one_test($pmarkdown, $md_file, $html_file) }; |
| 52 |
SKIP: { |
| 53 |
skip "Missing html file '${html_file}'" unless -f $html_file; |
| 54 |
if ($todo{$i}) { |
| 55 |
todo 'Not yet supported' => $test; |
| 56 |
} elsif ($bugs{$i}) { |
| 57 |
todo 'The spec is buggy' => $test; |
| 58 |
} else { |
| 59 |
$test->(); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
return $i; |
| 64 |
} |