Debian Codegraph @

t/boolean-schemas.t

raw
1 # vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et :
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 lib 't/lib';
16 use Helper;
17
18 my $js = JSON::Schema::Modern->new;
19
20 my @tests = (
21 { schema => false, valid => false, exception => 0 },
22 { schema => true, valid => true, exception => 0 },
23 { schema => JSON::PP::false, valid => false, exception => 0 },
24 { schema => JSON::PP::true, valid => true, exception => 0 },
25 { schema => {}, valid => true, exception => 0 },
26 { schema => 0, valid => false, exception => 1 },
27 { schema => 1, valid => false, exception => 1 },
28 { schema => \0, valid => false, exception => 1 },
29 { schema => \1, valid => false, exception => 1 },
30 );
31
32 foreach my $test (@tests) {
33 my $data = 'hello';
34 is(
35 dies {
36 my $result = $js->evaluate($data, $test->{schema});
37 ok(!($result->exception xor $test->{exception}), json_sprintf('%s is not a schema', $test->{schema}));
38 ok(!($result->valid xor $test->{valid}), json_sprintf('schema: %s evaluates to: %s', $test->{schema}, $test->{valid}));
39
40 cmp_result(
41 $result->TO_JSON,
42 {
43 valid => $test->{valid},
44 $test->{valid} ? () : (errors => supersetof()),
45 },
46 'invalid result structure looks correct',
47 );
48 },
49 undef,
50 'no exceptions in evaluate',
51 );
52 }
53
54 is_equal(
55 $js->evaluate('hello', [])->TO_JSON,
56 {
57 valid => false,
58 errors => [
59 {
60 instanceLocation => '',
61 keywordLocation => '',
62 error => 'invalid schema type: array',
63 },
64 ],
65 },
66 'invalid schema type results in error',
67 );
68
69 $js = JSON::Schema::Modern->new(scalarref_booleans => 1);
70
71 is_equal(
72 $js->evaluate('hello', \0)->TO_JSON,
73 {
74 valid => false,
75 errors => [
76 {
77 instanceLocation => '',
78 keywordLocation => '',
79 error => 'invalid schema type: reference to SCALAR',
80 },
81 ],
82 },
83 'scalarref for schema results in error, even when scalarref_booleans is true',
84 );
85
86 done_testing;