summaryrefslogtreecommitdiff
path: root/t/linkify.t
blob: 6dff0a02954a7c61b5d05fc79a994d1486d2e005 (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Test::More tests => 32;
  5. BEGIN { use_ok("IkiWiki"); }
  6. # Initialize link plugin
  7. %config=IkiWiki::defaultconfig();
  8. IkiWiki::loadplugins();
  9. my $prefix_directives;
  10. sub linkify ($$$$) {
  11. my $lpage=shift;
  12. my $page=shift;
  13. my $content=shift;
  14. my @existing_pages=@{shift()};
  15. # This is what linkify and htmllink need set right now to work.
  16. # This could change, if so, update it..
  17. %IkiWiki::pagecase=();
  18. %links=();
  19. foreach my $p (@existing_pages) {
  20. $IkiWiki::pagecase{lc $p}=$p;
  21. $links{$p}=[];
  22. $renderedfiles{"$p.mdwn"}=[$p];
  23. $destsources{$p}="$p.mdwn";
  24. }
  25. %config=IkiWiki::defaultconfig();
  26. $config{cgiurl}="http://somehost/ikiwiki.cgi";
  27. $config{srcdir}=$config{destdir}="/dev/null"; # placate checkconfig
  28. # currently coded for non usedirs mode (TODO: check both)
  29. $config{usedirs}=0;
  30. $config{prefix_directives}=$prefix_directives;
  31. IkiWiki::checkconfig();
  32. return IkiWiki::linkify($lpage, $page, $content);
  33. }
  34. sub links_to ($$) {
  35. my $link=shift;
  36. my $content=shift;
  37. if ($content =~ m!<a href="[^"]*\Q$link\E[^"]*"\s*[^>]*>!) {
  38. return 1;
  39. }
  40. else {
  41. print STDERR "# expected link to $link in $content\n";
  42. return;
  43. }
  44. }
  45. sub not_links_to ($$) {
  46. my $link=shift;
  47. my $content=shift;
  48. if ($content !~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
  49. return 1;
  50. }
  51. else {
  52. print STDERR "# expected no link to $link in $content\n";
  53. return;
  54. }
  55. }
  56. sub links_text ($$) {
  57. my $text=shift;
  58. my $content=shift;
  59. if ($content =~ m!>\Q$text\E</a>!) {
  60. return 1;
  61. }
  62. else {
  63. print STDERR "# expected link text $text in $content\n";
  64. return;
  65. }
  66. }
  67. # Tests that are the same for both styles of prefix directives.
  68. foreach $prefix_directives (0,1) {
  69. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "bar"])), "ok link");
  70. ok(links_to("bar_baz", linkify("foo", "foo", "link to [[bar_baz]] ok", ["foo", "bar_baz"])), "ok link");
  71. ok(not_links_to("bar", linkify("foo", "foo", "link to \\[[bar]] ok", ["foo", "bar"])), "escaped link");
  72. ok(links_to("page=bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo"])), "broken link");
  73. ok(links_to("bar", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
  74. ok(links_to("baz", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
  75. ok(links_to("bar", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link");
  76. ok(links_text("some page", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link text");
  77. ok(links_text("0", linkify("foo", "foo", "link to [[0|bar]] ok", ["foo", "bar"])), "named link to 0");
  78. 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");
  79. ok(links_to("foo/bar", linkify("foo/item", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "inline page link");
  80. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "same except not inline");
  81. ok(links_to("bar#baz", linkify("foo", "foo", "link to [[bar#baz]] ok", ["foo", "bar"])), "anchor link");
  82. }
  83. $prefix_directives=0;
  84. ok(not_links_to("some_page", linkify("foo", "foo", "link to [[some page]] ok", ["foo", "bar", "some_page"])),
  85. "link with whitespace, without prefix_directives");
  86. ok(not_links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])),
  87. "named link, with whitespace, without prefix_directives");
  88. $prefix_directives=1;
  89. ok(links_to("some_page", linkify("foo", "foo", "link to [[some page]] ok", ["foo", "bar", "some_page"])),
  90. "link with whitespace");
  91. ok(links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])),
  92. "named link, with whitespace");
  93. ok(links_text("some page", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])),
  94. "named link text, with whitespace");