forked from cloudfoundry/cli
/
contain_substring.go
49 lines (40 loc) · 1.27 KB
/
contain_substring.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
package matchers
import (
"fmt"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/onsi/gomega"
"strings"
)
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
}
matcher.failedAtIndex = 0
for _, actualValue := range actualStrings {
allStringsFound := true
for _, expectedValue := range matcher.expected[matcher.failedAtIndex] {
allStringsFound = allStringsFound && strings.Contains(terminal.Decolorize(actualValue), expectedValue)
}
if allStringsFound {
matcher.failedAtIndex++
if matcher.failedAtIndex == len(matcher.expected) {
return true, nil
}
}
}
return false, nil
}
func (matcher *SliceMatcher) FailureMessage(actual interface{}) string {
return fmt.Sprintf("expected to find \"%s\" in actual:\n'%s'\n", matcher.expected[matcher.failedAtIndex], actual)
}
func (matcher *SliceMatcher) NegatedFailureMessage(actual interface{}) string {
return fmt.Sprintf("expected to not find \"%s\" in actual:\n'%s'\n", matcher.expected[matcher.failedAtIndex], actual)
}