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