]> git.donarmstrong.com Git - liborg-parser-perl.git/blob - t/base_element.t
Import original source of Org-Parser 0.23
[liborg-parser-perl.git] / t / base_element.t
1 #!perl
2
3 use 5.010;
4 use strict;
5 use warnings;
6
7 use FindBin '$Bin';
8 use lib $Bin, "$Bin/t";
9
10 use Org::Parser;
11 use Test::More 0.96;
12 require "testlib.pl";
13
14 test_parse(
15     name => 'seniority(), prev_sibling(), next_sibling()',
16     filter_elements => 'Org::Element::Headline',
17     doc  => <<'_',
18 * h1
19 ** h2a
20 ** h2b
21 ** h2c
22 _
23     num => 4,
24     test_after_parse => sub {
25         my (%args) = @_;
26         my $h = $args{elements};
27         is($h->[0]->seniority, 0, "h1's seniority=0");
28         is($h->[1]->seniority, 0, "h2a's seniority=0");
29         is($h->[2]->seniority, 1, "h2b's seniority=1");
30         is($h->[3]->seniority, 2, "h2c's seniority=2");
31
32         ok(!defined($h->[0]->prev_sibling), "h1 doesnt have prev_sibling");
33         ok(!defined($h->[1]->prev_sibling), "h2a doesnt have prev_sibling");
34         is($h->[2]->prev_sibling->title->as_string, "h2a",
35            "h2b's prev_sibling=h2a");
36         is($h->[3]->prev_sibling->title->as_string, "h2b",
37            "h2c's pre_sibling=h2b");
38
39         ok(!defined($h->[0]->prev_sibling), "h1 doesnt have next_sibling");
40         is($h->[1]->next_sibling->title->as_string, "h2b",
41            "h2a's next_sibling=h2b");
42         is($h->[2]->next_sibling->title->as_string, "h2c",
43            "h2b's next_sibling=h2c");
44         ok(!defined($h->[3]->next_sibling), "h2c doesnt have next_sibling");
45     },
46 );
47
48 test_parse(
49     name => 'walk()',
50     doc  => <<'_',
51 #comment
52 * h <2011-03-22 >
53 text
54 _
55     test_after_parse => sub {
56         my (%args) = @_;
57         my $doc = $args{result};
58
59         my $n=0;
60         $doc->walk(sub{$n++});
61         # +1 is for document itself
62         # timestamp not walked (part of headline)
63         is($n, 3+1, "num of walked elements");
64     },
65 );
66
67 test_parse(
68     name => 'find(), walk_parents(), headline()',
69     doc  => <<'_',
70 * a
71 ** b
72 *** c
73 **** d
74 text
75 **** d2
76 _
77     test_after_parse => sub {
78         my (%args) = @_;
79         my $doc = $args{result};
80         my @res = $doc->find(
81             sub {
82                 $_[0]->isa('Org::Element::Headline') &&
83                     $_[0]->title->as_string =~ /^d/;
84             });
85         is(scalar(@res), 2, "find num results");
86         ok($res[1]->isa("Org::Element::Headline") &&
87                $res[1]->title->as_string eq 'd2', "find result #2");
88
89         my $d = $res[0];
90         my $res = "";
91         $d->walk_parents(
92             sub {
93                 my ($el, $parent) = @_;
94                 return if $parent->isa('Org::Document');
95                 $res .= $parent->title->as_string;
96             });
97         is($res, "cba", "walk_parents()");
98
99         is($d->headline->title->as_string, "c", "headline() 1");
100         is($d->children->[0]->headline->title->as_string, "d", "headline() 2");
101     },
102 );
103
104 test_parse(
105     name => 'remove()',
106     doc  => <<'_',
107 * a
108 * b
109 ** b2
110 *** b3
111 * c
112 _
113     filter_elements => 'Org::Element::Headline',
114     num => 5,
115      test_after_parse => sub {
116         my (%args) = @_;
117         my $doc    = $args{result};
118         my $elems  = $args{elements};
119         my ($a, $b, $b2, $b3, $c) = @$elems;
120
121         $b->remove;
122         my @res = $doc->find('Headline');
123         is(scalar(@res), 2, "remove() removes children");
124         is(scalar(@{$doc->children}), 2,
125            "remove() removes from parent's children");
126         is($a->next_sibling, $c, "a's next_sibling becomes c");
127         is($c->prev_sibling, $a, "c's prev_sibling becomes a");
128     },
129 );
130
131 done_testing();