-
Notifications
You must be signed in to change notification settings - Fork 1
/
rewriters_test.go
89 lines (85 loc) · 1.69 KB
/
rewriters_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
package genx_test
import (
"io/ioutil"
"log"
"regexp"
"testing"
"github.com/OneOfOne/genx"
)
func init() {
log.SetFlags(log.Lshortfile)
}
func TestAllTypes(t *testing.T) {
testCases := []struct {
Name string
Input map[string]string
FailIfMatch *regexp.Regexp
}{
{
"Delete:Type:interface{}",
map[string]string{
"type:interface{}": "-",
},
regexp.MustCompile(`interface\{\}`),
},
{
"Delete:Type:TypeWithKT",
map[string]string{"type:TypeWithKT": "-"},
regexp.MustCompile(`TypeWithKT`),
},
{
"Delete:Field:RemoveMe",
map[string]string{"field:RemoveMe": "-"},
regexp.MustCompile(`RemoveMe`),
},
{
"Rename:Field:Call",
map[string]string{"field:Call": "NewFuncName"},
regexp.MustCompile(`\.Call`),
},
{
"Type:interface{}=int",
map[string]string{
"type:VT": "int",
"type:interface{}": "int",
},
regexp.MustCompile(`interface\{\}`),
},
{
"Type:KT=int,VT=int",
map[string]string{
"type:KT": "uint64",
"type:VT": "int",
},
regexp.MustCompile(`KT|VT`),
},
{
"Func:DoStuff",
map[string]string{
"func:DoStuff": "-",
},
// regexp.MustCompile(`DoStuff\(|\sTwo\(`), // TODO: fix #5
regexp.MustCompile(`DoStuff\(`),
},
}
src, err := ioutil.ReadFile("./all_types.go")
fatalIf(t, err)
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
g := genx.New("", tc.Input)
pf, err := g.Parse("src.go", src)
if err != nil {
t.Errorf("%+v\n%s", err, pf.Src)
return
}
if tc.FailIfMatch.Match(pf.Src) {
t.Errorf("%s matched :(\n%s", tc.FailIfMatch, pf.Src)
}
})
}
}
func fatalIf(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}