-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern_internal_test.go
More file actions
75 lines (62 loc) · 1.99 KB
/
Copy pathpattern_internal_test.go
File metadata and controls
75 lines (62 loc) · 1.99 KB
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
package openapi
import (
"encoding/json/jsontext"
"encoding/json/v2"
"testing"
)
// bs is a backslash, so that \u escapes below are built rather than typed.
const bs = `\`
func TestPatternEscapes(t *testing.T) {
t.Parallel()
for _, tc := range []struct{ ecma, re2 string }{
{`^[^` + bs + `u0000-` + bs + `u001f` + bs + `u007f]+$`, `^[^\x{0000}-\x{001f}\x{007f}]+$`},
{bs + `uABCD`, `\x{ABCD}`},
// an escaped backslash followed by "u" is a literal backslash and "u".
{bs + bs + `u0041`, bs + bs + `u0041`},
{bs + bs + bs + `u0041`, `\\\x{0041}`},
// anything else is left as it is.
{`^[a-z]+\d{2}$`, `^[a-z]+\d{2}$`},
{bs + `u00`, bs + `u00`},
{`trailing\`, `trailing\`},
} {
if got := ecmaToRE2(tc.ecma); got != tc.re2 {
t.Errorf("ecmaToRE2(%q) = %q, want %q", tc.ecma, got, tc.re2)
}
if got := re2ToECMA(tc.re2); got != tc.ecma {
t.Errorf("re2ToECMA(%q) = %q, want %q", tc.re2, got, tc.ecma)
}
}
// RE2 escapes of up to 4 hex digits are padded; longer ones have no \u form.
for re2, ecma := range map[string]string{
`\x{41}`: bs + `u0041`,
`\x{10FFFF}`: `\x{10FFFF}`,
} {
if got := re2ToECMA(re2); got != ecma {
t.Errorf("re2ToECMA(%q) = %q, want %q", re2, got, ecma)
}
}
}
// TestSchema_PatternJSON round-trips an ECMA-262 pattern that Go's regexp
// only accepts in another syntax: it compiles, and is written back as read.
func TestSchema_PatternJSON(t *testing.T) {
t.Parallel()
in := `{"type":"string","pattern":"^[^` + bs + bs + `u0000-` + bs + bs + `u001f` + bs + bs + `u007f]+$"}`
var s Schema
if err := json.Unmarshal([]byte(in), &s, jsonOpts); err != nil {
t.Fatal(err)
}
if !s.Pattern.MatchString("page title") || s.Pattern.MatchString("line\nbreak") {
t.Errorf("pattern %q matches the wrong strings", s.Pattern)
}
out, err := json.Marshal(&s, jsonOpts)
if err != nil {
t.Fatal(err)
}
got := jsontext.Value(out)
if err := got.Compact(); err != nil {
t.Fatal(err)
}
if string(got) != in {
t.Errorf("got %s, want %s", got, in)
}
}