summaryrefslogtreecommitdiff
path: root/t/pagespec_match_list.t
blob: dd5dcc5b087bcff878b9c35e1904fdee856edb7c (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Test::More tests => 88;
  5. BEGIN { use_ok("IkiWiki"); }
  6. %config=IkiWiki::defaultconfig();
  7. $config{srcdir}=$config{destdir}="/dev/null";
  8. IkiWiki::checkconfig();
  9. %pagesources=(
  10. foo => "foo.mdwn",
  11. foo2 => "foo2.mdwn",
  12. foo3 => "foo3.mdwn",
  13. bar => "bar.mdwn",
  14. "post/1" => "post/1.mdwn",
  15. "post/2" => "post/2.mdwn",
  16. "post/3" => "post/3.mdwn",
  17. );
  18. $links{foo}=[qw{post/1 post/2}];
  19. $links{foo2}=[qw{bar}];
  20. $links{foo3}=[qw{bar}];
  21. is_deeply([pagespec_match_list("foo", "bar")], ["bar"]);
  22. is_deeply([sort(pagespec_match_list("foo", "* and !post/*"))], ["bar", "foo", "foo2", "foo3"]);
  23. is_deeply([sort(pagespec_match_list("foo", "post/*"))], ["post/1", "post/2", "post/3"]);
  24. is_deeply([pagespec_match_list("foo", "post/*", sort => "title", reverse => 1)],
  25. ["post/3", "post/2", "post/1"]);
  26. is_deeply([pagespec_match_list("foo", "post/*", sort => "title", num => 2)],
  27. ["post/1", "post/2"]);
  28. is_deeply([pagespec_match_list("foo", "post/*", sort => "title", num => 50)],
  29. ["post/1", "post/2", "post/3"]);
  30. is_deeply([pagespec_match_list("foo", "post/*", sort => "title",
  31. filter => sub { $_[0] =~ /3/}) ],
  32. ["post/1", "post/2"]);
  33. my $r=eval { pagespec_match_list("foo", "beep") };
  34. ok(eval { pagespec_match_list("foo", "beep") } == 0);
  35. ok(! $@, "does not fail with error when unable to match anything");
  36. eval { pagespec_match_list("foo", "this is not a legal pagespec!") };
  37. ok($@, "fails with error when pagespec bad");
  38. # A pagespec that requires page metadata should add influences
  39. # as an explicit dependency. In the case of a link, a links dependency.
  40. foreach my $spec ("* and link(bar)", "* or link(bar)") {
  41. pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
  42. ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
  43. ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
  44. ok($IkiWiki::depends_simple{foo2}{foo2} == $IkiWiki::DEPEND_LINKS);
  45. %IkiWiki::depends_simple=();
  46. %IkiWiki::depends=();
  47. pagespec_match_list("foo3", $spec, deptype => deptype("links"));
  48. ok($IkiWiki::depends{foo3}{$spec} & $IkiWiki::DEPEND_LINKS);
  49. ok(! ($IkiWiki::depends{foo3}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_PRESENCE)));
  50. ok($IkiWiki::depends_simple{foo3}{foo3} == $IkiWiki::DEPEND_LINKS);
  51. %IkiWiki::depends_simple=();
  52. %IkiWiki::depends=();
  53. }
  54. # a pagespec with backlinks() will add as an influence the page with the links
  55. foreach my $spec ("bar or (backlink(foo) and !*.png)", "backlink(foo)") {
  56. pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
  57. ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
  58. ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
  59. ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
  60. %IkiWiki::depends_simple=();
  61. %IkiWiki::depends=();
  62. pagespec_match_list("foo2", $spec, deptype => deptype("links"));
  63. ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_LINKS);
  64. ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_PRESENCE | $IkiWiki::DEPEND_CONTENT)));
  65. ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
  66. %IkiWiki::depends_simple=();
  67. %IkiWiki::depends=();
  68. pagespec_match_list("foo2", $spec, deptype => deptype("presence", "links"));
  69. ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
  70. ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_LINKS);
  71. ok(! ($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_CONTENT));
  72. ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
  73. %IkiWiki::depends_simple=();
  74. %IkiWiki::depends=();
  75. pagespec_match_list("foo2", $spec);
  76. ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_CONTENT);
  77. ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_PRESENCE | $IkiWiki::DEPEND_LINKS)));
  78. ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS);
  79. %IkiWiki::depends_simple=();
  80. %IkiWiki::depends=();
  81. }
  82. # Hard fails due to a glob, etc, will block influences of other anded terms.
  83. foreach my $spec ("nosuchpage and link(bar)", "link(bar) and nosuchpage",
  84. "link(bar) and */Discussion", "*/Discussion and link(bar)",
  85. "!foo2 and link(bar)", "link(bar) and !foo2") {
  86. pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
  87. ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
  88. ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
  89. ok(! exists $IkiWiki::depends_simple{foo2}{foo2}, "no influence from $spec");
  90. %IkiWiki::depends_simple=();
  91. %IkiWiki::depends=();
  92. }
  93. # A hard fail will not block influences of other ored terms.
  94. foreach my $spec ("nosuchpage or link(bar)", "link(bar) or nosuchpage",
  95. "link(bar) or */Discussion", "*/Discussion or link(bar)",
  96. "!foo2 or link(bar)", "link(bar) or !foo2",
  97. "link(bar) or (!foo2 and !foo1)") {
  98. pagespec_match_list("foo2", $spec, deptype => deptype("presence"));
  99. ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE);
  100. ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)));
  101. ok($IkiWiki::depends_simple{foo2}{foo2} == $IkiWiki::DEPEND_LINKS);
  102. %IkiWiki::depends_simple=();
  103. %IkiWiki::depends=();
  104. }