-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathdebug_test.go
277 lines (222 loc) · 5.54 KB
/
debug_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
package kgo
import (
"errors"
"github.com/stretchr/testify/assert"
"testing"
)
func TestDebug_DumpPrint(t *testing.T) {
defer func() {
r := recover()
assert.Empty(t, r)
}()
KDbug.DumpPrint(Version)
}
func TestDebug_DumpStacks(t *testing.T) {
defer func() {
r := recover()
assert.Empty(t, r)
}()
KDbug.DumpStacks()
}
func TestDebug_GetCallName(t *testing.T) {
defer func() {
r := recover()
assert.NotEmpty(t, r)
}()
var res string
res = KDbug.GetCallName(nil, false)
assert.Contains(t, res, "TestDebug_GetCallName")
res = KDbug.GetCallName(nil, true)
assert.Equal(t, "TestDebug_GetCallName", res)
res = KDbug.GetCallName("", false)
assert.Empty(t, res)
res = KDbug.GetCallName(KArr.ArrayRand, false)
assert.Contains(t, res, "ArrayRand")
res = KDbug.GetCallName(KArr.ArrayRand, true)
assert.Equal(t, "ArrayRand-fm", res)
//未实现的方法
KDbug.GetCallName(itfObj.noRealize, false)
}
func BenchmarkDebug_GetCallName(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
KDbug.GetCallName(KArr.ArrayRand, false)
}
}
func TestDebug_GetCallFile(t *testing.T) {
res := KDbug.GetCallFile()
assert.NotEmpty(t, res)
}
func BenchmarkDebug_GetCallFile(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
KDbug.GetCallFile()
}
}
func TestDebug_GetCallDir(t *testing.T) {
res := KDbug.GetCallDir()
assert.NotEmpty(t, res)
}
func BenchmarkDebug_GetCallDir(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
KDbug.GetCallDir()
}
}
func TestDebug_GetCallLine(t *testing.T) {
res := KDbug.GetCallLine()
assert.Greater(t, res, 1)
}
func BenchmarkDebug_GetCallLine(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
KDbug.GetCallLine()
}
}
func TestDebug_GetCallPackage(t *testing.T) {
var res string
res = KDbug.GetCallPackage()
assert.Equal(t, "kgo", res)
res = KDbug.GetCallPackage(KDbug.GetCallFile())
assert.Equal(t, "kgo", res)
res = KDbug.GetCallPackage(strHello)
assert.Empty(t, res)
}
func BenchmarkDebug_GetCallPackage(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
KDbug.GetCallPackage()
}
}
func TestDebug_HasMethod(t *testing.T) {
var tests = []struct {
input interface{}
method string
expected bool
}{
{intSpeedLight, "", false},
{strHello, "toString", false},
{KArr, "InIntSlice", true},
{&KArr, "InInt64Slice", true},
{&KArr, strHello, false},
}
for _, test := range tests {
actual := KDbug.HasMethod(test.input, test.method)
assert.Equal(t, actual, test.expected)
}
}
func BenchmarkDebug_HasMethod(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
KDbug.HasMethod(KArr, "SliceFill")
}
}
func TestDebug_GetMethod(t *testing.T) {
var res interface{}
res = KDbug.GetMethod(intSpeedLight, "")
assert.Nil(t, res)
res = KDbug.GetMethod(&KArr, "InIntSlice")
assert.NotNil(t, res)
res = KDbug.GetMethod(KArr, "InIntSlice")
assert.NotNil(t, res)
res = KDbug.GetMethod(KArr, strHello)
assert.Nil(t, res)
}
func BenchmarkDebug_GetMethod(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
KDbug.GetMethod(&KArr, "InIntSlice")
}
}
func TestDebug_CallMethod(t *testing.T) {
var res interface{}
var err error
//调用不存在的方法
res, err = KDbug.CallMethod(KArr, strHello)
assert.NotNil(t, err)
//有参数调用
res, err = KDbug.CallMethod(&KConv, "BaseConvert", "123456", 10, 16)
assert.Nil(t, err)
assert.NotEmpty(t, res)
//无参数调用
res, err = KDbug.CallMethod(&KOS, "Getcwd")
assert.Nil(t, err)
assert.NotEmpty(t, res)
}
func BenchmarkDebug_CallMethod(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = KDbug.CallMethod(&KOS, "Getcwd")
}
}
func TestDebug_GetFuncNames(t *testing.T) {
var res []string
//空字符串
res = KDbug.GetFuncNames("")
assert.Empty(t, res)
//空变量
res = KDbug.GetFuncNames(nil)
assert.Empty(t, res)
//非指针变量
res = KDbug.GetFuncNames(KStr)
assert.NotEmpty(t, res)
//指针变量
res = KDbug.GetFuncNames(&KConv)
assert.NotEmpty(t, res)
n1 := KDbug.GetFuncNames(KFile)
n2 := KDbug.GetFuncNames(KStr)
n3 := KDbug.GetFuncNames(KNum)
n4 := KDbug.GetFuncNames(KArr)
n5 := KDbug.GetFuncNames(KTime)
n6 := KDbug.GetFuncNames(KConv)
n7 := KDbug.GetFuncNames(KOS)
n8 := KDbug.GetFuncNames(KEncr)
n9 := KDbug.GetFuncNames(KDbug)
funTotal := len(n1) + len(n2) + len(n3) + len(n4) + len(n5) + len(n6) + len(n7) + len(n8) + len(n9)
dumpPrint("the package function total:", funTotal)
}
func BenchmarkDebug_GetFuncNames(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
KDbug.GetFuncNames(&KConv)
}
}
func TestDebug_WrapError(t *testing.T) {
var err = errors.New("a test error")
err1 := KDbug.WrapError(nil)
assert.NotNil(t, err1)
assert.Contains(t, err1.Error(), "parameter error")
err2 := KDbug.WrapError(err)
assert.NotNil(t, err2)
assert.Equal(t, err2, err)
err3 := KDbug.WrapError(err, helloEng, utf8Hello)
assert.NotNil(t, err3)
assert.Contains(t, err3.Error(), helloEng)
assert.Contains(t, err3.Error(), err.Error())
}
func BenchmarkDebug_WrapError(b *testing.B) {
var err = errors.New("a test error")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = KDbug.WrapError(err, helloEng, utf8Hello)
}
}
func TestDebug_Stacks(t *testing.T) {
var res1, res2, res3 []byte
var len1, len2, len3 int
res1 = KDbug.Stacks(-1)
len1 = len(res1)
assert.Greater(t, len1, 1)
res2 = KDbug.Stacks(0)
len2 = len(res2)
assert.Greater(t, len1, len2)
res3 = KDbug.Stacks(1)
len3 = len(res3)
assert.Greater(t, len2, len3)
}
func BenchmarkDebug_Stacks(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = KDbug.Stacks(0)
}
}