github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/testhelpers/matchers/contain_substrings.go (about) 1 package matchers 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/cloudfoundry/cli/cf/terminal" 8 "github.com/onsi/gomega" 9 ) 10 11 type SliceMatcher struct { 12 expected [][]string 13 failedAtIndex int 14 } 15 16 func ContainSubstrings(substrings ...[]string) gomega.OmegaMatcher { 17 return &SliceMatcher{expected: substrings} 18 } 19 20 //func (matcher *SliceMatcher) Match(actual interface{}) (success bool, err error) { 21 // actualStrings, ok := actual.([]string) 22 // if !ok { 23 // return false, nil 24 // } 25 // 26 // matcher.failedAtIndex = 0 27 // for _, actualValue := range actualStrings { 28 // allStringsFound := true 29 // for _, expectedValue := range matcher.expected[matcher.failedAtIndex] { 30 // allStringsFound = allStringsFound && strings.Contains(terminal.Decolorize(actualValue), expectedValue) 31 // } 32 // 33 // if allStringsFound { 34 // matcher.failedAtIndex++ 35 // if matcher.failedAtIndex == len(matcher.expected) { 36 // matcher.failedAtIndex-- 37 // return true, nil 38 // } 39 // } 40 // } 41 // 42 // return false, nil 43 //} 44 45 func (matcher *SliceMatcher) Match(actual interface{}) (success bool, err error) { 46 actualStrings, ok := actual.([]string) 47 if !ok { 48 return false, nil 49 } 50 51 allStringsMatched := make([]bool, len(matcher.expected)) 52 53 for index, expectedArray := range matcher.expected { 54 for _, actualValue := range actualStrings { 55 56 allStringsFound := true 57 58 for _, expectedValue := range expectedArray { 59 allStringsFound = allStringsFound && strings.Contains(terminal.Decolorize(actualValue), expectedValue) 60 } 61 62 if allStringsFound { 63 allStringsMatched[index] = true 64 break 65 } 66 } 67 } 68 69 for index, value := range allStringsMatched { 70 if !value { 71 matcher.failedAtIndex = index 72 return false, nil 73 } 74 } 75 76 return true, nil 77 } 78 79 func (matcher *SliceMatcher) FailureMessage(actual interface{}) string { 80 actualStrings, ok := actual.([]string) 81 if !ok { 82 return fmt.Sprintf("Expected actual to be a slice of strings, but it's actually a %T", actual) 83 } 84 85 return fmt.Sprintf("expected to find \"%s\" in actual:\n'%s'\n", matcher.expected[matcher.failedAtIndex], strings.Join(actualStrings, "\n")) 86 } 87 88 func (matcher *SliceMatcher) NegatedFailureMessage(actual interface{}) string { 89 actualStrings, ok := actual.([]string) 90 if !ok { 91 return fmt.Sprintf("Expected actual to be a slice of strings, but it's actually a %T", actual) 92 } 93 return fmt.Sprintf("expected to not find \"%s\" in actual:\n'%s'\n", matcher.expected[matcher.failedAtIndex], strings.Join(actualStrings, "\n")) 94 }