github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/internal/test/strings.go (about)

     1  package test
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"gotest.tools/v3/assert"
     8  	is "gotest.tools/v3/assert/cmp"
     9  )
    10  
    11  // CompareMultipleValues compares comma-separated values, whatever the order is
    12  func CompareMultipleValues(t *testing.T, value, expected string) {
    13  	// comma-separated values means probably a map input, which won't
    14  	// be guaranteed to have the same order as our expected value
    15  	// We'll create maps and use reflect.DeepEquals to check instead:
    16  	entriesMap := make(map[string]string)
    17  	for _, entry := range strings.Split(value, ",") {
    18  		k, v, _ := strings.Cut(entry, "=")
    19  		entriesMap[k] = v
    20  	}
    21  	expMap := make(map[string]string)
    22  	for _, exp := range strings.Split(expected, ",") {
    23  		k, v, _ := strings.Cut(exp, "=")
    24  		expMap[k] = v
    25  	}
    26  	assert.Check(t, is.DeepEqual(expMap, entriesMap))
    27  }