From 102391165ea850064b7e9adf57192441686c2fad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=9D=E6=A1=91?= <1554694323@qq.com> Date: Sun, 10 Jul 2022 12:15:25 +0800 Subject: [PATCH] test(errors): improve coverage (#2178) Co-authored-by: dengxiaochuan --- errors/errors_test.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/errors/errors_test.go b/errors/errors_test.go index f76d51511..5196d0b65 100644 --- a/errors/errors_test.go +++ b/errors/errors_test.go @@ -114,20 +114,37 @@ func TestCause(t *testing.T) { } func TestOther(t *testing.T) { + err := Errorf(10001, "test code 10001", "message") + // Code if !reflect.DeepEqual(Code(nil), 200) { t.Errorf("Code(nil) = %v, want %v", Code(nil), 200) } if !reflect.DeepEqual(Code(errors.New("test")), UnknownCode) { t.Errorf(`Code(errors.New("test")) = %v, want %v`, Code(nil), 200) } - if !reflect.DeepEqual(Reason(errors.New("test")), UnknownReason) { - t.Errorf(`Reason(errors.New("test")) = %v, want %v`, Reason(nil), UnknownReason) - } - err := Errorf(10001, "test code 10001", "message") if !reflect.DeepEqual(Code(err), 10001) { t.Errorf(`Code(err) = %v, want %v`, Code(err), 10001) } + // Reason + if !reflect.DeepEqual(Reason(nil), UnknownReason) { + t.Errorf(`Reason(nil) = %v, want %v`, Reason(nil), UnknownReason) + } + if !reflect.DeepEqual(Reason(errors.New("test")), UnknownReason) { + t.Errorf(`Reason(errors.New("test")) = %v, want %v`, Reason(nil), UnknownReason) + } if !reflect.DeepEqual(Reason(err), "test code 10001") { t.Errorf(`Reason(err) = %v, want %v`, Reason(err), "test code 10001") } + // Clone + err400 := Newf(http.StatusBadRequest, "BAD_REQUEST", "param invalid") + err400.Metadata = map[string]string{ + "key1": "val1", + "key2": "val2", + } + if cerr := Clone(err400); cerr == nil || cerr.Error() != err400.Error() { + t.Errorf("Clone(err) = %v, want %v", Clone(err400), err400) + } + if cerr := Clone(nil); cerr != nil { + t.Errorf("Clone(nil) = %v, want %v", Clone(err400), err400) + } }