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 d7658fd commit d4a49ac
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 15 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
path-to-profile: /tmp/coverage/combined.txt
flag-name: Go-${{ matrix.go }}
parallel: true
if: ${{ github.event.repository.fork == false }} # 仅在非 fork 时上传覆盖率

check-coverage:
name: Check coverage
Expand All @@ -51,14 +52,15 @@ jobs:
- uses: shogo82148/actions-goveralls@v1
with:
parallel-finished: true
if: ${{ github.event.repository.fork == false }} # 仅在非 fork 时检查覆盖率

# 发布 Release
release:
name: Release a new version
needs: [ lint, test ]
runs-on: ubuntu-latest
# 仅在推送标签时执行
if: ${{ success() && startsWith(github.ref, 'refs/tags/v') }}
# 仅在推送标签时执行 - && - 仅在非 fork 时执行发布
if: ${{ github.event.repository.fork == false && success() && startsWith(github.ref, 'refs/tags/v') }}
steps:
# 1. 检出代码
- name: Checkout code
Expand Down
4 changes: 2 additions & 2 deletions check_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ func None[V comparable](v V) {
}
}

func Null[V *any](v any) {
func Null[T any](v *T) {
if v != nil {
panic(errors.New("SHOULD BE NULL BUT IS FULL"))
}
}

func Full[V *any](v any) {
func Full[T any](v *T) {
if v == nil {
panic(errors.New("SHOULD BE FULL BUT IS NULL"))
}
Expand Down
21 changes: 20 additions & 1 deletion check_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/yyle88/done/internal/tests"
)

func TestGood(t *testing.T) {
Expand All @@ -27,6 +28,24 @@ type Example struct {
S string
}

func newExample2x() (*Example, error) {
func newExample() (*Example, error) {
return &Example{S: "xyz"}, nil
}

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

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

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

tests.ExpectPanic(t, func() {
var example *Example
Full(example)
})
}
8 changes: 4 additions & 4 deletions check_vce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import (
)

func TestVE(t *testing.T) {
ve := VCE(newExample2x())
ve := VCE(newExample())
t.Log(ve.V)
}

func TestVe_Done(t *testing.T) {
example := VCE(newExample2x()).Done()
example := VCE(newExample()).Done()
t.Log(example.S)
}

func TestVe_Good(t *testing.T) {
VCE(newExample2x()).Good()
VCE(newExample()).Good()
}

func TestVe_Nice(t *testing.T) {
example := VCE(newExample2x()).Nice()
example := VCE(newExample()).Nice()
t.Log(example.S)
}

Expand Down
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
19 changes: 19 additions & 0 deletions internal/tests/tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tests

import (
"testing"

"github.com/stretchr/testify/require"
)

func ExpectPanic(t *testing.T, run func()) {
defer func() {
if recoverFrom := recover(); recoverFrom != nil {
t.Logf("expect panic then catch panic [%v] -> [success]", recoverFrom)
return
}
require.Fail(t, "expect panic while not panic -> [failure]")
}()

run()
}
14 changes: 14 additions & 0 deletions internal/tests/tests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tests_test

import (
"testing"

"github.com/pkg/errors"
"github.com/yyle88/must/internal/tests"

Check failure on line 7 in internal/tests/tests_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

no required module provides package github.com/yyle88/must/internal/tests; to add it:

Check failure on line 7 in internal/tests/tests_test.go

View workflow job for this annotation

GitHub Actions / test (1.23.x)

no required module provides package github.com/yyle88/must/internal/tests; to add it:

Check failure on line 7 in internal/tests/tests_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

no required module provides package github.com/yyle88/must/internal/tests; to add it:

Check failure on line 7 in internal/tests/tests_test.go

View workflow job for this annotation

GitHub Actions / test (1.23.x)

no required module provides package github.com/yyle88/must/internal/tests; to add it:
)

func TestExpectPanic(t *testing.T) {
tests.ExpectPanic(t, func() {
panic(errors.New("expect-panic"))
})
}

0 comments on commit d4a49ac

Please sign in to comment.