summaryrefslogtreecommitdiff
path: root/t/linkify.t
blob: 18b47dcb78cd8b7eb7a75552bc5cf2c89aeb10de (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Test::More tests => 15;
  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. }
  20. %config=IkiWiki::defaultconfig();
  21. $config{cgiurl}="http://somehost/ikiwiki.cgi";
  22. return IkiWiki::linkify($lpage, $page, $content);
  23. }
  24. sub links_to ($$) {
  25. my $link=shift;
  26. my $content=shift;
  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. sub not_links_to ($$) {
  36. my $link=shift;
  37. my $content=shift;
  38. if ($content !~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
  39. return 1;
  40. }
  41. else {
  42. print STDERR "# expected no link to $link in $content\n";
  43. return;
  44. }
  45. }
  46. sub links_text ($$) {
  47. my $text=shift;
  48. my $content=shift;
  49. if ($content =~ m!>\Q$text\E</a>!) {
  50. return 1;
  51. }
  52. else {
  53. print STDERR "# expected link text $text in $content\n";
  54. return;
  55. }
  56. }
  57. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "bar"])), "ok link");
  58. ok(not_links_to("bar", linkify("foo", "foo", "link to \\[[bar]] ok", ["foo", "bar"])), "escaped link");
  59. ok(links_to("page=bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo"])), "broken link");
  60. ok(links_to("bar", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
  61. ok(links_to("baz", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
  62. ok(links_to("bar", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link");
  63. ok(links_text("some page", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link text");
  64. ok(links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link, with whitespace");
  65. ok(links_text("some page", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link text, with whitespace");
  66. ok(links_text("0", linkify("foo", "foo", "link to [[0|bar]] ok", ["foo", "bar"])), "named link to 0");
  67. 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");
  68. ok(links_to("foo/bar", linkify("foo/item", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "inline page link");
  69. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "same except not inline");
  70. ok(links_to("bar#baz", linkify("foo", "foo", "link to [[bar#baz]] ok", ["foo", "bar"])), "anchor link");