-
Notifications
You must be signed in to change notification settings - Fork 14
/
modelgen_test.go
106 lines (86 loc) · 2.38 KB
/
modelgen_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
/***
Copyright 2014 Cisco Systems Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/contiv/modelgen/generators"
)
// Simple test to parse json schema
func TestParseJsonSchema(t *testing.T) {
if err := generators.ParseTemplates(); err != nil {
t.Fatal(err)
}
dir, err := os.Open("testdata")
if err != nil {
t.Fatal(err)
}
dirnames, err := dir.Readdirnames(0)
if err != nil {
t.Fatal(err)
}
for _, name := range dirnames {
t.Logf("Parsing suite %s", name)
var schema *Schema
basepath := filepath.Join("testdata", name)
// Parse all files in input directory
err := filepath.Walk(basepath, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
// Ignore non-json files and ignore files in subdirs
if filepath.Ext(path) != ".json" {
return nil
}
t.Logf("Parsing file: %q\n", path)
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
// Parse the schema
sch, err := ParseSchema(b)
if err != nil {
return err
}
// Append to global schema
schema = MergeSchema(schema, sch)
return nil
})
if err != nil {
t.Fatal(err)
}
if schema == nil {
t.Fatal("Could not find schema, aborting.")
}
// Generate the code
goStr, err := schema.GenerateGo()
if err != nil {
t.Fatalf("Error generating go code. Err: %v", err)
}
output, err := ioutil.ReadFile(filepath.Join(basepath, "output.go"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(goStr, output) {
if err := ioutil.WriteFile("/tmp/generated.go", []byte(goStr), 0666); err != nil {
t.Fatalf("Err writing debug file `/tmp/generated.go` during failed test: %v", err)
}
fmt.Println("Generated code written to /tmp/generated.go")
t.Fatal("Generated string from input was not equal to output string")
}
}
}