From dce0f0b0c615e38ace9d21f135f41b4e641c61db Mon Sep 17 00:00:00 2001 From: Inhere Date: Sun, 24 Mar 2024 23:57:59 +0800 Subject: [PATCH] :sparkles: feat: assert - add new func: StrNotContains for check string value --- testutil/assert/asserts.go | 17 +++++++++++++++-- testutil/assert/asserts_test.go | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/testutil/assert/asserts.go b/testutil/assert/asserts.go index 68d3f8c2d..0b5224710 100644 --- a/testutil/assert/asserts.go +++ b/testutil/assert/asserts.go @@ -300,7 +300,7 @@ func ContainsElems[T comdef.ScalarType](t TestingT, list, sub []T, fmtAndArgs .. return fail(t, fmt.Sprintf("%#v\nShould contain: %#v", list, sub), fmtAndArgs) } -// StrContains asserts that the given strings is contains sub-string +// StrContains asserts that the given string should contain sub-string func StrContains(t TestingT, s, sub string, fmtAndArgs ...any) bool { if strings.Contains(s, sub) { return true @@ -313,7 +313,20 @@ func StrContains(t TestingT, s, sub string, fmtAndArgs ...any) bool { ) } -// StrCount asserts that the given strings is contains sub-string and count +// StrNotContains asserts that the given string should not contain sub-string +func StrNotContains(t TestingT, s, sub string, fmtAndArgs ...any) bool { + if !strings.Contains(s, sub) { + return true + } + + t.Helper() + return fail(t, + fmt.Sprintf("String check fail:\nGiven string: %#v\nShould not contains: %#v", s, sub), + fmtAndArgs, + ) +} + +// StrCount asserts that the given string should contain sub-string and count func StrCount(t TestingT, s, sub string, count int, fmtAndArgs ...any) bool { if strings.Count(s, sub) == count { return true diff --git a/testutil/assert/asserts_test.go b/testutil/assert/asserts_test.go index 79fa94883..8d891ffed 100644 --- a/testutil/assert/asserts_test.go +++ b/testutil/assert/asserts_test.go @@ -107,6 +107,7 @@ func TestCommon_fail(t *testing.T) { assert.StrContains(t, str, "TestCommon_fail") assert.StrContains(t, str, "goutil/testutil/assert/asserts_test.go:") assert.StrContains(t, str, "Expected nil, but got:") + assert.StrNotContains(t, str, "NOT EXIST") tc.Reset() assert.NotNil(tc, nil)