From e23abf75b3da03a826a4e7c52487c03f862a6b91 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 13 Oct 2025 18:50:18 -0400 Subject: Add support for array/slice/map/pointer in Contains/NotContains --- CHANGELOG.md | 7 +++++ assert/assert.go | 24 +++++++++++---- assert/assert_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++----- check/check.go | 69 +++++++++++++++++++++++++++++++++++++----- check/check_test.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++----- 5 files changed, 231 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f2e126..940c89f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- `Contains` and `NotContains` supported checking whether a string is + a substring of another. Now, it also supports checking whether an item is + contained within an array, slice, a key in a map, and + pointers to any of the supported types. + ## [0.4.0] - 2025-10-11 ### Added diff --git a/assert/assert.go b/assert/assert.go index b5bba9c..43bd5f2 100644 --- a/assert/assert.go +++ b/assert/assert.go @@ -140,24 +140,36 @@ func NotEqual(t checkmate.TestingT, actual, expected any, msgAndArgs ...any) { } } -// Contains asserts if a string contains a substring. -func Contains(t checkmate.TestingT, s, substr string, msgAndArgs ...any) { +// Contains asserts a container includes a specified item. +// +// Supported container types: +// - string: asserts if the string contains the given substring (item must be a string). +// - slice or array: asserts if the container contains an element equal to the item. +// - map: asserts if the map has the given key (item must be the key type). +// - pointer to any of the above: dereferences and asserts the underlying value. +func Contains(t checkmate.TestingT, container, item any, msgAndArgs ...any) { if ht, ok := t.(helperT); ok { ht.Helper() } - if passed := check.Contains(t, s, substr, msgAndArgs...); !passed { + if passed := check.Contains(t, container, item, msgAndArgs...); !passed { t.FailNow() } } -// NotContains asserts if a string does not contain a substring. -func NotContains(t checkmate.TestingT, s, substr string, msgAndArgs ...any) { +// NotContains asserts whether a container does not include a specified item. +// +// Supported container types: +// - string: asserts if the string does not contain the given substring (item must be a string). +// - slice or array: asserts if the container does not contain an element equal to the item. +// - map: asserts if the map does not have the given key (item must be the key type). +// - pointer to any of the above: dereferences and asserts the underlying value. +func NotContains(t checkmate.TestingT, container, item any, msgAndArgs ...any) { if ht, ok := t.(helperT); ok { ht.Helper() } - if passed := check.NotContains(t, s, substr, msgAndArgs...); !passed { + if passed := check.NotContains(t, container, item, msgAndArgs...); !passed { t.FailNow() } } diff --git a/assert/assert_test.go b/assert/assert_test.go index f07b14b..3145e3e 100644 --- a/assert/assert_test.go +++ b/assert/assert_test.go @@ -112,17 +112,17 @@ func wrappedAssertNotEqual(t checkmate.TestingT, args []any) { func wrappedAssertContains(t checkmate.TestingT, args []any) { if len(args) > 2 { - Contains(t, args[0].(string), args[1].(string), args[2:]...) + Contains(t, args[0], args[1], args[2:]...) } else { - Contains(t, args[0].(string), args[1].(string)) + Contains(t, args[0], args[1]) } } func wrappedAssertNotContains(t checkmate.TestingT, args []any) { if len(args) > 2 { - NotContains(t, args[0].(string), args[1].(string), args[2:]...) + NotContains(t, args[0], args[1], args[2:]...) } else { - NotContains(t, args[0].(string), args[1].(string)) + NotContains(t, args[0], args[1]) } } @@ -143,8 +143,14 @@ var passingTestFns = []struct { {"AssertNotDeepEqual", wrappedAssertNotDeepEqual, []any{5, 6}}, {"AssertEqual", wrappedAssertEqual, []any{5, 5}}, {"AssertNotEqual", wrappedAssertNotEqual, []any{5, 10}}, - {"AssertContains", wrappedAssertContains, []any{"hello world", "world"}}, - {"AssertNotContains", wrappedAssertNotContains, []any{"hello world", "goodbye"}}, + {"AssertContains - Substr", wrappedAssertContains, []any{"hello world", "world"}}, + {"AssertContains - Slice", wrappedAssertContains, []any{[]string{"hello", "world"}, "world"}}, + {"AssertContains - Array", wrappedAssertContains, []any{[2]string{"hello", "world"}, "world"}}, + {"AssertContains - Map", wrappedAssertContains, []any{map[string]int{"hello": 1, "world": 2}, "world"}}, + {"AssertNotContains - Substr", wrappedAssertNotContains, []any{"hello world", "goodbye"}}, + {"AssertNotContains - Slice", wrappedAssertNotContains, []any{[]string{"hello", "world"}, "goodbye"}}, + {"AssertNotContains - Array", wrappedAssertNotContains, []any{[2]string{"hello", "world"}, "goodbye"}}, + {"AssertNotContains - Map", wrappedAssertNotContains, []any{map[string]int{"hello": 1, "world": 2}, "goodbye"}}, } var failingTestFns = []struct { @@ -164,8 +170,12 @@ var failingTestFns = []struct { {"AssertNotDeepEqual", wrappedAssertNotDeepEqual, []any{5, 5}}, {"AssertEqual", wrappedAssertEqual, []any{5, 10}}, {"AssertNotEqual", wrappedAssertNotEqual, []any{5, 5}}, - {"AssertContains", wrappedAssertContains, []any{"hello world", "goodbye"}}, - {"AssertNotContains", wrappedAssertNotContains, []any{"hello world", "world"}}, + {"AssertContains - Substr not found", wrappedAssertContains, []any{"hello world", "goodbye"}}, + {"AssertContains - Slice item not found", wrappedAssertContains, []any{[]string{"hello", "world"}, "goodbye"}}, + {"AssertContains - Map key not found", wrappedAssertContains, []any{map[string]int{"hello": 1}, "goodbye"}}, + {"AssertNotContains - Substr found", wrappedAssertNotContains, []any{"hello world", "world"}}, + {"AssertNotContains - Slice item found", wrappedAssertNotContains, []any{[]string{"hello", "world"}, "world"}}, + {"AssertNotContains - Map key found", wrappedAssertNotContains, []any{map[string]int{"hello": 1, "world": 2}, "world"}}, } func TestOptionalMessageAndArgs(t *testing.T) { @@ -342,3 +352,55 @@ func TestAssertErrorIsChecksErrTree(t *testing.T) { t.Fatal("AssertErrorIs failed when it should have passed") } } + +func TestAssertContainsErrorCases(t *testing.T) { + t.Run("String container with non-string item", func(t *testing.T) { + mockT := &cmtest.MockT{} + Contains(mockT, "hello world", 123) + + if !mockT.FailNowCalled { + t.Error("Contains should have called FailNow when string container has non-string item") + } + if len(mockT.Logs) == 0 || !strings.Contains(mockT.Logs[0], "item must be string") { + t.Errorf("Expected error message about item type, got: %v", mockT.Logs) + } + }) + + t.Run("Unsupported container type", func(t *testing.T) { + mockT := &cmtest.MockT{} + Contains(mockT, 123, 1) + + if !mockT.FailNowCalled { + t.Error("Contains should have called FailNow for unsupported container type") + } + if len(mockT.Logs) == 0 || !strings.Contains(mockT.Logs[0], "unsupported container type") { + t.Errorf("Expected error message about unsupported type, got: %v", mockT.Logs) + } + }) +} + +func TestAssertNotContainsErrorCases(t *testing.T) { + t.Run("String container with non-string item", func(t *testing.T) { + mockT := &cmtest.MockT{} + NotContains(mockT, "hello world", 123) + + if !mockT.FailNowCalled { + t.Error("NotContains should have called FailNow when string container has non-string item") + } + if len(mockT.Logs) == 0 || !strings.Contains(mockT.Logs[0], "item must be string") { + t.Errorf("Expected error message about item type, got: %v", mockT.Logs) + } + }) + + t.Run("Unsupported container type", func(t *testing.T) { + mockT := &cmtest.MockT{} + NotContains(mockT, 123, 1) + + if !mockT.FailNowCalled { + t.Error("NotContains should have called FailNow for unsupported container type") + } + if len(mockT.Logs) == 0 || !strings.Contains(mockT.Logs[0], "unsupported container type") { + t.Errorf("Expected error message about unsupported type, got: %v", mockT.Logs) + } + }) +} diff --git a/check/check.go b/check/check.go index 25c5a73..6000d64 100644 --- a/check/check.go +++ b/check/check.go @@ -186,30 +186,83 @@ func NotEqual(t checkmate.TestingT, actual, expected any, msgAndArgs ...any) boo return check(t, actual != expected, msgAndArgs...) } -// Contains checks if a string contains a substring. -func Contains(t checkmate.TestingT, s, substr string, msgAndArgs ...any) bool { +// Contains checks whether a container includes a specified item. +// +// Supported container types: +// - string: checks if the string contains the given substring (item must be a string). +// - slice or array: checks if the container contains an element equal to the item. +// - map: checks if the map has the given key (item must be the key type). +// - pointer to any of the above: dereferences and checks the underlying value. +func Contains(t checkmate.TestingT, container, item any, msgAndArgs ...any) bool { if ht, ok := t.(helperT); ok { ht.Helper() } + contains, errMsg := containsCheck(container, item) + if errMsg != "" { + return check(t, false, "Contains: %s", errMsg) + } + if len(msgAndArgs) == 0 { - msgAndArgs = []any{"expected %s to contain %s", s, substr} + msgAndArgs = []any{"expected %v to contain %v", container, item} } - return check(t, strings.Contains(s, substr), msgAndArgs...) + return check(t, contains, msgAndArgs...) } -// NotContains checks if a string does not contain a substring. -func NotContains(t checkmate.TestingT, s, substr string, msgAndArgs ...any) bool { +// NotContains checks whether a container does not include a specified item. +// +// Supported container types: +// - string: checks if the string does not contain the given substring (item must be a string). +// - slice or array: checks if the container does not contain an element equal to the item. +// - map: checks if the map does not have the given key (item must be the key type). +// - pointer to any of the above: dereferences and checks the underlying value. +func NotContains(t checkmate.TestingT, container, item any, msgAndArgs ...any) bool { if ht, ok := t.(helperT); ok { ht.Helper() } + contains, errMsg := containsCheck(container, item) + if errMsg != "" { + return check(t, false, "NotContains: %s", errMsg) + } + if len(msgAndArgs) == 0 { - msgAndArgs = []any{"expected %s to not contain %s", s, substr} + msgAndArgs = []any{"expected %v to not contain %v", container, item} + } + + return check(t, !contains, msgAndArgs...) +} + +// containsCheck is a helper that checks whether a container includes an item. +// Returns (contains bool, errMsg string) where errMsg is non-empty on error. +func containsCheck(container, item any) (bool, string) { + val := reflect.ValueOf(container) + if val.Kind() == reflect.Pointer { + val = val.Elem() } - return check(t, !strings.Contains(s, substr), msgAndArgs...) + kind := val.Kind() + switch kind { + case reflect.String: + substr, ok := item.(string) + if !ok { + return false, "item must be string when container is a string" + } + return strings.Contains(val.String(), substr), "" + case reflect.Slice, reflect.Array: + for i := 0; i < val.Len(); i++ { + if reflect.DeepEqual(val.Index(i).Interface(), item) { + return true, "" + } + } + return false, "" + case reflect.Map: + keyVal := reflect.ValueOf(item) + return val.MapIndex(keyVal).IsValid(), "" + default: + return false, fmt.Sprintf("unsupported container type %T", container) + } } // Check evaluates a boolean condition and if the condition is false, diff --git a/check/check_test.go b/check/check_test.go index a5ab96c..3293ba4 100644 --- a/check/check_test.go +++ b/check/check_test.go @@ -111,17 +111,17 @@ func wrappedCheckNotEqual(t checkmate.TestingT, args []any) bool { func wrappedCheckContains(t checkmate.TestingT, args []any) bool { if len(args) > 2 { - return Contains(t, args[0].(string), args[1].(string), args[2:]...) + return Contains(t, args[0], args[1], args[2:]...) } else { - return Contains(t, args[0].(string), args[1].(string)) + return Contains(t, args[0], args[1]) } } func wrappedCheckNotContains(t checkmate.TestingT, args []any) bool { if len(args) > 2 { - return NotContains(t, args[0].(string), args[1].(string), args[2:]...) + return NotContains(t, args[0], args[1], args[2:]...) } else { - return NotContains(t, args[0].(string), args[1].(string)) + return NotContains(t, args[0], args[1]) } } @@ -142,8 +142,17 @@ var passingTestFns = []struct { {"CheckNotDeepEqual", wrappedCheckNotDeepEqual, []any{5, 6}}, {"CheckEqual", wrappedCheckEqual, []any{5, 5}}, {"CheckNotEqual", wrappedCheckNotEqual, []any{5, 10}}, - {"CheckContains", wrappedCheckContains, []any{"hello world", "world"}}, - {"CheckNotContains", wrappedCheckNotContains, []any{"hello world", "goodbye"}}, + {"CheckContains - Substr", wrappedCheckContains, []any{"hello world", "world"}}, + {"CheckContains - Slice", wrappedCheckContains, []any{[]string{"hello", "world"}, "world"}}, + {"CheckContains - Array", wrappedCheckContains, []any{[2]string{"hello", "world"}, "world"}}, + {"CheckContains - Map", wrappedCheckContains, []any{map[string]int{"hello": 1, "world": 2}, "world"}}, + {"CheckContains - Pointer to String", wrappedCheckContains, []any{func() *string { s := "hello world"; return &s }(), "world"}}, + {"CheckContains - Pointer to Slice", wrappedCheckContains, []any{func() *[]string { s := []string{"hello", "world"}; return &s }(), "world"}}, + {"CheckNotContains - Substr", wrappedCheckNotContains, []any{"hello world", "goodbye"}}, + {"CheckNotContains - Slice", wrappedCheckNotContains, []any{[]string{"hello", "world"}, "goodbye"}}, + {"CheckNotContains - Array", wrappedCheckNotContains, []any{[2]string{"hello", "world"}, "goodbye"}}, + {"CheckNotContains - Map", wrappedCheckNotContains, []any{map[string]int{"hello": 1, "world": 2}, "goodbye"}}, + {"CheckNotContains - Pointer to String", wrappedCheckNotContains, []any{func() *string { s := "hello world"; return &s }(), "goodbye"}}, } var failingTestFns = []struct { @@ -163,8 +172,14 @@ var failingTestFns = []struct { {"CheckNotDeepEqual", wrappedCheckNotDeepEqual, []any{5, 5}}, {"CheckEqual", wrappedCheckEqual, []any{5, 10}}, {"CheckNotEqual", wrappedCheckNotEqual, []any{5, 5}}, - {"CheckContains", wrappedCheckContains, []any{"hello world", "goodbye"}}, - {"CheckNotContains", wrappedCheckNotContains, []any{"hello world", "world"}}, + {"CheckContains - Substr not found", wrappedCheckContains, []any{"hello world", "goodbye"}}, + {"CheckContains - Slice item not found", wrappedCheckContains, []any{[]string{"hello", "world"}, "goodbye"}}, + {"CheckContains - Array item not found", wrappedCheckContains, []any{[2]string{"hello", "world"}, "goodbye"}}, + {"CheckContains - Map key not found", wrappedCheckContains, []any{map[string]int{"hello": 1}, "goodbye"}}, + {"CheckNotContains - Substr found", wrappedCheckNotContains, []any{"hello world", "world"}}, + {"CheckNotContains - Slice item found", wrappedCheckNotContains, []any{[]string{"hello", "world"}, "world"}}, + {"CheckNotContains - Array item found", wrappedCheckNotContains, []any{[2]string{"hello", "world"}, "world"}}, + {"CheckNotContains - Map key found", wrappedCheckNotContains, []any{map[string]int{"hello": 1, "world": 2}, "world"}}, } func TestOptionalMessageAndArgs(t *testing.T) { @@ -374,3 +389,55 @@ func TestCheckNilForNilPointer(t *testing.T) { t.Fatal("check.Nil should have returned true for nil pointer") } } + +func TestCheckContainsErrorCases(t *testing.T) { + t.Run("String container with non-string item", func(t *testing.T) { + mockT := &cmtest.MockT{} + passed := Contains(mockT, "hello world", 123) + + if passed { + t.Error("Contains should fail when string container has non-string item") + } + if len(mockT.Logs) == 0 || !strings.Contains(mockT.Logs[0], "item must be string") { + t.Errorf("Expected error message about item type, got: %v", mockT.Logs) + } + }) + + t.Run("Unsupported container type", func(t *testing.T) { + mockT := &cmtest.MockT{} + passed := Contains(mockT, 123, 1) + + if passed { + t.Error("Contains should fail for unsupported container type") + } + if len(mockT.Logs) == 0 || !strings.Contains(mockT.Logs[0], "unsupported container type") { + t.Errorf("Expected error message about unsupported type, got: %v", mockT.Logs) + } + }) +} + +func TestCheckNotContainsErrorCases(t *testing.T) { + t.Run("String container with non-string item", func(t *testing.T) { + mockT := &cmtest.MockT{} + passed := NotContains(mockT, "hello world", 123) + + if passed { + t.Error("NotContains should fail when string container has non-string item") + } + if len(mockT.Logs) == 0 || !strings.Contains(mockT.Logs[0], "item must be string") { + t.Errorf("Expected error message about item type, got: %v", mockT.Logs) + } + }) + + t.Run("Unsupported container type", func(t *testing.T) { + mockT := &cmtest.MockT{} + passed := NotContains(mockT, 123, 1) + + if passed { + t.Error("NotContains should fail for unsupported container type") + } + if len(mockT.Logs) == 0 || !strings.Contains(mockT.Logs[0], "unsupported container type") { + t.Errorf("Expected error message about unsupported type, got: %v", mockT.Logs) + } + }) +} -- cgit v1.2.3