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