-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.go
More file actions
105 lines (92 loc) · 3.26 KB
/
strings.go
File metadata and controls
105 lines (92 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package attest
import (
"fmt"
"regexp"
"strings"
"testing"
)
func containsAssert(t testing.TB, mode failMode, haystack, needle string, msgAndArgs ...any) {
t.Helper()
if !strings.Contains(haystack, needle) {
msg := formatMsg("string does not contain substring",
fmt.Sprintf("haystack: %q\nneedle: %q", haystack, needle))
report(t, mode, msg, msgAndArgs...)
}
}
// Contains asserts that haystack contains needle.
func Contains(t testing.TB, haystack, needle string, msgAndArgs ...any) {
t.Helper()
containsAssert(t, modeError, haystack, needle, msgAndArgs...)
}
func notContainsAssert(t testing.TB, mode failMode, haystack, needle string, msgAndArgs ...any) {
t.Helper()
if strings.Contains(haystack, needle) {
msg := formatMsg("string should not contain substring",
fmt.Sprintf("haystack: %q\nneedle: %q", haystack, needle))
report(t, mode, msg, msgAndArgs...)
}
}
// NotContains asserts that haystack does not contain needle.
func NotContains(t testing.TB, haystack, needle string, msgAndArgs ...any) {
t.Helper()
notContainsAssert(t, modeError, haystack, needle, msgAndArgs...)
}
func hasPrefixAssert(t testing.TB, mode failMode, s, prefix string, msgAndArgs ...any) {
t.Helper()
if !strings.HasPrefix(s, prefix) {
msg := formatMsg("string does not have expected prefix",
fmt.Sprintf("string: %q\nprefix: %q", s, prefix))
report(t, mode, msg, msgAndArgs...)
}
}
// HasPrefix asserts that s starts with prefix.
func HasPrefix(t testing.TB, s, prefix string, msgAndArgs ...any) {
t.Helper()
hasPrefixAssert(t, modeError, s, prefix, msgAndArgs...)
}
func hasSuffixAssert(t testing.TB, mode failMode, s, suffix string, msgAndArgs ...any) {
t.Helper()
if !strings.HasSuffix(s, suffix) {
msg := formatMsg("string does not have expected suffix",
fmt.Sprintf("string: %q\nsuffix: %q", s, suffix))
report(t, mode, msg, msgAndArgs...)
}
}
// HasSuffix asserts that s ends with suffix.
func HasSuffix(t testing.TB, s, suffix string, msgAndArgs ...any) {
t.Helper()
hasSuffixAssert(t, modeError, s, suffix, msgAndArgs...)
}
func equalFoldAssert(t testing.TB, mode failMode, expected, actual string, msgAndArgs ...any) {
t.Helper()
if !strings.EqualFold(expected, actual) {
msg := formatMsg("strings are not equal (case-insensitive)",
fmt.Sprintf("expected: %q\nactual: %q", expected, actual))
report(t, mode, msg, msgAndArgs...)
}
}
// EqualFold asserts that expected and actual are equal under case-insensitive comparison.
func EqualFold(t testing.TB, expected, actual string, msgAndArgs ...any) {
t.Helper()
equalFoldAssert(t, modeError, expected, actual, msgAndArgs...)
}
func matchesAssert(t testing.TB, mode failMode, s, pattern string, msgAndArgs ...any) {
t.Helper()
re, err := regexp.Compile(pattern)
if err != nil {
msg := formatMsg("invalid regexp pattern",
fmt.Sprintf("pattern: %q\nerror: %s", pattern, err.Error()))
report(t, mode, msg, msgAndArgs...)
return
}
if !re.MatchString(s) {
msg := formatMsg("string does not match pattern",
fmt.Sprintf("string: %q\npattern: %q", s, pattern))
report(t, mode, msg, msgAndArgs...)
}
}
// Matches asserts that s matches the regular expression pattern.
func Matches(t testing.TB, s, pattern string, msgAndArgs ...any) {
t.Helper()
matchesAssert(t, modeError, s, pattern, msgAndArgs...)
}