Debian Codegraph @

t/pattern.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 utf8;
14 use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
15
16 use lib 't/lib';
17 use Helper;
18
19 my $js = JSON::Schema::Modern->new;
20
21 my $tests = sub ($char, $test_substr) {
22 is_equal(
23 $js->evaluate($char, { pattern => '[a-z]' })->TO_JSON,
24 {
25 valid => false,
26 errors => [
27 {
28 instanceLocation => '',
29 keywordLocation => '/pattern',
30 error => 'pattern does not match',
31 },
32 ],
33 },
34 $test_substr.' LATIN SMALL LETTER E WITH ACUTE does not match the ascii range [a-z]',
35 );
36
37 is_equal(
38 $js->evaluate($char, { pattern => '\w' })->TO_JSON,
39 {
40 valid => true,
41 },
42 $test_substr.' LATIN SMALL LETTER E WITH ACUTE does match the "word" character class, because unicode semantics are used for matching',
43 );
44 };
45
46 my $letter = "é";
47 $tests->($letter, 'unchanged');
48
49 utf8::upgrade($letter);
50 $tests->($letter, 'upgraded');
51
52 utf8::downgrade($letter);
53 $tests->($letter, 'downgraded');
54
55
56 subtest 'empty pattern' => sub {
57 # create a "last successful match" in a containing scope
58 my $str = "furble" =~ s/fur/meow/r;
59
60 is_equal(
61 $js->evaluate('hello', { pattern => '' })->TO_JSON,
62 { valid => true },
63 'empty pattern in "pattern" will correctly match',
64 );
65
66 # create a new "last successful match"
67 $str = "furble" =~ s/fur/meow/r;
68
69 is_equal(
70 $js->evaluate(
71 { alpha => 'hello' },
72 {
73 patternProperties => { '' => true },
74 additionalProperties => false,
75 unevaluatedProperties => false,
76 },
77 )->TO_JSON,
78 { valid => true },
79 'empty pattern in "patternProperties" will correctly match',
80 );
81 };
82
83 done_testing;