response
[ikiwiki.git] / t / pagespec_match_list.t
blob244ad915991dfc622d01273f4cac6c2ad4fcdcc1
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More tests => 126;
6 BEGIN { use_ok("IkiWiki"); }
8 %config=IkiWiki::defaultconfig();
9 $config{srcdir}=$config{destdir}="/dev/null";
10 IkiWiki::checkconfig();
13 package IkiWiki::SortSpec;
15 sub cmp_path { $a cmp $b }
18 %pagesources=(
19 foo => "foo.mdwn",
20 foo2 => "foo2.mdwn",
21 foo3 => "foo3.mdwn",
22 bar => "bar.mdwn",
23 "post/1" => "post/1.mdwn",
24 "post/2" => "post/2.mdwn",
25 "post/3" => "post/3.mdwn",
27 $IkiWiki::pagectime{foo} = 2;
28 $IkiWiki::pagectime{foo2} = 2;
29 $IkiWiki::pagectime{foo3} = 1;
30 $IkiWiki::pagectime{foo4} = 1;
31 $IkiWiki::pagectime{foo5} = 1;
32 $IkiWiki::pagectime{bar} = 3;
33 $IkiWiki::pagectime{"post/1"} = 6;
34 $IkiWiki::pagectime{"post/2"} = 6;
35 $IkiWiki::pagectime{"post/3"} = 6;
36 $links{foo}=[qw{post/1 post/2}];
37 $links{foo2}=[qw{bar}];
38 $links{foo3}=[qw{bar}];
40 is_deeply([pagespec_match_list("foo", "bar")], ["bar"]);
41 is_deeply([sort(pagespec_match_list("foo", "* and !post/*"))], ["bar", "foo", "foo2", "foo3"]);
42 is_deeply([sort(pagespec_match_list("foo", "post/*"))], ["post/1", "post/2", "post/3"]);
43 is_deeply([pagespec_match_list("foo", "post/*", sort => "title")],
44 ["post/1", "post/2", "post/3"]);
45 is_deeply([pagespec_match_list("foo", "post/*", sort => "title", reverse => 1)],
46 ["post/3", "post/2", "post/1"]);
47 is_deeply([pagespec_match_list("foo", "post/*", sort => "title", num => 2)],
48 ["post/1", "post/2"]);
49 is_deeply([pagespec_match_list("foo", "post/*", sort => "title", num => 50)],
50 ["post/1", "post/2", "post/3"]);
51 is_deeply([pagespec_match_list("foo", "post/*", sort => "title", num => 50, reverse => 1)],
52 ["post/3", "post/2", "post/1"]);
53 is_deeply([pagespec_match_list("foo", "post/*", sort => "title",
54 filter => sub { $_[0] =~ /3/}) ],
55 ["post/1", "post/2"]);
56 is_deeply([pagespec_match_list("foo", "*", sort => "path", num => 2)],
57 ["bar", "foo"]);
58 is_deeply([pagespec_match_list("foo", "foo* or bar*",
59 sort => "-age title")], # oldest first, break ties by title
60 ["foo3", "foo", "foo2", "bar"]);
61 my $r=eval { pagespec_match_list("foo", "beep") };
62 ok(eval { pagespec_match_list("foo", "beep") } == 0);
63 ok(! $@, "does not fail with error when unable to match anything");
64 eval { pagespec_match_list("foo", "this is not a legal pagespec!") };
65 ok($@, "fails with error when pagespec bad");
67 # A pagespec that requires page metadata should add influences
68 # as an explicit dependency. In the case of a link, a links dependency.
69 foreach my $spec ("* and link(bar)", "* or link(bar)") {
70 pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
71 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
72 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
73 ok($IkiWiki::depends_simple{foo2}{foo2} == $IkiWiki::DEPEND_LINKS);
74 %IkiWiki::depends_simple=();
75 %IkiWiki::depends=();
76 pagespec_match_list("foo3", $spec, deptype => deptype("links"));
77 ok($IkiWiki::depends{foo3}{$spec} & $IkiWiki::DEPEND_LINKS);
78 ok(! ($IkiWiki::depends{foo3}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_PRESENCE)));
79 ok($IkiWiki::depends_simple{foo3}{foo3} == $IkiWiki::DEPEND_LINKS);
80 %IkiWiki::depends_simple=();
81 %IkiWiki::depends=();
84 # A link pagespec is influenced by the pages that currently contain the link.
85 # It is not influced by pages that do not currently contain the link,
86 # because if those pages were changed to contain it, regular dependency
87 # handling would be triggered.
88 foreach my $spec ("* and link(bar)", "link(bar)", "no_such_page or link(bar)") {
89 pagespec_match_list("foo2", $spec);
90 ok($IkiWiki::depends_simple{foo2}{foo2} == $IkiWiki::DEPEND_LINKS);
91 ok(! exists $IkiWiki::depends_simple{foo2}{foo}, $spec);
92 %IkiWiki::depends_simple=();
93 %IkiWiki::depends=();
96 # Oppositely, a pagespec that tests for pages that do not have a link
97 # is not influenced by pages that currently contain the link, but
98 # is instead influenced by pages that currently do not (but that
99 # could be changed to have it).
100 foreach my $spec ("* and !link(bar)", "* and !(!(!link(bar)))") {
101 pagespec_match_list("foo2", $spec);
102 ok(! exists $IkiWiki::depends_simple{foo2}{foo2});
103 ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS, $spec);
104 %IkiWiki::depends_simple=();
105 %IkiWiki::depends=();
108 # a pagespec with backlinks() will add as an influence the page with the links
109 foreach my $spec ("bar or (backlink(foo) and !*.png)", "backlink(foo)", "!backlink(foo)") {
110 pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
111 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
112 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
113 ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
114 ok(! exists $IkiWiki::depends_simple{foo2}{foo2});
115 %IkiWiki::depends_simple=();
116 %IkiWiki::depends=();
117 pagespec_match_list("foo2", $spec, deptype => deptype("links"));
118 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_LINKS);
119 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_PRESENCE | $IkiWiki::DEPEND_CONTENT)));
120 ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
121 ok(! exists $IkiWiki::depends_simple{foo2}{foo2});
122 %IkiWiki::depends_simple=();
123 %IkiWiki::depends=();
124 pagespec_match_list("foo2", $spec, deptype => deptype("presence", "links"));
125 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
126 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_LINKS);
127 ok(! ($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_CONTENT));
128 ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
129 ok(! exists $IkiWiki::depends_simple{foo2}{foo2});
130 %IkiWiki::depends_simple=();
131 %IkiWiki::depends=();
132 pagespec_match_list("foo2", $spec);
133 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_CONTENT);
134 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_PRESENCE | $IkiWiki::DEPEND_LINKS)));
135 ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
136 %IkiWiki::depends_simple=();
137 %IkiWiki::depends=();
140 # Hard fails due to a glob, etc, will block influences of other anded terms.
141 foreach my $spec ("nosuchpage and link(bar)", "link(bar) and nosuchpage",
142 "link(bar) and */Discussion", "*/Discussion and link(bar)",
143 "!foo2 and link(bar)", "link(bar) and !foo2") {
144 pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
145 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
146 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
147 ok(! exists $IkiWiki::depends_simple{foo2}{foo2}, "no influence from $spec");
148 %IkiWiki::depends_simple=();
149 %IkiWiki::depends=();
152 # A hard fail will not block influences of other ored terms.
153 foreach my $spec ("nosuchpage or link(bar)", "link(bar) or nosuchpage",
154 "link(bar) or */Discussion", "*/Discussion or link(bar)",
155 "!foo2 or link(bar)", "link(bar) or !foo2",
156 "link(bar) or (!foo2 and !foo1)") {
157 pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
158 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
159 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
160 ok($IkiWiki::depends_simple{foo2}{foo2} == $IkiWiki::DEPEND_LINKS);
161 %IkiWiki::depends_simple=();
162 %IkiWiki::depends=();
165 my @ps;
166 foreach my $p (100..500) {
167 $IkiWiki::pagectime{"p/$p"} = $p;
168 $pagesources{"p/$p"} = "p/$p.mdwn";
169 unshift @ps, "p/$p";
171 is_deeply([pagespec_match_list("foo", "p/*", sort => "age")],
172 [@ps]);
173 is_deeply([pagespec_match_list("foo", "p/*", sort => "age", num => 20)],
174 [@ps[0..19]]);