-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfailure_test.go
58 lines (44 loc) · 1.68 KB
/
failure_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
package failure_test
import (
"errors"
"testing"
"github.com/morikuni/failure/v2"
)
func TestFailure_Error(t *testing.T) {
st1 := failure.NewFailure(nil, []failure.Field{failure.Callers(0), failure.Message("test1")})
st2 := failure.NewFailure(st1, []failure.Field{failure.Message("test2")}, []failure.Field{failure.WithCode("code"), failure.Context{"a": "b"}})
equal(t, st1.Error(), "v2_test.TestFailure_Error(test1)")
equal(t, st2.Error(), "[code](test2, {a=b}): v2_test.TestFailure_Error(test1)")
}
func TestFailure_Unwrap(t *testing.T) {
err := errors.New("test")
wrap := failure.NewFailure(err)
noWrap := failure.NewFailure(nil, []failure.Field{failure.Message("aaa")})
equal(t, errors.Unwrap(wrap), err)
equal(t, errors.Unwrap(noWrap), nil)
}
func TestFailure_As(t *testing.T) {
st := failure.NewFailure(nil, []failure.Field{failure.Message("aaa"), failure.Context{"a": "b"}})
aser := st.(interface{ As(interface{}) bool })
var msg failure.Message
equal(t, aser.As(&msg), true)
equal(t, msg, failure.Message("aaa"))
var ctx failure.Context
equal(t, aser.As(&ctx), true)
equal(t, ctx, failure.Context{"a": "b"})
var cs failure.CallStack
equal(t, aser.As(&cs), false)
equal(t, cs, failure.CallStack(nil))
var f failure.Field
equal(t, aser.As(&f), true)
equal(t, f, failure.Messagef("aaa"))
var err error
equal(t, aser.As(&err), false)
equal(t, err, nil)
}
func TestFailure_Value(t *testing.T) {
st := failure.NewFailure(nil, []failure.Field{failure.Message("aaa"), failure.Context{"a": "b"}})
equal(t, st.Value(failure.KeyMessage), failure.Messagef("aaa"))
equal(t, st.Value(failure.KeyContext), failure.Context{"a": "b"})
equal(t, st.Value(failure.KeyCode), nil)
}