forked from silenceper/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
55 lines (48 loc) · 1.44 KB
/
error_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
package util
import "testing"
var okErrData string = `{"errcode": 0}`
var errData string = `{"errcode": 43101, "errmsg": "user refuse to accept the msg"}`
var expectError string = "Send Error , errcode=43101 , errmsg=user refuse to accept the msg"
func TestDecodeWithCommonErrorNoError(t *testing.T) {
err := DecodeWithCommonError([]byte(okErrData), "Send")
if err != nil {
t.Error("DecodeWithCommonError should not return error")
return
}
}
func TestDecodeWithCommonError(t *testing.T) {
err := DecodeWithCommonError([]byte(errData), "Send")
if err == nil {
t.Error("DecodeWithCommonError should return error")
return
}
cErr, ok := err.(*CommonError)
if !ok {
t.Errorf("DecodeWithCommonError should return *CommonError but %T", err)
return
}
if !(cErr.ErrCode == 43101 && cErr.ErrMsg == "user refuse to accept the msg" && cErr.Error() == expectError) {
t.Error("DecodeWithCommonError return bad *CommonError")
return
}
}
func TestDecodeWithError(t *testing.T) {
type DE struct {
CommonError
}
var obj DE
err := DecodeWithError([]byte(errData), &obj, "Send")
if err == nil {
t.Error("DecodeWithError should return error")
return
}
cErr, ok := err.(*CommonError)
if !ok {
t.Errorf("DecodeWithError should return *CommonError but %T", err)
return
}
if !(cErr.ErrCode == 43101 && cErr.ErrMsg == "user refuse to accept the msg" && cErr.Error() == expectError) {
t.Error("DecodeWithError return bad *CommonError")
return
}
}