forked from ogen-go/ogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ogen_test.go
70 lines (57 loc) · 1.35 KB
/
ogen_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
package ogen_test
import (
"embed"
"encoding/json"
"path"
"strings"
"testing"
helperyaml "github.com/ghodss/yaml"
"github.com/stretchr/testify/require"
"github.com/ogen-go/ogen"
"github.com/ogen-go/ogen/internal/testutil"
)
//go:embed _testdata
var testdata embed.FS
func walkTestdata(t *testing.T, root string, cb func(t *testing.T, file string, data []byte)) {
t.Helper()
testutil.WalkTestdata(t, testdata, root, cb)
}
func TestParse(t *testing.T) {
testcb := func(t *testing.T, file string, data []byte) {
t.Helper()
a := require.New(t)
var yamlInput, jsonInput []byte
if strings.HasSuffix(file, ".json") {
jsonInput = data
val, err := helperyaml.JSONToYAML(data)
a.NoError(err)
yamlInput = val
} else {
yamlInput = data
val, err := helperyaml.YAMLToJSON(data)
a.NoError(err)
jsonInput = val
}
jsonSpec, err := ogen.Parse(jsonInput)
a.NoError(err)
yamlSpec, err := ogen.Parse(yamlInput)
a.NoError(err)
{
jsonOutput, err := json.Marshal(jsonSpec)
a.NoError(err)
yamlOutput, err := json.Marshal(yamlSpec)
a.NoError(err)
a.JSONEq(string(jsonOutput), string(yamlOutput))
}
}
for _, dir := range []string{
"positive",
"negative",
"examples",
} {
dir := dir
t.Run(strings.ToTitle(dir[:1])+dir[1:], func(t *testing.T) {
walkTestdata(t, path.Join("_testdata", dir), testcb)
})
}
}