]> git.donarmstrong.com Git - liborg-parser-perl.git/blob - t/table.t
Import original source of Org-Parser 0.23
[liborg-parser-perl.git] / t / table.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 => 'non-table (missing extra character)',
16     filter_elements => 'Org::Element::Table',
17     doc  => <<'_',
18 |
19 _
20     num => 0,
21 );
22
23 test_parse(
24     name => 'table basic tests',
25     filter_elements => 'Org::Element::Table',
26     doc  => <<'_',
27 #+CAPTION: test caption
28 #+LABEL: tbl:test
29 | a | b   | c |
30 |---+-----+---|
31 | 1 |     | 2 |
32 | 3 | abc | 4 |
33 | one <2011-03-17 > three
34 _
35     num  => 1,
36     test_after_parse => sub {
37         my %args = @_;
38         my $doc = $args{result};
39         my $elems = $args{elements};
40         my $t = $elems->[0];
41         my ($r1, $r2, $r3, $r4, $r5) = @{ $t->children };
42         isa_ok($r1, "Org::Element::TableRow");
43         isa_ok($r2, "Org::Element::TableVLine");
44         isa_ok($r3, "Org::Element::TableRow");
45         isa_ok($r4, "Org::Element::TableRow");
46
47         my $c1a = $r1->children->[0];
48         isa_ok($c1a, "Org::Element::TableCell");
49         isa_ok($c1a->children->[0], "Org::Element::Text");
50
51         is($c1a->as_string, "a", "first cell's as_string");
52         is($r1->as_string, "|a|b|c\n", "first row's as_string");
53
54         # test inline elements inside cell
55         my $c5a = $r5->children->[0];
56         isa_ok($c5a->children->[0], "Org::Element::Text");
57         isa_ok($c5a->children->[1], "Org::Element::Timestamp");
58         isa_ok($c5a->children->[2], "Org::Element::Text");
59
60         is($t->row_count, 4, "row_count() method");
61         is($t->column_count, 3, "column_count() method");
62         isa_ok($t->rows->[0], "Org::Element::TableRow");
63         isa_ok($t->rows->[0]->cells->[0], 'Org::Element::TableCell');
64
65         is_deeply($r1->as_array, ["a", "b", "c"], "row's as_array() method")
66             or diag explain $r1->as_array;
67         is_deeply($t->as_aoa,
68                   [["a", "b", "c"],
69                    [1, '', 2],
70                    [3, "abc", 4],
71                    ["one <2011-03-17 Thu> three"]],
72                   "table's as_aoa() method")
73             or diag explain $t->as_aoa;
74     },
75 );
76
77 done_testing();
78