-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate.go
199 lines (165 loc) · 4.08 KB
/
generate.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
//go:generate go run generate.go service/*.json
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"text/template"
"github.com/datacratic/aws-sdk-go/internal/fixtures/helpers"
"github.com/datacratic/aws-sdk-go/internal/model/api"
"github.com/datacratic/aws-sdk-go/internal/util"
)
type TestSuite struct {
API *api.API
PackageName string
APIVersion string `json:"api_version"`
Cases []*TestCase
}
type TestCase struct {
API *api.API
Description string
Operation string
Input interface{}
Assertions []*TestAssertion
}
type TestAssertion struct {
Case *TestCase
Assertion string
Context string
Path string
Expected interface{}
}
var tplTestSuite = template.Must(template.New("testsuite").Parse(`
// +build integration
package {{ .API.PackageName }}_test
import (
"testing"
"github.com/datacratic/aws-sdk-go/aws"
"github.com/datacratic/aws-sdk-go/internal/util/utilassert"
"github.com/datacratic/aws-sdk-go/service/{{ .API.PackageName }}"
"github.com/stretchr/testify/assert"
)
var (
_ = assert.Equal
_ = utilassert.Match
)
{{ range $_, $t := .Cases }}{{ $t.GoCode }}{{ end }}
`))
var tplTestCase = template.Must(template.New("testcase").Parse(`
func Test{{ .TestName }}(t *testing.T) {
client := {{ .API.NewAPIGoCodeWithPkgName "nil" }}
resp, e := client.{{ .API.ExportableName .Operation }}({{ .InputCode }})
err := aws.Error(e)
_, _, _ = resp, e, err // avoid unused warnings
{{ range $_, $a := .Assertions }}{{ $a.GoCode }}{{ end }}
}
`))
func (t *TestSuite) setup() {
_, d, _, _ := runtime.Caller(1)
file := filepath.Join(path.Dir(d), "..", "..", "..", "apis",
t.PackageName, t.APIVersion+".normal.json")
t.API = &api.API{}
t.API.Attach(file)
for _, c := range t.Cases {
c.API = t.API
for _, a := range c.Assertions {
a.Case = c
}
}
}
func (t *TestSuite) write() {
_, d, _, _ := runtime.Caller(1)
file := filepath.Join(path.Dir(d), "..", "..", "..", "service",
t.PackageName, "integration_test.go")
var buf bytes.Buffer
if err := tplTestSuite.Execute(&buf, t); err != nil {
panic(err)
}
b := []byte(util.GoFmt(buf.String()))
ioutil.WriteFile(file, b, 0644)
}
func (t *TestCase) TestName() string {
out := ""
for _, v := range strings.Split(t.Description, " ") {
out += util.Capitalize(v)
}
return out
}
func (t *TestCase) GoCode() string {
var buf bytes.Buffer
if err := tplTestCase.Execute(&buf, t); err != nil {
panic(err)
}
return util.GoFmt(buf.String())
}
func (t *TestCase) InputCode() string {
op := t.API.Operations[t.API.ExportableName(t.Operation)]
if op.InputRef.Shape == nil {
return ""
}
return helpers.ParamsStructFromJSON(t.Input, op.InputRef.Shape, true)
}
func (t *TestAssertion) GoCode() string {
call, actual, expected := "", "", fmt.Sprintf("%#v", t.Expected)
if expected == "<nil>" {
expected = "nil"
}
switch t.Context {
case "error":
actual = "err"
case "data":
actual = "resp"
default:
panic("unsupported assertion context " + t.Context)
}
if t.Path != "" {
actual += "." + util.Capitalize(t.Path)
}
switch t.Assertion {
case "typeof":
return "" // do nothing for typeof checks
case "equal":
if actual == "err" && expected == "nil" {
call = "assert.NoError"
} else {
call = "assert.Equal"
}
case "notequal":
call = "assert.NotEqual"
case "contains":
call = "utilassert.Match"
default:
panic("unsupported assertion type " + t.Assertion)
}
return fmt.Sprintf("%s(t, %s, %s)\n", call, expected, actual)
}
func GenerateIntegrationSuite(testFile string) {
pkgName := strings.Replace(filepath.Base(testFile), ".json", "", -1)
suite := &TestSuite{PackageName: pkgName}
if file, err := os.Open(testFile); err == nil {
defer file.Close()
if err = json.NewDecoder(file).Decode(&suite); err != nil {
panic(err)
}
suite.setup()
suite.write()
} else {
panic(err)
}
}
func main() {
files := []string{}
for _, arg := range os.Args[1:] {
paths, _ := filepath.Glob(arg)
files = append(files, paths...)
}
for _, file := range files {
GenerateIntegrationSuite(file)
}
}