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