4 use Test::More tests => 107;
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{bar} = 3;
31 $IkiWiki::pagectime{"post/1"} = 6;
32 $IkiWiki::pagectime{"post/2"} = 6;
33 $IkiWiki::pagectime{"post/3"} = 6;
34 $links{foo}=[qw{post/1 post/2}];
35 $links{foo2}=[qw{bar}];
36 $links{foo3}=[qw{bar}];
38 is_deeply([pagespec_match_list("foo", "bar")], ["bar"]);
39 is_deeply([sort(pagespec_match_list("foo", "* and !post/*"))], ["bar", "foo", "foo2", "foo3"]);
40 is_deeply([sort(pagespec_match_list("foo", "post/*"))], ["post/1", "post/2", "post/3"]);
41 is_deeply([pagespec_match_list("foo", "post/*", sort => "title")],
42 ["post/1", "post/2", "post/3"]);
43 is_deeply([pagespec_match_list("foo", "post/*", sort => "title", reverse => 1)],
44 ["post/3", "post/2", "post/1"]);
45 is_deeply([pagespec_match_list("foo", "post/*", sort => "title", num => 2)],
46 ["post/1", "post/2"]);
47 is_deeply([pagespec_match_list("foo", "post/*", sort => "title", num => 50)],
48 ["post/1", "post/2", "post/3"]);
49 is_deeply([pagespec_match_list("foo", "post/*", sort => "title", num => 50, reverse => 1)],
50 ["post/3", "post/2", "post/1"]);
51 is_deeply([pagespec_match_list("foo", "post/*", sort => "title",
52 filter => sub { $_[0] =~ /3/}) ],
53 ["post/1", "post/2"]);
54 is_deeply([pagespec_match_list("foo", "*", sort => "path", num => 2)],
56 is_deeply([pagespec_match_list("foo", "foo* or bar*",
57 sort => "-age title")], # oldest first, break ties by title
58 ["foo3", "foo", "foo2", "bar"]);
59 my $r=eval { pagespec_match_list("foo", "beep") };
60 ok(eval { pagespec_match_list("foo", "beep") } == 0);
61 ok(! $@, "does not fail with error when unable to match anything");
62 eval { pagespec_match_list("foo", "this is not a legal pagespec!") };
63 ok($@, "fails with error when pagespec bad");
65 # A pagespec that requires page metadata should add influences
66 # as an explicit dependency. In the case of a link, a links dependency.
67 foreach my $spec ("* and link(bar)", "* or link(bar)") {
68 pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
69 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
70 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
71 ok($IkiWiki::depends_simple{foo2}{foo2} == $IkiWiki::DEPEND_LINKS);
72 %IkiWiki::depends_simple=();
74 pagespec_match_list("foo3", $spec, deptype => deptype("links"));
75 ok($IkiWiki::depends{foo3}{$spec} & $IkiWiki::DEPEND_LINKS);
76 ok(! ($IkiWiki::depends{foo3}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_PRESENCE)));
77 ok($IkiWiki::depends_simple{foo3}{foo3} == $IkiWiki::DEPEND_LINKS);
78 %IkiWiki::depends_simple=();
82 # a pagespec with backlinks() will add as an influence the page with the links
83 foreach my $spec ("bar or (backlink(foo) and !*.png)", "backlink(foo)", "!backlink(foo)") {
84 pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
85 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
86 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
87 ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
88 %IkiWiki::depends_simple=();
90 pagespec_match_list("foo2", $spec, deptype => deptype("links"));
91 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_LINKS);
92 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_PRESENCE | $IkiWiki::DEPEND_CONTENT)));
93 ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
94 %IkiWiki::depends_simple=();
96 pagespec_match_list("foo2", $spec, deptype => deptype("presence", "links"));
97 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
98 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_LINKS);
99 ok(! ($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_CONTENT));
100 ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
101 %IkiWiki::depends_simple=();
102 %IkiWiki::depends=();
103 pagespec_match_list("foo2", $spec);
104 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_CONTENT);
105 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_PRESENCE | $IkiWiki::DEPEND_LINKS)));
106 ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
107 %IkiWiki::depends_simple=();
108 %IkiWiki::depends=();
111 # Hard fails due to a glob, etc, will block influences of other anded terms.
112 foreach my $spec ("nosuchpage and link(bar)", "link(bar) and nosuchpage",
113 "link(bar) and */Discussion", "*/Discussion and link(bar)",
114 "!foo2 and link(bar)", "link(bar) and !foo2") {
115 pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
116 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
117 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
118 ok(! exists $IkiWiki::depends_simple{foo2}{foo2}, "no influence from $spec");
119 %IkiWiki::depends_simple=();
120 %IkiWiki::depends=();
123 # A hard fail will not block influences of other ored terms.
124 foreach my $spec ("nosuchpage or link(bar)", "link(bar) or nosuchpage",
125 "link(bar) or */Discussion", "*/Discussion or link(bar)",
126 "!foo2 or link(bar)", "link(bar) or !foo2",
127 "link(bar) or (!foo2 and !foo1)") {
128 pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
129 ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
130 ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
131 ok($IkiWiki::depends_simple{foo2}{foo2} == $IkiWiki::DEPEND_LINKS);
132 %IkiWiki::depends_simple=();
133 %IkiWiki::depends=();
137 foreach my $p (100..500) {
138 $IkiWiki::pagectime{"p/$p"} = $p;
139 $pagesources{"p/$p"} = "p/$p.mdwn";
142 is_deeply([pagespec_match_list("foo", "p/*", sort => "age")],
144 is_deeply([pagespec_match_list("foo", "p/*", sort => "age", num => 20)],