]> sipb.mit.edu Git - ikiwiki.git/blob - t/linkify.t
Added a comment
[ikiwiki.git] / t / linkify.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More tests => 32;
5
6 BEGIN { use_ok("IkiWiki"); }
7
8 # Initialize link plugin
9 %config=IkiWiki::defaultconfig();
10 IkiWiki::loadplugins();
11
12 my $prefix_directives;
13
14 sub linkify ($$$$) {
15         my $lpage=shift;
16         my $page=shift;
17
18         my $content=shift;
19         my @existing_pages=@{shift()};
20         
21         # This is what linkify and htmllink need set right now to work.
22         # This could change, if so, update it..
23         %IkiWiki::pagecase=();
24         %links=();
25         foreach my $p (@existing_pages) {
26                 $IkiWiki::pagecase{lc $p}=$p;
27                 $links{$p}=[];
28                 $renderedfiles{"$p.mdwn"}=[$p];
29                 $destsources{$p}="$p.mdwn";
30         }
31
32         %config=IkiWiki::defaultconfig();
33         $config{cgiurl}="http://somehost/ikiwiki.cgi";
34         $config{srcdir}=$config{destdir}="/dev/null"; # placate checkconfig
35         # currently coded for non usedirs mode (TODO: check both)
36         $config{usedirs}=0;
37         $config{prefix_directives}=$prefix_directives;
38
39         IkiWiki::checkconfig();
40
41         return IkiWiki::linkify($lpage, $page, $content);
42 }
43
44 sub links_to ($$) {
45         my $link=shift;
46         my $content=shift;
47         
48         if ($content =~ m!<a href="[^"]*\Q$link\E[^"]*"\s*[^>]*>!) {
49                 return 1;
50         }
51         else {
52                 print STDERR "# expected link to $link in $content\n";
53                 return;
54         }
55 }
56
57 sub not_links_to ($$) {
58         my $link=shift;
59         my $content=shift;
60         
61         if ($content !~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
62                 return 1;
63         }
64         else {
65                 print STDERR "# expected no link to $link in $content\n";
66                 return;
67         }
68 }
69
70 sub links_text ($$) {
71         my $text=shift;
72         my $content=shift;
73         
74         if ($content =~ m!>\Q$text\E</a>!) {
75                 return 1;
76         }
77         else {
78                 print STDERR "# expected link text $text in $content\n";
79                 return;
80         }
81 }
82
83 # Tests that are the same for both styles of prefix directives.
84 foreach $prefix_directives (0,1) {
85         ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "bar"])), "ok link");
86         ok(links_to("bar_baz", linkify("foo", "foo", "link to [[bar_baz]] ok", ["foo", "bar_baz"])), "ok link");
87         ok(not_links_to("bar", linkify("foo", "foo", "link to \\[[bar]] ok", ["foo", "bar"])), "escaped link");
88         ok(links_to("page=bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo"])), "broken link");
89         ok(links_to("bar", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
90         ok(links_to("baz", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
91         ok(links_to("bar", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link");
92         ok(links_text("some page", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link text");
93         ok(links_text("0", linkify("foo", "foo", "link to [[0|bar]] ok", ["foo", "bar"])), "named link to 0");
94         ok(links_text("Some long, & complex page name.", linkify("foo", "foo", "link to [[Some_long,_&_complex_page_name.|bar]] ok, and this is not a link]] here", ["foo", "bar"])), "complex named link text");
95         ok(links_to("foo/bar", linkify("foo/item", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "inline page link");
96         ok(links_to("bar",     linkify("foo",      "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "same except not inline");
97         ok(links_to("bar#baz", linkify("foo",      "foo", "link to [[bar#baz]] ok", ["foo", "bar"])), "anchor link");
98 }
99
100 $prefix_directives=0;
101 ok(not_links_to("some_page", linkify("foo", "foo", "link to [[some page]] ok", ["foo", "bar", "some_page"])),
102         "link with whitespace, without prefix_directives");
103 ok(not_links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])),
104         "named link, with whitespace, without prefix_directives");
105
106 $prefix_directives=1;
107 ok(links_to("some_page", linkify("foo", "foo", "link to [[some page]] ok", ["foo", "bar", "some_page"])),
108         "link with whitespace");
109 ok(links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])),
110         "named link, with whitespace");
111 ok(links_text("some page", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])),
112         "named link text, with whitespace");