aboutsummaryrefslogtreecommitdiffstats
path: root/check
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2025-10-13 18:50:18 -0400
committerEugene Triguba <hello@eugenetriguba.com>2025-10-13 18:50:18 -0400
commite23abf75b3da03a826a4e7c52487c03f862a6b91 (patch)
tree86e8697e044c80d33d4ff1e7fff88d81ae2624a1 /check
parentAdd test script, scripts/ folder, and use test script in ci (diff)
downloadcheckmate-e23abf75b3da03a826a4e7c52487c03f862a6b91.zip
Add support for array/slice/map/pointer in Contains/NotContains
Diffstat (limited to 'check')
-rw-r--r--check/check.go69
-rw-r--r--check/check_test.go83
2 files changed, 136 insertions, 16 deletions
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)
+ }
+ })
+}