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