-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentity_test.go
287 lines (266 loc) · 7.81 KB
/
identity_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
package gospec
import "testing"
// func (s *Spec) Identical(v, t string) bool
// 1. type A1 = B 则 A1 与 B 类型相同
// type A1 = B , they are identical
func TestIdentical01(t *testing.T) {
s := NewSpec(`type B int; type A1 = B`)
if !s.Identical("A1", "B") {
t.Error(`test rule failed`)
}
}
// 2. type A2 B 则 A2 与 B 类型不同
// type A2 B , they are different
func TestIdentical02(t *testing.T) {
s := NewSpec(`type B int; type A2 B`)
if s.Identical("A2", "B") {
t.Error(`test rule failed`)
}
}
// 3.1. 数组:如果 元素类型 和 数组长度 都相同,那么类型相同
// Two array types are identical if they have identical element types and the same array length.
func TestIdentical03(t *testing.T) {
s := NewSpec(`var a311, a312 [2]int; var a313 [2]int64`)
if !s.Identical("a311", "a312") {
t.Error(`test rule failed`)
}
if s.Identical("a311", "a313") {
t.Error(`test rule failed`)
}
}
// 3.2 切片:如果 元素类型 相同,那么类型相同
// Two slice types are identical if they have identical element types.
func TestIdentical04(t *testing.T) {
s := NewSpec(`var a321, a322 []int; var a323 []int64`)
if !s.Identical("a321", "a322") {
t.Error(`test rule failed`)
}
if s.Identical("a321", "a323") {
t.Error(`test rule failed`)
}
}
// 3.3 结构体: 如果 属性顺序 相同,且对应属性的名字、类型、标签都相同,那么类型相同。
// 注意:不同包里面的结构体的未导出的属性一定不相同
// 简单来说,如果在不同的包里的两个结构体满足前述条件,那么,只有当它们所有的属性都是导出属性的时候,它们俩类型才相同,只要有未导出属性,那么它们俩类型一定不相同
// Two struct types are identical if they have the same sequence of fields, and if corresponding fields have the same names, and identical types, and identical tags. Non-exported field names from different packages are always different.
func TestIdentical05(t *testing.T) {
s := NewSpec(`
type B int
type A1 = B
var a331 struct {
x int "one"
Y string
c []A1 ` + "`B slice`" + `
}
var a332 struct {
x int "one"
Y string
c []B ` + "`B slice`" + `
}
`)
if !s.Identical("a331", "a332") {
t.Error(`test rule failed`)
}
specOfStruct1 := NewSpec(`
package PA
var a333 struct {
X int "one"
Y string
C []int ` + "`int slice`" + `
}
var a335 struct {
X int "one"
y string
C []int ` + "`int slice`" + `
}
`)
specOfStruct2 := NewSpec(`
package QA
var a334 struct {
X int "one"
Y string
C []int ` + "`int slice`" + `
}
var a336 struct {
X int "one"
y string
C []int ` + "`int slice`" + `
}
`)
// Identical(v, t types.Object) bool
if !Identical(specOfStruct1.MustGetValidTypeObject("a333"), specOfStruct2.MustGetValidTypeObject("a334")) {
t.Error(`test rule failed`)
}
if Identical(specOfStruct1.MustGetValidTypeObject("a335"), specOfStruct2.MustGetValidTypeObject("a336")) {
t.Error(`test rule failed`)
}
}
// 3.4 指针:如果 基本类型(base type) 相同,那么类型相同
// Two pointer types are identical if they have identical base types.
func TestIdentical06(t *testing.T) {
s := NewSpec(`
type B int
type A1 = B
var a341 *B
var a342 *A1
`)
if !s.Identical("a341", "a342") {
t.Error(`test rule failed`)
}
}
// 3.5 函数:如果两者具有 相同数量 的参数和返回值,相应的参数和返回值的 类型相同,并且要么两个函数都有可变参数,要么都没有。
// 参数名和结果名不需要匹配。
// Two function types are identical if they have the same number of parameters and result values, corresponding parameter and result types are identical, and either both functions are variadic or neither is. Parameter and result names are not required to match.
func TestIdentical07(t *testing.T) {
s := NewSpec(`
var a351 func(a, b int, z float64, opt ...interface{}) (success bool)
var a352 func(x int, y int, z float64, too ...interface{}) (ok bool)
`)
if !s.Identical("a351", "a352") {
t.Error(`test rule failed`)
}
}
// 3.6 接口:如果两者的方法集内的方法的 名称、类型 都相同,那么类型相同。
// 来自不同程序包的未导出方法名称始终是不同的。 方法的顺序无关紧要。
// Two interface types are identical if they have the same set of methods with the same names and identical function types. Non-exported method names from different packages are always different. The order of the methods is irrelevant.
func TestIdentical08(t *testing.T) {
s := NewSpec(`
type B int
type A1 = B
var a361 interface {
X() int
y(string)
c() []A1
}
var a362 interface {
c() []B
X() int
y(string)
}
`)
if !s.Identical("a361", "a362") {
t.Error(`test rule failed`)
}
specOfInterface1 := NewSpec(`
package PA
var a363 interface {
X() int
Y(string)
C() []int
}
var a365 interface {
X() int
Y(string)
c() []int
}
`)
specOfInterface2 := NewSpec(`
package QA
var a364 interface {
C() []int
X() int
Y(string)
}
var a366 interface {
X() int
c() []int
Y(string)
}
`)
// Identical(v, t types.Object) bool
if !Identical(specOfInterface1.MustGetValidTypeObject("a363"), specOfInterface2.MustGetValidTypeObject("a364")) {
t.Error(`test rule failed`)
}
// Identical(v, t types.Object) bool
if Identical(specOfInterface1.MustGetValidTypeObject("a365"), specOfInterface2.MustGetValidTypeObject("a366")) {
t.Error(`test rule failed`)
}
}
// 3.7 字典:如果两者的 键和值的类型都相同,那么类型相同
// Two map types are identical if they have identical key and element types.
func TestIdentical09(t *testing.T) {
s := NewSpec(`
type B int
type A1 = B
var a371 map[A1]string
var a372 map[B]string
`)
if !s.Identical("a371", "a372") {
t.Error(`test rule failed`)
}
}
// 3.8 管道:如果两者的 元素类型相同、方向相同,那么类型相同
// Two channel types are identical if they have identical element types and the same direction.
func TestIdentical10(t *testing.T) {
s := NewSpec(`
type B int
type A1 = B
var a381 chan<- A1
var a382 chan<- B
`)
if !s.Identical("a381", "a382") {
t.Error(`test rule failed`)
}
}
//3.9 basic type:诸如 int、string 这种简单类型,字面量相同,那么类型相同
func TestIdentical11(t *testing.T) {
s := NewSpec(`var a391 byte; var a392 byte`)
if !s.Identical("a391", "a392") {
t.Error(`test rule failed`)
}
}
// Identical(v, t types.Type) bool
// or
// Identical(v, t types.Object) bool
// or
// Identical((code, v, t string) bool
func TestIdentical12(t *testing.T) {
s := NewSpec(`type B int; type A1 = B`)
if !Identical(s.MustGetValidType("A1"), s.MustGetValidType("B")) {
t.Error(`test rule failed`)
}
if !Identical(s.MustGetValidTypeObject("A1"), s.MustGetValidTypeObject("B")) {
t.Error(`test rule failed`)
}
if !Identical(`type B int; type A1 = B`, "A1", "B") {
t.Error(`test rule failed`)
}
}
// func (s *Spec) IdenticalIgnoreTags(v, t string) bool
// IdenticalIgnoreTags(v, t types.Type) bool
// or
// IdenticalIgnoreTags(v, t types.Object) bool
// or
// IdenticalIgnoreTags((code, v, t string) bool
// 忽略标签,然后判断结构体类型是否相等
func TestIdentical13(t *testing.T) {
code := `
type U = struct {
Name string
Address *struct {
Street string
City string
}
}
type V = struct {
Name string ` + "`" + `json:"name"` + "`" + `
Address *struct {
Street string ` + "`" + `json:"street"` + "`" + `
City string ` + "`" + `json:"city"` + "`" + `
} ` + "`" + `json:"address"` + "`" + `
}
`
s := NewSpec(code)
if !s.IdenticalIgnoreTags("U", "V") {
t.Error(`test rule failed`)
}
if !IdenticalIgnoreTags(s.MustGetValidType("U"), s.MustGetValidType("V")) {
t.Error(`test rule failed`)
}
if !IdenticalIgnoreTags(s.MustGetValidTypeObject("U"), s.MustGetValidTypeObject("V")) {
t.Error(`test rule failed`)
}
if !IdenticalIgnoreTags(code, "U", "V") {
t.Error(`test rule failed`)
}
}