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