Skip to content

Commit

Permalink
增加字符串断言函数
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Feb 21, 2025
1 parent 6365aa9 commit d0dab04
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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.19
github.com/yyle88/zaplog v0.0.20
go.uber.org/zap v1.27.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yyle88/mutexmap v1.0.10 h1:PIsx9KTdK6h1yH5NzBrPl21RrZ/XsE4IbgulMlhJzZE=
github.com/yyle88/mutexmap v1.0.10/go.mod h1:QUYDuARLPlGj414kHewQ5tt8jkDxQXoai8H3C4Gg+yc=
github.com/yyle88/zaplog v0.0.19 h1:O4g7EO+uks7DkXbBXAje0LRvrBbkPuMD4gphuXw9jAo=
github.com/yyle88/zaplog v0.0.19/go.mod h1:jN9/2IXYlpHOgoIyOTid1EPlUyGBWzFjr0dX9YKLkC0=
github.com/yyle88/zaplog v0.0.20 h1:ByVj51j2C9p5tWw8wbIdmdrMK5IS6VECh/U746t321E=
github.com/yyle88/zaplog v0.0.20/go.mod h1:jN9/2IXYlpHOgoIyOTid1EPlUyGBWzFjr0dX9YKLkC0=
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
8 changes: 0 additions & 8 deletions must.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ func Full[T any](v *T) {
}
}

// Equal expects the values to be equal. Panics if they are not equal.
// Equal 期望值相等。如果值不相等,则触发 panic。
func Equal[V comparable](a, b V) {
if a != b {
zaplog.ZAPS.Skip1.LOG.Panic("A AND B ARE NOT EQUAL", zap.Any("a", a), zap.Any("b", b))
}
}

// Equals expects the values to be equal. Panics if they are not equal.
// Equals 期望值相等。如果值不相等,则触发 panic。
func Equals[V comparable](a, b V) {
Expand Down
24 changes: 24 additions & 0 deletions muststrings/must_strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package muststrings

import (
"strings"

"github.com/yyle88/zaplog"
"go.uber.org/zap"
)

// HasPrefix checks if the string has the specified prefix, panics if not.
// HasPrefix 检查字符串是否有指定的前缀,没有则触发 panic。
func HasPrefix(a string, prefix string) {
if !strings.HasPrefix(a, prefix) {
zaplog.ZAPS.Skip1.LOG.Panic("expect string to have prefix while it does not", zap.String("string", a), zap.String("prefix", prefix))
}
}

// HasSuffix checks if the string has the specified suffix, panics if not.
// HasSuffix 检查字符串是否有指定的后缀,没有则触发 panic。
func HasSuffix(a string, suffix string) {
if !strings.HasSuffix(a, suffix) {
zaplog.ZAPS.Skip1.LOG.Panic("expect string to have suffix while it does not", zap.String("string", a), zap.String("suffix", suffix))
}
}
24 changes: 24 additions & 0 deletions muststrings/must_strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package muststrings_test

import (
"testing"

"github.com/yyle88/must/internal/tests"
"github.com/yyle88/must/muststrings"
)

func TestHasPrefix(t *testing.T) {
muststrings.HasPrefix("hello", "he")

tests.ExpectPanic(t, func() {
muststrings.HasPrefix("hello", "hi")
})
}

func TestHasSuffix(t *testing.T) {
muststrings.HasSuffix("world", "ld")

tests.ExpectPanic(t, func() {
muststrings.HasSuffix("world", "lo")
})
}

0 comments on commit d0dab04

Please sign in to comment.