-
Notifications
You must be signed in to change notification settings - Fork 85
/
schema_test.go
153 lines (139 loc) · 3.31 KB
/
schema_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
package ogen
import (
"encoding/json"
"fmt"
"testing"
"github.com/go-faster/yaml"
"github.com/stretchr/testify/require"
)
type encoding struct {
marshal func(any) ([]byte, error)
unmarshal func([]byte, any) error
compare func(a *require.Assertions, got, want string, msgArgs ...any)
}
func testCustomEncoding(
createVal func() any,
input string,
wantErr bool,
e encoding,
) func(t *testing.T) {
return func(t *testing.T) {
a := require.New(t)
val := createVal()
err := e.unmarshal([]byte(input), val)
if wantErr {
a.Error(err)
t.Logf("Input: %q", input)
t.Logf("Error: %+v", err)
return
}
a.NoError(err)
data, err := e.marshal(val)
a.NoError(err)
e.compare(a, input, string(data))
}
}
func testCustomEncodings(
createVal func() any,
input string,
wantErr bool,
) func(t *testing.T) {
js := encoding{
marshal: json.Marshal,
unmarshal: json.Unmarshal,
compare: (*require.Assertions).JSONEq,
}
yml := encoding{
marshal: yaml.Marshal,
unmarshal: yaml.Unmarshal,
compare: (*require.Assertions).YAMLEq,
}
return func(t *testing.T) {
t.Run("YAML", testCustomEncoding(
createVal,
input,
wantErr,
yml,
))
t.Run("JSON", testCustomEncoding(
createVal,
input,
wantErr,
js,
))
}
}
func TestProperties(t *testing.T) {
create := func() any {
return &Properties{}
}
tests := []struct {
data string
value Properties
wantErr bool
}{
{`{"foo":{"type":"string"}, "bar":{"type":"number"}}`, Properties{
{Name: "foo", Schema: &Schema{Type: "string"}},
{Name: "bar", Schema: &Schema{Type: "number"}},
}, false},
// Invalid YAML.
{`{`, Properties{}, true},
{`{]`, Properties{}, true},
// Invalid type.
{`{"foobar":"string"}`, Properties{}, true},
{`0`, Properties{}, true},
}
for i, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("Test%d", i+1), testCustomEncodings(create, tt.data, tt.wantErr))
}
}
func TestAdditionalProperties(t *testing.T) {
create := func() any {
return &AdditionalProperties{}
}
tests := []struct {
data string
value AdditionalProperties
wantErr bool
}{
{`{"type":"string"}`, AdditionalProperties{Schema: Schema{Type: "string"}}, false},
{`false`, AdditionalProperties{Bool: new(bool)}, false},
// Invalid YAML.
{`{`, AdditionalProperties{}, true},
{`{]`, AdditionalProperties{}, true},
// Invalid type.
{`[]`, AdditionalProperties{}, true},
{`{"type": {}}`, AdditionalProperties{}, true},
{`0`, AdditionalProperties{}, true},
}
for i, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("Test%d", i+1), testCustomEncodings(create, tt.data, tt.wantErr))
}
}
func TestPatternProperties(t *testing.T) {
create := func() any {
return &PatternProperties{}
}
tests := []struct {
data string
value PatternProperties
wantErr bool
}{
{`{"\\w+":{"type":"string"}, "\\d+":{"type":"number"}}`, PatternProperties{
{Pattern: "\\w+", Schema: &Schema{Type: "string"}},
{Pattern: "\\d+", Schema: &Schema{Type: "number"}},
}, false},
// Invalid JSON.
{`{`, PatternProperties{}, true},
{`{]`, PatternProperties{}, true},
// Invalid type.
{`{"^[a-zA-Z0-9]*$":"string"}`, PatternProperties{}, true},
{`0`, PatternProperties{}, true},
}
for i, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("Test%d", i+1), testCustomEncodings(create, tt.data, tt.wantErr))
}
}