-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate_test.go
172 lines (148 loc) · 3.69 KB
/
generate_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
165
166
167
168
169
170
171
172
package cuetsy_test
import (
"flag"
"fmt"
"io/ioutil"
"path/filepath"
"testing"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"cuelang.org/go/pkg/strings"
"github.com/google/go-cmp/cmp"
"github.com/grafana/cuetsy"
"github.com/grafana/cuetsy/internal/cuetxtar"
"github.com/stretchr/testify/require"
"golang.org/x/tools/txtar"
"gotest.tools/assert"
)
const CasesDir = "testdata"
type TestCaseType int
const (
TSType TestCaseType = 0
ErrorType TestCaseType = 1
)
type Case struct {
CaseType TestCaseType
Name string
CUE string
TS string
ERROR string
}
var updateGolden = flag.Bool("update-golden", false, "Update golden files with test results")
func TestGenerateWithImports(t *testing.T) {
test := cuetxtar.TxTarTest{
Root: "./testdata/imports",
Name: "gen",
Update: *updateGolden,
ToDo: map[string]string{
"imports/oneref_verbose": "Figure out how to disambiguate struct literals from the struct-with-braces-and-one-element case",
},
}
importMappers := map[string]func(s string) (string, error){
"imports/imports": func(s string) (string, error) {
if s == "example.com/dep" {
return "@example/deps", nil
}
return s, nil
},
}
ctx := cuecontext.New()
test.Run(t, func(t *cuetxtar.Test) {
v := ctx.BuildInstance(t.ValidInstances()[0])
if v.Err() != nil {
t.Fatal(v.Err())
}
im := func(s string) (string, error) {
return "", nil
}
if i, ok := importMappers[t.Name]; ok {
im = i
}
b, err := cuetsy.Generate(v, cuetsy.Config{
Export: true,
ImportMapper: im,
})
if err != nil {
errors.Print(t, err, nil)
t.Fatal(errors.Details(err, nil))
}
_, _ = t.Write(b)
})
}
func TestGenerate(t *testing.T) {
cases, err := loadCases(CasesDir)
if err != nil {
t.Fatal(err)
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
ctx := cuecontext.New()
i := ctx.CompileString(c.CUE, cue.Filename(c.Name+".cue"))
if err != nil {
t.Fatal(err)
}
out, err := cuetsy.Generate(i.Value(), cuetsy.Config{
ImportMapper: func(path string) (string, error) {
return path, nil
},
Export: true,
})
if c.CaseType == ErrorType {
assert.Error(t, err, c.ERROR)
} else {
if err != nil {
t.Fatal(err)
}
if s := cmp.Diff(c.TS, string(out)); s != "" {
t.Fatal(s)
}
}
})
}
}
func TestGenerateSingleAST(t *testing.T) {
ctx := cuecontext.New()
v := ctx.CompileString("a: *\"foo\" | string")
p, err := cuetsy.GenerateSingleAST("My-Invalid-Name", v, cuetsy.TypeInterface)
require.NoError(t, err)
assert.Equal(t, "export interface MyInvalidName {\n a: string;\n}", p.T.String())
assert.Equal(t, "export const defaultMyInvalidName: Partial<MyInvalidName> = {\n a: 'foo',\n};", p.D.String())
}
func loadCases(dir string) ([]Case, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
var cases []Case
for _, fi := range files {
if fi.IsDir() {
continue
}
file := filepath.Join(dir, fi.Name())
a, err := txtar.ParseFile(file)
if err != nil {
return nil, err
}
if len(a.Files) != 2 {
return nil, fmt.Errorf("malformed test case '%s': Must contain exactly two files (CUE and TS/ERR), but has %d", file, len(a.Files))
}
name := strings.TrimSuffix(fi.Name(), ".txtar")
if strings.HasSuffix(name, "error") {
cases = append(cases, Case{
CaseType: ErrorType,
Name: name,
CUE: string(a.Files[0].Data),
ERROR: strings.TrimSuffix(string(a.Files[1].Data), "\n"),
})
} else {
cases = append(cases, Case{
CaseType: TSType,
Name: name,
CUE: string(a.Files[0].Data),
TS: string(a.Files[1].Data),
})
}
}
return cases, nil
}