summaryrefslogtreecommitdiff
path: root/t/linkify.t
blob: d9c4c0474f04f2128f8eaa7bb73c35c1860530a4 (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. $destsources{$page}="$page.mdwn";
  20. }
  21. %config=IkiWiki::defaultconfig();
  22. $config{cgiurl}="http://somehost/ikiwiki.cgi";
  23. # currently coded for non usedirs mode (TODO: check both)
  24. $config{usedirs}=0;
  25. return IkiWiki::linkify($lpage, $page, $content);
  26. }
  27. sub links_to ($$) {
  28. my $link=shift;
  29. my $content=shift;
  30. if ($content =~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
  31. return 1;
  32. }
  33. else {
  34. print STDERR "# expected link to $link in $content\n";
  35. return;
  36. }
  37. }
  38. sub not_links_to ($$) {
  39. my $link=shift;
  40. my $content=shift;
  41. if ($content !~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
  42. return 1;
  43. }
  44. else {
  45. print STDERR "# expected no link to $link in $content\n";
  46. return;
  47. }
  48. }
  49. sub links_text ($$) {
  50. my $text=shift;
  51. my $content=shift;
  52. if ($content =~ m!>\Q$text\E</a>!) {
  53. return 1;
  54. }
  55. else {
  56. print STDERR "# expected link text $text in $content\n";
  57. return;
  58. }
  59. }
  60. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "bar"])), "ok link");
  61. ok(links_to("bar_baz", linkify("foo", "foo", "link to [[bar_baz]] ok", ["foo", "bar_baz"])), "ok link");
  62. ok(not_links_to("bar", linkify("foo", "foo", "link to \\[[bar]] ok", ["foo", "bar"])), "escaped link");
  63. ok(links_to("page=bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo"])), "broken link");
  64. ok(links_to("bar", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
  65. ok(links_to("baz", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
  66. ok(links_to("bar", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link");
  67. ok(links_text("some page", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link text");
  68. ok(links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link, with whitespace");
  69. ok(links_text("some page", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])), "named link text, with whitespace");
  70. ok(links_text("0", linkify("foo", "foo", "link to [[0|bar]] ok", ["foo", "bar"])), "named link to 0");
  71. 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");
  72. ok(links_to("foo/bar", linkify("foo/item", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "inline page link");
  73. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "same except not inline");
  74. ok(links_to("bar#baz", linkify("foo", "foo", "link to [[bar#baz]] ok", ["foo", "bar"])), "anchor link");