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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
use 5.010001;
use strict;
use warnings;
use Test::More 0.96;
use Test::Exception;
use Pandoc::Elements;
# Make sure examples from RFC 6901 sec 5 work as metadata
my $doc = pandoc_json(<<'JSON');
[ { "unMeta": {
"":{"c":"0","t":"MetaString"},
" ":{"c":"7","t":"MetaString"},
"a/b":{"c":"1","t":"MetaString"},
"c%d":{"c":"2","t":"MetaString"},
"e^f":{"c":"3","t":"MetaString"},
"foo":{"c":[
{"c":[{"c":"bar","t":"Str"}],"t":"MetaInlines"},
{"c":[{"c":"baz","t":"Str"}],"t":"MetaInlines"}
],"t":"MetaList"},
"g|h":{"c":"4","t":"MetaString"},
"i\\j":{"c":"5","t":"MetaString"},
"k\"l":{"c":"6","t":"MetaString"},
"m~n":{"c":"8","t":"MetaString"}
} }, [] ]
JSON
my @tests = (
[ 'RFC 6901' => [
[ "" => $doc->value ],
[ "/foo" => [ "bar", "baz" ] ],
[ "/foo/0" => "bar" ],
[ "/" => 0 ],
[ "/a~1b" => 1 ],
[ "/c%d" => 2 ],
[ "/e^f" => 3 ],
[ "/g|h" => 4 ],
[ "/i\\j" => 5 ],
[ "/k\"l" => 6 ],
[ "/ " => 7 ],
[ "/m~0n" => 8 ],
],
],
# If the 'pointer' string does not start with a forward
# slash or is empty the whole string is a plain key.
[ 'plain-key' => [
[ "foo" => [ "bar", "baz" ] ],
[ "foo/0" => undef ],
[ "a/b" => 1 ],
[ "c%d" => 2 ],
[ "e^f" => 3 ],
[ "g|h" => 4 ],
[ "i\\j" => 5 ],
[ "k\"l" => 6 ],
[ " " => 7 ],
[ "m~n" => 8 ],
],
],
);
for my $test ( @tests ) {
my($name, $subtests) = @$test;
subtest $name => sub {
for my $subtest ( @$subtests ) {
my ( $pointer, $expected ) = @$subtest;
my $value = $doc->value( $pointer );
is_deeply $value, $expected, "'$pointer'"
or note explain { got => $value, expected => $expected };
next unless defined $expected;
lives_ok { $doc->value( $pointer, strict => 1 ) } "'$pointer' strict";
}
};
}
subtest strict => sub {
my @tests = (
[ '/foo/2' =>
qr{\QList index 2 out of range in (sub)pointer "/2" in pointer "/foo/2"\E}
],
[ '/foo/bar' =>
qr{\QNode "/bar" not a valid list index in (sub)pointer "/bar" in pointer "/foo/bar"\E}
],
[ '/quux' =>
qr{\QNode "/quux" doesn't correspond to any key in (sub)pointer "/quux" in pointer "/quux"\E}
],
[ 'quux' =>
qr{\QNode "quux" doesn't correspond to any key in (sub)pointer "quux" in pointer "quux"\E}
],
[ '/e^f/g/h' =>
qr{\QNo list or mapping "/g" in (sub)pointer "/g/h" in pointer "/e^f/g/h"\E}
],
);
for my $test ( @tests ) {
my ( $pointer, $regex ) = @$test;
throws_ok { $doc->value( $pointer, strict => 1 ) } $regex,
"'$pointer' throws";
# # For Pandoc::Metadata::Error
# my $exception = $@;
# isa_ok $exception, 'Pandoc::Metadata::Error', "'$pointer' exception";
# is $exception->{pointer}, $pointer, "'$pointer' pointer";
# note explain $exception->data;
}
};
subtest indices => sub {
my @list = map {; MetaString $_ } 0 .. 122;
my %meta = ( long_list => MetaList \@list );
my $doc = Document [ { unMeta => \%meta }, [] ];
my @range = 90 .. 112;
my @returns = map {; $doc->value("/long_list/$_") } @range;
is scalar(@returns), 23, 'return list length' or note scalar @returns;
is_deeply [ @returns ], [ @range ], 'correct return values';
};
done_testing;
__END__
---
{
"foo": ["bar", "baz"],
"": 0,
"a/b": 1,
"c%d": 2,
"e^f": 3,
"g|h": 4,
"i\\j": 5,
"k\"l": 6,
" ": 7,
"m~n": 8
}
The following JSON strings evaluate to the accompanying values:
"" // the whole document
"/foo" ["bar", "baz"]
"/foo/0" "bar"
"/" 0
"/a~1b" 1
"/c%d" 2
"/e^f" 3
"/g|h" 4
"/i\\j" 5
"/k\"l" 6
"/ " 7
"/m~0n" 8
my $doc = pandoc->parse( markdown => <<'END_OF_MD', '--standalone' );
---
foo:
- bar
- baz
'': 0
a/b: 1
c%d: 2
e^f: 3
g|h: 4
i\j: 5
k"l: 6
' ': 7
m~n: 8
...
END_OF_MD
say $doc->meta->to_json;
|