-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstring.go
92 lines (75 loc) · 2.59 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
package assert
import (
"regexp"
"strings"
)
type String struct {
logFacade *logFacade
actual string
}
func (a *String) IsEqualTo(expected string) *String {
return a.isTrue(a.actual == expected,
"Expected <%s>, but was <%s>.", expected, a.actual)
}
func (a *String) IsNotEqualTo(unexpected string) *String {
return a.isTrue(a.actual != unexpected,
"Expected string not equal to <%s>, but was equal.", unexpected)
}
func (a *String) IsEmpty() *String {
return a.isTrue(a.actual == "",
"Expected string to be empty, but was <%s>.", a.actual)
}
func (a *String) IsNotEmpty() *String {
return a.isTrue(a.actual != "",
"Expected string to not be empty, but was.",
)
}
func (a *String) IsInSlice(expectedSlice []string) *String {
return a.isTrue(stringIsInSlice(expectedSlice, a.actual),
"Expected string to be in slice <%v>, but wasn't.", expectedSlice)
}
func (a *String) IsNotInSlice(expectedSlice []string) *String {
return a.isTrue(!stringIsInSlice(expectedSlice, a.actual),
"Expected string to not be in slice <%v>, but was.", expectedSlice)
}
func (a *String) Contains(substring string) *String {
return a.isTrue(strings.Contains(a.actual, substring),
"Expected string <%s> to contain <%s>, but didn't.", a.actual, substring)
}
func (a *String) DoesNotContain(substring string) *String {
return a.isTrue(!strings.Contains(a.actual, substring),
"Expected string <%s> to not contain <%s>, but it did.", a.actual, substring)
}
func (a *String) IsLowerCase() *String {
return a.isTrue(strings.ToLower(a.actual) == a.actual,
"Expected string <%s> to be lower case, but wasn't.", a.actual)
}
func (a *String) IsNotLowerCase() *String {
return a.isTrue(strings.ToLower(a.actual) != a.actual,
"Expected string <%s> to not be lower case, but was.", a.actual)
}
func (a *String) IsUpperCase() *String {
return a.isTrue(strings.ToUpper(a.actual) == a.actual,
"Expected string <%s> to be upper case, but wasn't.", a.actual)
}
func (a *String) IsNotUpperCase() *String {
return a.isTrue(strings.ToUpper(a.actual) != a.actual,
"Expected string <%s> to not be upper case, but was.", a.actual)
}
func (a *String) Matches(pattern string) *String {
matched, _ := regexp.MatchString(pattern, a.actual)
return a.isTrue(matched,
"Expected string <%s> to match <%s>, but didn't.", a.actual, pattern)
}
func (a *String) isTrue(condition bool, format string, args ...interface{}) *String {
logIfFalse(a.logFacade, condition, format, args...)
return a
}
func stringIsInSlice(slice []string, expectedString string) bool {
for _, v := range slice {
if v == expectedString {
return true
}
}
return false
}