1 Star 1 Fork 0

水不要鱼/errors

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
wrap_test.go 2.17 KB
一键复制 编辑 原始数据 按行查看 历史
水不要鱼 提交于 2024-08-09 02:07 . 调整
// Copyright 2024 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package errors
import (
"io"
"testing"
)
// go test -v -cover -count=1 -test.cpu=1 -run=^TestWrap$
func TestWrap(t *testing.T) {
testCases := []struct {
code int32
message string
}{
{code: -1000, message: "io timeout"},
{code: 1000, message: "need login"},
}
for _, testCase := range testCases {
err := Wrap(testCase.code, testCase.message)
if err.Code() != testCase.code {
t.Errorf("err.Code() %d != testCase.code %d", err.Code(), testCase.code)
}
if err.Message() != testCase.message {
t.Errorf("err.Message() %s != testCase.code %s", err.Message(), testCase.message)
}
if err.Unwrap() != nil {
t.Errorf("err.Unwrap() %+v != nil", err.Unwrap())
}
}
}
// go test -v -cover -count=1 -test.cpu=1 -run=^TestWrapWith$
func TestWrapWith(t *testing.T) {
testCases := []struct {
code int32
message string
cause error
}{
{code: -1000, message: "eof", cause: io.EOF},
{code: 1000, message: "need login"},
}
for _, testCase := range testCases {
err := Wrap(testCase.code, testCase.message).With(testCase.cause)
if err.Code() != testCase.code {
t.Errorf("err.Code() %d != testCase.code %d", err.Code(), testCase.code)
}
if err.Message() != testCase.message {
t.Errorf("err.Message() %s != testCase.code %s", err.Message(), testCase.message)
}
if err.Unwrap() != testCase.cause {
t.Errorf("err.Unwrap() %+v != testCase.code %+v", err.Unwrap(), testCase.cause)
}
}
}
// go test -v -cover -count=1 -test.cpu=1 -run=^TestErrorError$
func TestErrorError(t *testing.T) {
testCases := []struct {
code int32
message string
cause error
errorString string
}{
{code: -1000, message: "eof", cause: io.EOF, errorString: "-1000: eof (EOF)"},
{code: 1000, message: "need login", errorString: "1000: need login"},
}
for _, testCase := range testCases {
err := Wrap(testCase.code, testCase.message).With(testCase.cause)
if err.Error() != testCase.errorString {
t.Errorf("err.Error() %s != testCase.errorString %s", err.Error(), testCase.errorString)
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/FishGoddess/errors.git
git@gitee.com:FishGoddess/errors.git
FishGoddess
errors
errors
v0.6.0-alpha

搜索帮助