-
Notifications
You must be signed in to change notification settings - Fork 4
/
errors_test.go
98 lines (76 loc) · 1.73 KB
/
errors_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
// SPDX-FileCopyrightText: 2014-2024 caixw
//
// SPDX-License-Identifier: MIT
package assert
import (
"errors"
"fmt"
"testing"
)
func TestAssertion_Error(t *testing.T) {
a := New(t, false)
err := errors.New("test")
a.Error(err, "a.Error(err) failed")
a.ErrorString(err, "test", "ErrorString(err) failed")
err2 := &errorImpl{msg: "msg"}
a.Error(err2, "ErrorString(errorImpl) failed")
a.ErrorString(err2, "msg", "ErrorString(errorImpl) failed")
var err3 error
a.NotError(err3, "var err1 error failed")
err4 := errors.New("err4")
err5 := fmt.Errorf("err5 with %w", err4)
a.ErrorIs(err5, err4)
}
func TestAssertion_Panic(t *testing.T) {
a := New(t, false)
f1 := func() {
panic("panic message")
}
a.Panic(f1)
a.PanicString(f1, "message")
a.PanicType(f1, "abc")
a.PanicValue(f1, "panic message")
f1 = func() {
panic(errors.New("panic"))
}
a.PanicType(f1, errors.New("abc"))
f1 = func() {
panic(&errorImpl{msg: "panic"})
}
a.PanicType(f1, &errorImpl{msg: "abc"})
f1 = func() {}
a.NotPanic(f1)
}
func TestHasPanic(t *testing.T) {
f1 := func() {
panic("panic")
}
if has, _ := hasPanic(f1); !has {
t.Error("f1未发生panic")
}
f2 := func() {
f1()
}
if has, msg := hasPanic(f2); !has {
t.Error("f2未发生panic")
} else if msg != "panic" {
t.Errorf("f2发生了panic,但返回信息不正确,应为[panic],但其实返回了%v", msg)
}
f3 := func() {
defer func() {
if msg := recover(); msg != nil {
t.Logf("TestHasPanic.f3 recover msg:[%v]", msg)
}
}()
f1()
}
if has, msg := hasPanic(f3); has {
t.Errorf("f3发生了panic,其信息为:[%v]", msg)
}
f4 := func() {
//todo
}
if has, msg := hasPanic(f4); has {
t.Errorf("f4发生panic,其信息为[%v]", msg)
}
}