forked from aclindsa/ofxgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.go
56 lines (49 loc) · 1.6 KB
/
request_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ofxgo
import (
"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 *Request, expected string) {
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())
}
}
}