-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathhelpers.go
55 lines (46 loc) · 1.36 KB
/
helpers.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
package smtpmock
import (
"fmt"
"regexp"
"time"
)
// Regex builder
func newRegex(regexPattern string) (*regexp.Regexp, error) {
return regexp.Compile(regexPattern)
}
// Matches string to regex pattern
func matchRegex(strContext, regexPattern string) bool {
regex, err := newRegex(regexPattern)
if err != nil {
return false
}
return regex.MatchString(strContext)
}
// Returns string by regex pattern capture group index.
// For cases when regex not matched or capture group not found returns empty string
func regexCaptureGroup(str string, regexPattern string, captureGroup int) (capturedString string) {
var regex *regexp.Regexp
defer func() { _ = recover() }()
regex, _ = newRegex(regexPattern)
capturedString = regex.FindStringSubmatch(str)[captureGroup]
return capturedString
}
// Returns true if the given string is present in slice, otherwise returns false
func isIncluded(slice []string, target string) bool {
if len(slice) > 0 {
for _, item := range slice {
if item == target {
return true
}
}
}
return false
}
// Returns server with port number follows {server}:{portNumber} pattern
func serverWithPortNumber(server string, portNumber int) string {
return fmt.Sprintf("%s:%d", server, portNumber)
}
// Sleeps for the given duration in milliseconds
func sleepMilliseconds(duration int) {
time.Sleep(time.Duration(duration) * time.Millisecond)
}