diff options
Diffstat (limited to 't')
-rwxr-xr-x | t/add_depends.t | 62 | ||||
-rwxr-xr-x | t/pagespec_match.t | 23 | ||||
-rwxr-xr-x | t/pagespec_match_list.t | 85 | ||||
-rwxr-xr-x | t/pagespec_match_result.t | 77 | ||||
-rwxr-xr-x | t/yesno.t | 4 |
5 files changed, 249 insertions, 2 deletions
diff --git a/t/add_depends.t b/t/add_depends.t new file mode 100755 index 000000000..9b074818c --- /dev/null +++ b/t/add_depends.t @@ -0,0 +1,62 @@ +#!/usr/bin/perl +use warnings; +use strict; +use Test::More tests => 38; + +BEGIN { use_ok("IkiWiki"); } +%config=IkiWiki::defaultconfig(); +$config{srcdir}=$config{destdir}="/dev/null"; +IkiWiki::checkconfig(); + +$pagesources{"foo$_"}="foo$_.mdwn" for 0..9; + +# avoids adding an unparseable pagespec +ok(! add_depends("foo0", "foo and (bar")); +ok(! add_depends("foo0", "foo another")); + +# simple and not-so-simple dependencies split +ok(add_depends("foo0", "*")); +ok(add_depends("foo0", "bar")); +ok(add_depends("foo0", "BAZ")); +ok(exists $IkiWiki::depends_simple{foo0}{"bar"}); +ok(exists $IkiWiki::depends_simple{foo0}{"baz"}); # lowercase +ok(! exists $IkiWiki::depends_simple{foo0}{"*"}); +ok(! exists $IkiWiki::depends{foo0}{"bar"}); +ok(! exists $IkiWiki::depends{foo0}{"baz"}); + +# default dependencies are content dependencies +ok($IkiWiki::depends{foo0}{"*"} & $IkiWiki::DEPEND_CONTENT); +ok(! ($IkiWiki::depends{foo0}{"*"} & ($IkiWiki::DEPEND_PRESENCE | $IkiWiki::DEPEND_LINKS))); +ok($IkiWiki::depends_simple{foo0}{"bar"} & $IkiWiki::DEPEND_CONTENT); +ok(! ($IkiWiki::depends_simple{foo0}{"bar"} & ($IkiWiki::DEPEND_PRESENCE | $IkiWiki::DEPEND_LINKS))); + +# adding other dep types standalone +ok(add_depends("foo2", "*", deptype("presence"))); +ok(add_depends("foo2", "bar", deptype("links"))); +ok($IkiWiki::depends{foo2}{"*"} & $IkiWiki::DEPEND_PRESENCE); +ok(! ($IkiWiki::depends{foo2}{"*"} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS))); +ok($IkiWiki::depends_simple{foo2}{"bar"} & $IkiWiki::DEPEND_LINKS); +ok(! ($IkiWiki::depends_simple{foo2}{"bar"} & ($IkiWiki::DEPEND_PRESENCE | $IkiWiki::DEPEND_CONTENT))); + +# adding combined dep types +ok(add_depends("foo2", "baz", deptype("links", "presence"))); +ok($IkiWiki::depends_simple{foo2}{"baz"} & $IkiWiki::DEPEND_LINKS); +ok($IkiWiki::depends_simple{foo2}{"baz"} & $IkiWiki::DEPEND_PRESENCE); +ok(! ($IkiWiki::depends_simple{foo2}{"baz"} & $IkiWiki::DEPEND_CONTENT)); + +# adding dep types to existing dependencies should merge the flags +ok(add_depends("foo2", "baz")); +ok($IkiWiki::depends_simple{foo2}{"baz"} & $IkiWiki::DEPEND_LINKS); +ok($IkiWiki::depends_simple{foo2}{"baz"} & $IkiWiki::DEPEND_PRESENCE); +ok(($IkiWiki::depends_simple{foo2}{"baz"} & $IkiWiki::DEPEND_CONTENT)); +ok(add_depends("foo2", "bar", deptype("presence"))); # had only links before +ok($IkiWiki::depends_simple{foo2}{"bar"} & ($IkiWiki::DEPEND_LINKS | $IkiWiki::DEPEND_PRESENCE)); +ok(! ($IkiWiki::depends_simple{foo2}{"bar"} & $IkiWiki::DEPEND_CONTENT)); +ok(add_depends("foo0", "bar", deptype("links"))); # had only content before +ok($IkiWiki::depends{foo0}{"*"} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS)); +ok(! ($IkiWiki::depends{foo0}{"*"} & $IkiWiki::DEPEND_PRESENCE)); + +# content is the default if unknown types are entered +ok(add_depends("foo9", "*", deptype("monkey"))); +ok($IkiWiki::depends{foo9}{"*"} & $IkiWiki::DEPEND_CONTENT); +ok(! ($IkiWiki::depends{foo9}{"*"} & ($IkiWiki::DEPEND_PRESENCE | $IkiWiki::DEPEND_LINKS))); diff --git a/t/pagespec_match.t b/t/pagespec_match.t index 64408f489..d529106f7 100755 --- a/t/pagespec_match.t +++ b/t/pagespec_match.t @@ -1,7 +1,7 @@ #!/usr/bin/perl use warnings; use strict; -use Test::More tests => 54; +use Test::More tests => 64; BEGIN { use_ok("IkiWiki"); } @@ -88,3 +88,24 @@ ok(! pagespec_match("foo", "no_such_function(foo)"), "foo"); my $ret=pagespec_match("foo", "(invalid"); ok(! $ret, "syntax error"); ok($ret =~ /syntax error/, "error message"); + +$ret=pagespec_match("foo", "bar or foo"); +ok($ret, "simple match"); +is($ret, "foo matches foo", "stringified return"); + +my $i=pagespec_match("foo", "link(bar)")->influences; +is(join(",", keys %$i), 'foo', "link is influenced by the page with the link"); +$i=pagespec_match("bar", "backlink(foo)")->influences; +is(join(",", keys %$i), 'foo', "backlink is influenced by the page with the link"); +$i=pagespec_match("bar", "backlink(foo)")->influences; +is(join(",", keys %$i), 'foo', "backlink is influenced by the page with the link"); +$i=pagespec_match("bar", "created_before(foo)")->influences; +is(join(",", keys %$i), 'foo', "created_before is influenced by the comparison page"); +$i=pagespec_match("bar", "created_after(foo)")->influences; +is(join(",", keys %$i), 'foo', "created_after is influenced by the comparison page"); +$i=pagespec_match("foo", "link(baz) and created_after(bar)")->influences; +is(join(",", sort keys %$i), 'bar,foo', "influences add up over AND"); +$i=pagespec_match("foo", "link(baz) and created_after(bar)")->influences; +is(join(",", sort keys %$i), 'bar,foo', "influences add up over OR"); +$i=pagespec_match("foo", "!link(baz) and !created_after(bar)")->influences; +is(join(",", sort keys %$i), 'bar,foo', "influences unaffected by negation"); diff --git a/t/pagespec_match_list.t b/t/pagespec_match_list.t new file mode 100755 index 000000000..301197cc1 --- /dev/null +++ b/t/pagespec_match_list.t @@ -0,0 +1,85 @@ +#!/usr/bin/perl +use warnings; +use strict; +use Test::More tests => 49; + +BEGIN { use_ok("IkiWiki"); } + +%config=IkiWiki::defaultconfig(); +$config{srcdir}=$config{destdir}="/dev/null"; +IkiWiki::checkconfig(); + +%pagesources=( + foo => "foo.mdwn", + foo2 => "foo2.mdwn", + foo3 => "foo3.mdwn", + bar => "bar.mdwn", + "post/1" => "post/1.mdwn", + "post/2" => "post/2.mdwn", + "post/3" => "post/3.mdwn", +); +$links{foo}=[qw{post/1 post/2}]; +$links{foo2}=[qw{bar}]; +$links{foo3}=[qw{bar}]; + +is_deeply([pagespec_match_list("foo", "bar")], ["bar"]); +is_deeply([sort(pagespec_match_list("foo", "* and !post/*"))], ["bar", "foo", "foo2", "foo3"]); +is_deeply([sort(pagespec_match_list("foo", "post/*"))], ["post/1", "post/2", "post/3"]); +is_deeply([pagespec_match_list("foo", "post/*", sort => "title", reverse => 1)], + ["post/3", "post/2", "post/1"]); +is_deeply([pagespec_match_list("foo", "post/*", sort => "title", num => 2)], + ["post/1", "post/2"]); +is_deeply([pagespec_match_list("foo", "post/*", sort => "title", num => 50)], + ["post/1", "post/2", "post/3"]); +is_deeply([pagespec_match_list("foo", "post/*", sort => "title", + filter => sub { $_[0] =~ /3/}) ], + ["post/1", "post/2"]); +my $r=eval { pagespec_match_list("foo", "beep") }; +ok(eval { pagespec_match_list("foo", "beep") } == 0); +ok(! $@, "does not fail with error when unable to match anything"); +eval { pagespec_match_list("foo", "this is not a legal pagespec!") }; +ok($@, "fails with error when pagespec bad"); + +# A pagespec that requires page metadata should add influences +# as an explicit dependency. In the case of a link, a links dependency. +foreach my $spec ("* and link(bar)", "* or link(bar)") { + pagespec_match_list("foo2", $spec, deptype => deptype("presence")); + ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE); + ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS))); + ok($IkiWiki::depends_simple{foo2}{foo2} == $IkiWiki::DEPEND_LINKS); + %IkiWiki::depends_simple=(); + %IkiWiki::depends=(); + pagespec_match_list("foo3", $spec, deptype => deptype("links")); + ok($IkiWiki::depends{foo3}{$spec} & $IkiWiki::DEPEND_LINKS); + ok(! ($IkiWiki::depends{foo3}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_PRESENCE))); + ok($IkiWiki::depends_simple{foo3}{foo3} == $IkiWiki::DEPEND_LINKS); + %IkiWiki::depends_simple=(); + %IkiWiki::depends=(); +} + +# a pagespec with backlinks() will add as an influence the page with the links +foreach my $spec ("bar or (backlink(foo) and !*.png)", "backlink(foo)") { + pagespec_match_list("foo2", $spec, deptype => deptype("presence")); + ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE); + ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS))); + ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS); + %IkiWiki::depends_simple=(); + %IkiWiki::depends=(); + pagespec_match_list("foo2", $spec, deptype => deptype("links")); + ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_LINKS); + ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_PRESENCE | $IkiWiki::DEPEND_CONTENT))); + ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS); + %IkiWiki::depends_simple=(); + %IkiWiki::depends=(); + pagespec_match_list("foo2", $spec, deptype => deptype("presence", "links")); + ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_PRESENCE); + ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_LINKS); + ok(! ($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_CONTENT)); + ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS); + %IkiWiki::depends_simple=(); + %IkiWiki::depends=(); + pagespec_match_list("foo2", $spec); + ok($IkiWiki::depends{foo2}{$spec} & $IkiWiki::DEPEND_CONTENT); + ok(! ($IkiWiki::depends{foo2}{$spec} & ($IkiWiki::DEPEND_PRESENCE | $IkiWiki::DEPEND_LINKS))); + ok($IkiWiki::depends_simple{foo2}{foo} == $IkiWiki::DEPEND_LINKS); +} diff --git a/t/pagespec_match_result.t b/t/pagespec_match_result.t new file mode 100755 index 000000000..c2112bf14 --- /dev/null +++ b/t/pagespec_match_result.t @@ -0,0 +1,77 @@ +#!/usr/bin/perl +use warnings; +use strict; +use Test::More tests => 71; + +BEGIN { use_ok("IkiWiki"); } + +# Note that new objects have to be constructed freshly for each test, since +# object states are mutated as they are combined. +sub S { IkiWiki::SuccessReason->new("match", @_) } +sub F { IkiWiki::FailReason->new("no match", @_) } +sub E { IkiWiki::ErrorReason->new("error in matching", @_) } + +ok(S() eq "match"); +ok(F() eq "no match"); +ok(E() eq "error in matching"); + +ok(S()); +ok(! F()); +ok(! E()); + +ok(!(! S())); +ok(!(!(! F))); +ok(!(!(! E))); + +ok(S() | F()); +ok(F() | S()); +ok(!(F() | E())); +ok(!(!S() | F() | E())); + +ok(S() & S() & S()); +ok(!(S() & E())); +ok(!(S() & F())); +ok(!(S() & F() & E())); +ok(S() & (F() | F() | S())); + +# influences are always merged, no matter the operation performed, +# as long as the two items are always both present +foreach my $op ('$s | $f', '$s & $f', '$s & $f & E()', '$s | E() | $f', + '! $s | ! $f', '!(!(!$s)) | $f') { + my $s=S(foo => 1, bar => 1); + is($s->influences->{foo}, 1); + is($s->influences->{bar}, 1); + my $f=F(bar => 2, baz => 1); + is($f->influences->{bar}, 2); + is($f->influences->{baz}, 1); + my $c = eval $op; + ok(ref $c); + is($c->influences->{foo}, 1, "foo ($op)"); + is($c->influences->{bar}, (1 | 2), "bar ($op)"); + is($c->influences->{baz}, 1, "baz ($op)"); +} + +my $s=S(foo => 0, bar => 1); +$s->influences(baz => 1); +ok(! $s->influences->{foo}, "removed 0 influence"); +ok(! $s->influences->{bar}, "removed 1 influence"); +ok($s->influences->{baz}, "set influence"); +ok($s->influences_static); + +# influence blocking +my $r=F()->block & S(foo => 1); +ok(! $r->influences->{foo}, "failed blocker & influence -> does not pass"); +$r=F()->block | S(foo => 1); +ok($r->influences->{foo}, "failed blocker | influence -> does pass"); +$r=S(foo => 1) & F()->block; +ok(! $r->influences->{foo}, "influence & failed blocker -> does not pass"); +$r=S(foo => 1) | F()->block; +ok($r->influences->{foo}, "influence | failed blocker -> does pass"); +$r=S(foo => 1) & F()->block & S(foo => 2); +ok(! $r->influences->{foo}, "influence & failed blocker & influence -> does not pass"); +$r=S(foo => 1) | F()->block | S(foo => 2); +ok($r->influences->{foo}, "influence | failed blocker | influence -> does pass"); +$r=S()->block & S(foo => 1); +ok($r->influences->{foo}, "successful blocker -> does pass"); +$r=(! S()->block) & S(foo => 1); +ok(! $r->influences->{foo}, "! successful blocker -> failed blocker"); @@ -1,7 +1,7 @@ #!/usr/bin/perl use warnings; use strict; -use Test::More tests => 10; +use Test::More tests => 11; BEGIN { use_ok("IkiWiki"); } @@ -19,3 +19,5 @@ ok(IkiWiki::yesno("NO") == 0); ok(IkiWiki::yesno("1") == 1); ok(IkiWiki::yesno("0") == 0); ok(IkiWiki::yesno("mooooooooooo") == 0); + +ok(IkiWiki::yesno(undef) == 0); |