test(go): 添加 errors.AsType、net 及手机号正则匹配测试

This commit is contained in:
张育新
2026-08-08 18:20:19 +08:00
parent a2dbd51f35
commit 722a5b13e0
3 changed files with 65 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
package error
import (
"errors"
"fmt"
"testing"
)
type MatchError struct {
code int
err error
}
func (e *MatchError) Error() string {
return fmt.Sprintf("code: %d, error: %v", e.code, e.err)
}
func GetMatchError() error {
return &MatchError{code: 1, err: errors.New("123")}
}
func GetError() error {
return errors.New("123")
}
func TestError(t *testing.T) {
if err, ok := errors.AsType[*MatchError](GetMatchError()); ok {
t.Log(err)
} else {
t.Errorf("expected MatchError, got %T", GetMatchError())
}
if err, ok := errors.AsType[*MatchError](GetError()); ok {
t.Log(err)
} else {
t.Errorf("expected MatchError, got %T", GetError())
}
}