-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathfunc_test.go
More file actions
166 lines (147 loc) · 3.79 KB
/
func_test.go
File metadata and controls
166 lines (147 loc) · 3.79 KB
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
package goutil_test
import (
"errors"
"runtime"
"strings"
"testing"
"github.com/gookit/goutil"
"github.com/gookit/goutil/x/assert"
)
func TestFuncName(t *testing.T) {
name := goutil.FuncName(goutil.PkgName)
assert.Eq(t, "github.com/gookit/goutil.PkgName", name)
name = goutil.FuncName(goutil.PanicIfErr)
assert.Eq(t, "github.com/gookit/goutil.PanicIfErr", name)
err := goutil.Go(func() error {
return nil
})
assert.NoErr(t, err)
}
func TestCallOn(t *testing.T) {
assert.NoErr(t, goutil.CallOn(false, func() error {
return errors.New("a error")
}))
assert.Err(t, goutil.CallOn(true, func() error {
return errors.New("a error")
}))
err := goutil.CallOrElse(true, func() error {
return errors.New("a error 001")
}, func() error {
return errors.New("a error 002")
})
assert.ErrMsg(t, err, "a error 001")
err = goutil.IfElseFn(false, func() error {
return errors.New("a error 001")
}, func() error {
return errors.New("a error 002")
})
assert.ErrMsg(t, err, "a error 002")
}
func TestSafeRun(t *testing.T) {
t.Run("NoPanic_ReturnsNil", func(t *testing.T) {
err := goutil.SafeRun(func() {})
assert.Nil(t, err)
})
t.Run("PanicWithError_ReturnsError", func(t *testing.T) {
expectedErr := errors.New("test error")
err := goutil.SafeRun(func() {
panic(expectedErr)
})
assert.Equal(t, expectedErr, err)
})
t.Run("PanicWithNonError_ReturnsFormattedError", func(t *testing.T) {
expectedMsg := "test message"
err := goutil.SafeRun(func() {
panic(expectedMsg)
})
assert.ErrMsg(t, err, expectedMsg)
})
}
func TestSafeRunWithError(t *testing.T) {
t.Run("NormalExecution_ReturnsNil", func(t *testing.T) {
err := goutil.SafeRunWithError(func() error {
return nil
})
assert.NoErr(t, err)
})
t.Run("NormalExecution_ReturnsError", func(t *testing.T) {
expectedErr := errors.New("test error")
err := goutil.SafeRunWithError(func() error {
return expectedErr
})
assert.Err(t, expectedErr, err)
})
t.Run("PanicWithErrorMessage_ReturnsError", func(t *testing.T) {
expectedErr := errors.New("panic error")
err := goutil.SafeRunWithError(func() error {
panic(expectedErr)
})
assert.Err(t, expectedErr, err)
})
t.Run("PanicWithNonErrorValue_ReturnsError", func(t *testing.T) {
expectedMsg := "non-error panic"
err := goutil.SafeRunWithError(func() error {
panic(expectedMsg)
})
assert.ErrMsg(t, err, expectedMsg)
})
}
func TestSafeGo(t *testing.T) {
t.Run("Normal execution", func(t *testing.T) {
var called bool
goutil.SafeGo(func() {
called = true
}, func(err error) {
t.Fatal("Expected no error")
})
waitForGoroutine()
if !called {
t.Fail()
}
})
t.Run("Panic captured", func(t *testing.T) {
expected := "panic occurred"
goutil.SafeGo(func() {
panic(expected)
}, func(err error) {
if err == nil || !strings.Contains(err.Error(), expected) {
t.Fatalf("Expected error containing %q, got %v", expected, err)
}
})
waitForGoroutine()
})
}
func TestSafeGoWithError(t *testing.T) {
t.Run("Function returns error", func(t *testing.T) {
expected := errors.New("test error")
goutil.SafeGoWithError(func() error {
return expected
}, func(err error) {
if err != expected {
t.Fail()
}
})
waitForGoroutine()
})
t.Run("Panic captured in SafeGoWithError", func(t *testing.T) {
expected := "panic inside SafeGoWithError"
goutil.SafeGoWithError(func() error {
panic(expected)
}, func(err error) {
if err == nil || !strings.Contains(err.Error(), expected) {
t.Fatalf("Expected error containing %q, got %v", expected, err)
}
})
waitForGoroutine()
})
}
// 等待 goroutine 执行完成
func waitForGoroutine() {
// 简单等待 goroutine 完成(适用于简单测试)
for i := 0; i < 10; i++ {
if active := runtime.NumGoroutine(); active <= 1 {
break
}
runtime.Gosched()
}
}