Skip to content

Commit

Permalink
✨ feat: assert - add new func: StrNotContains for check string value
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 24, 2024
1 parent 1c08e6b commit dce0f0b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
17 changes: 15 additions & 2 deletions testutil/assert/asserts.go
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions testutil/assert/asserts_test.go
Expand Up @@ -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)
Expand Down

0 comments on commit dce0f0b

Please sign in to comment.