| 1 |
#!/usr/bin/env perl -w |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Test::More 0.96; |
| 6 |
use File::Spec::Functions qw(catfile); |
| 7 |
use File::Basename qw(basename); |
| 8 |
use Text::Markup::Cmd; |
| 9 |
use Carp; |
| 10 |
|
| 11 |
# Need to have at least one test outside subtests, in case no subtests are run |
| 12 |
# at all. So it might as well be this. |
| 13 |
BEGIN { use_ok 'Text::Markup' or die; } |
| 14 |
|
| 15 |
sub slurp($) { |
| 16 |
my ($file) = @_; |
| 17 |
open my $fh, '<:raw', $file or die "Cannot open $file: $!\n"; |
| 18 |
local $/; |
| 19 |
return <$fh>; |
| 20 |
} |
| 21 |
|
| 22 |
my %expected_for = ( |
| 23 |
mediawiki => sub { |
| 24 |
my $html = slurp catfile('t', 'html', "mediawiki.html"); |
| 25 |
$html =~ s/ö/CGI::escapeHTML(do { use utf8; 'ö' })/e |
| 26 |
if eval { CGI->VERSION >= 4.11 && CGI->VERSION < 4.14 }; |
| 27 |
return $html; |
| 28 |
}, |
| 29 |
asciidoc => sub { |
| 30 |
my $html = slurp catfile('t', 'html', "asciidoc.html"); |
| 31 |
$html =~ s/ü/\\xFC/ if WIN32; |
| 32 |
return $html; |
| 33 |
}, |
| 34 |
); |
| 35 |
|
| 36 |
my %parsed_filter_for = ( |
| 37 |
rest => sub { |
| 38 |
# docutils space character before closing tag of XML declaration in Nov |
| 39 |
# 2022 (https://github.com/docutils/docutils/commit/f93b895), so remove |
| 40 |
# it when we run tests against older versions. |
| 41 |
$_[0] =~ s/ \?>/\?>/; |
| 42 |
}, |
| 43 |
); |
| 44 |
|
| 45 |
my @loaded = Text::Markup->formats; |
| 46 |
my %regex_for = Text::Markup->format_matchers; |
| 47 |
|
| 48 |
while (my $data = <DATA>) { |
| 49 |
next if $data =~ /^#/; |
| 50 |
chomp $data; |
| 51 |
my ($name, $format, $module, $req, @exts) = split /,/ => $data; |
| 52 |
subtest "Testing $name format" => sub { |
| 53 |
do { |
| 54 |
local $@; |
| 55 |
eval "use $req; 1;"; |
| 56 |
if ($@) { |
| 57 |
die $@ if $ENV{TEXT_MARKUP_TEST_ALL} |
| 58 |
&& !$ENV{"TEXT_MARKUP_SKIP_\U$name"}; |
| 59 |
plan skip_all => "$req not loading: $@"; |
| 60 |
} |
| 61 |
} if $req; |
| 62 |
|
| 63 |
plan tests => @exts + 7; |
| 64 |
use_ok $module or next; |
| 65 |
|
| 66 |
if ($format ne 'none') { # Not loaded yet; tested below. |
| 67 |
is_deeply [Text::Markup->formats], \@loaded, |
| 68 |
"$format should be loaded"; |
| 69 |
} |
| 70 |
|
| 71 |
my $parser = new_ok 'Text::Markup'; |
| 72 |
for my $ext (@exts) { |
| 73 |
is $parser->guess_format("foo.$ext"), $format, |
| 74 |
"Should guess that .$ext extension is $format"; |
| 75 |
} |
| 76 |
|
| 77 |
# Parse the markup. |
| 78 |
my $html = $parser->parse( |
| 79 |
file => catfile('t', 'markups', "$name.txt"), |
| 80 |
format => $format, |
| 81 |
); |
| 82 |
if (my $f = $parsed_filter_for{$name}) { |
| 83 |
$f->($html) |
| 84 |
} |
| 85 |
|
| 86 |
# Load the expected output. |
| 87 |
my $loader = $expected_for{$name} ||= sub { |
| 88 |
slurp catfile('t', 'html', "$name.html") |
| 89 |
}; |
| 90 |
my $expect = $loader->(); |
| 91 |
|
| 92 |
# They should be the same! |
| 93 |
is $html, $expect, "Parse $name file"; |
| 94 |
|
| 95 |
is $parser->parse( |
| 96 |
file => catfile('t', 'empty.txt'), |
| 97 |
format => $format, |
| 98 |
), undef, "Parse empty $name file"; |
| 99 |
|
| 100 |
# Try recognizing in plain text. |
| 101 |
is $parser->guess_format('hello.txt'), undef, |
| 102 |
'guess_format should not match .txt'; |
| 103 |
$module->import(qr/txt/); |
| 104 |
is $parser->guess_format('hello.txt'), $format, |
| 105 |
'Now guess_format should match .txt'; |
| 106 |
$module->import($regex_for{$format}); |
| 107 |
|
| 108 |
if ($format eq 'none') { |
| 109 |
@loaded = sort (@loaded, 'none'); |
| 110 |
is_deeply [Text::Markup->formats], \@loaded, |
| 111 |
"$format should be loaded"; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
done_testing; |
| 117 |
|
| 118 |
__DATA__ |
| 119 |
# Name,Format,Format Module,Required Module,extensions |
| 120 |
markdown,markdown,Text::Markup::Markdown,Text::Markdown::Discount 0.13,md,mkdn,mkd,mdown,markdown |
| 121 |
commonmark,markdown,Text::Markup::CommonMark,CommonMark 0.290000,md,mkdn,mkd,mdown,markdown |
| 122 |
html,html,Text::Markup::HTML,,html,htm,xhtml,xhtm |
| 123 |
pod,pod,Text::Markup::Pod,Pod::Simple::XHTML 3.15,pod,pm,pl |
| 124 |
trac,trac,Text::Markup::Trac,Text::Trac 0.10,trac,trc |
| 125 |
textile,textile,Text::Markup::Textile,Text::Textile 2.10,textile |
| 126 |
mediawiki,mediawiki,Text::Markup::Mediawiki,Text::MediawikiFormat 1.0,wiki,mwiki,mediawiki |
| 127 |
multimarkdown,multimarkdown,Text::Markup::Multimarkdown,Text::MultiMarkdown 1.000033,mmd,mmkdn,mmkd,mmdown,mmarkdown |
| 128 |
rest,rest,Text::Markup::Rest,Text::Markup::Rest,rest,rst |
| 129 |
asciidoc,asciidoc,Text::Markup::Asciidoc,Text::Markup::Asciidoc,asciidoc,asc,adoc |
| 130 |
asciidoctor,asciidoc,Text::Markup::Asciidoctor,Text::Markup::Asciidoctor,asciidoc,asc,adoc |
| 131 |
bbcode,bbcode,Text::Markup::Bbcode,Parse::BBCode,bbcode,bb |
| 132 |
creole,creole,Text::Markup::Creole,Text::WikiCreole,creole |
| 133 |
none,none,Text::Markup::None,, |