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