1
0
mirror of https://github.com/aclindsa/ofxgo.git synced 2024-10-30 05:20:05 -04:00
ofxgo/request_test.go

58 lines
1.6 KiB
Go
Raw Normal View History

package ofxgo_test
import (
"github.com/aclindsa/ofxgo"
"regexp"
"strings"
"testing"
)
// match leading and trailing whitespace on each line
var ignoreSpacesRe = regexp.MustCompile("(?m)^[ \t]+|[ \t]*$[\r\n]+")
func marshalCheckRequest(t *testing.T, request *ofxgo.Request, expected string) {
2019-06-14 01:40:22 -04:00
t.Helper()
buf, err := request.Marshal()
if err != nil {
t.Fatalf("%s: Unexpected error marshalling request: %s\n", t.Name(), err)
}
actualString := buf.String()
// Ignore spaces between XML elements
expectedString := ignoreSpacesRe.ReplaceAllString(expected, "")
actualString = ignoreSpacesRe.ReplaceAllString(actualString, "")
if expectedString != actualString {
compareLength := len(expectedString)
if len(actualString) < compareLength {
compareLength = len(actualString)
}
for i := 0; i < compareLength; i++ {
if expectedString[i] != actualString[i] {
firstDifferencePosition := 13
displayStart := i - 10
prefix := "..."
suffix := "..."
if displayStart < 0 {
prefix = ""
firstDifferencePosition = i
displayStart = 0
}
displayEnd := displayStart + 40
if displayEnd > compareLength {
suffix = ""
displayEnd = compareLength
}
t.Fatalf("%s expected '%s%s%s',\ngot '%s%s%s'\n %s^ first difference\n", t.Name(), prefix, expectedString[displayStart:displayEnd], suffix, prefix, actualString[displayStart:displayEnd], suffix, strings.Repeat(" ", firstDifferencePosition))
}
}
if len(actualString) > compareLength {
t.Fatalf("%s: Actual string longer than expected string\n", t.Name())
} else {
t.Fatalf("%s: Actual string shorter than expected string\n", t.Name())
}
}
}