-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
template_test.go
511 lines (426 loc) · 8.61 KB
/
template_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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// Tests for template
package main
import (
"bytes"
"go/build"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"testing"
)
func init() {
testingMode = true
}
type TestTemplate struct {
title string
args string
pkg string
in string
outName string
out string
}
const basicTest = `package tt
// template type Set(A)
type A int
func init() {}
type Set struct { a A }
func NewSet(a A) A { return A(0) }
func NewSizedSet(a A) A { return A(1) }
func UtilityFunc1() {}
func utilityFunc() {}
func (a A) f0() {}
func (a *A) F1() {}
type N struct{}
type M struct{ N }
type K struct{ N int }
func (a *A) N() {}
func (a *A) O() { a.N() }
func f2() M { return M{N: N{}} }
func f3() K { return K{N: 0} }
func f4() N { return f2().N }
func f5() int { return f3().N }
func f6() {
N := 0
_ = N
}
var AVar1 int
var aVar2 int
var (
AVar3 int
aVar4 int
)
`
var tests = []TestTemplate{
{
title: "Simple test public",
args: "MySet(int)",
pkg: "main",
in: basicTest,
outName: "gotemplate_MySet.go",
out: `// Code generated by gotemplate. DO NOT EDIT.
package main
// template type Set(A)
func init() {}
type MySet struct{ a int }
func NewMySet(a int) int { return int(0) }
func NewSizedMySet(a int) int { return int(1) }
func UtilityFunc1MySet() {}
func utilityFuncMySet() {}
func (a int) f0() {}
func (a *int) F1() {}
type NMySet struct{}
type MMySet struct{ NMySet }
type KMySet struct{ N int }
func (a *int) N() {}
func (a *int) O() { a.N() }
func f2MySet() MMySet { return MMySet{NMySet: NMySet{}} }
func f3MySet() KMySet { return KMySet{N: 0} }
func f4MySet() NMySet { return f2MySet().NMySet }
func f5MySet() int { return f3MySet().N }
func f6MySet() {
N := 0
_ = N
}
var AVar1MySet int
var aVar2MySet int
var (
AVar3MySet int
aVar4MySet int
)
`,
},
{
title: "Simple test private",
args: "mySet(float64)",
pkg: "main",
in: basicTest,
outName: "gotemplate_mySet.go",
out: `// Code generated by gotemplate. DO NOT EDIT.
package main
// template type Set(A)
func init() {}
type mySet struct{ a float64 }
func newMySet(a float64) float64 { return float64(0) }
func newSizedMySet(a float64) float64 { return float64(1) }
func utilityFunc1MySet() {}
func utilityFuncMySet() {}
func (a float64) f0() {}
func (a *float64) F1() {}
type nMySet struct{}
type mMySet struct{ nMySet }
type kMySet struct{ N int }
func (a *float64) N() {}
func (a *float64) O() { a.N() }
func f2MySet() mMySet { return mMySet{nMySet: nMySet{}} }
func f3MySet() kMySet { return kMySet{N: 0} }
func f4MySet() nMySet { return f2MySet().nMySet }
func f5MySet() int { return f3MySet().N }
func f6MySet() {
N := 0
_ = N
}
var aVar1MySet int
var aVar2MySet int
var (
aVar3MySet int
aVar4MySet int
)
`,
},
{
title: "Test function",
args: "Min(int8, func(a int8, b int8) bool { return a < b })",
pkg: "main",
in: `package tt
// template type TT(A, Less)
type A int
func Less(a, b A) bool { return a < b }
func TT(a, b A) A {
if Less(a, b) {
return a
}
return b
}
`,
outName: "gotemplate_Min.go",
out: `// Code generated by gotemplate. DO NOT EDIT.
package main
// template type TT(A, Less)
func Min(a, b int8) int8 {
if func(a int8, b int8) bool {
return a < b
}(a, b) {
return a
}
return b
}
`,
},
{
title: "Simple Test constants",
args: "Vector2(float32, 2)",
pkg: "main",
in: `package tt
// template type Vector(A, n)
type A float32
const n = 3
type Vector [n]A
func (v Vector) Add(b Vector) {
for i := range v {
v[i] += b[i]
}
}
`,
outName: "gotemplate_Vector2.go",
out: `// Code generated by gotemplate. DO NOT EDIT.
package main
// template type Vector(A, n)
type Vector2 [2]float32
func (v Vector2) Add(b Vector2) {
for i := range v {
v[i] += b[i]
}
}
`,
},
{
title: "Test constants",
args: "Matrix22(float32, 2, 2)",
pkg: "main",
in: `package mat
// template type Matrix(A, n, m)
type A float32
const (
n, a, b, m = 1, 2, 3, 1
)
type Matrix [n][m]A
func (mat Matrix) Add(x Matrix) {
for i := range mat {
for j := range mat[i] {
mat[i][j] += x[i][j]
}
}
}
`,
outName: "gotemplate_Matrix22.go",
out: `// Code generated by gotemplate. DO NOT EDIT.
package main
// template type Matrix(A, n, m)
const (
aMatrix22, bMatrix22 = 2, 3
)
type Matrix22 [2][2]float32
func (mat Matrix22) Add(x Matrix22) {
for i := range mat {
for j := range mat[i] {
mat[i][j] += x[i][j]
}
}
}
`,
},
{
title: "Test vars",
args: "ProgXX(xx1, xx2, xx3, xx4, xx5, xx6)",
pkg: "main",
in: `package prog
// template type Prog(a, b, c, d, e, f)
type A float32
var (
a, z = 1, 2
b, n, m, c = 3, 4, 5, 6
d = 7
)
var (
o = 8
e = 8
)
var (
oo = 9
)
var (
p, f, q = 11, 12, 13
)
func Prog() int {return a+b+c+d+e+f}
`,
outName: "gotemplate_ProgXX.go",
out: `// Code generated by gotemplate. DO NOT EDIT.
package main
// template type Prog(a, b, c, d, e, f)
type AProgXX float32
var (
zProgXX = 2
nProgXX, mProgXX = 4, 5
)
var (
oProgXX = 8
)
var (
ooProgXX = 9
)
var (
pProgXX, qProgXX = 11, 13
)
func ProgXX() int { return xx1 + xx2 + xx3 + xx4 + xx5 + xx6 }
`,
},
{
title: "Test complex type decls",
args: "tmpl(int, string, map[string]map[string]chan int, float32, rune, chan []string)",
pkg: "main",
in: `package tt
// template type TMPL(A, B, C, D, E, F)
type A int
type TMPL struct {
a A
b B
c C
d D
e E
f F
}
type ImportantType bool
type (
ImportantType1 int
B struct {
v map[int][][][]rune
}
importantType2 map[int]int
C chan struct {
x []string
}
)
type (
D rune
importantType3 struct{}
E string
F map[string]int
)
`,
outName: "gotemplate_tmpl.go",
out: `// Code generated by gotemplate. DO NOT EDIT.
package main
// template type TMPL(A, B, C, D, E, F)
type tmpl struct {
a int
b string
c map[string]map[string]chan int
d float32
e rune
f chan []string
}
type importantTypeTmpl bool
type (
importantType1Tmpl int
importantType2Tmpl map[int]int
)
type (
importantType3Tmpl struct{}
)
`,
},
}
func testTemplate(t *testing.T, test *TestTemplate) {
// Disable logging
log.SetOutput(ioutil.Discard)
// Make temporary directory
dir, err := ioutil.TempDir("", "gotemplate_test")
if err != nil {
t.Fatalf("Failed to make temp dir: %v", err)
}
defer func() {
err := os.RemoveAll(dir)
if err != nil {
t.Logf("Failed to remove temp dir: %v", err)
}
}()
// Make subdirectories
src := path.Join(dir, "src")
err = os.Mkdir(src, 0700)
if err != nil {
t.Fatalf("Failed to make dir %q: %v", src, err)
}
input := path.Join(src, "input")
err = os.Mkdir(input, 0700)
if err != nil {
t.Fatalf("Failed to make dir %q: %v", input, err)
}
output := path.Join(src, "output")
err = os.Mkdir(output, 0700)
if err != nil {
t.Fatalf("Failed to make dir %q: %v", output, err)
}
// Change directory to output directory
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to read cwd: %v", err)
}
err = os.Chdir(output)
if err != nil {
t.Fatalf("Failed to cd %q dir: %v", output, err)
}
defer func() {
err := os.Chdir(cwd)
if err != nil {
t.Logf("Failed to change back to cwd: %v", err)
}
}()
// Set GOPATH to directory
build.Default.GOPATH = dir
// Write template input
tmpl := path.Join(input, "main.go")
err = ioutil.WriteFile(tmpl, []byte(test.in), 0600)
if err != nil {
t.Fatalf("Failed to write %q: %v", tmpl, err)
}
// Write main.go for output
main := path.Join(output, "main.go")
err = ioutil.WriteFile(main, []byte("package main"), 0600)
if err != nil {
t.Fatalf("Failed to write %q: %v", main, err)
}
// Instantiate template
template := newTemplate(output, "input", test.args)
template.instantiate()
// Check output
expectedFile := path.Join(output, test.outName)
actualBytes, err := ioutil.ReadFile(expectedFile)
if err != nil {
t.Fatalf("Failed to read %q: %v", expectedFile, err)
}
actual := string(actualBytes)
if actual != test.out {
t.Errorf(`Output is wrong
Got
-------------
%s
-------------
Expected
-------------
%s
-------------
`, actual, test.out)
actualFile := expectedFile + ".actual"
err = ioutil.WriteFile(actualFile, []byte(test.out), 0600)
if err != nil {
t.Fatalf("Failed to write %q: %v", actualFile, err)
}
cmd := exec.Command("diff", "-u", actualFile, expectedFile)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
_ = cmd.Run()
t.Errorf("Diff\n----\n%s", out.String())
}
}
func TestSub(t *testing.T) {
fatalf = func(format string, args ...interface{}) {
t.Fatalf(format, args...)
}
for i := range tests {
t.Logf("Test[%d] %q", i, tests[i].title)
testTemplate(t, &tests[i])
}
}