-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
executable file
·194 lines (156 loc) · 3.88 KB
/
utils_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
package goerrors
import (
"reflect"
"testing"
)
type X struct {
int
j float32
}
type A struct {
}
type B struct {
A
X
}
type C struct {
B
}
type D struct {
A
B
X
}
type E struct {
D
C
}
const (
n1 = "github.com/corebreaker/goerrors.A"
n2 = "github.com/corebreaker/goerrors.B"
n3 = "github.com/corebreaker/goerrors.C"
n4 = "github.com/corebreaker/goerrors.D"
n5 = "github.com/corebreaker/goerrors.E"
)
var (
tA = reflect.TypeOf(A{})
tB = reflect.TypeOf(B{})
tC = reflect.TypeOf(C{})
tD = reflect.TypeOf(D{})
tE = reflect.TypeOf(E{})
)
func TestConcat(t *testing.T) {
if _concat(nil, nil) != nil {
t.Error("Concatenation of 2 nil lists should be nil")
}
lst := _concat([]string{}, nil)
if (lst == nil) || (len(lst) > 0) {
t.Error("Concatenation of 2 empty list should be should: ", lst)
}
lst0 := make([]string, 0)
lst1 := []string{"A", "B"}
lst = _concat(lst0, nil)
if (lst == nil) || (len(lst) > 0) {
t.Error("Concat with RHS nil list should not return nil and non empty list (LHS list)")
}
if len(_concat(nil, lst0)) != 0 {
t.Error("Concat with RHS empty list should have length as zero")
}
if len(_concat(lst0, lst0)) != 0 {
t.Error("Concat same empty list should have length as zero")
}
if len(_concat(lst0, make([]string, 0))) != 0 {
t.Error("Concat two empty lists should have length as zero")
}
if len(_concat(lst1, lst1)) != (2 * len(lst1)) {
t.Error("Concat same list should have length as twice the length of the list")
}
if len(_concat(lst1, []string{"X"})) != 3 {
t.Error("Concat two list should have the good length")
}
}
func TestStructHierarchyZeroLevel(t *testing.T) {
res := _getTypeHierarchy(tA, tA)
if res == nil {
t.Error("With zero level, it returns nil")
}
if len(res) != 1 {
t.Fatal("With zero level, it should return a list with length 1; the return is:", res)
}
if res[0] != n1 {
t.Errorf("With zero level, it should return a list one type %v; the return is: %v", n1, res)
}
}
func TestStructHierarchyOneLevel(t *testing.T) {
res := _getTypeHierarchy(tB, tA)
if res == nil {
t.Error("With one level, it returns nil")
}
if len(res) != 2 {
t.Fatal("With one level, it should return a list with length 2; the return is:", res)
}
if (res[0] != n2) || (res[1] != n1) {
t.Errorf("With one levels, it should return a list with types %v and %v; the return is: %v", n2, n1, res)
}
}
func TestStructHierarchyTwoLevels(t *testing.T) {
res := _getTypeHierarchy(tC, tA)
if res == nil {
t.Error("With two levels, it returns nil")
}
if len(res) != 3 {
t.Fatal("With two levels, it should return a list with length 3; the return is:", res)
}
if (res[0] != n3) || (res[1] != n2) || (res[2] != n1) {
t.Errorf("With two levels, it should return a list with types %v, %v and %v; the return is: %v",
n3,
n2,
n1,
res)
}
}
func TestStructHierarchyThreeLevels(t *testing.T) {
res := _getTypeHierarchy(tD, tA)
if res == nil {
t.Error("With three levels, it returns nil")
}
if len(res) != 4 {
t.Fatal("With three levels, it should return a list with length 4; the return is:", res)
}
if (res[0] != n4) || (res[1] != n1) || (res[2] != n2) || (res[3] != n1) {
t.Errorf("With two levels, it should return a list with types %v, %v, %v and %v; the return is: %v",
n4,
n1,
n2,
n1,
res)
}
}
func TestStructHierarchyFourLevels(t *testing.T) {
res := _getTypeHierarchy(tE, tA)
if res == nil {
t.Error("With four levels, it returns nil")
}
if len(res) != 8 {
t.Fatal("With four levels, it should return a list with length 8; the return is:", res)
}
if (res[0] != n5) ||
(res[1] != n4) ||
(res[2] != n1) ||
(res[3] != n2) ||
(res[4] != n1) ||
(res[5] != n3) ||
(res[6] != n2) ||
(res[7] != n1) {
t.Errorf("With two levels, it should return a list with types %v, %v, %v, %v, %v, %v, %v and %v; the return is: %v",
n5,
n4,
n1,
n2,
n1,
n3,
n2,
n1,
res)
}
}