| 1 |
#!/usr/bin/perl |
| 2 |
use warnings; |
| 3 |
use strict; |
| 4 |
|
| 5 |
use Test::More; |
| 6 |
use File::Spec; |
| 7 |
use HTML::Entities; |
| 8 |
use HTML::WikiConverter; |
| 9 |
*e = \&encode_entities; |
| 10 |
|
| 11 |
my $more_tests = <<END_TESTS; |
| 12 |
__NEXT__ |
| 13 |
entities (1) |
| 14 |
__H__ |
| 15 |
To enter a '<' in your input, use "&lt;" |
| 16 |
__W__ |
| 17 |
To enter a '<' in your input, use "&lt;" |
| 18 |
__NEXT__ |
| 19 |
entities (2) |
| 20 |
__H__ |
| 21 |
To enter a '<' in your input, use "&lt;" |
| 22 |
__W__ |
| 23 |
To enter a '<' in your input, use "&lt;" |
| 24 |
__NEXT__ |
| 25 |
strip comments |
| 26 |
__H__ |
| 27 |
A <!-- stripped --> comment |
| 28 |
__W__ |
| 29 |
A comment |
| 30 |
__NEXT__ |
| 31 |
strip head |
| 32 |
__H__ |
| 33 |
<html> |
| 34 |
<head><title>fun stuff</title></head> |
| 35 |
<body> |
| 36 |
<p>Crazy stuff here</p> |
| 37 |
</body> |
| 38 |
</html> |
| 39 |
__W__ |
| 40 |
Crazy stuff here |
| 41 |
__NEXT__ |
| 42 |
strip scripts |
| 43 |
__H__ |
| 44 |
<html> |
| 45 |
<head><script>bogus stuff</script></head> |
| 46 |
<body> |
| 47 |
<script>maliciousCode()</script> |
| 48 |
<p>benevolent text</p> |
| 49 |
</body> |
| 50 |
</html> |
| 51 |
__W__ |
| 52 |
benevolent text |
| 53 |
END_TESTS |
| 54 |
|
| 55 |
sub runtests { |
| 56 |
my %arg = @_; |
| 57 |
|
| 58 |
$arg{strip_comments} = 1; |
| 59 |
$arg{wrap_in_html} = 1; |
| 60 |
$arg{base_uri} ||= 'http://www.test.com'; |
| 61 |
my $minimal = $arg{minimal} || 0; |
| 62 |
|
| 63 |
my $data = $arg{data} || ''; |
| 64 |
$data .= entity_tests() . $more_tests unless $minimal; |
| 65 |
|
| 66 |
my @tests = split /__NEXT__\n/, $data; |
| 67 |
my $numtests = @tests; |
| 68 |
#$numtests += 1 unless $minimal; # file test |
| 69 |
plan tests => $numtests; |
| 70 |
|
| 71 |
# Delete unrecognized HTML::WikiConverter options |
| 72 |
delete $arg{$_} for qw/ data minimal /; |
| 73 |
|
| 74 |
my $wc = new HTML::WikiConverter(%arg); |
| 75 |
foreach my $test ( @tests ) { |
| 76 |
$test =~ s/^(.*?)\n//; my $name = $1; |
| 77 |
my( $html, $wiki ) = split /__W__\n/, $test; |
| 78 |
$html =~ s/__H__\n//; |
| 79 |
|
| 80 |
for( $html, $wiki ) { s/^\n+//; s/\n+$// } |
| 81 |
is( $wc->html2wiki($html), $wiki, $name ); |
| 82 |
} |
| 83 |
|
| 84 |
#file_test($wc) unless $minimal; |
| 85 |
} |
| 86 |
|
| 87 |
sub entity_tests { |
| 88 |
my $tmpl = "__NEXT__\n%s\n__H__\n%s\n__W__\n%s\n"; # test-name, html-input, expected-wiki-output |
| 89 |
|
| 90 |
my $data = ''; |
| 91 |
my @chars = ( '<', '>', '&' ); |
| 92 |
foreach my $char ( @chars ) { |
| 93 |
( my $charname = e($char) ) =~ s/[&;]//g; |
| 94 |
$data .= sprintf $tmpl, "literal ($charname)", $char, e($char) |
| 95 |
. sprintf $tmpl, "encode ($charname)", e($char), e($char) |
| 96 |
. sprintf $tmpl, "meta ($charname)", e(e($char)), e(e($char)); |
| 97 |
} |
| 98 |
|
| 99 |
return $data; |
| 100 |
} |
| 101 |
|
| 102 |
sub _slurp { |
| 103 |
my $path = shift; |
| 104 |
open H, $path or die "couldn't open $path: $!"; |
| 105 |
local $/; |
| 106 |
my $c = <H>; |
| 107 |
close H; |
| 108 |
return $c; |
| 109 |
} |
| 110 |
|
| 111 |
sub file_test { |
| 112 |
my $wc = shift; |
| 113 |
my $lc_dialect = lc $wc->dialect; |
| 114 |
my $infile = File::Spec->catfile( 't', 'complete.html' ); |
| 115 |
my $outfile = File::Spec->catfile( 't', "complete.$lc_dialect" ); |
| 116 |
|
| 117 |
SKIP: { |
| 118 |
skip "Couldn't find $infile (ignore this)", 1 unless -e $infile; |
| 119 |
skip "Couldn't find $outfile (ignore this)", 1 unless -e $outfile; |
| 120 |
my( $got, $expect ) = ( $wc->html2wiki( file => $infile, slurp => 1 ), _slurp($outfile) ); |
| 121 |
for( $got, $expect ) { s/^\n+//; s/\n+$// } |
| 122 |
is( $got, $expect, 'read from file' ); |
| 123 |
}; |
| 124 |
} |
| 125 |
|
| 126 |
1; |