-
Notifications
You must be signed in to change notification settings - Fork 8
/
other_test.go
208 lines (201 loc) · 4.83 KB
/
other_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
package gotype
import (
"fmt"
"go/token"
"reflect"
"strconv"
"strings"
"testing"
)
func TestOther(t *testing.T) {
var testpath = []string{
"github.com/wzshiming/gotype/testdata/value",
"github.com/wzshiming/gotype/testdata/kind",
"github.com/wzshiming/gotype/testdata/type",
"github.com/wzshiming/gotype/testdata/pkg",
"./testdata/value",
"./testdata/kind",
"./testdata/type",
"./testdata/pkg",
}
for _, src := range testpath {
testAll(t, src)
}
}
func testAll(t *testing.T, src string) {
imp := getImporter(t)
scope, err := imp.Import(src, "")
if err != nil {
t.Fatal(err)
}
fset := imp.FileSet()
num := scope.NumChild()
for i := 0; i != num; i++ {
v := scope.Child(i)
testType(t, fset, v)
}
}
func testType(t *testing.T, fset *token.FileSet, v Type) {
for _, line := range strings.Split(v.Doc().Text(), "\n") {
if line == "" {
continue
}
v := v
pos := fset.Position(v.Origin().Pos())
tag := reflect.StructTag(line)
if data, ok := tag.Lookup("To"); ok {
st := strings.Split(data, ",")
for _, to := range st {
method := strings.Split(to, ":")
switch method[0] {
case "Elem":
v = v.Elem()
case "Declaration":
v = v.Declaration()
case "Key":
v = v.Key()
case "In":
if len(method) < 2 {
t.Fatal(pos, "Error In num: ", to)
}
i, _ := strconv.ParseInt(method[1], 10, 64)
v = v.In(int(i))
case "Out":
if len(method) < 2 {
t.Fatal(pos, "Error Out num: ", to)
}
i, _ := strconv.ParseInt(method[1], 10, 64)
v = v.Out(int(i))
case "Field":
if len(method) < 2 {
t.Fatal(pos, "Error Field num: ", to)
}
i, err := strconv.ParseInt(method[1], 10, 64)
if err != nil {
t.Fatal(pos, "Error Field: ", err)
}
if v.NumField() <= int(i) {
t.Fatal(pos, "Error Out of index range: ", to)
}
v = v.Field(int(i))
case "Method":
if len(method) < 2 {
t.Fatal(pos, "Error Method num: ", to)
}
i, err := strconv.ParseInt(method[1], 10, 64)
if err != nil {
t.Fatal(pos, "Error Method: ", err)
}
if v.NumMethod() <= int(i) {
t.Fatal(pos, "Error Out of index range: ", to)
}
v = v.Method(int(i))
case "Child":
if len(method) < 2 {
t.Fatal(pos, "Error Child num: ", to)
}
i, err := strconv.ParseInt(method[1], 10, 64)
if err != nil {
t.Fatal(pos, "Error Child: ", err)
}
if v.NumMethod() <= int(i) {
t.Fatal(pos, "Error Out of index range: ", to)
}
v = v.Child(int(i))
case "FieldByName":
if len(method) < 2 {
t.Fatal(pos, "Error FieldByName num: ", to)
}
v, ok = v.FieldByName(method[1])
if !ok {
t.Fatal(pos, "Error not found: ", to)
}
case "MethodByName":
if len(method) < 2 {
t.Fatal(pos, "Error MethodByName num: ", to)
}
v, ok = v.MethodByName(method[1])
if !ok {
t.Fatal(pos, "Error not found: ", to)
}
case "ChildByName":
if len(method) < 2 {
t.Fatal(pos, "Error ChildByName num: ", to)
}
v, ok = v.ChildByName(method[1])
if !ok {
t.Fatal(pos, "Error not found: ", to)
}
default:
t.Fatal(pos, "Error to: ", to)
}
}
}
if data, ok := tag.Lookup("Value"); ok {
val := v.Value()
if data != val {
t.Fatal(pos, "Error value:", val, ":", data)
}
}
if data, ok := tag.Lookup("Name"); ok {
name := v.Name()
if data != name {
t.Fatal(pos, "Error name:", name, ":", data)
}
}
if data, ok := tag.Lookup("String"); ok {
str := v.String()
if data != str {
t.Fatal(pos, "Error string:", str, ":", data)
}
}
if data, ok := tag.Lookup("Kind"); ok {
kind := v.Kind().String()
if data != kind {
t.Fatal(pos, "Error kind:", kind, ":", data)
}
}
if data, ok := tag.Lookup("Len"); ok {
l := fmt.Sprint(v.Len())
if data != l {
t.Fatal(pos, "Error len:", l, ":", data)
}
}
if data, ok := tag.Lookup("NumChild"); ok {
num := fmt.Sprint(v.NumChild())
if data != num {
t.Fatal(pos, "Error num child:", num, ":", data)
}
}
if data, ok := tag.Lookup("NumMethod"); ok {
num := fmt.Sprint(v.NumMethod())
if data != num {
t.Fatal(pos, "Error num method:", num, ":", data)
}
}
if data, ok := tag.Lookup("NumIn"); ok {
num := fmt.Sprint(v.NumIn())
if data != num {
t.Fatal(pos, "Error num in:", num, ":", data)
}
}
if data, ok := tag.Lookup("NumOut"); ok {
num := fmt.Sprint(v.NumOut())
if data != num {
t.Fatal(pos, "Error num out:", num, ":", data)
}
}
if data, ok := tag.Lookup("NumField"); ok {
num := fmt.Sprint(v.NumField())
if data != num {
t.Fatal(pos, "Error num field:", num, ":", data)
}
}
if data, ok := tag.Lookup("Tag"); ok {
num := string(v.Tag())
if data != num {
t.Fatal(pos, "Error tag:", num, ":", data)
}
}
}
}