diff options
Diffstat (limited to 'check/check.go')
| -rw-r--r-- | check/check.go | 69 |
1 files changed, 61 insertions, 8 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, |