-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstring.go
127 lines (110 loc) · 4.19 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
package verify
import (
"fmt"
"regexp"
"strings"
)
// FluentString encapsulates assertions for string object.
type FluentString[T ~string] struct {
FluentOrdered[T]
}
// String is used for testing a string object.
func String[T ~string](got T) FluentString[T] {
return FluentString[T]{FluentOrdered[T]{FluentObj[T]{FluentAny[T]{got}}}}
}
// Empty tests if the string is not empty.
func (x FluentString[T]) Empty() FailureMessage {
if x.Got == "" {
return ""
}
return FailureMessage(fmt.Sprintf("the value was not an empty string\ngot: %q", x.Got))
}
// NotEmpty tests if the string is not empty.
func (x FluentString[T]) NotEmpty() FailureMessage {
if x.Got != "" {
return ""
}
return "the value was \"\""
}
// Contain tests if the string contains the substring.
func (x FluentString[T]) Contain(substr string) FailureMessage {
if strings.Contains(string(x.Got), substr) {
return ""
}
return FailureMessage(fmt.Sprintf("the value does not contain the substring\ngot: %q\nsubstr: %q", x.Got, substr))
}
// NotContain tests if the string does not contain the substring.
func (x FluentString[T]) NotContain(substr string) FailureMessage {
if !strings.Contains(string(x.Got), substr) {
return ""
}
return FailureMessage(fmt.Sprintf("the value contains the substring\ngot: %q\nsubstr: %q", x.Got, substr))
}
// Prefix tests if the string starts with the prefix.
func (x FluentString[T]) Prefix(prefix string) FailureMessage {
if strings.HasPrefix(string(x.Got), prefix) {
return ""
}
return FailureMessage(fmt.Sprintf("the value does not have the prefix\ngot: %q\nprefix: %q", x.Got, prefix))
}
// NoPrefix tests if the string does not start with the prefix.
func (x FluentString[T]) NoPrefix(prefix string) FailureMessage {
if !strings.HasPrefix(string(x.Got), prefix) {
return ""
}
return FailureMessage(fmt.Sprintf("the value has the prefix\ngot: %q\nprefix: %q", x.Got, prefix))
}
// Sufix tests if the string ends with the sufix.
func (x FluentString[T]) Sufix(sufix string) FailureMessage {
if strings.HasSuffix(string(x.Got), sufix) {
return ""
}
return FailureMessage(fmt.Sprintf("the value does not have the sufix\ngot: %q\nsufix: %q", x.Got, sufix))
}
// NoSufix tests if the string does not end with the sufix.
func (x FluentString[T]) NoSufix(sufix string) FailureMessage {
if !strings.HasSuffix(string(x.Got), sufix) {
return ""
}
return FailureMessage(fmt.Sprintf("the value has the sufix\ngot: %q\nsufix: %q", x.Got, sufix))
}
// EqualFold tests if the values interpreted as UTF-8 strings,
// are equal under simple Unicode case-folding,
// which is a more general form of case-insensitivity.
func (x FluentString[T]) EqualFold(want string) FailureMessage {
if strings.EqualFold(string(x.Got), want) {
return ""
}
return FailureMessage(fmt.Sprintf("the string values are not equal under Unicode case folding\ngot: %q\nwant: %q", x.Got, want))
}
// NotEqualFold tests if the values interpreted as UTF-8 strings,
// are not equal under simple Unicode case-folding,
// which is a more general form of case-insensitivity.
func (x FluentString[T]) NotEqualFold(want string) FailureMessage {
if !strings.EqualFold(string(x.Got), want) {
return ""
}
return FailureMessage(fmt.Sprintf("the string values are equal under Unicode case folding\ngot: %q\nwant: %q", x.Got, want))
}
// MatchRegex tests if the string matches the regular expression.
func (x FluentString[T]) MatchRegex(regex *regexp.Regexp) FailureMessage {
if regex.MatchString(string(x.Got)) {
return ""
}
return FailureMessage(fmt.Sprintf("the string value does not match the regular expression\ngot: %q\nregex: %s", x.Got, regex.String()))
}
// NotMatchRegex tests if the string does not match the regular expression.
func (x FluentString[T]) NotMatchRegex(regex *regexp.Regexp) FailureMessage {
if !regex.MatchString(string(x.Got)) {
return ""
}
return FailureMessage(fmt.Sprintf("the string value matches the regular expression\ngot: %q\nregex: %s", x.Got, regex.String()))
}
// Len tests the length of the string.
func (x FluentString[T]) Len(want int) FailureMessage {
gotLen := len(x.Got)
if gotLen != want {
return FailureMessage(fmt.Sprintf("has different length\ngot: %+v\nlen: %v\nwant: %v", x.Got, gotLen, want))
}
return ""
}