Add new preprocessor directive syntax¸ using a '!' prefix.
[ikiwiki] / 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         $config{srcdir}=$config{destdir}="/dev/null"; # placate checkconfig
28         # currently coded for non usedirs mode (TODO: check both)
29         $config{usedirs}=0;
30
31         # currently coded for prefix_directives=0 (TODO: check both)
32         # Not setting $config{prefix_directives}=0 explicitly; instead, let the
33         # tests break if the default changes, as a reminder to update the
34         # tests.
35
36         IkiWiki::checkconfig();
37
38         return IkiWiki::linkify($lpage, $page, $content);
39 }
40
41 sub links_to ($$) {
42         my $link=shift;
43         my $content=shift;
44         
45         if ($content =~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
46                 return 1;
47         }
48         else {
49                 print STDERR "# expected link to $link in $content\n";
50                 return;
51         }
52 }
53
54 sub not_links_to ($$) {
55         my $link=shift;
56         my $content=shift;
57         
58         if ($content !~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
59                 return 1;
60         }
61         else {
62                 print STDERR "# expected no link to $link in $content\n";
63                 return;
64         }
65 }
66
67 sub links_text ($$) {
68         my $text=shift;
69         my $content=shift;
70         
71         if ($content =~ m!>\Q$text\E</a>!) {
72                 return 1;
73         }
74         else {
75                 print STDERR "# expected link text $text in $content\n";
76                 return;
77         }
78 }
79
80
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(not_links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link, with whitespace");
90 ok(not_links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link text, with whitespace");
91 ok(links_text("0", linkify("foo", "foo", "link to [[0|bar]] ok", ["foo", "bar"])), "named link to 0");
92 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");
93 ok(links_to("foo/bar", linkify("foo/item", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "inline page link");
94 ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "same except not inline");
95 ok(links_to("bar#baz", linkify("foo", "foo", "link to [[bar#baz]] ok", ["foo", "bar"])), "anchor link");