-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yangyile
committed
Feb 21, 2025
1 parent
6365aa9
commit d0dab04
Showing
5 changed files
with
51 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} |