-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_test.go
85 lines (82 loc) · 1.57 KB
/
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
package main
import (
"github.com/stretchr/testify/assert"
"go/ast"
"go/parser"
"go/token"
"testing"
)
func TestFileVisitor_Get(t *testing.T) {
testCases := []struct {
src string
want *File
}{
{
src: `
package morm_gen
import (
"fmt"
sqlx "database/sql"
)
import (
dri "database/sql/driver"
)
type (
TestStructType struct {
// Public is a field
// @type string
PublicField string
Ptr *sqlx.NullString
Struct sqlx.NullInt64
Age *int8
Slice []byte
}
)
`,
want: &File{
Package: "morm_gen",
Imports: []string{`"fmt"`, `sqlx "database/sql"`, `dri "database/sql/driver"`},
Types: []Type{
{
Name: "TestStructType",
Fields: []Field{
{
Name: "PublicField",
Type: "string",
},
{
Name: "Ptr",
Type: "*sqlx.NullString",
},
{
Name: "Struct",
Type: "sqlx.NullInt64",
},
{
Name: "Age",
Type: "*int8",
},
{
Name: "Slice",
Type: "[]byte",
},
},
},
},
},
},
}
for _, tc := range testCases {
fset := token.NewFileSet() // 创建一个新的文件集合 fset,用于存储源代码解析的位置信息
// 使用 parser.ParseFile 函数解析源代码,并将解析结果保存在变量 f 中
f, err := parser.ParseFile(fset, "src.go", tc.src, parser.ParseComments)
if err != nil {
t.Fatal(err)
}
tv := &SingleFileEntryVisitor{}
// 使用 ast.Walk 函数遍历抽象语法树(AST)
ast.Walk(tv, f)
file := tv.Get()
assert.Equal(t, tc.want, file)
}
}