summaryrefslogtreecommitdiff
path: root/t/linkify.t
blob: 1b297433a57e4ccc146da35ad9dc9d4c1fc3788e (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. }
  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(links_to("bar_baz", linkify("foo", "foo", "link to [[bar_baz]] ok", ["foo", "bar_baz"])), "ok link");
  59. ok(not_links_to("bar", linkify("foo", "foo", "link to \\[[bar]] ok", ["foo", "bar"])), "escaped link");
  60. ok(links_to("page=bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo"])), "broken link");
  61. ok(links_to("bar", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
  62. ok(links_to("baz", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
  63. ok(links_to("bar", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link");
  64. ok(links_text("some page", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link text");
  65. ok(links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link, with whitespace");
  66. ok(links_text("some page", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link text, with whitespace");
  67. ok(links_text("0", linkify("foo", "foo", "link to [[0|bar]] ok", ["foo", "bar"])), "named link to 0");
  68. 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");
  69. ok(links_to("foo/bar", linkify("foo/item", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "inline page link");
  70. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "same except not inline");
  71. ok(links_to("bar#baz", linkify("foo", "foo", "link to [[bar#baz]] ok", ["foo", "bar"])), "anchor link");