-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
84 lines (68 loc) · 2.49 KB
/
search.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
package assertion
import (
"fmt"
"reflect"
"strings"
)
// StartsWith returns true if a given string starts with the given needle substring
func (a *Assertion) StartsWith(value, needle string, msgArgs ...interface{}) bool {
if !strings.HasPrefix(value, needle) {
a.addErrorMsg(fmt.Sprintf(errMsgNotStartsWith, value, needle), msgArgs...)
return false
}
return true
}
// EndsWith returns true if a given string ends with the given needle substring
func (a *Assertion) EndsWith(value, needle string, msgArgs ...interface{}) bool {
if !strings.HasSuffix(value, needle) {
a.addErrorMsg(fmt.Sprintf(errMsgNotEndsWith, value, needle), msgArgs...)
return false
}
return true
}
// Contains returns true if a given string ends with the given needle substring
func (a *Assertion) Contains(value, needle string, msgArgs ...interface{}) bool {
if !strings.Contains(value, needle) {
a.addErrorMsg(fmt.Sprintf(errMsgNotContains, value, needle), msgArgs...)
return false
}
return true
}
// StartsWithInsensitive returns true if a given string starts with the given
// needle substring with insensitive case
func (a *Assertion) StartsWithInsensitive(value, needle string, msgArgs ...interface{}) bool {
if !strings.HasPrefix(strings.ToLower(value), strings.ToLower(needle)) {
a.addErrorMsg(fmt.Sprintf(errMsgNotStartsWith, value, needle), msgArgs...)
return false
}
return true
}
// EndsWithInsensitive returns true if a given string ends with the given needle substring
func (a *Assertion) EndsWithInsensitive(value, needle string, msgArgs ...interface{}) bool {
if !strings.HasSuffix(strings.ToLower(value), strings.ToLower(needle)) {
a.addErrorMsg(fmt.Sprintf(errMsgNotEndsWith, value, needle), msgArgs...)
return false
}
return true
}
// ContainsInsensitive returns true if a given string ends with the given needle substring
func (a *Assertion) ContainsInsensitive(value, needle string, msgArgs ...interface{}) bool {
if !strings.Contains(strings.ToLower(value), strings.ToLower(needle)) {
a.addErrorMsg(fmt.Sprintf(errMsgNotContains, value, needle), msgArgs...)
return false
}
return true
}
// HasKey returns true if a given key exists on the a given map
func (a *Assertion) HasKey(value interface{}, key interface{}, msgArgs ...interface{}) bool {
v := reflect.ValueOf(value)
if v.Kind() == reflect.Map {
for _, k := range v.MapKeys() {
if reflect.DeepEqual(k.Interface(), key){
return true
}
}
}
a.addErrorMsg(fmt.Sprintf(errMsgNotHasKey, value, key), msgArgs...)
return false
}