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