]> sipb.mit.edu Git - ikiwiki.git/blob - t/linkify.t
web commit by tuomov
[ikiwiki.git] / t / linkify.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More tests => 13;
5
6 BEGIN { use_ok("IkiWiki"); }
7
8 sub linkify ($$$$) {
9         my $lpage=shift;
10         my $page=shift;
11
12         my $content=shift;
13         my @existing_pages=@{shift()};
14         
15         # This is what linkify and htmllink need set right now to work.
16         # This could change, if so, update it..
17         %IkiWiki::pagecase=();
18         %links=();
19         foreach my $page (@existing_pages) {
20                 $IkiWiki::pagecase{lc $page}=$page;
21                 $links{$page}=[];
22                 $renderedfiles{"$page.mdwn"}=[$page];
23         }
24         %config=IkiWiki::defaultconfig();
25         $config{cgiurl}="http://somehost/ikiwiki.cgi";
26
27         return IkiWiki::linkify($lpage, $page, $content);
28 }
29
30 sub links_to ($$) {
31         my $link=shift;
32         my $content=shift;
33         
34         if ($content =~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
35                 return 1;
36         }
37         else {
38                 print STDERR "# expected link to $link in $content\n";
39                 return;
40         }
41 }
42
43 sub not_links_to ($$) {
44         my $link=shift;
45         my $content=shift;
46         
47         if ($content !~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
48                 return 1;
49         }
50         else {
51                 print STDERR "# expected no link to $link in $content\n";
52                 return;
53         }
54 }
55
56 sub links_text ($$) {
57         my $text=shift;
58         my $content=shift;
59         
60         if ($content =~ m!>\Q$text\E</a>!) {
61                 return 1;
62         }
63         else {
64                 print STDERR "# expected link text $text in $content\n";
65                 return;
66         }
67 }
68
69
70 ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "bar"])), "ok link");
71 ok(not_links_to("bar", linkify("foo", "foo", "link to \\[[bar]] ok", ["foo", "bar"])), "escaped link");
72 ok(links_to("page=bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo"])), "broken link");
73 ok(links_to("bar", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
74 ok(links_to("baz", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
75 ok(links_to("bar", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link");
76 ok(links_text("some page", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link text");
77 ok(links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link, with whitespace");
78 ok(links_text("some page", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link text, with whitespace");
79 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");
80 ok(links_to("foo/bar", linkify("foo/item", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "inline page link");
81 ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "same except not inline");
82