You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.2 KiB
60 lines
1.2 KiB
1 year ago
|
package integrationtest
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
const WILDCARD = "[WILDCARD]"
|
||
|
|
||
|
func AssertMatchesText(t *testing.T, expected, actual string) {
|
||
|
if !strings.Contains(expected, WILDCARD) {
|
||
|
assert.Equal(t, expected, actual)
|
||
|
} else if !wildcardMatch(expected, actual) {
|
||
|
t.Errorf("texts do not match. expected \"%s\", got \"%s\"", expected, actual)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func wildcardMatch(expected, actual string) bool {
|
||
|
return patternMatch(expected, actual, WILDCARD)
|
||
|
}
|
||
|
|
||
|
func patternMatch(expected, actual, pattern string) bool {
|
||
|
if expected == pattern {
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
parts := strings.Split(expected, pattern)
|
||
|
if len(parts) == 1 {
|
||
|
return expected == actual
|
||
|
}
|
||
|
|
||
|
if !strings.HasPrefix(actual, parts[0]) {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
if lines := strings.Split(expected, "\n"); len(lines) > 0 && lines[0] == pattern {
|
||
|
actual = "\n" + actual
|
||
|
}
|
||
|
|
||
|
t := []string{actual[:len(parts[0])], actual[len(parts[0]):]}
|
||
|
for i, part := range parts {
|
||
|
if i == 0 {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if i == len(parts)-1 && (part == "" || part == "\n") {
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
if found := strings.Index(t[1], part); found != -1 {
|
||
|
t = []string{t[1][:found+len(part)], t[1][found+len(part):]}
|
||
|
} else {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
return t[1] == ""
|
||
|
}
|