forked from marcusolsson/json-schema-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_test.go
More file actions
72 lines (61 loc) · 1.59 KB
/
schema_test.go
File metadata and controls
72 lines (61 loc) · 1.59 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
package main
import (
"bytes"
"flag"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
var update = flag.Bool("update", false, "update .golden files")
func TestSchema(t *testing.T) {
schemaTests := []struct {
name string
schema string
level int
}{
{name: "address", schema: "address.schema.json"},
{name: "arrays", schema: "arrays.schema.json"},
{name: "basic", schema: "basic.schema.json"},
{name: "calendar", schema: "calendar.schema.json"},
{name: "card", schema: "card.schema.json"},
{name: "geographical-location", schema: "geographical-location.schema.json"},
{name: "ref-hell", schema: "ref-hell.schema.json"},
{name: "union", schema: "union.schema.json"},
{name: "deep-headings", schema: "ref-hell.schema.json", level: 5},
}
for _, tt := range schemaTests {
t.Run(tt.name, func(t *testing.T) {
f, err := os.Open(filepath.Join("testdata", tt.schema))
if err != nil {
t.Fatal(err)
}
defer f.Close()
schema, err := newSchema(f, "testdata")
if err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
if tt.level > 0 {
buf.WriteString(schema.Markdown(tt.level))
} else {
buf.WriteString(schema.Markdown(1))
}
gp := filepath.Join("testdata", strings.Replace(t.Name()+".golden", "/", "_", -1))
if *update {
if err := ioutil.WriteFile(gp, buf.Bytes(), 0644); err != nil {
t.Fatal("failed to update golden ")
}
}
g, err := ioutil.ReadFile(gp)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf.Bytes(), g) {
t.Log(buf.String())
t.Errorf("data does not match .golden file")
}
})
}
}