-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter_test.go
71 lines (62 loc) · 1.52 KB
/
converter_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
package yajsonschema
import (
"encoding/json"
"flag"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/go-test/deep"
)
var update = flag.Bool("update", false, "update .json files")
func TestAllCorrectSchemas(t *testing.T) {
t.Parallel()
matches, err := filepath.Glob(filepath.Join("testdata", "*.yaml"))
if err != nil {
t.Fatal(err)
}
for _, inputFileName := range matches {
testName := strings.TrimSuffix(inputFileName, ".yaml")
outputFileName := testName + ".json"
inputFileName := inputFileName
t.Run(testName, func(t *testing.T) {
t.Parallel()
testSchema(t, inputFileName, outputFileName)
})
}
}
func testSchema(t *testing.T, inputFileName, outputFileName string) {
yamlSchemaFile, err := os.Open(inputFileName)
if err != nil {
t.Fatal(err)
}
actualOutput, err := Convert(yamlSchemaFile)
if err != nil {
t.Fatal(err)
}
// We have to deserialise and reserialise this to ensure
// that the types match.
actualBytesOutput, err := json.MarshalIndent(actualOutput, "", " ")
if err != nil {
t.Fatal(err)
}
if *update {
ioutil.WriteFile(outputFileName, actualBytesOutput, 0644)
}
expectedBytesOutput, err := ioutil.ReadFile(outputFileName)
if err != nil {
t.Fatal(err)
}
var expected interface{}
if err = json.Unmarshal(expectedBytesOutput, &expected); err != nil {
t.Fatal(err)
}
var actual interface{}
if err = json.Unmarshal(actualBytesOutput, &actual); err != nil {
t.Fatal(err)
}
if diff := deep.Equal(actual, expected); diff != nil {
t.Error(diff)
}
}