aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md6
-rw-r--r--assert/assert.go22
-rw-r--r--assert/assert_test.go20
-rw-r--r--check/check.go26
-rw-r--r--check/check_test.go20
5 files changed, 94 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ec5ab38..b792b7c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
+## [Unreleased]
+
+### Added
+
+- `Contains` and `NotContains` functions for checking if a string contains a substring.
+
## [0.3.3] - 2025-07-18
### Changed
diff --git a/assert/assert.go b/assert/assert.go
index 1bb5b2b..b5bba9c 100644
--- a/assert/assert.go
+++ b/assert/assert.go
@@ -140,6 +140,28 @@ 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) {
+ if ht, ok := t.(helperT); ok {
+ ht.Helper()
+ }
+
+ if passed := check.Contains(t, s, substr, msgAndArgs...); !passed {
+ t.FailNow()
+ }
+}
+
+// NotContains asserts if a string does not contain a substring.
+func NotContains(t checkmate.TestingT, s, substr string, msgAndArgs ...any) {
+ if ht, ok := t.(helperT); ok {
+ ht.Helper()
+ }
+
+ if passed := check.NotContains(t, s, substr, msgAndArgs...); !passed {
+ t.FailNow()
+ }
+}
+
type helperT interface {
Helper()
}
diff --git a/assert/assert_test.go b/assert/assert_test.go
index afe01af..f07b14b 100644
--- a/assert/assert_test.go
+++ b/assert/assert_test.go
@@ -110,6 +110,22 @@ 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:]...)
+ } else {
+ Contains(t, args[0].(string), args[1].(string))
+ }
+}
+
+func wrappedAssertNotContains(t checkmate.TestingT, args []any) {
+ if len(args) > 2 {
+ NotContains(t, args[0].(string), args[1].(string), args[2:]...)
+ } else {
+ NotContains(t, args[0].(string), args[1].(string))
+ }
+}
+
var passingTestFns = []struct {
name string
assertionFn assertFn
@@ -127,6 +143,8 @@ 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"}},
}
var failingTestFns = []struct {
@@ -146,6 +164,8 @@ 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"}},
}
func TestOptionalMessageAndArgs(t *testing.T) {
diff --git a/check/check.go b/check/check.go
index 6fd320e..25c5a73 100644
--- a/check/check.go
+++ b/check/check.go
@@ -186,6 +186,32 @@ 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 {
+ if ht, ok := t.(helperT); ok {
+ ht.Helper()
+ }
+
+ if len(msgAndArgs) == 0 {
+ msgAndArgs = []any{"expected %s to contain %s", s, substr}
+ }
+
+ return check(t, strings.Contains(s, substr), msgAndArgs...)
+}
+
+// NotContains checks if a string does not contain a substring.
+func NotContains(t checkmate.TestingT, s, substr string, msgAndArgs ...any) bool {
+ if ht, ok := t.(helperT); ok {
+ ht.Helper()
+ }
+
+ if len(msgAndArgs) == 0 {
+ msgAndArgs = []any{"expected %s to not contain %s", s, substr}
+ }
+
+ return check(t, !strings.Contains(s, substr), msgAndArgs...)
+}
+
// Check evaluates a boolean condition and if the condition is false,
// it will log out a message and mark the test as failed. However, it does
// not immediately stop execution, unlike the assert functions.
diff --git a/check/check_test.go b/check/check_test.go
index 10d3551..a5ab96c 100644
--- a/check/check_test.go
+++ b/check/check_test.go
@@ -109,6 +109,22 @@ 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:]...)
+ } else {
+ return Contains(t, args[0].(string), args[1].(string))
+ }
+}
+
+func wrappedCheckNotContains(t checkmate.TestingT, args []any) bool {
+ if len(args) > 2 {
+ return NotContains(t, args[0].(string), args[1].(string), args[2:]...)
+ } else {
+ return NotContains(t, args[0].(string), args[1].(string))
+ }
+}
+
var passingTestFns = []struct {
name string
fn checkFn
@@ -126,6 +142,8 @@ 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"}},
}
var failingTestFns = []struct {
@@ -145,6 +163,8 @@ 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"}},
}
func TestOptionalMessageAndArgs(t *testing.T) {