-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_test.go
124 lines (119 loc) · 2.9 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
package errata
import (
"bytes"
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGenerate(t *testing.T) {
tests := []struct {
name string
args CodeGenConfig
assertion func(t *testing.T, output string)
expectedErr func(err error) (error, bool)
}{
{
name: "basic",
args: CodeGenConfig{
Source: "fixtures/basic.hcl",
Template: "fixtures/basic.tmpl",
Package: "errata",
},
assertion: func(t *testing.T, output string) {
assert.Equal(t, "Basic template with no substitutions", output)
},
},
{
name: "variable substitution",
args: CodeGenConfig{
Source: "fixtures/basic.hcl",
Template: "fixtures/substitution.tmpl",
Package: "errata",
},
assertion: func(t *testing.T, output string) {
assert.Equal(t, "errata", output)
},
},
{
name: "golang",
args: CodeGenConfig{
Source: "fixtures/basic.hcl",
Template: "golang",
Package: "errata",
},
assertion: func(t *testing.T, output string) {
assert.Contains(t, output, "func NewCode1Err")
},
},
{
name: "missing builtin template",
args: CodeGenConfig{
Source: "fixtures/basic.hcl",
Template: "missing",
Package: "errata",
},
expectedErr: func(err error) (error, bool) {
var expected *FileNotFoundErr
ok := errors.As(err, &expected)
return expected, ok
},
},
{
name: "missing provided template",
args: CodeGenConfig{
Source: "fixtures/basic.hcl",
Template: "fixtures/missing.tmpl",
Package: "errata",
},
expectedErr: func(err error) (error, bool) {
var expected *FileNotFoundErr
ok := errors.As(err, &expected)
return expected, ok
},
},
{
name: "template syntax error",
args: CodeGenConfig{
Source: "fixtures/basic.hcl",
Template: "fixtures/invalid-syntax.tmpl",
Package: "errata",
},
expectedErr: func(err error) (error, bool) {
var expected *InvalidSyntaxErr
ok := errors.As(err, &expected)
return expected, ok
},
},
{
name: "validation error: label/arg name clash",
args: CodeGenConfig{
Source: "fixtures/label-arg-clash.hcl",
Template: "golang",
Package: "errata",
},
expectedErr: func(err error) (error, bool) {
var expected *ArgumentLabelNameClashErr
ok := errors.As(err, &expected)
return expected, ok
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var w bytes.Buffer
err := Generate(tt.args, &w)
if err != nil {
// yes, this would be a bit easier with generics, but I don't see this as a compelling enough
// reason to make the lib depend on >=1.18
expected, ok := tt.expectedErr(err)
t.Log(err.Error())
assert.Truef(t, ok, "Expecting error of type %T", expected)
return
}
if tt.assertion == nil {
t.Fatalf("Assertion func must be defined for %q", tt.name)
}
tt.assertion(t, w.String())
})
}
}