summaryrefslogtreecommitdiff
path: root/t/linkify.t
blob: 400e2e8932feb2c0d5f444e09b5037c111f7f651 (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Test::More tests => 16;
  5. BEGIN { use_ok("IkiWiki"); }
  6. sub linkify ($$$$) {
  7. my $lpage=shift;
  8. my $page=shift;
  9. my $content=shift;
  10. my @existing_pages=@{shift()};
  11. # This is what linkify and htmllink need set right now to work.
  12. # This could change, if so, update it..
  13. %IkiWiki::pagecase=();
  14. %links=();
  15. foreach my $page (@existing_pages) {
  16. $IkiWiki::pagecase{lc $page}=$page;
  17. $links{$page}=[];
  18. $renderedfiles{"$page.mdwn"}=[$page];
  19. $destsources{$page}="$page.mdwn";
  20. }
  21. %config=IkiWiki::defaultconfig();
  22. $config{cgiurl}="http://somehost/ikiwiki.cgi";
  23. $config{srcdir}=$config{destdir}="/dev/null"; # placate checkconfig
  24. # currently coded for non usedirs mode (TODO: check both)
  25. $config{usedirs}=0;
  26. # currently coded for prefix_directives=0 (TODO: check both)
  27. # Not setting $config{prefix_directives}=0 explicitly; instead, let the
  28. # tests break if the default changes, as a reminder to update the
  29. # tests.
  30. IkiWiki::checkconfig();
  31. return IkiWiki::linkify($lpage, $page, $content);
  32. }
  33. sub links_to ($$) {
  34. my $link=shift;
  35. my $content=shift;
  36. if ($content =~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
  37. return 1;
  38. }
  39. else {
  40. print STDERR "# expected link to $link in $content\n";
  41. return;
  42. }
  43. }
  44. sub not_links_to ($$) {
  45. my $link=shift;
  46. my $content=shift;
  47. if ($content !~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
  48. return 1;
  49. }
  50. else {
  51. print STDERR "# expected no link to $link in $content\n";
  52. return;
  53. }
  54. }
  55. sub links_text ($$) {
  56. my $text=shift;
  57. my $content=shift;
  58. if ($content =~ m!>\Q$text\E</a>!) {
  59. return 1;
  60. }
  61. else {
  62. print STDERR "# expected link text $text in $content\n";
  63. return;
  64. }
  65. }
  66. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "bar"])), "ok link");
  67. ok(links_to("bar_baz", linkify("foo", "foo", "link to [[bar_baz]] ok", ["foo", "bar_baz"])), "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(not_links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link, with whitespace");
  75. ok(not_links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link text, with whitespace");
  76. ok(links_text("0", linkify("foo", "foo", "link to [[0|bar]] ok", ["foo", "bar"])), "named link to 0");
  77. 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");
  78. ok(links_to("foo/bar", linkify("foo/item", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "inline page link");
  79. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "same except not inline");
  80. ok(links_to("bar#baz", linkify("foo", "foo", "link to [[bar#baz]] ok", ["foo", "bar"])), "anchor link");