]> sipb.mit.edu Git - ikiwiki.git/blob - t/linkify.t
close
[ikiwiki.git] / t / linkify.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More tests => 16;
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                 $destsources{$page}="$page.mdwn";
24         }
25         %config=IkiWiki::defaultconfig();
26         $config{cgiurl}="http://somehost/ikiwiki.cgi";
27         # currently coded for non usedirs mode (TODO: check both)
28         $config{usedirs}=0;
29
30         return IkiWiki::linkify($lpage, $page, $content);
31 }
32
33 sub links_to ($$) {
34         my $link=shift;
35         my $content=shift;
36         
37         if ($content =~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
38                 return 1;
39         }
40         else {
41                 print STDERR "# expected link to $link in $content\n";
42                 return;
43         }
44 }
45
46 sub not_links_to ($$) {
47         my $link=shift;
48         my $content=shift;
49         
50         if ($content !~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
51                 return 1;
52         }
53         else {
54                 print STDERR "# expected no link to $link in $content\n";
55                 return;
56         }
57 }
58
59 sub links_text ($$) {
60         my $text=shift;
61         my $content=shift;
62         
63         if ($content =~ m!>\Q$text\E</a>!) {
64                 return 1;
65         }
66         else {
67                 print STDERR "# expected link text $text in $content\n";
68                 return;
69         }
70 }
71
72
73 ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "bar"])), "ok link");
74 ok(links_to("bar_baz", linkify("foo", "foo", "link to [[bar_baz]] ok", ["foo", "bar_baz"])), "ok link");
75 ok(not_links_to("bar", linkify("foo", "foo", "link to \\[[bar]] ok", ["foo", "bar"])), "escaped link");
76 ok(links_to("page=bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo"])), "broken link");
77 ok(links_to("bar", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
78 ok(links_to("baz", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
79 ok(links_to("bar", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link");
80 ok(links_text("some page", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link text");
81 ok(links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link, with whitespace");
82 ok(links_text("some page", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link text, with whitespace");
83 ok(links_text("0", linkify("foo", "foo", "link to [[0|bar]] ok", ["foo", "bar"])), "named link to 0");
84 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");
85 ok(links_to("foo/bar", linkify("foo/item", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "inline page link");
86 ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "same except not inline");
87 ok(links_to("bar#baz", linkify("foo", "foo", "link to [[bar#baz]] ok", ["foo", "bar"])), "anchor link");