File: read_serialized_file

package info (click to toggle)
libjson-schema-modern-perl 0.611-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,352 kB
  • sloc: perl: 3,935; makefile: 9
file content (111 lines) | stat: -rw-r--r-- 3,214 bytes parent folder | download
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
# vim: set ts=8 sts=2 sw=2 tw=100 et ft=perl :
use strictures 2;
use 5.020;
use stable 0.031 'postderef';
use experimental 'signatures';
no autovivification warn => qw(fetch store exists delete);
use if "$]" >= 5.022, experimental => 're_strict';
no if "$]" >= 5.031009, feature => 'indirect';
no if "$]" >= 5.033001, feature => 'multidimensional';
no if "$]" >= 5.033006, feature => 'bareword_filehandles';
no if "$]" >= 5.041009, feature => 'smartmatch';
no feature 'switch';
use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8

use Test2::V0 ();
use Sereal::Decoder;

use lib 't/lib';
use Helper;

my $hub = Test2::API::test2_stack->top;
$hub->set_count(13);

# this is a test that is run from t/serialization.t, incorporating its results into that file.

my @serialized_attributes = sort qw(
  specification_version
  output_format
  short_circuit
  max_traversal_depth
  validate_formats
  validate_content_schemas
  collect_annotations
  scalarref_booleans
  stringy_numbers
  strict
  _resource_index
  _vocabulary_classes
  _metaschema_vocabulary_classes
);

my $result = subtest 'thaw object in a separate process' => sub {
  local $/;
  binmode STDIN, ':raw';
  my $thawed = Sereal::Decoder->new->decode(<>);

  cmp_result(
    [ sort keys %$thawed ],
    [ sort @serialized_attributes ],
    'thawed object in a new process contains all the right keys',
  );

  cmp_result(
    $thawed->evaluate(1, 'https://my_schema')->TO_JSON,
    {
      valid => true,
      annotations => [
        map +{
          instanceLocation => '',
          keywordLocation => '/'.$_,
          absoluteKeywordLocation => 'https://my_schema#/'.$_,
          annotation => Test::Deep::ignore,
        }, 'format', sort qw(type unknown properties contentMediaType contentSchema),
      ],
    },
    'in thawed object, evaluate data against schema with custom dialect; format and unknown keywords are collected as annotations',
  );

  my $strict_metaschema = {
    '$id' => 'https://my_strict_metaschema',
    '$schema' => 'https://json-schema.org/draft/2020-12/schema',
    '$vocabulary' => {
      'https://json-schema.org/draft/2020-12/vocab/core' => true,
      'https://json-schema.org/draft/2020-12/vocab/format-assertion' => true,
    },
  };

  my $strict_schema = {
    '$id' => 'https://my_strict_schema',
    '$schema' => 'https://my_strict_metaschema',
    type => 'number',
    format => 'ipv4',
    unknown => 1,
    properties => { hello => false },
    contentMediaType => 'application/json',
    contentSchema => {},
  };
  $thawed->add_schema($strict_metaschema);
  $thawed->add_schema($strict_schema);

  cmp_deeply(
    $thawed->evaluate('foo', 'https://my_strict_schema')->TO_JSON,
    {
      valid => false,
      errors => [
        {
          instanceLocation => '',
          keywordLocation => '/format',
          absoluteKeywordLocation => 'https://my_strict_schema#/format',
          error => 'not a valid ipv4',
        },
      ],
    },
    'evaluate data against schema with custom dialect; format-assertion is used',
  );
};

# skip the END block which would normally try to print a plan
Test2::API::test2_stack->top->set_no_ending(1);

exit($result ? 0 : -1);