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