2017-03-29 05:51:12 -04:00
|
|
|
package ofxgo_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aclindsa/ofxgo"
|
|
|
|
"regexp"
|
2017-03-31 20:16:44 -04:00
|
|
|
"strings"
|
2017-03-29 05:51:12 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-06-14 21:20:08 -04:00
|
|
|
// match leading and trailing whitespace on each line
|
|
|
|
var ignoreSpacesRe = regexp.MustCompile("(?m)^[ \t]+|[ \t]*$[\r\n]+")
|
2017-03-29 05:51:12 -04:00
|
|
|
|
|
|
|
func marshalCheckRequest(t *testing.T, request *ofxgo.Request, expected string) {
|
2019-06-14 01:40:22 -04:00
|
|
|
t.Helper()
|
2017-03-29 05:51:12 -04:00
|
|
|
buf, err := request.Marshal()
|
|
|
|
if err != nil {
|
2017-03-30 07:04:54 -04:00
|
|
|
t.Fatalf("%s: Unexpected error marshalling request: %s\n", t.Name(), err)
|
2017-03-29 05:51:12 -04:00
|
|
|
}
|
|
|
|
actualString := buf.String()
|
|
|
|
|
|
|
|
// Ignore spaces between XML elements
|
2019-06-14 21:20:08 -04:00
|
|
|
expectedString := ignoreSpacesRe.ReplaceAllString(expected, "")
|
|
|
|
actualString = ignoreSpacesRe.ReplaceAllString(actualString, "")
|
2017-03-29 05:51:12 -04:00
|
|
|
|
|
|
|
if expectedString != actualString {
|
|
|
|
compareLength := len(expectedString)
|
|
|
|
if len(actualString) < compareLength {
|
|
|
|
compareLength = len(actualString)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < compareLength; i++ {
|
|
|
|
if expectedString[i] != actualString[i] {
|
2017-03-31 20:16:44 -04:00
|
|
|
firstDifferencePosition := 13
|
2017-03-29 05:51:12 -04:00
|
|
|
displayStart := i - 10
|
2017-03-31 20:16:44 -04:00
|
|
|
prefix := "..."
|
|
|
|
suffix := "..."
|
2017-03-29 05:51:12 -04:00
|
|
|
if displayStart < 0 {
|
2017-03-31 20:16:44 -04:00
|
|
|
prefix = ""
|
|
|
|
firstDifferencePosition = i
|
2017-03-29 05:51:12 -04:00
|
|
|
displayStart = 0
|
|
|
|
}
|
|
|
|
displayEnd := displayStart + 40
|
|
|
|
if displayEnd > compareLength {
|
2017-03-31 20:16:44 -04:00
|
|
|
suffix = ""
|
2017-03-29 05:51:12 -04:00
|
|
|
displayEnd = compareLength
|
|
|
|
}
|
2017-03-31 20:16:44 -04:00
|
|
|
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))
|
2017-03-29 05:51:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|