c7bfbcd382c30c5e15cddd725efc94e3553e3ec1
[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 # currently coded for non usedirs mode (TODO: check both)
9 $config{usedirs}=0;
10
11 sub linkify ($$$$) {
12         my $lpage=shift;
13         my $page=shift;
14
15         my $content=shift;
16         my @existing_pages=@{shift()};
17         
18         # This is what linkify and htmllink need set right now to work.
19         # This could change, if so, update it..
20         %IkiWiki::pagecase=();
21         %links=();
22         foreach my $page (@existing_pages) {
23                 $IkiWiki::pagecase{lc $page}=$page;
24                 $links{$page}=[];
25                 $renderedfiles{"$page.mdwn"}=[$page];
26                 $destsources{$page}="$page.mdwn";
27         }
28         %config=IkiWiki::defaultconfig();
29         $config{cgiurl}="http://somehost/ikiwiki.cgi";
30
31         return IkiWiki::linkify($lpage, $page, $content);
32 }
33
34 sub links_to ($$) {
35         my $link=shift;
36         my $content=shift;
37         
38         if ($content =~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
39                 return 1;
40         }
41         else {
42                 print STDERR "# expected link to $link in $content\n";
43                 return;
44         }
45 }
46
47 sub not_links_to ($$) {
48         my $link=shift;
49         my $content=shift;
50         
51         if ($content !~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
52                 return 1;
53         }
54         else {
55                 print STDERR "# expected no link to $link in $content\n";
56                 return;
57         }
58 }
59
60 sub links_text ($$) {
61         my $text=shift;
62         my $content=shift;
63         
64         if ($content =~ m!>\Q$text\E</a>!) {
65                 return 1;
66         }
67         else {
68                 print STDERR "# expected link text $text in $content\n";
69                 return;
70         }
71 }
72
73
74 ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "bar"])), "ok link");
75 ok(links_to("bar_baz", linkify("foo", "foo", "link to [[bar_baz]] ok", ["foo", "bar_baz"])), "ok link");
76 ok(not_links_to("bar", linkify("foo", "foo", "link to \\[[bar]] ok", ["foo", "bar"])), "escaped link");
77 ok(links_to("page=bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo"])), "broken link");
78 ok(links_to("bar", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
79 ok(links_to("baz", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
80 ok(links_to("bar", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link");
81 ok(links_text("some page", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link text");
82 ok(links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link, with whitespace");
83 ok(links_text("some page", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link text, with whitespace");
84 ok(links_text("0", linkify("foo", "foo", "link to [[0|bar]] ok", ["foo", "bar"])), "named link to 0");
85 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");
86 ok(links_to("foo/bar", linkify("foo/item", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "inline page link");
87 ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "same except not inline");
88 ok(links_to("bar#baz", linkify("foo", "foo", "link to [[bar#baz]] ok", ["foo", "bar"])), "anchor link");