-
Notifications
You must be signed in to change notification settings - Fork 3
/
firemodel_test.go
164 lines (140 loc) · 3.68 KB
/
firemodel_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
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
package firemodel_test
import (
_ "github.com/visor-tax/firemodel/langs/go"
_ "github.com/visor-tax/firemodel/langs/ios"
_ "github.com/visor-tax/firemodel/langs/ts"
"bytes"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/visor-tax/firemodel"
)
const fixturesRoot = "testfixtures/firemodel"
func TestFiremodelFromSchema(t *testing.T) {
file1, err := os.Open("example/firemodel.example.firemodel")
if err != nil {
panic(err)
}
defer file1.Close()
file2, err := os.Open("example/firemodel.common.firemodel")
if err != nil {
panic(err)
}
defer file2.Close()
schema, err := firemodel.ParseSchema(io.MultiReader(file1, file2))
if err != nil {
panic(err)
}
runTest(t, schema)
}
func (ctx *testCtx) firemodelConfig(testName string) *firemodel.Config {
return &firemodel.Config{
Languages: []firemodel.Language{
{Language: "ios", Output: "./swift/"},
{Language: "go", Output: "./go"},
{Language: "ts", Output: "./ts/"},
},
SourceCoderProvider: ctx.newTestSourceCodeProvider(testName),
}
}
type inMemoryFilesByName map[string]*inMemoryFile
func (filesByName inMemoryFilesByName) keys() []string {
ret := make([]string, 0, len(filesByName))
for name := range filesByName {
ret = append(ret, name)
}
return ret
}
type testCtx struct {
prefix string
files inMemoryFilesByName
}
func runTest(t *testing.T, schema *firemodel.Schema) {
ctx := testCtx{
prefix: path.Join(fixturesRoot, t.Name()),
files: make(inMemoryFilesByName),
}
if isUpdate() {
if err := os.RemoveAll(ctx.prefix); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(ctx.prefix, 0700); err != nil {
t.Fatal(err)
}
}
if err := firemodel.Run(schema, ctx.firemodelConfig(t.Name())); err != nil {
t.Fatal(err)
}
if !isUpdate() {
fixtures, err := filepath.Glob(path.Join(ctx.prefix, "*", "*"))
if err != nil {
panic(err)
}
if len(fixtures) != len(ctx.files) {
t.Errorf("Fixtures do not match up with generated files. Fixtures %v, Actual: %v", fixtures, ctx.files.keys())
}
for _, filename := range fixtures {
actualBuf, ok := ctx.files[filename]
if !ok {
t.Errorf("Missing generated file for fixture %s", filename)
continue
}
actual := actualBuf.Bytes()
exp, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatalf("fixture not found for generated file: %v", err)
}
if !bytes.Equal(exp, actual) {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(string(exp), string(actual), true)
t.Error(dmp.DiffPrettyText(diffs))
t.Log("If this diff looks ok, re-run tests with FIREMODEL_UPDATE_FIXTURES=true")
}
}
}
}
func (ctx *testCtx) newTestSourceCodeProvider(testName string) func(prefix string) firemodel.SourceCoder {
return func(prefix string) firemodel.SourceCoder {
coder := &testSourceCoder{
prefix: path.Join(ctx.prefix, prefix),
files: ctx.files,
}
if err := os.MkdirAll(coder.prefix, 0700); err != nil {
panic(err)
}
return coder
}
}
type testSourceCoder struct {
prefix string
files map[string]*inMemoryFile
}
func isUpdate() bool {
_, update := os.LookupEnv("FIREMODEL_UPDATE_FIXTURES")
return update
}
func (ctx *testSourceCoder) NewFile(filename string) (io.WriteCloser, error) {
if isUpdate() {
return os.OpenFile(path.Join(ctx.prefix, filename), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
} else {
var file inMemoryFile
ctx.files[path.Join(ctx.prefix, filename)] = &file
return &file, nil
}
}
func (ctx *testSourceCoder) Flush() error {
return nil
}
type inMemoryFile struct {
bytes.Buffer
}
func (f *inMemoryFile) Write(p []byte) (n int, err error) {
return f.Buffer.Write(p)
}
func (_ *inMemoryFile) Close() error {
return nil
}