-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathsuite_test.go
165 lines (151 loc) · 3.79 KB
/
suite_test.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package jsonschema_test
import (
"encoding/json"
"errors"
"fmt"
"os"
"path"
"slices"
"strings"
"testing"
"github.com/santhosh-tekuri/jsonschema/v6"
)
var skip = []string{
"ecmascript-regex.json",
"zeroTerminatedFloats.json",
"idn-email.json", "idn-hostname.json",
}
func testFile(t *testing.T, suite, fpath string, draft *jsonschema.Draft) {
optional := strings.Contains(fpath, "/optional/")
fpath = path.Join(suite, "tests", fpath)
t.Log("FILE:", fpath)
file, err := os.Open(fpath)
if err != nil {
t.Fatal(err)
}
defer file.Close()
url := "http://testsuites.com/schema.json"
var groups []struct {
Description string
Schema any
Tests []struct {
Description string
Data any
Valid bool
}
}
dec := json.NewDecoder(file)
dec.UseNumber()
if err := dec.Decode(&groups); err != nil {
t.Fatal(err)
}
for _, group := range groups {
t.Log(group.Description)
c := jsonschema.NewCompiler()
c.DefaultDraft(draft)
if optional {
c.AssertFormat()
c.AssertContent()
}
loader := jsonschema.SchemeURLLoader{
"file": jsonschema.FileLoader{},
"http": suiteRemotes(suite),
}
c.UseLoader(loader)
if err := c.AddResource(url, group.Schema); err != nil {
t.Fatalf("add resource failed: %v", err)
}
sch, err := c.Compile(url)
if err != nil {
t.Fatalf("schema compilation failed: %v", err)
}
for _, test := range group.Tests {
t.Logf(" %s", test.Description)
err := sch.Validate(test.Data)
if err != nil {
for _, line := range strings.Split(fmt.Sprintf("%v", err), "\n") {
t.Logf(" %s", line)
}
for _, line := range strings.Split(fmt.Sprintf("%#v", err), "\n") {
t.Logf(" %s", line)
}
if verr, ok := err.(*jsonschema.ValidationError); ok {
detailed, err := json.MarshalIndent(verr.DetailedOutput(), "", " ")
if err != nil {
t.Fatal(err)
}
t.Logf("detailed: %s", string(detailed))
basic, err := json.MarshalIndent(verr.BasicOutput(), "", " ")
if err != nil {
t.Fatal(err)
}
t.Logf("basic: %s", string(basic))
}
}
if got := err == nil; got != test.Valid {
t.Errorf(" valid: got %v, want %v", got, test.Valid)
gsch, _ := json.Marshal(group.Schema)
t.Log("schema:", string(gsch))
data, _ := json.Marshal(test.Data)
t.Log("data:", string(data))
t.FailNow()
}
}
}
}
func testDir(t *testing.T, suite, dpath string, draft *jsonschema.Draft) {
dir := path.Join(suite, "tests", dpath)
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
return
}
t.Fatal(err)
}
ee, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
for _, e := range ee {
if e.IsDir() {
testDir(t, suite, path.Join(dpath, e.Name()), draft)
continue
}
if path.Ext(e.Name()) != ".json" {
continue
}
if slices.Contains(skip, e.Name()) {
continue
}
testFile(t, suite, path.Join(dpath, e.Name()), draft)
}
}
func testSuite(t *testing.T, suite string) {
if _, err := os.Stat(suite); err != nil {
if os.IsNotExist(err) {
return
}
t.Fatal(err)
}
testDir(t, suite, "draft4", jsonschema.Draft4)
testDir(t, suite, "draft6", jsonschema.Draft6)
testDir(t, suite, "draft7", jsonschema.Draft7)
testDir(t, suite, "draft2019-09", jsonschema.Draft2019)
testDir(t, suite, "draft2020-12", jsonschema.Draft2020)
}
func TestSuites(t *testing.T) {
testSuite(t, "./testdata/JSON-Schema-Test-Suite")
testSuite(t, "./testdata/Extra-Test-Suite")
}
// --
type suiteRemotes string
func (rl suiteRemotes) Load(url string) (any, error) {
if rem, ok := strings.CutPrefix(url, "http://localhost:1234/"); ok {
f, err := os.Open(path.Join(string(rl), "remotes", rem))
if err != nil {
return nil, err
}
defer f.Close()
return jsonschema.UnmarshalJSON(f)
}
return nil, errors.New("no internet")
}