File: usemod.t

package info (click to toggle)
libwiki-toolkit-formatter-usemod-perl 0.25-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 176 kB
  • sloc: perl: 258; makefile: 2
file content (197 lines) | stat: -rw-r--r-- 7,081 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use strict;
local $^W = 1;

use Test::More tests => 34;

use_ok( "Wiki::Toolkit::Formatter::UseMod" );

print "#\n#### Testing default configuration\n#\n";
my $wikitext = <<WIKITEXT;

==== Welcome ====

This is some WikiText.

: This should be a
: definition list with data
: but no terms

==== LinkInAHeader ====

 pig
 pig

==== Header with an = in ====

 spaces  are significant

Another WikiWord.

WIKITEXT

my $formatter = Wiki::Toolkit::Formatter::UseMod->new;
isa_ok( $formatter, "Wiki::Toolkit::Formatter::UseMod" );
my $html = $formatter->format($wikitext);

like( $html, qr|<a href="wiki.pl\?WikiText">WikiText</a>|,
      "WikiWords made into links" );
like( $html, qr|<h4>Welcome</h4>|, "headings work" );
like( $html,
      qr|<h4><a href="wiki.pl\?LinkInAHeader">LinkInAHeader</a></h4>|,
      "...links work in headers" );
like( $html, qr|<h4>Header with an = in</h4>|, "...headers may contain =" );
like( $html, qr|<dl>\s*<dd>&nbsp;This should be a</dd>\s*<dd>&nbsp;definition list with data</dd>\s*<dd>&nbsp;but no terms</dd>\s*</dl>|,
      "leading : made into <dl>" );
like( $html, qr|<pre>\npig\npig\n</pre>|,
      "leading space makes <pre>" );

my @links = $formatter->find_internal_links($wikitext);
is_deeply( [ sort @links ], [ "LinkInAHeader", "WikiText", "WikiWord" ],
	   "find_internal_links seems to work" );
print "# Found internal links: " . join(", ", sort @links) . "\n";

print "#\n#### Testing HTML escaping\n#\n";
$wikitext = <<WIKITEXT;

&pound;

<i>
<strike>

WIKITEXT

$formatter = Wiki::Toolkit::Formatter::UseMod->new;
$html = $formatter->format($wikitext);
like( $html, qr|&pound;|, "Entities preserved by default." );
unlike( $html, qr|<strike>|, "HTML tags escaped by default" );

$formatter = Wiki::Toolkit::Formatter::UseMod->new( allowed_tags => [ "strike" ] );
$html = $formatter->format($wikitext);
like( $html, qr|<strike>|, "...but not when we allow them" );
unlike( $html, qr|<i>|, "...and ones we don't explicitly allow are escaped" );
like( $html, qr|&pound;|, "Entities still preserved." );

print "#\n#### Testing extended links\n#\n";
$wikitext = <<WIKITEXT;

This is an [[Extended Link]].

This is a lower-case [[extended link]].

This is an [[Extended, Link, With, Commas]].

This is a [[Extended Link|titled extended link]].

This is a [[Extended, Link, With, Commas|titled extended link with commas]].

This is a [[Extended Link Two | title with leading whitespace]].

This is [[Another Link|another titled link]].

WIKITEXT

my $have_mockobject;
eval { require Test::MockObject; };
if ( !$@ ) {
    $have_mockobject = 1;
}

SKIP: {
    skip "Can't find Test::MockObject", 20 unless $have_mockobject;

    my $wiki = Test::MockObject->new;
    $wiki->mock( "node_exists",
	          sub {
                        my ($self, $node) = @_;
                        if ( $node eq "Extended Link"
                             or $node eq "Extended, Link, With, Commas"
                             or $node eq "Extended Link Two"
		             or $node eq "Another Link" ) {
		            return 1;
		        } else {
		            return 0;
                        }
		      }
    );

    # Test with munged URLs.
    $formatter = Wiki::Toolkit::Formatter::UseMod->new( extended_links => 1,
                                                        munge_urls     => 1 );
    $html = $formatter->format($wikitext, $wiki);

    SKIP: {
        skip "Broken by Text::WikiFormat bug http://rt.cpan.org/Public/Bug/Display.html?id=34402", 5;

    like( $html, qr|<a href="wiki.pl\?Extended_Link">Extended Link</a>|,
          "extended links work" );
    like( $html, qr|<a href="wiki.pl\?Extended_Link">extended link</a>|,
          "...and are forced ucfirst" );
    like( $html, qr|<a href="wiki.pl\?Extended_Link">titled extended link</a>|,
          "...and titles work" );
    like( $html, qr|[^ ]title with leading whitespace|,
          "...and don't show leading whitespace" );
    like( $html, qr|<a href="wiki.pl\?Extended_Link_Two">|,
          "...and titled nodes with trailing whitespace are munged correctly "
          . "before formatting" );
    
    } # end SKIP

    # Test with munged URLs and implicit links turned off.
    $formatter = Wiki::Toolkit::Formatter::UseMod->new( extended_links => 1,
                                                        implicit_links => 0,
                                                        munge_urls     => 1 );
    $html = $formatter->format($wikitext, $wiki);
    like( $html, qr|<a href="wiki.pl\?Extended_Link">Extended Link</a>|,
          "extended links work" );
    like( $html, qr|<a href="wiki.pl\?Extended_Link">extended link</a>|,
          "...and are forced ucfirst" );
    like( $html, qr|<a href="wiki.pl\?Extended_Link">titled extended link</a>|,
          "...and titles work" );
    like( $html, qr|[^ ]title with leading whitespace|,
          "...and don't show leading whitespace" );
    like( $html, qr|<a href="wiki.pl\?Extended_Link_Two">|,
          "...and titled nodes with trailing whitespace are munged correctly "
          . "before formatting" );
    like( $html, qr/Extended%2C_Link%2C_With%2C_Commas/,
          "...commas in URLs are escaped by default" );
    $formatter = Wiki::Toolkit::Formatter::UseMod->new(
        extended_links => 1, implicit_links => 0, munge_urls => 1,
        escape_url_commas => 0 );
    $html = $formatter->format($wikitext, $wiki);
    unlike( $html, qr/%2C/,
            "...but not if we ask for them not to be" );

    # Test with unmunged URLs.
    $formatter = Wiki::Toolkit::Formatter::UseMod->new( extended_links => 1 );
    $html = $formatter->format($wikitext, $wiki);

    like( $html, qr|<a href="wiki.pl\?Extended%20Link">Extended Link</a>|,
          "extended links work with unmunged URLs" );
    like( $html, qr|<a href="wiki.pl\?Extended%20Link">extended link</a>|,
          "...and are forced ucfirst" );
    like( $html,
          qr|<a href="wiki.pl\?Extended%20Link">titled extended link</a>|,
          "...and titles work" );
    like( $html, qr/Extended%2C%20Link%2C%20With%2C%20Commas/,
          "...commas in URLs are escaped by default" );
    # Test with escape_url_commas = 0
    $formatter = Wiki::Toolkit::Formatter::UseMod->new(
        extended_links => 1, escape_url_commas => 0 );
    $html = $formatter->format($wikitext, $wiki);
    unlike( $html, qr/%2C/,
            "...but not if we ask for them not to be" );

    @links = $formatter->find_internal_links($wikitext);
    print "# Found links: " . join("; ", @links) . "\n";
    my %linkhash = map { $_ => 1 } @links;
    ok( ! defined $linkhash{"extended link"},
        "find_internal_links respects ucfirst" );
    ok( ! defined $linkhash{"Extended Link "},
        "...and drops trailing whitespace" );
    is_deeply( \@links,
               [ "Extended Link", "Extended Link",
                 "Extended, Link, With, Commas", "Extended Link",
                 "Extended, Link, With, Commas",
                 "Extended Link Two", "Another Link" ],
               "...and gets the right order" );
}