Debian Codegraph @

t/cached-metaschemas.t

raw
1 # vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et :
2 use strictures 2;
3 use stable 0.031 'postderef';
4 use experimental 'signatures';
5 no autovivification warn => qw(fetch store exists delete);
6 use if "$]" >= 5.022, experimental => 're_strict';
7 no if "$]" >= 5.031009, feature => 'indirect';
8 no if "$]" >= 5.033001, feature => 'multidimensional';
9 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
10 no if "$]" >= 5.041009, feature => 'smartmatch';
11 no feature 'switch';
12 use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
13
14 use List::Util 'unpairs';
15
16 use constant METASCHEMA => 'https://json-schema.org/draft/2019-09/schema';
17
18 use lib 't/lib';
19 use Helper;
20
21 # spec version -> vocab classes
22 my %vocabularies = unpairs(JSON::Schema::Modern->new->__all_metaschema_vocabulary_classes);
23
24 subtest 'load cached metaschema' => sub {
25 my $js = JSON::Schema::Modern->new;
26
27 is_equal(
28 $js->_get_resource(METASCHEMA),
29 undef,
30 'this resource is not yet known',
31 );
32
33 cmp_result(
34 $js->_get_or_load_resource(METASCHEMA),
35 my $resource = +{
36 canonical_uri => str(METASCHEMA),
37 path => '',
38 specification_version => 'draft2019-09',
39 vocabularies => $vocabularies{'draft2019-09'},
40 document => all(
41 isa('JSON::Schema::Modern::Document'),
42 methods(
43 schema => superhashof({
44 '$schema' => str(METASCHEMA),
45 '$id' => METASCHEMA,
46 }),
47 canonical_uri => str(METASCHEMA),
48 resource_index => ignore,
49 ),
50 ),
51 },
52 'loaded metaschema from sharedir cache',
53 );
54
55 cmp_result(
56 $js->_get_resource(METASCHEMA),
57 $resource,
58 'this resource is now in the resource index',
59 );
60 };
61
62 subtest 'resource collision with cached metaschema' => sub {
63 my $js = JSON::Schema::Modern->new;
64 cmp_result(
65 $js->evaluate(1, { '$id' => METASCHEMA })->TO_JSON,
66 {
67 valid => false,
68 errors => [
69 {
70 instanceLocation => '',
71 keywordLocation => '',
72 error => re(qr{^EXCEPTION: \Quri "${ \METASCHEMA }" conflicts with an existing cached schema resource\E}),
73 },
74 ],
75 },
76 'cannot introduce another schema whose id collides with a cached schema that exists in global cache',
77 );
78
79 cmp_result(
80 $js->evaluate(1, { '$id' => 'http://json-schema.org/draft-07/schema#' })->TO_JSON,
81 {
82 valid => false,
83 errors => [
84 {
85 instanceLocation => '',
86 keywordLocation => '',
87 error => re(qr{^EXCEPTION: \Quri "http://json-schema.org/draft-07/schema" conflicts with an existing cached schema resource\E}),
88 },
89 ],
90 },
91 'cannot introduce another schema whose id collides with a cached schema, even if it isn\'t loaded yet',
92 );
93 };
94
95 done_testing;