File: metadata.t

package info (click to toggle)
libpandoc-elements-perl 0.38-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 732 kB
  • sloc: perl: 1,630; makefile: 15; sh: 1
file content (116 lines) | stat: -rw-r--r-- 3,670 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use strict;
use Test::More 0.96;
use Pandoc::Elements;
use Scalar::Util qw[ blessed reftype ];
use JSON::PP;

my $doc = pandoc_json(<<JSON);
[ { "unMeta": {
      "true": { "t": "MetaBool", "c": true },
      "false": { "t": "MetaBool", "c": false },
      "string": { "t": "MetaString", "c": "hello\\nworld" },
      "blocks": { "t": "MetaBlocks", "c": [
          {"t": "Para", "c": [{"t":"Str","c":"x"}]},
          {"t": "Para", "c": [{"t":"Str","c":"y"}]}
      ] },
      "map": { "t": "MetaMap", "c": {
        "string": { "t": "MetaString", "c": "0" },
        "list": { "t": "MetaList", "c": [
            { "t": "MetaString", "c": "a" },
            { "t": "MetaString", "c": "b" },
            { "t": "MetaBool", "c": false }
        ] },
        "/~": { "t": "MetaBool", "c": "true" }
      } }
} }, [] ]
JSON

# MetaBool

ok $doc->meta->{true}->content, 'true';
ok !$doc->meta->{false}->content, 'false';

foreach (1, '1', 'true', 'TRUE', 42, 'wtf') {
    my $m = MetaBool($_);
    ok $m->content;
    is '{"c":true,"t":"MetaBool"}', $m->to_json, "true: $_";
}

foreach (0, '', 'false', 'FALSE', undef) {
    my $m = MetaBool($_);
    ok !$m->content;
    is '{"c":false,"t":"MetaBool"}', $m->to_json, "false: $_";
}

is_deeply $doc->value('/true', boolean => 'JSON::PP'), JSON::PP::true, 'JSON::PP::true'; 
is_deeply $doc->value('/false', boolean => 'JSON::PP'), JSON::PP::false, 'JSON::PP::false'; 

# MetaString

is $doc->meta->{string}->content, "hello\nworld";
is $doc->meta->{string}->value, "hello\nworld";

# MetaInlines
{
    my $m = MetaInlines [ Str "foo" ];
    is '{"c":[{"c":"foo","t":"Str"}],"t":"MetaInlines"}',
        $m->to_json, 'MetaInlines';
    is $m->string, 'foo', 'MetaInlines->string';
}

# MetaBlocks
{
    my $m = MetaBlocks [ Para [ Str "x" ], Para [ Str "y" ] ];
    is $m->string, "x\n\ny", 'MetaBlocks->string';
}

# [meta]value

is_deeply $doc->value, {
    false => 0,
    true => 1,
    string => "hello\nworld",
    blocks => "x\n\ny",
    map => {
        string => "0",
        list => ["a", "b", 0],
        '/~' => 1
    }
  }, 'value';

is $doc->value('false'), 0, 'value("false")';
is $doc->value('/false'), 0, 'value("/false")';
is $doc->value('true'), 1, 'value("true")';
is $doc->value('/true'), 1, 'value("/true")';
is $doc->value('/map/string'), "0", 'value("/map/string")';
is $doc->value('/map/list/0'), "a", 'value("/map/list/0")';
is $doc->value('/map/list/2'), "0", 'value("/map/list/2")';
is $doc->value('string'), "hello\nworld", 'value("string")';
is $doc->value('/string'), "hello\nworld", 'value("/string")';
is_deeply $doc->value('/map/~1~0', boolean => 'JSON::PP'),
    JSON::PP::true, 'value("/map/~1~0")';
is_deeply $doc->value('string', element => 'keep'),
    $doc->meta->{string}->content, 'value("string", elements => keep)';
is_deeply $doc->value('/string', element => 'keep'),
    $doc->meta->{string}->content, 'value("/string", elements => keep)';
is $doc->value('blocks'), "x\n\ny", 'value("blocks")';
is $doc->value('/blocks'), "x\n\ny", 'value("/blocks")';
is_deeply $doc->value('blocks', element => 'keep'),
    $doc->meta->{blocks}->content, 'value("blocks", elements => keep)';
is_deeply $doc->value('/blocks', element => 'keep'),
    $doc->meta->{blocks}->content, 'value("/blocks", elements => keep)';

foreach (qw(x map/x true/x blocks/x map/list/x map/list/3)) {
    is $doc->value($_), undef, "value('$_')";
    is $doc->value($_), undef, "value('/$_')";
}

my $doc = do {
    local (@ARGV, $/) = ('t/documents/meta.json');
    pandoc_json(<>);
};

is_deeply { map { $_ => $doc->metavalue($_) } keys %{$doc->meta} },
    $doc->metavalue, 'Document->metavalue';

done_testing;