-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
143 lines (134 loc) · 2.75 KB
/
string.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package assert
import (
"fmt"
"strings"
"testing"
)
// StringEmpty asserts that s is empty.
//
//nolint:thelper // It's called below.
func StringEmpty(tb testing.TB, s string, opts ...Option) bool {
ok := s == ""
if !ok {
tb.Helper()
Fail(
tb,
"string_empty",
fmt.Sprintf("not empty:\ns = %q", s),
opts...,
)
}
return ok
}
// StringNotEmpty asserts that s is not empty.
//
//nolint:thelper // It's called below.
func StringNotEmpty(tb testing.TB, s string, opts ...Option) bool {
ok := s != ""
if !ok {
tb.Helper()
Fail(
tb,
"string_not_empty",
"empty",
opts...,
)
}
return ok
}
// StringLen asserts that s has length l.
//
//nolint:thelper // It's called below.
func StringLen(tb testing.TB, s string, l int, opts ...Option) bool {
ok := len(s) == l
if !ok {
tb.Helper()
Fail(
tb,
"string_len",
fmt.Sprintf("unexpected length:\nexpected = %d\nactual = %d", l, len(s)),
opts...,
)
}
return ok
}
// StringContains asserts that s contains substr.
//
//nolint:thelper // It's called below.
func StringContains(tb testing.TB, s, substr string, opts ...Option) bool {
ok := strings.Contains(s, substr)
if !ok {
tb.Helper()
Fail(
tb,
"string_contains",
fmt.Sprintf("not contains:\ns = %q\nsubstr = %q", s, substr),
opts...,
)
}
return ok
}
// StringNotContains asserts that s does not contain substr.
//
//nolint:thelper // It's called below.
func StringNotContains(tb testing.TB, s, substr string, opts ...Option) bool {
ok := !strings.Contains(s, substr)
if !ok {
tb.Helper()
Fail(
tb,
"string_not_contains",
fmt.Sprintf("contains:\ns = %q\nsubstr = %q", s, substr),
opts...,
)
}
return ok
}
// StringHasPrefix asserts that s begins with prefix.
//
//nolint:thelper // It's called below.
func StringHasPrefix(tb testing.TB, s, prefix string, opts ...Option) bool {
ok := strings.HasPrefix(s, prefix)
if !ok {
tb.Helper()
Fail(
tb,
"string_has_prefix",
fmt.Sprintf("no prefix:\ns = %q\nprefix = %q", s, prefix),
opts...,
)
}
return ok
}
// StringHasSuffix asserts that s ends with suffix.
//
//nolint:thelper // It's called below.
func StringHasSuffix(tb testing.TB, s, suffix string, opts ...Option) bool {
ok := strings.HasSuffix(s, suffix)
if !ok {
tb.Helper()
Fail(
tb,
"string_has_suffix",
fmt.Sprintf("no suffix:\ns = %q\nsuffix = %q", s, suffix),
opts...,
)
}
return ok
}
// StringEqualFold asserts that s1 and s2 are equal, ignoring case.
//
//nolint:thelper // It's called below.
func StringEqualFold(tb testing.TB, s1, s2 string, opts ...Option) bool {
ok := strings.EqualFold(s1, s2)
if !ok {
tb.Helper()
Fail(
tb,
"string_equal_fold",
fmt.Sprintf("not equal fold:\ns1 = %q\ns2 = %q", s1, s2),
opts...,
)
}
return ok
}