source: cgi-wiki-formatter-usemod/trunk/t/usemod.t @ 131

Last change on this file since 131 was 131, checked in by kake, 17 years ago

0.12 - Indentation causes <pre>.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1use strict;
2local $^W = 1;
3
4use Test::More tests => 25;
5use Test::MockObject;
6
7use_ok( "CGI::Wiki::Formatter::UseMod" );
8
9print "#\n#### Testing default configuration\n#\n";
10my $wikitext = <<WIKITEXT;
11
12==== Welcome ====
13
14This is some WikiText.
15
16: This should be a
17: definition list with data
18: but no terms
19
20==== LinkInAHeader ====
21
22 pig
23 pig
24
25==== Header with an = in ====
26
27 spaces  are significant
28
29Another WikiWord.
30
31WIKITEXT
32
33my $formatter = CGI::Wiki::Formatter::UseMod->new;
34isa_ok( $formatter, "CGI::Wiki::Formatter::UseMod" );
35my $html = $formatter->format($wikitext);
36
37like( $html, qr|<a href="wiki.pl\?WikiText">WikiText</a>|,
38      "WikiWords made into links" );
39like( $html, qr|<h4>Welcome</h4>|, "headings work" );
40like( $html,
41      qr|<h4><a href="wiki.pl\?LinkInAHeader">LinkInAHeader</a></h4>|,
42      "...links work in headers" );
43like( $html, qr|<h4>Header with an = in</h4>|, "...headers may contain =" );
44like( $html, qr|<dl>\s*<dt><dd>This should be a\s*<dt><dd>definition list with data\s*<dt><dd>but no terms\s*</dl>|,
45      "leading : made into <dl>" );
46like( $html, qr|<pre>\npig\npig\n</pre>|,
47      "leading space makes <pre>" );
48
49my @links = $formatter->find_internal_links($wikitext);
50is_deeply( [ sort @links ], [ "LinkInAHeader", "WikiText", "WikiWord" ],
51           "find_internal_links seems to work" );
52print "# Found internal links: " . join(", ", sort @links) . "\n";
53
54print "#\n#### Testing HTML escaping\n#\n";
55$wikitext = <<WIKITEXT;
56
57&pound;
58
59<i>
60<strike>
61
62WIKITEXT
63
64$formatter = CGI::Wiki::Formatter::UseMod->new;
65$html = $formatter->format($wikitext);
66like( $html, qr|&pound;|, "Entities preserved by default." );
67unlike( $html, qr|<strike>|, "HTML tags escaped by default" );
68
69$formatter = CGI::Wiki::Formatter::UseMod->new( allowed_tags => [ "strike" ] );
70$html = $formatter->format($wikitext);
71like( $html, qr|<strike>|, "...but not when we allow them" );
72unlike( $html, qr|<i>|, "...and ones we don't explicitly allow are escaped" );
73like( $html, qr|&pound;|, "Entities still preserved." );
74
75print "#\n#### Testing extended links\n#\n";
76$wikitext = <<WIKITEXT;
77
78This is an [[Extended Link]].
79
80This is a lower-case [[extended link]].
81
82This is a [[Extended Link|titled extended link]].
83
84This is a [[Extended Link Two | title with leading whitespace]].
85
86This is [[Another Link|another titled link]].
87
88WIKITEXT
89
90my $wiki = Test::MockObject->new;
91$wiki->mock( "node_exists",
92            sub { my ($self, $node) = @_;
93                  if ( $node eq "Extended Link" or $node eq "Extended Link Two"
94                       or $node eq "Another Link" ) {
95                      return 1;
96                  } else {
97                      return 0;
98                  }
99                }
100);
101
102# Test with munged URLs.
103$formatter = CGI::Wiki::Formatter::UseMod->new( extended_links => 1,
104                                                munge_urls     => 1 );
105$html = $formatter->format($wikitext, $wiki);
106
107like( $html, qr|<a href="wiki.pl\?Extended_Link">Extended Link</a>|,
108      "extended links work" );
109like( $html, qr|<a href="wiki.pl\?Extended_Link">extended link</a>|,
110      "...and are forced ucfirst" );
111like( $html, qr|<a href="wiki.pl\?Extended_Link">titled extended link</a>|,
112      "...and titles work" );
113like( $html, qr|[^ ]title with leading whitespace|,
114      "...and don't show leading whitespace" );
115like( $html, qr|<a href="wiki.pl\?Extended_Link_Two">|,
116      "...and titled nodes with trailing whitespace are munged correctly before formatting" );
117
118# Test with unmunged URLs.
119$formatter = CGI::Wiki::Formatter::UseMod->new( extended_links => 1 );
120$html = $formatter->format($wikitext, $wiki);
121
122like( $html, qr|<a href="wiki.pl\?Extended%20Link">Extended Link</a>|,
123      "extended links work with unmunged URLs" );
124like( $html, qr|<a href="wiki.pl\?Extended%20Link">extended link</a>|,
125      "...and are forced ucfirst" );
126like( $html, qr|<a href="wiki.pl\?Extended%20Link">titled extended link</a>|,
127      "...and titles work" );
128
129@links = $formatter->find_internal_links($wikitext);
130print "# Found links: " . join(", ", @links) . "\n";
131my %linkhash = map { $_ => 1 } @links;
132ok( ! defined $linkhash{"extended link"},
133    "find_internal_links respects ucfirst" );
134ok( ! defined $linkhash{"Extended Link "},
135    "...and drops trailing whitespace" );
136is_deeply( \@links, [ "Extended Link", "Extended Link", "Extended Link", "Extended Link Two", "Another Link" ], "...and gets the right order" );
Note: See TracBrowser for help on using the repository browser.