Skip to content

Commit a16e5ce

Browse files
committed
feat: add string Contains, HasPrefix, and HasSuffix.
1 parent 929e590 commit a16e5ce

File tree

4 files changed

+805
-10
lines changed

4 files changed

+805
-10
lines changed

builtin.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,58 @@ import (
55
"testing"
66
)
77

8+
// ContainsString tests whether the string contains the substring or not, and it set the result to
9+
// fail if the string does not contains the substring.
10+
//
11+
// ContainsString(t, "Hello world", "") // success
12+
// ContainsString(t, "Hello world", "Hello") // success
13+
// ContainsString(t, "Hello world", "world") // success
14+
// ContainsString(t, "Hello world", "hello") // fail
15+
func ContainsString(t *testing.T, str, substr string, message ...any) error {
16+
t.Helper()
17+
18+
return tryContainsString(t, false, str, substr, message...)
19+
}
20+
21+
// ContainsStringNow tests whether the string contains the substring or not, and it will terminate the
22+
// execution if the string does not contains the substring.
23+
//
24+
// ContainsStringNow(t, "Hello world", "") // success
25+
// ContainsStringNow(t, "Hello world", "Hello") // success
26+
// ContainsStringNow(t, "Hello world", "world") // success
27+
// ContainsStringNow(t, "Hello world", "hello") // fail and stop the execution
28+
// // never runs
29+
func ContainsStringNow(t *testing.T, str, substr string, message ...any) error {
30+
t.Helper()
31+
32+
return tryContainsString(t, true, str, substr, message...)
33+
}
34+
35+
// NotContainsString tests whether the string contains the substring or not, and it set the result
36+
// to fail if the string contains the substring.
37+
//
38+
// NotContainsString(t, "Hello world", "") // fail
39+
// NotContainsString(t, "Hello world", "Hello") // fail
40+
// NotContainsString(t, "Hello world", "world") // fail
41+
// NotContainsString(t, "Hello world", "hello") // success
42+
func NotContainsString(t *testing.T, str, substr string, message ...any) error {
43+
t.Helper()
44+
45+
return tryNotContainsString(t, false, str, substr, message...)
46+
}
47+
48+
// NotContainsStringNow tests whether the string contains the substring or not, and it will terminate the
49+
// execution if the string does not contains the substring.
50+
//
51+
// NotContainsStringNow(t, "Hello world", "hello") // success
52+
// NotContainsStringNow(t, "Hello world", "Hello") // fail and stop the execution
53+
// // never runs
54+
func NotContainsStringNow(t *testing.T, str, substr string, message ...any) error {
55+
t.Helper()
56+
57+
return tryNotContainsString(t, true, str, substr, message...)
58+
}
59+
860
// DeepEqual tests the deep equality between actual and expect parameters. It'll set the result to
961
// fail if they are not deeply equal, and it doesn't stop the execution.
1062
//
@@ -114,6 +166,110 @@ func NotEqualNow(t *testing.T, actual, expect any, message ...any) error {
114166
return tryNotEqual(t, true, actual, expect, message...)
115167
}
116168

169+
// HasPrefixString tests whether the string has the prefix string or not, and it set the result to
170+
// fail if the string does not have the prefix string.
171+
//
172+
// HasPrefixString(t, "Hello world", "") // success
173+
// HasPrefixString(t, "Hello world", "Hello") // success
174+
// HasPrefixString(t, "Hello world", "world") // fail
175+
// HasPrefixString(t, "Hello world", "hello") // fail
176+
func HasPrefixString(t *testing.T, str, prefix string, message ...any) error {
177+
t.Helper()
178+
179+
return tryHasPrefixString(t, false, str, prefix, message...)
180+
}
181+
182+
// HasPrefixStringNow tests whether the string has the prefix string or not, and it will terminate
183+
// the execution if the string does not have the prefix string.
184+
//
185+
// HasPrefixStringNow(t, "Hello world", "") // success
186+
// HasPrefixStringNow(t, "Hello world", "Hello") // success
187+
// HasPrefixStringNow(t, "Hello world", "hello") // fail and stop the execution
188+
// // never runs
189+
func HasPrefixStringNow(t *testing.T, str, prefix string, message ...any) error {
190+
t.Helper()
191+
192+
return tryHasPrefixString(t, true, str, prefix, message...)
193+
}
194+
195+
// NotHasPrefixString tests whether the string has the prefix string or not, and it set the result
196+
// to fail if the string have the prefix string.
197+
//
198+
// NotHasPrefixString(t, "Hello world", "hello") // success
199+
// NotHasPrefixString(t, "Hello world", "world") // success
200+
// NotHasPrefixString(t, "Hello world", "") // fail
201+
// NotHasPrefixString(t, "Hello world", "Hello") // fail
202+
func NotHasPrefixString(t *testing.T, str, prefix string, message ...any) error {
203+
t.Helper()
204+
205+
return tryNotHasPrefixString(t, false, str, prefix, message...)
206+
}
207+
208+
// NotHasPrefixStringNow tests whether the string has the prefix string or not, and it will
209+
// terminate the execution if the string have the prefix string.
210+
//
211+
// NotHasPrefixStringNow(t, "Hello world", "hello") // success
212+
// NotHasPrefixStringNow(t, "Hello world", "world") // success
213+
// NotHasPrefixStringNow(t, "Hello world", "Hello") // fail and stop the execution
214+
// // never runs
215+
func NotHasPrefixStringNow(t *testing.T, str, prefix string, message ...any) error {
216+
t.Helper()
217+
218+
return tryNotHasPrefixString(t, true, str, prefix, message...)
219+
}
220+
221+
// HasSuffixString tests whether the string has the suffix string or not, and it set the result to
222+
// fail if the string does not have the suffix string.
223+
//
224+
// HasSuffixString(t, "Hello world", "") // success
225+
// HasSuffixString(t, "Hello world", "world") // success
226+
// HasSuffixString(t, "Hello world", "World") // fail
227+
// HasSuffixString(t, "Hello world", "hello") // fail
228+
func HasSuffixString(t *testing.T, str, suffix string, message ...any) error {
229+
t.Helper()
230+
231+
return tryHasSuffixString(t, false, str, suffix, message...)
232+
}
233+
234+
// HasSuffixStringNow tests whether the string has the suffix string or not, and it will terminate
235+
// the execution if the string does not have the suffix string.
236+
//
237+
// HasSuffixStringNow(t, "Hello world", "") // success
238+
// HasSuffixStringNow(t, "Hello world", "world") // success
239+
// HasSuffixStringNow(t, "Hello world", "World") // fail and stop the execution
240+
// // never runs
241+
func HasSuffixStringNow(t *testing.T, str, suffix string, message ...any) error {
242+
t.Helper()
243+
244+
return tryHasSuffixString(t, true, str, suffix, message...)
245+
}
246+
247+
// NotHasSuffixString tests whether the string has the suffix string or not, and it set the result
248+
// to fail if the string have the suffix string.
249+
//
250+
// NotHasSuffixString(t, "Hello world", "Hello") // success
251+
// NotHasSuffixString(t, "Hello world", "World") // success
252+
// NotHasSuffixString(t, "Hello world", "") // fail
253+
// NotHasSuffixString(t, "Hello world", "world") // fail
254+
func NotHasSuffixString(t *testing.T, str, suffix string, message ...any) error {
255+
t.Helper()
256+
257+
return tryNotHasSuffixString(t, false, str, suffix, message...)
258+
}
259+
260+
// NotHasSuffixStringNow tests whether the string has the suffix string or not, and it will
261+
// terminate the execution if the string have the suffix string.
262+
//
263+
// NotHasSuffixStringNow(t, "Hello world", "hello") // success
264+
// NotHasSuffixStringNow(t, "Hello world", "World") // success
265+
// NotHasSuffixStringNow(t, "Hello world", "world") // fail and stop the execution
266+
// // never runs
267+
func NotHasSuffixStringNow(t *testing.T, str, suffix string, message ...any) error {
268+
t.Helper()
269+
270+
return tryNotHasSuffixString(t, true, str, suffix, message...)
271+
}
272+
117273
// Match tests whether the string matches the regular expression or not.
118274
//
119275
// pattern := regexp.MustCompile(`^https?:\/\/`)

error.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@ package assert
33
import "fmt"
44

55
const (
6-
defaultErrMessageEqual string = "%v == %v"
7-
defaultErrMessageNotEqual string = "%v != %v"
8-
defaultErrMessageMatch string = "the input did not match the regular expression"
9-
defaultErrMessageNotMatch string = "the input match the regular expression"
10-
defaultErrMessageNil string = "expect nil, got %v"
11-
defaultErrMessageNotNil string = "expect not nil, got nil"
12-
defaultErrMessagePanic string = "missing expected panic"
13-
defaultErrMessageNotPanic string = "got unwanted error: %v"
14-
defaultErrMessageTrue string = "the expression evaluated to a falsy value"
15-
defaultErrMessageNotTrue string = "the expression evaluated to a truthy value"
6+
defaultErrMessageEqual string = "%v == %v"
7+
defaultErrMessageNotEqual string = "%v != %v"
8+
defaultErrMessageContainsString string = "expect contains \"%s\""
9+
defaultErrMessageNotContainsString string = "expect did not contain \"%s\""
10+
defaultErrMessageHasPrefixString string = "expect has prefix \"%s\""
11+
defaultErrMessageNotHasPrefixString string = "expect has no prefix \"%s\""
12+
defaultErrMessageHasSuffixString string = "expect has suffix \"%s\""
13+
defaultErrMessageNotHasSuffixString string = "expect has no suffix \"%s\""
14+
defaultErrMessageMatch string = "the input did not match the regular expression"
15+
defaultErrMessageNotMatch string = "the input match the regular expression"
16+
defaultErrMessageNil string = "expect nil, got %v"
17+
defaultErrMessageNotNil string = "expect not nil, got nil"
18+
defaultErrMessagePanic string = "missing expected panic"
19+
defaultErrMessageNotPanic string = "got unwanted error: %v"
20+
defaultErrMessageTrue string = "the expression evaluated to a falsy value"
21+
defaultErrMessageNotTrue string = "the expression evaluated to a truthy value"
1622
)
1723

1824
// AssertionError indicates the failure of an assertion.

0 commit comments

Comments
 (0)