github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/util/testhelpers/matchers/contain_substrings.go (about)

     1  package matchers
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/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  	allStringsMatched := make([]bool, len(matcher.expected))
    27  
    28  	for index, expectedArray := range matcher.expected {
    29  		for _, actualValue := range actualStrings {
    30  
    31  			allStringsFound := true
    32  
    33  			for _, expectedValue := range expectedArray {
    34  				allStringsFound = allStringsFound && strings.Contains(terminal.Decolorize(actualValue), expectedValue)
    35  			}
    36  
    37  			if allStringsFound {
    38  				allStringsMatched[index] = true
    39  				break
    40  			}
    41  		}
    42  	}
    43  
    44  	for index, value := range allStringsMatched {
    45  		if !value {
    46  			matcher.failedAtIndex = index
    47  			return false, nil
    48  		}
    49  	}
    50  
    51  	return true, nil
    52  }
    53  
    54  func (matcher *SliceMatcher) FailureMessage(actual interface{}) string {
    55  	actualStrings, ok := actual.([]string)
    56  	if !ok {
    57  		return fmt.Sprintf("Expected actual to be a slice of strings, but it's actually a %T", actual)
    58  	}
    59  
    60  	return fmt.Sprintf("expected to find \"%s\" in actual:\n'%s'\n", matcher.expected[matcher.failedAtIndex], strings.Join(actualStrings, "\n"))
    61  }
    62  
    63  func (matcher *SliceMatcher) NegatedFailureMessage(actual interface{}) string {
    64  	actualStrings, ok := actual.([]string)
    65  	if !ok {
    66  		return fmt.Sprintf("Expected actual to be a slice of strings, but it's actually a %T", actual)
    67  	}
    68  	return fmt.Sprintf("expected to not find \"%s\" in actual:\n'%s'\n", matcher.expected[matcher.failedAtIndex], strings.Join(actualStrings, "\n"))
    69  }