summaryrefslogtreecommitdiff
path: root/t/linkify.t
blob: 2ed3face30db096b4ade26ccf9f93544393d3cb5 (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::pagecase=();
  13. %IkiWiki::links=();
  14. foreach my $page (@existing_pages) {
  15. $IkiWiki::pagecase{lc $page}=$page;
  16. $IkiWiki::links{$page}=[];
  17. $IkiWiki::renderedfiles{"$page.mdwn"}=$page;
  18. }
  19. %IkiWiki::config=IkiWiki::defaultconfig();
  20. return IkiWiki::linkify($lpage, $page, $content);
  21. }
  22. sub links_to ($$) {
  23. my $link=shift;
  24. my $content=shift;
  25. if ($content =~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
  26. return 1;
  27. }
  28. else {
  29. print STDERR "# expected link to $link in $content\n";
  30. return;
  31. }
  32. }
  33. sub not_links_to ($$) {
  34. my $link=shift;
  35. my $content=shift;
  36. if ($content !~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
  37. return 1;
  38. }
  39. else {
  40. print STDERR "# expected no link to $link in $content\n";
  41. return;
  42. }
  43. }
  44. sub links_text ($$) {
  45. my $text=shift;
  46. my $content=shift;
  47. if ($content =~ m!>\Q$text\E</a>!) {
  48. return 1;
  49. }
  50. else {
  51. print STDERR "# expected link text $text in $content\n";
  52. return;
  53. }
  54. }
  55. BEGIN { use_ok("IkiWiki::Render"); }
  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");