Skip to content

Commit

Permalink
chore: rename ContainsItem to SliceContains (and add NotSliceContains)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Sep 17, 2024
1 parent 9b12f17 commit 96885de
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
33 changes: 23 additions & 10 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,19 @@ func Contains(t testing.TB, haystack string, needle string, msgAndArgs ...any) {
t.Fatalf("%s\nNeedle: %q\nHaystack: %q\n", msg, needle, haystack)
}

// ContainsItem asserts that "haystack" contains "needle".
func ContainsItem[T any](t testing.TB, haystack []T, needle T, msgAndArgs ...interface{}) {
// NotContains asserts that "haystack" does not contain "needle".
func NotContains(t testing.TB, haystack string, needle string, msgAndArgs ...any) {
if !strings.Contains(haystack, needle) {
return
}
t.Helper()
msg := formatMsgAndArgs("Haystack should not contain needle.", msgAndArgs...)
quotedHaystack, quotedNeedle, positions := needlePosition(haystack, needle)
t.Fatalf("%s\nNeedle: %s\nHaystack: %s\n %s\n", msg, quotedNeedle, quotedHaystack, positions)
}

// SliceContains asserts that "haystack" contains "needle".
func SliceContains[T any](t testing.TB, haystack []T, needle T, msgAndArgs ...interface{}) {
t.Helper()
for _, item := range haystack {
if objectsAreEqual(item, needle) {
Expand All @@ -128,15 +139,17 @@ func ContainsItem[T any](t testing.TB, haystack []T, needle T, msgAndArgs ...int
t.Fatalf("%s\nNeedle: %s\nHaystack: %s\n", msg, needleRepr, haystackRepr)
}

// NotContains asserts that "haystack" does not contain "needle".
func NotContains(t testing.TB, haystack string, needle string, msgAndArgs ...any) {
if !strings.Contains(haystack, needle) {
return
}
// NotSliceContains asserts that "haystack" does not contain "needle".
func NotSliceContains[T any](t testing.TB, haystack []T, needle T, msgAndArgs ...interface{}) {
t.Helper()
msg := formatMsgAndArgs("Haystack should not contain needle.", msgAndArgs...)
quotedHaystack, quotedNeedle, positions := needlePosition(haystack, needle)
t.Fatalf("%s\nNeedle: %s\nHaystack: %s\n %s\n", msg, quotedNeedle, quotedHaystack, positions)
for _, item := range haystack {
if objectsAreEqual(item, needle) {
msg := formatMsgAndArgs("Haystack should not contain needle.", msgAndArgs...)
needleRepr := repr.String(needle, repr.Indent(" "))
haystackRepr := repr.String(haystack, repr.Indent(" "))
t.Fatalf("%s\nNeedle: %s\nHaystack: %s\n", msg, needleRepr, haystackRepr)
}
}
}

// Zero asserts that a value is its zero value.
Expand Down
21 changes: 15 additions & 6 deletions assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,30 @@ func TestContains(t *testing.T) {
})
}

func TestContainsItem(t *testing.T) {
func TestNotContains(t *testing.T) {
assertOk(t, "NotFound", func(t testing.TB) {
NotContains(t, "a haystack with a needle in it", "screw")
})
assertFail(t, "Found", func(t testing.TB) {
NotContains(t, "a haystack with a needle in it", "needle")
})
}

func TestSliceContains(t *testing.T) {
assertOk(t, "Found", func(t testing.TB) {
ContainsItem(t, []string{"hello", "world"}, "hello")
SliceContains(t, []string{"hello", "world"}, "hello")
})
assertFail(t, "NotFound", func(t testing.TB) {
ContainsItem(t, []string{"hello", "world"}, "goodbye")
SliceContains(t, []string{"hello", "world"}, "goodbye")
})
}

func TestNotContains(t *testing.T) {
func TestNotSliceContains(t *testing.T) {
assertOk(t, "NotFound", func(t testing.TB) {
NotContains(t, "a haystack with a needle in it", "screw")
NotSliceContains(t, []string{"hello", "world"}, "goodbye")
})
assertFail(t, "Found", func(t testing.TB) {
NotContains(t, "a haystack with a needle in it", "needle")
NotSliceContains(t, []string{"hello", "world"}, "hello")
})
}

Expand Down

0 comments on commit 96885de

Please sign in to comment.