aboutsummaryrefslogtreecommitdiffstats
path: root/assert
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 /assert
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 'assert')
-rw-r--r--assert/assert.go24
-rw-r--r--assert/assert_test.go78
2 files changed, 88 insertions, 14 deletions
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)
+ }
+ })
+}