| 1 |
# A package to execute the a test suite based on a Markdown file containing |
| 2 |
# alternating code snippets in Markdown and HTML. Where the HTML is supposedly |
| 3 |
# what the tool must render given the Markdown input. |
| 4 |
# |
| 5 |
# This is typically the syntax of our Syntax.md file. |
| 6 |
# |
| 7 |
# This test looks for successive fenced code spans at the root level of the |
| 8 |
# document, one with an `md` info string and then one with an `html` info |
| 9 |
# string, that are not separated by any other Markdown construct except |
| 10 |
# paragraphs of texts. When found, the content of the two code-spans are used as |
| 11 |
# the definition of a test. |
| 12 |
# |
| 13 |
# The most recently seen header (at any level) is used as the name of the test. |
| 14 |
|
| 15 |
package PmarkdownTest; |
| 16 |
|
| 17 |
use strict; |
| 18 |
use warnings; |
| 19 |
use utf8; |
| 20 |
use feature ':5.24'; |
| 21 |
|
| 22 |
use Exporter 'import'; |
| 23 |
use HtmlSanitizer; |
| 24 |
use JSON 'from_json'; |
| 25 |
use Markdown::Perl; |
| 26 |
use Test2::V0; |
| 27 |
|
| 28 |
our @EXPORT = qw(test_suite); |
| 29 |
|
| 30 |
sub md_blocks_to_tests { |
| 31 |
my (@blocks) = @_; |
| 32 |
my @output; |
| 33 |
my $title; |
| 34 |
my %test; |
| 35 |
my $i = 1; |
| 36 |
for my $bl (@blocks) { |
| 37 |
if ($bl->{type} eq 'heading') { |
| 38 |
undef %test; |
| 39 |
$title = $bl->{content}; |
| 40 |
} elsif ($bl->{type} eq 'code') { |
| 41 |
if ($bl->{info} =~ m/^md(?:\s+(?<inline>inline))?\s*$/) { |
| 42 |
%test = (md => $bl->{content}, inline => !!(defined $+{inline})); |
| 43 |
} elsif ($bl->{info} eq 'html' && %test) { |
| 44 |
push @output, { %test, html => $bl->{content}, title => $title, index => $i++ }; |
| 45 |
undef %test; |
| 46 |
} else { |
| 47 |
undef %test; |
| 48 |
} |
| 49 |
} elsif ($bl->{type} ne 'paragraph') { |
| 50 |
undef %test; |
| 51 |
} |
| 52 |
} |
| 53 |
return @output; |
| 54 |
} |
| 55 |
|
| 56 |
sub test_suite { |
| 57 |
my (%opt) = @_; |
| 58 |
|
| 59 |
my $test_data; |
| 60 |
{ |
| 61 |
local $/ = undef; |
| 62 |
open my $f, '<:encoding(utf-8)', $opt{md_file}; |
| 63 |
my $md_data = <$f>; |
| 64 |
close $f; |
| 65 |
# TODO: possibly we want a different object here |
| 66 |
my $parser = Markdown::Perl->new(mode => $opt{file_parse_mode}); |
| 67 |
$test_data = $parser->_parse($md_data); |
| 68 |
} |
| 69 |
|
| 70 |
my @tests = md_blocks_to_tests(@{$test_data}); |
| 71 |
|
| 72 |
my $pmarkdown = Markdown::Perl->new(mode => $opt{mode}, warn_for_unused_input => 0); |
| 73 |
|
| 74 |
my %todo = map { $_ => 1 } @{$opt{todo} // []}; |
| 75 |
my %bugs = map { $_ => 1 } @{$opt{bugs} // []}; |
| 76 |
for my $t (@tests) { |
| 77 |
next if exists $opt{test_num} && $opt{test_num} != $t->{index}; |
| 78 |
|
| 79 |
my @opt; |
| 80 |
push @opt, render_naked_paragraphs => $t->{inline}; |
| 81 |
my $out = $pmarkdown->convert($t->{md}, @opt); |
| 82 |
my $val = sanitize_html($out); |
| 83 |
my $expected = sanitize_html($t->{html}); |
| 84 |
|
| 85 |
my $title = sprintf "%s (%d)", $t->{title}, $t->{index}; |
| 86 |
my $test = sub { is($val, $expected, $title, 'Input markdown:', $t->{md}, "\n") }; |
| 87 |
|
| 88 |
if ($todo{$t->{index}}) { |
| 89 |
todo 'Not yet supported' => $test; |
| 90 |
} elsif ($bugs{$t->{index}}) { |
| 91 |
todo 'The spec is buggy' => $test; |
| 92 |
} else { |
| 93 |
$test->(); |
| 94 |
} |
| 95 |
} |
| 96 |
} |