-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_file_test.go
427 lines (346 loc) · 8.34 KB
/
main_file_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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package codegen
import (
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
const (
packageName = "packageName"
codeGen = "my-nihongo-codegen"
)
func formatFile(file *File) string {
return formatString(file.GoString())
}
func formatSb(sb strings.Builder) string {
return formatString(sb.String())
}
func formatString(strVal string) string {
if bytes, err := format.Source([]byte(strVal)); err != nil {
panic(err)
} else {
return string(bytes)
}
}
func TestFileSave(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
import "strings"
func myFunc(param1 string, sb strings.Builder) {
if len(sb) != 0 {
return
} else {
sb.WriteString("str")
return
}
}
`
path, err := os.Getwd()
assert.Nil(t, err)
path = filepath.Join(path, "_save_test.go")
f := NewFile(packageName, codeGen)
f.AddImport("strings")
f.Func("myFunc").Params(
Param("param1", "string"),
QualParam("sb", "strings", "Builder"),
).Block(
If(Identifier("sb").IsNotEmpty()).Block(
Return(),
).Else(
Identifier("sb").Call("WriteString").Args(String("str")),
Return(),
),
)
err = f.Save(path)
assert.Nil(t, err)
defer os.Remove(path)
content, err := ioutil.ReadFile(path)
assert.Nil(t, err)
got := string(content)
assert.Equal(t, want, got)
}
func TestFileAddImport(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
import "strings"
`
f := NewFile(packageName, codeGen)
f.AddImport("strings")
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileAddImportAlias(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
import alias "strings"
`
f := NewFile(packageName, codeGen)
f.AddImportAlias("strings", "alias")
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileDeclaration(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
`
f := NewFile(packageName, codeGen)
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileImport(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
import "strings"
`
f := NewFile(packageName, codeGen)
f.Imports(
Import("strings"),
)
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileImportAlias(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
import str "strings"
`
f := NewFile(packageName, codeGen)
f.Imports(
ImportAlias("strings", "str"),
)
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileImports(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
import (
"os"
str "strings"
)
`
f := NewFile(packageName, codeGen)
f.Imports(
Import("os"),
ImportAlias("strings", "str"),
)
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileCommentF(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
// this is a file comment
`
f := NewFile(packageName, codeGen)
f.CommentF("this is a file %s", "comment")
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileFunc(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
func funcName(param1 *alias.typeName, param2 string) (string, error) {
if val, err := os.Getenv("env_var"); err != nil {
return nil, errors.Errorf("this is a format %d", 123)
} else {
if len(val) != 0 {
return val.myField, nil
} else {
return nil, err
}
}
}
`
f := NewFile(packageName, codeGen)
param1, param2 := QualParam("param1", "alias", "typeName").Pointer(), Param("param2", "string")
f.Func("funcName").Params(param1, param2).ReturnTypes(Type("string"), ReturnTypeError()).Block(
IfDecl(
Declare("val", "err").Values(QualFuncCall("os", "Getenv").Args(String("env_var"))),
Err().IsNotNil(),
).Block(
Return(Nil(), QualFuncCall("errors", "Errorf").Args(String("this is a format %d"), Int(123))),
).Else(
If(Identifier("val").IsNotEmpty()).Block(
Return(Identifier("val").Field("myField"), Nil()),
).Else(
Return(Nil(), Err()),
),
),
)
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileMethodGetter(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
func (m *MyType) funcName() int {
return m.field
}
`
this, retType := This("MyType").Pointer(), Type("int")
f := NewFile(packageName, codeGen)
f.Method(this, "funcName").
ReturnTypes(retType).
Block(
Return(
Identifier("m").Field("field"),
),
)
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileDeclareInterface(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
type myInterface interface {
myFunc() string
}
`
f := NewFile(packageName, codeGen)
f.Interface("myInterface").Funcs(
FuncDecl("myFunc").ReturnTypes(Type("string")),
)
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileDeclareInterfaceDynamic(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
type myInterface interface {
MyFunc1()
MyFunc2()
MyFunc3()
}
// MyFunc1 does something
func (m *myInterface) MyFunc1() {
}
// MyFunc2 does something
func (m *myInterface) MyFunc2() {
}
// MyFunc3 does something
func (m *myInterface) MyFunc3() {
}
`
f := NewFile(packageName, codeGen)
decl := f.Interface("myInterface")
for i := 0; i < 3; i++ {
methodName := fmt.Sprintf("MyFunc%d", i+1)
decl.AddFunc(FuncDecl(methodName))
f.CommentF("%s does something", methodName)
f.Method(
This("myInterface").Pointer(),
methodName,
)
}
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileDeclareStruct(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
type myStruct struct {
prop string
}
`
f := NewFile(packageName, codeGen)
f.Struct("myStruct").Props(
Property("prop", "string"),
)
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileDeclareStructDynamic(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
type myStruct struct {
myProp1 string
myProp1_NEW alias.MyType
myProp2 string
myProp2_NEW alias.MyType
myProp3 string
myProp3_NEW alias.MyType
}
// MyProp1 does something
func (m *myStruct) MyProp1() {
}
// MyProp2 does something
func (m *myStruct) MyProp2() {
}
// MyProp3 does something
func (m *myStruct) MyProp3() {
}
`
f := NewFile(packageName, codeGen)
decl := f.Struct("myStruct")
for i := 0; i < 3; i++ {
propName := fmt.Sprintf("myProp%d", i+1)
methodName := strings.Title(propName)
decl.AddProp(propName, "string")
decl.AddQualProp(fmt.Sprintf("%s_NEW", propName), "alias", "MyType")
f.CommentF("%s does something", methodName)
f.Method(
This("myStruct").Pointer(),
methodName,
)
}
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileDeclareType(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
type myType int
`
f := NewFile(packageName, codeGen)
f.Type("myType", "int")
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileDeclareQualType(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
type myType alias.MyType
`
f := NewFile(packageName, codeGen)
f.QualType("myType", "alias", "MyType")
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileVariableDeclaration(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
var var1, var2 string
var var3 alias.MyType
`
f := NewFile(packageName, codeGen)
f.DeclareVars(
Var("var1", "string"),
Var("var2", "string"),
QualVar("var3", "alias", "MyType"),
)
got := formatFile(f)
assert.Equal(t, want, got)
}
func TestFileFunctionWithFor(t *testing.T) {
const want = `// Code generated by my-nihongo-codegen. DO NOT EDIT.
package packageName
func myFunc() {
for true {
}
if false {
}
}
`
f := NewFile(packageName, codeGen)
f.Func("myFunc").Block(
For(nil, Identifier("true"), nil),
If(Identifier("false")),
)
got := formatFile(f)
assert.Equal(t, want, got)
}