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