summaryrefslogtreecommitdiff
path: root/t/linkify.t
blob: 962456c30c2930779c98966e1783da0234ff1045 (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. return IkiWiki::linkify($lpage, $page, $content);
  24. }
  25. sub links_to ($$) {
  26. my $link=shift;
  27. my $content=shift;
  28. if ($content =~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
  29. return 1;
  30. }
  31. else {
  32. print STDERR "# expected link to $link in $content\n";
  33. return;
  34. }
  35. }
  36. sub not_links_to ($$) {
  37. my $link=shift;
  38. my $content=shift;
  39. if ($content !~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
  40. return 1;
  41. }
  42. else {
  43. print STDERR "# expected no link to $link in $content\n";
  44. return;
  45. }
  46. }
  47. sub links_text ($$) {
  48. my $text=shift;
  49. my $content=shift;
  50. if ($content =~ m!>\Q$text\E</a>!) {
  51. return 1;
  52. }
  53. else {
  54. print STDERR "# expected link text $text in $content\n";
  55. return;
  56. }
  57. }
  58. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "bar"])), "ok link");
  59. ok(links_to("bar_baz", linkify("foo", "foo", "link to [[bar_baz]] ok", ["foo", "bar_baz"])), "ok link");
  60. ok(not_links_to("bar", linkify("foo", "foo", "link to \\[[bar]] ok", ["foo", "bar"])), "escaped link");
  61. ok(links_to("page=bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo"])), "broken link");
  62. ok(links_to("bar", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
  63. ok(links_to("baz", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
  64. ok(links_to("bar", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link");
  65. ok(links_text("some page", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link text");
  66. ok(links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link, with whitespace");
  67. ok(links_text("some page", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link text, with whitespace");
  68. ok(links_text("0", linkify("foo", "foo", "link to [[0|bar]] ok", ["foo", "bar"])), "named link to 0");
  69. 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");
  70. ok(links_to("foo/bar", linkify("foo/item", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "inline page link");
  71. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "same except not inline");
  72. ok(links_to("bar#baz", linkify("foo", "foo", "link to [[bar#baz]] ok", ["foo", "bar"])), "anchor link");