Debian Codegraph @

t/read_serialized_file

raw
1 # vim: set ts=8 sts=2 sw=2 tw=100 et ft=perl :
2 use strictures 2;
3 use 5.020;
4 use stable 0.031 'postderef';
5 use experimental 'signatures';
6 no autovivification warn => qw(fetch store exists delete);
7 use if "$]" >= 5.022, experimental => 're_strict';
8 no if "$]" >= 5.031009, feature => 'indirect';
9 no if "$]" >= 5.033001, feature => 'multidimensional';
10 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
11 no if "$]" >= 5.041009, feature => 'smartmatch';
12 no feature 'switch';
13 use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
14
15 use Sereal::Decoder;
16
17 use lib 't/lib';
18 use Helper;
19
20 my $hub = Test2::API::test2_stack->top;
21 $hub->set_count(14);
22
23 # this is a test that is run from t/serialization.t, incorporating its results into that file.
24
25 my @serialized_attributes = sort qw(
26 specification_version
27 output_format
28 short_circuit
29 max_depth
30 validate_formats
31 validate_content_schemas
32 collect_annotations
33 scalarref_booleans
34 stringy_numbers
35 strict
36 _resource_index
37 _vocabulary_classes
38 _metaschema_vocabulary_classes
39 );
40
41 my $result = Test2::V0::subtest 'thaw object in a separate process' => sub {
42 local $/;
43 binmode STDIN, ':raw';
44 my $thawed = Sereal::Decoder->new->decode(<>);
45
46 cmp_result(
47 [ sort keys %$thawed ],
48 [ sort @serialized_attributes ],
49 'thawed object in a new process contains all the right keys',
50 );
51
52 cmp_result(
53 $thawed->evaluate(1, 'https://my_schema')->TO_JSON,
54 {
55 valid => true,
56 annotations => [
57 map +{
58 instanceLocation => '',
59 keywordLocation => '/'.$_,
60 absoluteKeywordLocation => 'https://my_schema#/'.$_,
61 annotation => Test::Deep::ignore,
62 }, 'format', sort qw(type unknown properties contentMediaType contentSchema),
63 ],
64 },
65 'in thawed object, evaluate data against schema with custom dialect; format and unknown keywords are collected as annotations',
66 );
67
68 my $strict_metaschema = {
69 '$id' => 'https://my_strict_metaschema',
70 '$schema' => 'https://json-schema.org/draft/2020-12/schema',
71 '$vocabulary' => {
72 'https://json-schema.org/draft/2020-12/vocab/core' => true,
73 'https://json-schema.org/draft/2020-12/vocab/format-assertion' => true,
74 },
75 };
76
77 my $strict_schema = {
78 '$id' => 'https://my_strict_schema',
79 '$schema' => 'https://my_strict_metaschema',
80 type => 'number',
81 format => 'ipv4',
82 unknown => 1,
83 properties => { hello => false },
84 contentMediaType => 'application/json',
85 contentSchema => {},
86 };
87 $thawed->add_schema($strict_metaschema);
88 $thawed->add_schema($strict_schema);
89
90 cmp_result(
91 $thawed->evaluate('foo', 'https://my_strict_schema')->TO_JSON,
92 {
93 valid => false,
94 errors => [
95 {
96 instanceLocation => '',
97 keywordLocation => '/format',
98 absoluteKeywordLocation => 'https://my_strict_schema#/format',
99 error => 'not a valid ipv4',
100 },
101 ],
102 },
103 'evaluate data against schema with custom dialect; format-assertion is used',
104 );
105 };
106
107 # skip the END block which would normally try to print a plan
108 Test2::API::test2_stack->top->set_no_ending(1);
109
110 exit($result ? 0 : -1);