-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuilder_test.go
201 lines (168 loc) · 3.88 KB
/
builder_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
package skyencoder
import (
"fmt"
"go/build"
"go/types"
"io/ioutil"
"os"
"path/filepath"
"testing"
"golang.org/x/tools/go/loader"
// needed to verify test output
_ "github.com/skycoin/skycoin/src/coin" // needed to verify test output
)
func removeFile(fn string) {
os.Remove(fn)
}
func verifyProgramCompiles(t *testing.T, dir string) {
// Load the package with the least restrictive parsing and type checking,
// so that a package that doesn't compile can still have a struct declaration extracted
cfg := loader.Config{
Build: &build.Default,
ParserMode: 0,
TypeChecker: types.Config{
IgnoreFuncBodies: false, // ignore functions
FakeImportC: false, // ignore import "C"
DisableUnusedImportCheck: false, // ignore unused imports
},
AllowErrors: false,
}
loadTests := true
unused, err := cfg.FromArgs([]string{dir}, loadTests)
if err != nil {
t.Fatal(err)
}
if len(unused) != 0 {
t.Fatalf("Had unused args to cfg.FromArgs: %v", unused)
}
_, err = cfg.Load()
if err != nil {
t.Fatal(err)
}
}
func testBuildCode(t *testing.T, structName, filename string) []byte {
program, err := LoadProgram([]string{"."}, nil)
if err != nil {
t.Fatal(err)
}
sInfo, err := FindStructInfoInProgram(program, structName)
if err != nil {
t.Fatal(err)
}
src, err := BuildStructEncoder(sInfo, "", filename, true)
if err != nil {
t.Fatal(err)
}
// Go's parser and loader packages do not accept []byte, only filenames, so save the result to disk
// and clean it up after the test
defer removeFile(filename)
err = ioutil.WriteFile(filename, src, 0644)
if err != nil {
t.Fatal(err)
}
verifyProgramCompiles(t, ".")
return src
}
func TestBuildSkycoinSignedBlock(t *testing.T) {
importPath := "github.com/skycoin/skycoin/src/coin"
structName := "SignedBlock"
fullPath, err := FindDiskPathOfImport(importPath)
if err != nil {
t.Fatal(err)
}
filename := filepath.Join(fullPath, "signed_block_skyencoder_xxxyyy.go")
program, err := LoadProgram([]string{importPath}, nil)
if err != nil {
t.Fatal(err)
}
sInfo, err := FindStructInfoInProgram(program, structName)
if err != nil {
t.Fatal(err)
}
src, err := BuildStructEncoder(sInfo, "", filename, true)
if err != nil {
t.Fatal(err)
}
// Go's parser and loader packages do not accept []byte, only filenames, so save the result to disk
// and clean it up after the test
defer removeFile(filename)
err = ioutil.WriteFile(filename, src, 0644)
if err != nil {
t.Fatal(err)
}
verifyProgramCompiles(t, importPath)
}
func testBuildCodeFails(t *testing.T, structName, filename string) {
program, err := LoadProgram([]string{"."}, nil)
if err != nil {
t.Fatal(err)
}
sInfo, err := FindStructInfoInProgram(program, structName)
if err != nil {
t.Fatal(err)
}
_, err = BuildStructEncoder(sInfo, "", filename, true)
if err == nil {
t.Fatal("Expected BuildStructEncoder error")
}
}
/* Invalid structs */
type MaxLenInt struct {
Int64 int64 `enc:",maxlen=4"`
}
type MaxLenInvalid struct {
String string `enc:",maxlen=foo"`
}
type OmitEmptyInt struct {
Int64 int64 `enc:',omitempty"`
}
type OmitEmptyNotFinal struct {
Int64 int64
Extra []byte `enc:",omitempty"`
String string
}
type EmptyStructSlice1 struct {
Foo []struct{}
}
type EmptyStructSlice2 struct {
Foo []struct {
unexported int64
}
}
type EmptyStructSlice3 struct {
Foo []struct {
Ignored int64 `enc:"-"`
}
}
func TestBuildFails(t *testing.T) {
cases := []struct {
name string
}{
{
name: "MaxLenInt",
},
{
name: "MaxLenInvalid",
},
{
name: "OmitEmptyInt",
},
{
name: "OmitEmptyNotFinal",
},
{
name: "EmptyStructSlice1",
},
{
name: "EmptyStructSlice2",
},
{
name: "EmptyStructSlice3",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
testBuildCodeFails(t, tc.name, fmt.Sprintf("./%s_skyencoder_test.go", ToSnakeCase(tc.name)))
})
}
}