-
Notifications
You must be signed in to change notification settings - Fork 928
/
contain_substrings.go
69 lines (54 loc) · 1.75 KB
/
contain_substrings.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
package matchers
import (
"fmt"
"strings"
"code.cloudfoundry.org/cli/cf/terminal"
"github.com/onsi/gomega"
)
type SliceMatcher struct {
expected [][]string
failedAtIndex int
}
func ContainSubstrings(substrings ...[]string) gomega.OmegaMatcher {
return &SliceMatcher{expected: substrings}
}
func (matcher *SliceMatcher) Match(actual interface{}) (success bool, err error) {
actualStrings, ok := actual.([]string)
if !ok {
return false, nil
}
allStringsMatched := make([]bool, len(matcher.expected))
for index, expectedArray := range matcher.expected {
for _, actualValue := range actualStrings {
allStringsFound := true
for _, expectedValue := range expectedArray {
allStringsFound = allStringsFound && strings.Contains(terminal.Decolorize(actualValue), expectedValue)
}
if allStringsFound {
allStringsMatched[index] = true
break
}
}
}
for index, value := range allStringsMatched {
if !value {
matcher.failedAtIndex = index
return false, nil
}
}
return true, nil
}
func (matcher *SliceMatcher) FailureMessage(actual interface{}) string {
actualStrings, ok := actual.([]string)
if !ok {
return fmt.Sprintf("Expected actual to be a slice of strings, but it's actually a %T", actual)
}
return fmt.Sprintf("expected to find \"%s\" in actual:\n'%s'\n", matcher.expected[matcher.failedAtIndex], strings.Join(actualStrings, "\n"))
}
func (matcher *SliceMatcher) NegatedFailureMessage(actual interface{}) string {
actualStrings, ok := actual.([]string)
if !ok {
return fmt.Sprintf("Expected actual to be a slice of strings, but it's actually a %T", actual)
}
return fmt.Sprintf("expected to not find \"%s\" in actual:\n'%s'\n", matcher.expected[matcher.failedAtIndex], strings.Join(actualStrings, "\n"))
}