-
Notifications
You must be signed in to change notification settings - Fork 191
/
goutil_test.go
91 lines (75 loc) · 1.81 KB
/
goutil_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
package goutil_test
import (
"errors"
"fmt"
"testing"
"github.com/gookit/goutil"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
)
var testSrvAddr string
func TestMain(m *testing.M) {
s := testutil.NewEchoServer()
defer s.Close()
testSrvAddr = s.HTTPHost()
fmt.Println("Test server listen on:", testSrvAddr)
m.Run()
}
func TestPkgName(t *testing.T) {
name := goutil.PkgName(goutil.FuncName(goutil.PanicIfErr))
assert.Eq(t, "github.com/gookit/goutil", name)
}
func TestPanicIfErr(t *testing.T) {
goutil.PanicIf(false, "")
assert.Panics(t, func() {
goutil.PanicIf(true, "a error msg")
})
goutil.PanicIfErr(nil)
assert.Panics(t, func() {
goutil.PanicIfErr(errors.New("a error"))
})
goutil.PanicErr(nil)
assert.Panics(t, func() {
goutil.PanicErr(errors.New("a error"))
})
goutil.MustOK(nil)
assert.Panics(t, func() {
goutil.MustOK(errors.New("a error"))
})
assert.Eq(t, "hi", goutil.Must("hi", nil))
assert.Panics(t, func() {
goutil.Must("hi", errors.New("a error"))
})
assert.NotPanics(t, func() {
goutil.MustIgnore(nil, nil)
})
assert.Panics(t, func() {
goutil.MustIgnore(nil, errors.New("a error"))
})
}
func TestPanicf(t *testing.T) {
assert.Panics(t, func() {
goutil.Panicf("hi %s", "inhere")
})
}
func TestErrOnFail(t *testing.T) {
err := errors.New("a error")
assert.Err(t, goutil.ErrOnFail(false, err))
assert.NoErr(t, goutil.ErrOnFail(true, err))
}
func TestOrValue(t *testing.T) {
assert.Eq(t, "ab", goutil.OrValue(true, "ab", "dc"))
assert.Eq(t, "dc", goutil.OrValue(false, "ab", "dc"))
}
func TestOrReturn(t *testing.T) {
assert.Eq(t, "ab", goutil.OrReturn(true, func() string {
return "ab"
}, func() string {
return "dc"
}))
assert.Eq(t, "dc", goutil.OrReturn(false, func() string {
return "ab"
}, func() string {
return "dc"
}))
}