summaryrefslogtreecommitdiff
path: root/t/linkify.t
blob: d1d02cd2731cb2c170871344c6438a52347c982a (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Test::More tests => 13;
  5. sub linkify ($$$$) {
  6. my $lpage=shift;
  7. my $page=shift;
  8. my $content=shift;
  9. my @existing_pages=@{shift()};
  10. # This is what linkify and htmllink need set right now to work.
  11. # This could change, if so, update it..
  12. %IkiWiki::links=();
  13. foreach my $page (@existing_pages) {
  14. $IkiWiki::links{$page}=[];
  15. $IkiWiki::renderedfiles{"$page.mdwn"}=$page;
  16. }
  17. %IkiWiki::config=IkiWiki::defaultconfig();
  18. return IkiWiki::linkify($lpage, $page, $content);
  19. }
  20. sub links_to ($$) {
  21. my $link=shift;
  22. my $content=shift;
  23. if ($content =~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
  24. return 1;
  25. }
  26. else {
  27. print STDERR "# expected link to $link in $content\n";
  28. return;
  29. }
  30. }
  31. sub not_links_to ($$) {
  32. my $link=shift;
  33. my $content=shift;
  34. if ($content !~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
  35. return 1;
  36. }
  37. else {
  38. print STDERR "# expected no link to $link in $content\n";
  39. return;
  40. }
  41. }
  42. sub links_text ($$) {
  43. my $text=shift;
  44. my $content=shift;
  45. if ($content =~ m!>\Q$text\E</a>!) {
  46. return 1;
  47. }
  48. else {
  49. print STDERR "# expected link text $text in $content\n";
  50. return;
  51. }
  52. }
  53. BEGIN { use_ok("IkiWiki::Render"); }
  54. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "bar"])), "ok link");
  55. ok(not_links_to("bar", linkify("foo", "foo", "link to \\[[bar]] ok", ["foo", "bar"])), "escaped link");
  56. ok(links_to("page=bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo"])), "broken link");
  57. ok(links_to("bar", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
  58. ok(links_to("baz", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
  59. ok(links_to("bar", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link");
  60. ok(links_text("some page", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link text");
  61. ok(links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link, with whitespace");
  62. ok(links_text("some page", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link text, with whitespace");
  63. 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");
  64. ok(links_to("foo/bar", linkify("foo/item", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "inline page link");
  65. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "same except not inline");