* Rebuilding on upgrade to this version is recommended.
[ikiwiki] / t / linkify.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More tests => 11;
5
6 sub linkify ($$$) {
7         my $page=shift;
8         my $content=shift;
9         my @existing_pages=@{shift()};
10         
11         # This is what linkify and htmllink need set right now to work.
12         # This could change, if so, update it..
13         %IkiWiki::links=();
14         foreach my $page (@existing_pages) {
15                 $IkiWiki::links{$page}=[];
16                 $IkiWiki::renderedfiles{"$page.mdwn"}=$page;
17         }
18         %IkiWiki::config=IkiWiki::defaultconfig();
19
20         return IkiWiki::linkify($page, $content);
21 }
22
23 sub links_to ($$) {
24         my $link=shift;
25         my $content=shift;
26         
27         if ($content =~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
28                 return 1;
29         }
30         else {
31                 print STDERR "# expected link to $link in $content\n";
32                 return;
33         }
34 }
35
36 sub not_links_to ($$) {
37         my $link=shift;
38         my $content=shift;
39         
40         if ($content !~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
41                 return 1;
42         }
43         else {
44                 print STDERR "# expected no link to $link in $content\n";
45                 return;
46         }
47 }
48
49 sub links_text ($$) {
50         my $text=shift;
51         my $content=shift;
52         
53         if ($content =~ m!>\Q$text\E</a>!) {
54                 return 1;
55         }
56         else {
57                 print STDERR "# expected link text $text in $content\n";
58                 return;
59         }
60 }
61
62
63 BEGIN { use_ok("IkiWiki::Render"); }
64
65 ok(links_to("bar", linkify("foo", "link to [[bar]] ok", ["foo", "bar"])), "ok link");
66 ok(not_links_to("bar", linkify("foo", "link to \\[[bar]] ok", ["foo", "bar"])), "escaped link");
67 ok(links_to("page=bar", linkify("foo", "link to [[bar]] ok", ["foo"])), "broken link");
68 ok(links_to("bar", linkify("foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
69 ok(links_to("baz", linkify("foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
70 ok(links_to("bar", linkify("foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link");
71 ok(links_text("some page", linkify("foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link text");
72 ok(links_to("bar", linkify("foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link, with whitespace");
73 ok(links_text("some page", linkify("foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link text, with whitespace");
74 ok(links_text("Some long, & complex page name.", linkify("foo", "link to [[Some long, & complex page name.|bar]] ok, and this is not a link]] here", ["foo", "bar"])), "complex named link text");