Skip to content

Commit

Permalink
修改断言指针非空和空的函数
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Dec 25, 2024
1 parent 58ad6c8 commit 1fbcbd8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ go 1.22.6
require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.10.0
github.com/yyle88/zaplog v0.0.16
github.com/yyle88/zaplog v0.0.17
go.uber.org/zap v1.27.0
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/yyle88/mutexmap v1.0.8 // indirect
github.com/yyle88/mutexmap v1.0.9 // indirect
go.uber.org/multierr v1.11.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yyle88/mutexmap v1.0.8 h1:VntAdXID5wbk211LZEPVK96jQBxIcfVIbQuk9cv3P/8=
github.com/yyle88/mutexmap v1.0.8/go.mod h1:QUYDuARLPlGj414kHewQ5tt8jkDxQXoai8H3C4Gg+yc=
github.com/yyle88/zaplog v0.0.16 h1:ZCxQhq3+nWeWMAXIzeA1EA4exRq5Pn8pXTpEw1GjyD4=
github.com/yyle88/zaplog v0.0.16/go.mod h1:0ct8Rh6uE5i9RG+xbH6d4/pyDBt9JmxBqHNCI+T4wiM=
github.com/yyle88/mutexmap v1.0.9 h1:g1nfNmsReU/AgcGkLgPEKrBeMThJJBrJuvU7i3DOVC0=
github.com/yyle88/mutexmap v1.0.9/go.mod h1:QUYDuARLPlGj414kHewQ5tt8jkDxQXoai8H3C4Gg+yc=
github.com/yyle88/zaplog v0.0.17 h1:2exjnXROfkNRvcKZDAFUM2KDpca1n2OKrEH73pTvrX0=
github.com/yyle88/zaplog v0.0.17/go.mod h1:SJvxdHW7zWu1r+K66oBPBWljiGnDjCV8le3SzQnJpBw=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand Down
2 changes: 1 addition & 1 deletion internal/tests/tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import (

func TestExpectPanic(t *testing.T) {
tests.ExpectPanic(t, func() {
panic(errors.New("exp"))
panic(errors.New("expect-panic"))
})
}
4 changes: 2 additions & 2 deletions must.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ func None[V comparable](a V) {

// Null expects the value to be nil. Panics if the value is non-nil.
// Null 期望值为 nil。如果值不为 nil,则触发 panic。
func Null[V *any](v any) {
func Null[T any](v *T) {
if v != nil {
zaplog.ZAPS.P1.LOG.Panic("SHOULD BE NULL BUT IS FULL")
}
}

// Full expects the value to be non-nil. Panics if the value is nil.
// Full 期望值为非 nil。如果值为 nil,则触发 panic。
func Full[V *any](v any) {
func Full[T any](v *T) {
if v == nil {
zaplog.ZAPS.P1.LOG.Panic("SHOULD BE FULL BUT IS NULL")
}
Expand Down
22 changes: 22 additions & 0 deletions must_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,25 @@ func TestContains(t *testing.T) {
must.Contains([]int{1, 2, 3}, 4)
})
}

type Example struct {
S string
}

func TestNull(t *testing.T) {
var example *Example
must.Null(example)

tests.ExpectPanic(t, func() {
must.Null(&Example{S: "abc"})
})
}

func TestFull(t *testing.T) {
must.Full(&Example{S: "abc"})

tests.ExpectPanic(t, func() {
var example *Example
must.Full(example)
})
}

0 comments on commit 1fbcbd8

Please sign in to comment.