]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - t/map.t
(no commit message)
[git.ikiwiki.info.git] / t / map.t
diff --git a/t/map.t b/t/map.t
index a21ae91bff95e2d206079df98312554d7ecf37f7..5d4713d6fc9604d91018280c253911082d75c212 100755 (executable)
--- a/t/map.t
+++ b/t/map.t
@@ -3,9 +3,16 @@ package IkiWiki;
 
 use warnings;
 use strict;
 
 use warnings;
 use strict;
-use HTML::TreeBuilder;
 use Test::More;
 
 use Test::More;
 
+BEGIN {
+        unless (eval { require XML::Twig }) {
+                eval q{
+                        use Test::More skip_all => "XML::Twig is not available"
+                }
+        }
+}
+
 BEGIN { use_ok("IkiWiki"); }
 BEGIN { use_ok("IkiWiki::Render"); }
 BEGIN { use_ok("IkiWiki::Plugin::map"); }
 BEGIN { use_ok("IkiWiki"); }
 BEGIN { use_ok("IkiWiki::Render"); }
 BEGIN { use_ok("IkiWiki::Plugin::map"); }
@@ -56,6 +63,12 @@ foreach my $page (@pages) {
        writefile("$page.mdwn", "t/tmp", "your ad here");
 }
 
        writefile("$page.mdwn", "t/tmp", "your ad here");
 }
 
+sub comment {
+       my $str = shift;
+       $str =~ s/^/# /gm;
+       print $str;
+}
+
 sub node {
        my $name = shift;
        my $kids = shift;
 sub node {
        my $name = shift;
        my $kids = shift;
@@ -72,8 +85,8 @@ sub check_nodes {
 
        # expected is a list of hashes
        # ul is a list of li
 
        # expected is a list of hashes
        # ul is a list of li
-       foreach my $li ($ul->content_list) {
-               my @kids = $li->content_list;
+       foreach my $li ($ul->children) {
+               my @kids = $li->children;
 
                is($li->tag, 'li');
 
 
                is($li->tag, 'li');
 
@@ -83,16 +96,15 @@ sub check_nodes {
                my $a = $kids[0];
 
                if ($expectation->{parent}) {
                my $a = $kids[0];
 
                if ($expectation->{parent}) {
-                       is($a->attr('class'), 'mapparent');
+                       is($a->att('class'), 'mapparent');
                }
                else {
                }
                else {
-                       is($a->attr('class'), 'mapitem');
+                       is($a->att('class'), 'mapitem');
                }
 
                }
 
-               is_deeply([$a->content_list], [$expectation->{name}]);
+               is_deeply([$a->text], [$expectation->{name}]);
 
                if (@{$expectation->{kids}}) {
 
                if (@{$expectation->{kids}}) {
-                       is($kids[1]->tag, 'ul');
                        is(scalar @kids, 2);
 
                        check_nodes($kids[1], $expectation->{kids});
                        is(scalar @kids, 2);
 
                        check_nodes($kids[1], $expectation->{kids});
@@ -106,30 +118,37 @@ sub check_nodes {
 sub check {
        my $pagespec = shift;
        my $expected = shift;
 sub check {
        my $pagespec = shift;
        my $expected = shift;
+       comment("*** $pagespec ***\n");
 
        my $html = IkiWiki::Plugin::map::preprocess(pages => $pagespec,
                page => 'map',
                destpage => 'map');
 
        my $html = IkiWiki::Plugin::map::preprocess(pages => $pagespec,
                page => 'map',
                destpage => 'map');
-       my $tree = HTML::TreeBuilder->new;
-       $tree->implicit_tags(0);
-       $tree->unbroken_text(1);
-       $tree->strict_end(1);
-       $tree->strict_names(1);
-       $tree->strict_comment(1);
-       $tree->empty_element_tags(1);
-       $tree->parse_content($html);
-       my $fragment = $tree->disembowel;
-       print $fragment->dump;
+       comment($html);
+       my $tree = XML::Twig->new(pretty_print => 'indented');
+       eval {
+               $tree->parse($html);
+       };
+       if ($@) {
+               print "malformed XML: $@\n$html\n";
+               ok(0);
+       }
+       my $fragment = $tree->root;
 
        is($fragment->tag, 'div');
 
        is($fragment->tag, 'div');
-       is($fragment->attr('class'), 'map');
+       is($fragment->att('class'), 'map');
 
 
-       check_nodes(($fragment->content_list)[0], $expected);
+       if (@$expected) {
+               check_nodes(($fragment->children)[0], $expected);
+       }
+       else {
+               ok(! $fragment->children);
+       }
 
 
-       $fragment->delete;
-       print "<!-- -->\n";
+       $tree->dispose;
 }
 
 }
 
+check('doesnotexist', []);
+
 check('alpha', [node('alpha', [])]);
 
 check('alpha/*',
 check('alpha', [node('alpha', [])]);
 
 check('alpha/*',
@@ -172,6 +191,52 @@ check('alpha or alpha/1 or beta',
                node('beta', []),
        ]);
 
                node('beta', []),
        ]);
 
+check('alpha/1 or beta',
+       [
+               node('alpha', [
+                       node('1', []),
+               ], parent => 1),
+               node('beta', []),
+       ]);
+
+check('alpha/1/i* or alpha/2/a or beta',
+       [
+               node('alpha', [
+                       node('1', [
+                               node('i', []),
+                               node('ii', []),
+                               node('iii', []),
+                               node('iv', []),
+                       ], parent => 1),
+                       node('2', [
+                               node('a', []),
+                       ], parent => 1),
+               ], parent => 1),
+               node('beta', []),
+       ]);
+
+check('alpha/1/i* or alpha/2/a',
+       [
+               node('1', [
+                       node('i', []),
+                       node('ii', []),
+                       node('iii', []),
+                       node('iv', []),
+               ], parent => 1),
+               node('2', [
+                       node('a', []),
+               ], parent => 1),
+       ]);
+
+check('alpha/1/i*',
+       [
+               node('i', []),
+               node('ii', []),
+               node('iii', []),
+               node('iv', []),
+       ]);
+
+ok(! system("rm -rf t/tmp"));
 done_testing;
 
 1;
 done_testing;
 
 1;