Changed license of madduck's python plugins from GPL-2 to BSD-2-clause.
[ikiwiki.git] / t / pagespec_match.t
bloba37b06e8e7480173df642782f96e35debd6b6922
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More tests => 87;
6 BEGIN { use_ok("IkiWiki"); }
8 ok(pagespec_match("foo", "*"));
9 ok(!pagespec_match("foo", ""));
10 ok(pagespec_match("foo", "!bar"));
11 ok(pagespec_match("page", "?ag?"));
12 ok(! pagespec_match("page", "?a?g?"));
13 ok(pagespec_match("foo.png", "*.*"));
14 ok(! pagespec_match("foo", "*.*"));
15 ok(pagespec_match("foo", "foo or bar"), "simple list");
16 ok(pagespec_match("bar", "foo or bar"), "simple list 2");
17 ok(pagespec_match("foo", "f?? and !foz"));
18 ok(! pagespec_match("foo", "f?? and !foo"));
19 ok(! pagespec_match("foo", "* and !foo"));
20 ok(! pagespec_match("foo", "foo and !foo"));
21 ok(! pagespec_match("foo.png", "* and !*.*"));
22 ok(pagespec_match("foo", "(bar or ((meep and foo) or (baz or foo) or beep))"));
23 ok(pagespec_match("foo", "(
24 bar
25 or (
26 (meep and foo)
28 (baz or foo)
29 or beep
31 )"), "multiline complex pagespec");
32 ok(! pagespec_match("a/foo", "foo", location => "a/b"), "nonrelative fail");
33 ok(! pagespec_match("foo", "./*", location => "a/b"), "relative fail");
34 ok(pagespec_match("a/foo", "./*", location => "a/b"), "relative");
35 ok(pagespec_match("a/b/foo", "./*", location => "a/b"), "relative 2");
36 ok(pagespec_match("a/foo", "./*", "a/b"), "relative oldstyle call");
37 ok(pagespec_match("foo", "./*", location => "a"), "relative toplevel");
38 ok(pagespec_match("foo/bar", "*", location => "baz"), "absolute");
39 ok(! pagespec_match("foo", "foo and bar"), "foo and bar");
40 ok(pagespec_match("{f}oo", "{*}*"), "curly match");
41 ok(! pagespec_match("foo", "{*}*"), "curly !match");
43 ok(pagespec_match("somepage", "user(frodo)", user => "frodo"));
44 ok(pagespec_match("somepage", "user(frodo)", user => "Frodo"));
45 ok(! pagespec_match("somepage", "user(frodo)", user => "Sam"));
46 ok(pagespec_match("somepage", "user(*o)", user => "Bilbo"));
47 ok(pagespec_match("somepage", "user(*o)", user => "frodo"));
48 ok(! pagespec_match("somepage", "user(*o)", user => "Sam"));
49 ok(pagespec_match("somepage", "user(http://*.myopenid.com/)", user => "http://foo.myopenid.com/"));
50 ok(pagespec_match("somepage", "user(*://*)", user => "http://foo.myopenid.com/"));
52 # The link and backlink stuff needs this.
53 $config{userdir}="";
54 $links{foo}=[qw{bar baz}];
55 $links{bar}=[];
56 $links{baz}=[];
57 $links{meh}=[];
58 $links{"bugs/foo"}=[qw{bugs/done}];
59 $links{"bugs/done"}=[];
60 $links{"bugs/bar"}=[qw{done}];
61 $links{"done"}=[];
62 $links{"done"}=[];
63 $links{"examples/softwaresite/bugs/fails_to_frobnicate"}=[qw{done}];
64 $links{"examples/softwaresite/bugs/done"}=[];
65 $links{"ook"}=[qw{/blog/tags/foo}];
66 foreach my $p (keys %links) {
67 $pagesources{$p}="$p.mdwn";
69 $pagesources{"foo.png"}="foo.png";
70 $pagesources{"foo"}="foo.mdwn";
71 $IkiWiki::hooks{htmlize}{mdwn}={};
73 ok(pagespec_match("foo", "foo"), "simple");
74 ok(! pagespec_match("foo", "bar"), "simple fail");
75 ok(pagespec_match("foo", "foo"), "simple glob");
76 ok(pagespec_match("foo", "f*"), "simple glob fail");
77 ok(pagespec_match("foo", "page(foo)"), "page()");
78 print pagespec_match("foo", "page(foo)")."\n";
79 ok(! pagespec_match("foo", "page(bar)"), "page() fail");
80 ok(! pagespec_match("foo.png", "page(foo.png)"), "page() fails on non-page");
81 ok(! pagespec_match("foo.png", "page(foo*)"), "page() fails on non-page glob");
82 ok(pagespec_match("foo", "page(foo)"), "page() glob");
83 ok(pagespec_match("foo", "page(f*)"), "page() glob fail");
84 ok(pagespec_match("foo", "link(bar)"), "link");
85 ok(pagespec_match("foo", "link(.)", location => "bar"), "link with .");
86 ok(! pagespec_match("foo", "link(.)"), "link with . but missing location");
87 ok(pagespec_match("foo", "link(ba?)"), "glob link");
88 ok(! pagespec_match("foo", "link(quux)"), "failed link");
89 ok(! pagespec_match("foo", "link(qu*)"), "failed glob link");
90 ok(pagespec_match("bugs/foo", "link(done)", location => "bugs/done"), "link match to bestlink");
91 ok(! pagespec_match("examples/softwaresite/bugs/done", "link(done)",
92 location => "bugs/done"), "link match to bestlink");
93 ok(pagespec_match("examples/softwaresite/bugs/fails_to_frobnicate",
94 "link(./done)", location => "examples/softwaresite/bugs/done"), "link relative");
95 ok(! pagespec_match("foo", "link(./bar)", location => "foo/bar"), "link relative fail");
96 ok(pagespec_match("bar", "backlink(foo)"), "backlink");
97 ok(! pagespec_match("quux", "backlink(foo)"), "failed backlink");
98 ok(! pagespec_match("bar", ""), "empty pagespec should match nothing");
99 ok(! pagespec_match("bar", " "), "blank pagespec should match nothing");
100 ok(pagespec_match("ook", "link(blog/tags/foo)"), "link internal absolute success");
101 ok(pagespec_match("ook", "link(/blog/tags/foo)"), "link explicit absolute success");
102 ok(pagespec_match("meh", "!link(done)"), "negated failing match is a success");
104 $ENV{TZ}="GMT";
105 $IkiWiki::pagectime{foo}=1154532692; # Wed Aug 2 11:26 EDT 2006
106 $IkiWiki::pagectime{bar}=1154532695; # after
107 ok(pagespec_match("foo", "created_before(bar)"));
108 ok(! pagespec_match("foo", "created_after(bar)"));
109 ok(! pagespec_match("bar", "created_before(foo)"));
110 ok(pagespec_match("bar", "created_after(foo)"));
111 ok(pagespec_match("foo", "creation_year(2006)"), "year");
112 ok(! pagespec_match("foo", "creation_year(2005)"), "other year");
113 ok(pagespec_match("foo", "creation_month(8)"), "month");
114 ok(! pagespec_match("foo", "creation_month(9)"), "other month");
115 ok(pagespec_match("foo", "creation_day(2)"), "day");
116 ok(! pagespec_match("foo", "creation_day(3)"), "other day");
118 ok(! pagespec_match("foo", "no_such_function(foo)"), "foo");
120 my $ret=pagespec_match("foo", "(invalid");
121 ok(! $ret, "syntax error");
122 ok($ret =~ /syntax error/, "error message");
124 $ret=pagespec_match("foo", "bar or foo");
125 ok($ret, "simple match");
126 is($ret, "foo matches foo", "stringified return");
128 my $i=pagespec_match("foo", "link(bar)")->influences;
129 is(join(",", keys %$i), 'foo', "link is influenced by the page with the link");
130 $i=pagespec_match("bar", "backlink(foo)")->influences;
131 is(join(",", keys %$i), 'foo', "backlink is influenced by the page with the link");
132 $i=pagespec_match("bar", "backlink(foo)")->influences;
133 is(join(",", keys %$i), 'foo', "backlink is influenced by the page with the link");
134 $i=pagespec_match("bar", "created_before(foo)")->influences;
135 is(join(",", keys %$i), 'foo', "created_before is influenced by the comparison page");
136 $i=pagespec_match("bar", "created_after(foo)")->influences;
137 is(join(",", keys %$i), 'foo', "created_after is influenced by the comparison page");
138 $i=pagespec_match("foo", "link(baz) and created_after(bar)")->influences;
139 is(join(",", sort keys %$i), 'bar,foo', "influences add up over AND");
140 $i=pagespec_match("foo", "link(baz) and created_after(bar)")->influences;
141 is(join(",", sort keys %$i), 'bar,foo', "influences add up over OR");
142 $i=pagespec_match("foo", "!link(baz) and !created_after(bar)")->influences;
143 is(join(",", sort keys %$i), 'bar,foo', "influences unaffected by negation");
144 $i=pagespec_match("foo", "!link(baz) and !created_after(bar)")->influences;
145 is(join(",", sort keys %$i), 'bar,foo', "influences unaffected by negation");
146 $i=pagespec_match("meh", "!link(done)")->influences;
147 is(join(",", sort keys %$i), 'meh', "a negated, failing link test is successful, so the page is a link influence");