summaryrefslogtreecommitdiff
path: root/t/preprocess.t
blob: 7bb9878d0fdbfd16d2db04af6190bf11209c50c2 (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Test::More tests => 31;
  5. BEGIN { use_ok("IkiWiki"); }
  6. $IkiWiki::hooks{preprocess}{foo}{call}=sub {
  7. my @bits;
  8. while (@_) {
  9. my $key=shift;
  10. my $value=shift;
  11. next if $key eq 'page' || $key eq 'destpage' || $key eq 'preview';
  12. if (length $value) {
  13. push @bits, "$key => $value";
  14. }
  15. else {
  16. push @bits, $key;
  17. }
  18. }
  19. return "foo(".join(", ", @bits).")";
  20. };
  21. is(IkiWiki::preprocess("foo", "foo", "[[foo]]", 0, 0), "[[foo]]", "not wikilink");
  22. is(IkiWiki::preprocess("foo", "foo", "[[foo ]]", 0, 0), "foo()", "simple");
  23. is(IkiWiki::preprocess("foo", "foo", "[[!foo ]]", 0, 0), "foo()", "prefixed");
  24. is(IkiWiki::preprocess("foo", "foo", "[[!foo]]", 0, 0), "[[!foo]]", "prefixed, no space");
  25. is(IkiWiki::preprocess("foo", "foo", "[[foo a=1]]", 0, 0), "foo(a => 1)");
  26. is(IkiWiki::preprocess("foo", "foo", q{[[foo a="1"]]}, 0, 0), "foo(a => 1)");
  27. is(IkiWiki::preprocess("foo", "foo", q{[[foo a="""1"""]]}, 0, 0), "foo(a => 1)");
  28. is(IkiWiki::preprocess("foo", "foo", q{[[foo a=""]]}, 0, 0), "foo(a)");
  29. is(IkiWiki::preprocess("foo", "foo", q{[[foo a="" b="1"]]}, 0, 0), "foo(a, b => 1)");
  30. is(IkiWiki::preprocess("foo", "foo", q{[[foo a=""""""]]}, 0, 0), "foo(a)");
  31. is(IkiWiki::preprocess("foo", "foo", q{[[foo a="""""" b="1"]]}, 0, 0), "foo(a, b => 1)");
  32. is(IkiWiki::preprocess("foo", "foo", q{[[foo a="""""" b="""1"""]]}, 0, 0), "foo(a, b => 1)");
  33. is(IkiWiki::preprocess("foo", "foo", q{[[foo a="""""" b=""""""]]}, 0, 0), "foo(a, b)");
  34. is(IkiWiki::preprocess("foo", "foo", q{[[foo a="" b=""""""]]}, 0, 0), "foo(a, b)");
  35. is(IkiWiki::preprocess("foo", "foo", q{[[foo a="" b="""1"""]]}, 0, 0), "foo(a, b => 1)");
  36. is(IkiWiki::preprocess("foo", "foo", "[[foo a=\"1 2 3 4\"]]", 0, 0), "foo(a => 1 2 3 4)");
  37. is(IkiWiki::preprocess("foo", "foo", "[[foo ]] then [[foo a=2]]", 0, 0),
  38. "foo() then foo(a => 2)");
  39. is(IkiWiki::preprocess("foo", "foo", "[[foo b c \"d and e=f\"]]", 0, 0), "foo(b, c, d and e=f)");
  40. is(IkiWiki::preprocess("foo", "foo", "[[foo a=1 b c=1]]", 0, 0),
  41. "foo(a => 1, b, c => 1)");
  42. is(IkiWiki::preprocess("foo", "foo", "[[foo a=1 b c=1 \t\t]]", 0, 0),
  43. "foo(a => 1, b, c => 1)", "whitespace");
  44. is(IkiWiki::preprocess("foo", "foo", "[[foo a=1\nb \nc=1]]", 0, 0),
  45. "foo(a => 1, b, c => 1)", "multiline directive");
  46. is(IkiWiki::preprocess("foo", "foo", "[[foo a=1 a=2 a=3]]", 0, 0),
  47. "foo(a => 1, a => 2, a => 3)", "dup item");
  48. is(IkiWiki::preprocess("foo", "foo", '[[foo a="[[bracketed]]" b=1]]', 0, 0),
  49. "foo(a => [[bracketed]], b => 1)");
  50. my $multiline="here is my \"first\"
  51. !! [[multiline ]] !!
  52. string!";
  53. is(IkiWiki::preprocess("foo", "foo", '[[foo a="""'.$multiline.'"""]]', 0, 0),
  54. "foo(a => $multiline)");
  55. is(IkiWiki::preprocess("foo", "foo", '[[foo """'.$multiline.'"""]]', 0, 0),
  56. "foo($multiline)");
  57. is(IkiWiki::preprocess("foo", "foo", '[[foo a="""'.$multiline.'""" b="foo"]]', 0, 0),
  58. "foo(a => $multiline, b => foo)");
  59. is(IkiWiki::preprocess("foo", "foo", '[[foo a="""'."\n".$multiline."\n".'""" b="foo"]]', 0, 0),
  60. "foo(a => $multiline, b => foo)", "leading/trailing newline stripped");
  61. my $long='[[foo a="""'.("a" x 100000).'';
  62. is(IkiWiki::preprocess("foo", "foo", $long, 0, 0), $long,
  63. "unterminated triple-quoted string inside unterminated directive(should not warn about over-recursion)");
  64. is(IkiWiki::preprocess("foo", "foo", $long."]]", 0, 0), $long."]]",
  65. "unterminated triple-quoted string is not treated as a bare word");
  66. TODO: {
  67. local $TODO = "nested strings not yet implemented";
  68. $multiline='here is a string containing another [[foo val="""string""]]';
  69. is(IkiWiki::preprocess("foo", "foo", '[[foo a="""'.$multiline.'"""]]', 0, 0),
  70. "foo(a=> $multiline)", "nested multiline strings");
  71. }