Debian Codegraph @

t/boolean-data.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 Data::Dumper;
16 use lib 't/lib';
17 use Helper;
18
19 sub serialize { Data::Dumper->new([ $_[0] ])->Indent(0)->Terse(1)->Sortkeys(1)->Dump }
20
21 my ($test_schema, $failure_result);
22
23 subtest 'strict booleans (default)' => sub {
24 my $js = JSON::Schema::Modern->new;
25 is_equal(
26 $js->evaluate($_, { type => 'boolean' })->TO_JSON,
27 { valid => true },
28 'in data, '.serialize($_).' is a boolean',
29 )
30 foreach (
31 false,
32 true,
33 );
34
35 is_equal(
36 $js->evaluate($_->[1], { type => 'boolean' })->TO_JSON,
37 {
38 valid => false,
39 errors => [
40 {
41 instanceLocation => '',
42 keywordLocation => '/type',
43 error => 'got '.$_->[0].', not boolean',
44 },
45 ],
46 },
47 'correct error generated from type for '.serialize($_->[1]),
48 )
49 foreach (
50 [ null => undef ],
51 [ integer => 0 ],
52 [ integer => 1 ],
53 [ string => '0' ],
54 [ string => '1' ],
55 [ string => 'false' ],
56 [ string => 'true' ],
57 [ 'reference to SCALAR' => \0 ],
58 [ 'reference to SCALAR' => \1 ],
59 );
60
61 is_equal(
62 $js->evaluate(
63 $_->[1],
64 $test_schema = {
65 allOf => [ { type => 'boolean' }, { type => ['boolean','object'] } ],
66 anyOf => [ { const => false }, { const => true } ],
67 enum => [ false, true ],
68 }
69 )->TO_JSON,
70 $failure_result = {
71 valid => false,
72 errors => [
73 {
74 instanceLocation => '',
75 keywordLocation => '/enum',
76 error => 'value does not match (from enum 0: wrong type: '.$_->[0].' vs boolean; from enum 1: wrong type: '.$_->[0].' vs boolean)',
77
78 },
79 {
80 instanceLocation => '',
81 keywordLocation => '/allOf/0/type',
82 error => 'got '.$_->[0].', not boolean',
83 },
84 {
85 instanceLocation => '',
86 keywordLocation => '/allOf/1/type',
87 error => 'got '.$_->[0].', not one of boolean, object',
88 },
89 {
90 instanceLocation => '',
91 keywordLocation => '/allOf',
92 error => 'subschemas 0, 1 are not valid',
93 },
94 {
95 instanceLocation => '',
96 keywordLocation => '/anyOf/0/const',
97 error => 'value does not match (wrong type: '.$_->[0].' vs boolean)',
98 },
99 {
100 instanceLocation => '',
101 keywordLocation => '/anyOf/1/const',
102 error => 'value does not match (wrong type: '.$_->[0].' vs boolean)',
103 },
104 {
105 instanceLocation => '',
106 keywordLocation => '/anyOf',
107 error => 'no subschemas are valid',
108 },
109 ],
110 },
111 'in data, '.serialize($_->[1]).' not is a boolean',
112 )
113 foreach (
114 [ null => undef ],
115 [ integer => 0 ],
116 [ integer => 1 ],
117 [ string => '0' ],
118 [ string => '1' ],
119 [ string => 'false' ],
120 [ string => 'true' ],
121 [ 'reference to SCALAR' => \0 ],
122 [ 'reference to SCALAR' => \1 ],
123 );
124 };
125
126 subtest 'scalarref_booleans = 1' => sub {
127 my $js = JSON::Schema::Modern->new(scalarref_booleans => 1);
128 is_equal(
129 $js->evaluate($_, $test_schema)->TO_JSON,
130 { valid => true },
131 'in data, '.serialize($_).' is a boolean',
132 )
133 foreach (
134 false,
135 true,
136 \0,
137 \1,
138 );
139
140 is_equal(
141 $js->evaluate($_->[1], $test_schema)->TO_JSON,
142 {
143 valid => false,
144 errors => [
145 do {
146 my ($type, $value) = $_->@*;
147 map +{
148 $_->%*,
149 $_->{keywordLocation} =~ /\/type\z/ ? (error => $_->{error} =~ s/^got .*, not/got $type, not/r)
150 : $_->{keywordLocation} =~ /\/(const|enum)\z/ ? (error => $_->{error} =~ s/wrong type: \K.+? vs boolean/$type vs boolean/gr)
151 : (),
152 }, $failure_result->{errors}->@*,
153 },
154 ],
155 },
156 'correct error generated from type for '.serialize($_),
157 )
158 foreach (
159 [ null => undef ],
160 [ integer => 0 ],
161 [ integer => 1 ],
162 [ string => '0' ],
163 [ string => '1' ],
164 [ string => 'false' ],
165 [ string => 'true' ],
166 );
167
168 is_equal(
169 $js->evaluate(
170 [
171 undef,
172 0,
173 1,
174 '0',
175 '1',
176 'false',
177 'true',
178 \0,
179 \1,
180 ],
181 { uniqueItems => true },
182 )->TO_JSON,
183 { valid => true },
184 'items are all considered unique when types differ, even when perl treats them similarly',
185 );
186
187 is_equal(
188 $js->evaluate($_, { uniqueItems => true })->TO_JSON,
189 {
190 valid => false,
191 errors => [
192 {
193 instanceLocation => '',
194 keywordLocation => '/uniqueItems',
195 error => 'items at indices 0 and 1 are not unique',
196 },
197 ],
198 },
199 'scalarrefs compare as identical to their counterpart booleans',
200 )
201 foreach (
202 [ \0, false ],
203 [ false, \0 ],
204 [ \1, true ],
205 [ true, \1 ],
206 );
207 };
208
209 done_testing;