From 722a5b13e049923fb039b21227de17c44080b4d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=82=B2=E6=96=B0?= Date: Sat, 8 Aug 2026 18:20:19 +0800 Subject: [PATCH] =?UTF-8?q?test(go):=20=E6=B7=BB=E5=8A=A0=20errors.AsType?= =?UTF-8?q?=E3=80=81net=20=E5=8F=8A=E6=89=8B=E6=9C=BA=E5=8F=B7=E6=AD=A3?= =?UTF-8?q?=E5=88=99=E5=8C=B9=E9=85=8D=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/testing/error/error_test.go | 34 ++++++++++++++++++++++++++ go/testing/net/net_test.go | 18 ++++++++++++++ go/testing/regularMatch/regexp_test.go | 13 ++++++++++ 3 files changed, 65 insertions(+) create mode 100644 go/testing/error/error_test.go create mode 100644 go/testing/net/net_test.go diff --git a/go/testing/error/error_test.go b/go/testing/error/error_test.go new file mode 100644 index 0000000..00f1c89 --- /dev/null +++ b/go/testing/error/error_test.go @@ -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()) + } +} diff --git a/go/testing/net/net_test.go b/go/testing/net/net_test.go new file mode 100644 index 0000000..6877f21 --- /dev/null +++ b/go/testing/net/net_test.go @@ -0,0 +1,18 @@ +package net + +import ( + "net" + "testing" +) + +func TestNet(t *testing.T) { + s := net.JoinHostPort("127.0.0.1", "8080") + + t.Log(s) + t.Log(s) + addrs, err := net.LookupAddr("198.18.0.18") + if err != nil { + t.Fatal(err) + } + t.Log(addrs) +} diff --git a/go/testing/regularMatch/regexp_test.go b/go/testing/regularMatch/regexp_test.go index 444bbea..49c54fb 100644 --- a/go/testing/regularMatch/regexp_test.go +++ b/go/testing/regularMatch/regexp_test.go @@ -19,3 +19,16 @@ func TestChineseRegExp(t *testing.T) { t.Errorf("Expected [世, 界], got %v", matchStrSlice) } } + +func TestChinaMobile(t *testing.T) { + reg, err := regexp.Compile(`^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$`) + if err != nil { + t.Errorf("Failed to compile regex: %v", err) + } + // matchStrSlice := reg.FindAllString("123-12345678 1234-12345678", -1) + // if len(matchStrSlice) != 2 { + // t.Errorf("Expected 2 matches, got %d", len(matchStrSlice)) + // } + matchStrSlice := reg.FindAllString("17623862704", -1) + t.Log(matchStrSlice[0]) +}