-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathintegration_suite_test.go
More file actions
263 lines (214 loc) · 5.99 KB
/
integration_suite_test.go
File metadata and controls
263 lines (214 loc) · 5.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package testintegration
import (
"fmt"
"maps"
"os"
"testing"
"github.com/go-openapi/swag/jsonutils"
"github.com/go-openapi/swag/jsonutils/adapters"
easyjson "github.com/go-openapi/swag/jsonutils/adapters/easyjson/json"
"github.com/go-openapi/swag/jsonutils/adapters/ifaces"
fixtures "github.com/go-openapi/swag/jsonutils/fixtures_test"
"github.com/go-openapi/testify/v2/require"
"github.com/mailru/easyjson/jlexer"
"github.com/mailru/easyjson/jwriter"
)
var (
_ EJMarshaler = &EasyOrderedObject{}
_ EJUnmarshaler = &EasyOrderedObject{}
_ EJMarshaler = &EasyObject{}
_ EJUnmarshaler = &EasyObject{}
_ EJMarshaler = &EasyTarget{}
_ EJUnmarshaler = &EasyTarget{}
)
func TestMain(m *testing.M) {
easyjson.Register(adapters.Registry)
os.Exit(m.Run())
}
type EasyObject struct {
*MockEJMarshaler
*MockEJUnmarshaler
inner easyjson.MapSlice
}
func newEasyObject() *EasyObject {
a := &EasyObject{} // we may leave the inner member to nil
a.MockEJMarshaler = &MockEJMarshaler{
MarshalEasyJSONFunc: func(w *jwriter.Writer) {
a.inner.MarshalEasyJSON(w)
},
}
a.MockEJUnmarshaler = &MockEJUnmarshaler{
UnmarshalEasyJSONFunc: func(l *jlexer.Lexer) {
a.inner.UnmarshalEasyJSON(l)
// reshuffle mappings: all inner MapSlice's become maps
if len(a.inner) == 0 {
return
}
changed := make(map[string]any)
for k, v := range a.inner.OrderedItems() {
ordered, ok := v.(ifaces.Ordered)
if !ok {
continue
}
m := maps.Collect(ordered.OrderedItems())
changed[k] = m
}
a.inner.SetOrderedItems(maps.All(changed))
},
}
return a
}
type EasyTarget struct {
*MockEJMarshaler
*MockEJUnmarshaler
}
type EasyOrderedObject struct {
*MockEJMarshaler
*MockEJUnmarshaler
inner easyjson.MapSlice
}
func newEasyOrderedObject() *EasyOrderedObject {
a := &EasyOrderedObject{
// we may leave the inner member to nil
}
a.MockEJMarshaler = &MockEJMarshaler{
MarshalEasyJSONFunc: func(w *jwriter.Writer) {
a.inner.MarshalEasyJSON(w)
},
}
a.MockEJUnmarshaler = &MockEJUnmarshaler{
UnmarshalEasyJSONFunc: func(l *jlexer.Lexer) {
a.inner.UnmarshalEasyJSON(l)
},
}
return a
}
func (o EasyOrderedObject) MarshalEasyJSON(w *jwriter.Writer) {
o.MockEJMarshaler.MarshalEasyJSON(w)
}
func (o *EasyOrderedObject) UnmarshalEasyJSON(l *jlexer.Lexer) {
o.MockEJUnmarshaler.UnmarshalEasyJSON(l)
}
type EasyOrderedTarget struct {
easyjson.MapSlice
*MockEJMarshaler
*MockEJUnmarshaler
}
func (o *EasyOrderedTarget) MarshalEasyJSON(w *jwriter.Writer) {
o.MockEJMarshaler.MarshalEasyJSON(w)
}
func (o *EasyOrderedTarget) UnmarshalEasyJSON(l *jlexer.Lexer) {
o.MockEJUnmarshaler.UnmarshalEasyJSON(l)
}
type assertionType uint8
const (
assertionTypeUnordered assertionType = iota
assertionTypeOrdered
)
type option func(*options)
type assertion func(v any) func(*testing.T)
type options struct {
readAssertions []assertion
writeAssertions []assertion
}
func withAssertionsAfterRead(fn ...assertion) option {
return func(o *options) {
o.readAssertions = append(o.readAssertions, fn...)
}
}
func withAssertionsAfterWrite(fn ...assertion) option {
return func(o *options) {
o.writeAssertions = append(o.writeAssertions, fn...)
}
}
func runTestSuite[V any, T any](valueConstructor func() *V, targetConstructor func() *T, assertAs assertionType, extras ...option) func(*testing.T) {
return func(t *testing.T) {
t.Helper()
harness := fixtures.NewHarness(t)
harness.Init()
for name, test := range harness.AllTests() {
t.Run(name, testJSONTransforms(test, valueConstructor, targetConstructor, assertAs, extras...))
}
}
}
type Error string
func (e Error) Error() string { return string(e) }
const errTestConfig Error = "error in test config"
func testJSONTransforms[V any, T any](test fixtures.Fixture, valueConstructor func() *V, targetConstructor func() *T, assertAs assertionType, extras ...option) func(*testing.T) {
var expectation string
switch assertAs {
case assertionTypeOrdered:
expectation = "identical" // same JSON, with key order kept
case assertionTypeUnordered:
expectation = "equivalent" // same JSON, the order of keys notwithstanding
default:
panic(fmt.Errorf("invalid assertionType: %d: %w", assertAs, errTestConfig))
}
var o options
for _, apply := range extras {
apply(&o)
}
return func(t *testing.T) {
t.Helper()
t.Run(fmt.Sprintf("ReadJSON then WriteJSON should produce %s JSON", expectation), func(t *testing.T) {
var value V
if valueConstructor != nil {
value = *valueConstructor()
}
if test.ExpectError() {
require.Error(t, jsonutils.ReadJSON(test.JSONBytes(), &value))
for _, fn := range o.readAssertions {
if fn == nil {
continue
}
fn(value)(t)
}
return
}
require.NoError(t, jsonutils.ReadJSON(test.JSONBytes(), &value))
for _, fn := range o.readAssertions {
if fn == nil {
continue
}
fn(value)(t)
}
jazon, err := jsonutils.WriteJSON(value)
require.NoError(t, err)
for _, fn := range o.writeAssertions {
if fn == nil {
continue
}
fn(value)(t)
}
switch assertAs {
case assertionTypeOrdered:
fixtures.JSONEqualOrderedBytes(t, test.JSONBytes(), jazon)
case assertionTypeUnordered:
require.JSONEqBytes(t, test.JSONBytes(), jazon)
}
t.Run(fmt.Sprintf("FromDynamicJSON then WriteJSON should produce %s JSON", expectation), func(t *testing.T) {
var target T
if targetConstructor != nil {
target = *targetConstructor()
}
require.NoError(t, jsonutils.FromDynamicJSON(value, &target))
jazon, err := jsonutils.WriteJSON(target)
require.NoError(t, err)
for _, fn := range o.writeAssertions {
if fn == nil {
continue
}
fn(target)(t)
}
switch assertAs {
case assertionTypeOrdered:
fixtures.JSONEqualOrderedBytes(t, test.JSONBytes(), jazon)
case assertionTypeUnordered:
require.JSONEqBytes(t, test.JSONBytes(), jazon)
}
})
})
}
}