-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors_api_test.go
60 lines (55 loc) · 1.4 KB
/
errors_api_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
package errors
import (
"reflect"
"testing"
)
func TestIs(t *testing.T) {
err := InvalidParameter.New()
if !InvalidParameter.Is(err) {
t.Error("Isの値が一致しません")
}
if !InvalidParameter.Is(Wrap(err)) {
t.Error("Isの値が一致しません")
}
}
func TestAs(t *testing.T) {
var err error
err0 := Wrap(nil)
err1 := InvalidParameter.New("test")
err2 := InvalidParameter.New("test").Crit()
err3 := Wrap(InvalidParameter.New("test"), "wrap")
err4 := Wrap(Wrap(InvalidParameter.New("test"), "wrap"), "wrap2")
err5 := InvalidParameter.New("test").AddData("user_id", 123)
errConv := func(e AppError) error {
return e
}
errConvApp := func(e AppError) *appError {
if e == nil {
return nil
}
return e.(*appError)
}
tests := []struct {
name string
err error
want *appError
}{
{"nil1 ", err, nil},
{"nil2 ", errConv(nil), nil},
{"nil2 ", errConvApp(nil), nil},
{"nil3 ", errConv(errConvApp(nil)), nil},
{"w nil", errConv(err0), nil},
{"new ", errConv(err1), errConvApp(err1)},
{"level", errConv(err2), errConvApp(err2)},
{"wrap ", errConv(err3), errConvApp(err3)},
{"wraps", errConv(err4), errConvApp(err4)},
{"data ", errConv(err5), errConvApp(err5)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := AsAppError(tt.err); !reflect.DeepEqual(got, tt.want) {
t.Errorf("\nerror: %+v\nwant : %+v", got, tt.want)
}
})
}
}