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