4 use Test::More tests => 115;
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 }
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)],
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 ok($IkiWiki::depends_simple{foo2}{foo} != $IkiWiki::DEPEND_LINKS);
75 %IkiWiki::depends_simple=();
77 pagespec_match_list("foo3", $spec, deptype => deptype("links"));
78 ok($IkiWiki::depends{foo3}{$spec} & $IkiWiki::DEPEND_LINKS);
79 ok(! ($IkiWiki::depends{foo3}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_PRESENCE)));
80 ok($IkiWiki::depends_simple{foo3}{foo3} == $IkiWiki::DEPEND_LINKS);
81 ok($IkiWiki::depends_simple{foo3}{foo} != $IkiWiki::DEPEND_LINKS);
82 %IkiWiki::depends_simple=();
85 # Above we tested that a link pagespec is influenced
86 # by the pages that currently contain the link.
88 # Oppositely, a pagespec that tests for pages that do not have a link
89 # is not influenced by pages that currently contain the link, but
90 # is instead influenced by pages that currently do not (but that
91 # could be changed to have it).
92 foreach my $spec ("* and !link(bar)", "* and !(!(!link(bar)))") {
93 pagespec_match_list("foo2", $spec);
94 ok($IkiWiki::depends_simple{foo2}{foo2} != $IkiWiki::DEPEND_LINKS);
95 ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
96 %IkiWiki::depends_simple=();
100 # a pagespec with backlinks() will add as an influence the page with the links
101 foreach my $spec ("bar or (backlink(foo) and !*.png)", "backlink(foo)", "!backlink(foo)") {
102 pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
103 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
104 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
105 ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
106 %IkiWiki::depends_simple=();
107 %IkiWiki::depends=();
108 pagespec_match_list("foo2", $spec, deptype => deptype("links"));
109 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_LINKS);
110 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_PRESENCE | $IkiWiki::DEPEND_CONTENT)));
111 ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
112 %IkiWiki::depends_simple=();
113 %IkiWiki::depends=();
114 pagespec_match_list("foo2", $spec, deptype => deptype("presence", "links"));
115 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
116 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_LINKS);
117 ok(! ($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_CONTENT));
118 ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
119 %IkiWiki::depends_simple=();
120 %IkiWiki::depends=();
121 pagespec_match_list("foo2", $spec);
122 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_CONTENT);
123 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_PRESENCE | $IkiWiki::DEPEND_LINKS)));
124 ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
125 %IkiWiki::depends_simple=();
126 %IkiWiki::depends=();
129 # Hard fails due to a glob, etc, will block influences of other anded terms.
130 foreach my $spec ("nosuchpage and link(bar)", "link(bar) and nosuchpage",
131 "link(bar) and */Discussion", "*/Discussion and link(bar)",
132 "!foo2 and link(bar)", "link(bar) and !foo2") {
133 pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
134 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
135 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
136 ok(! exists $IkiWiki::depends_simple{foo2}{foo2}, "no influence from $spec");
137 %IkiWiki::depends_simple=();
138 %IkiWiki::depends=();
141 # A hard fail will not block influences of other ored terms.
142 foreach my $spec ("nosuchpage or link(bar)", "link(bar) or nosuchpage",
143 "link(bar) or */Discussion", "*/Discussion or link(bar)",
144 "!foo2 or link(bar)", "link(bar) or !foo2",
145 "link(bar) or (!foo2 and !foo1)") {
146 pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
147 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
148 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
149 ok($IkiWiki::depends_simple{foo2}{foo2} == $IkiWiki::DEPEND_LINKS);
150 %IkiWiki::depends_simple=();
151 %IkiWiki::depends=();
155 foreach my $p (100..500) {
156 $IkiWiki::pagectime{"p/$p"} = $p;
157 $pagesources{"p/$p"} = "p/$p.mdwn";
160 is_deeply([pagespec_match_list("foo", "p/*", sort => "age")],
162 is_deeply([pagespec_match_list("foo", "p/*", sort => "age", num => 20)],