@@ -11,6 +11,7 @@ import (
1111 "go/types"
1212 "io/ioutil"
1313 "path/filepath"
14+ "regexp"
1415 "runtime"
1516 "strings"
1617 "testing"
@@ -19,6 +20,34 @@ import (
1920// Pass -update to go test to update the output of the test files.
2021var flagUpdate = flag .Bool ("update" , false , "Update images based on test output." )
2122
23+ // normalizeResult normalizes test results across Go versions.
24+ // Currently, it does so by using consistent newlines, and wrapping some
25+ // function contents to be consistent with Go 1.14.
26+ func normalizeResult (result string ) string {
27+ actual := strings .Replace (result , "\r \n " , "\n " , - 1 )
28+
29+ // The bitfield getter is no longer aligned with setter, so remove spaces.
30+ re := regexp .MustCompile (`(bitfield_.\(\) C\.uchar) +{` )
31+ actual = re .ReplaceAllString (actual , "$1 {" )
32+
33+ // The bitfield setter is now wrapped.
34+ re = regexp .MustCompile (`(set_bitfield_.\(.+\)) +{ (.+) }` )
35+ actual = re .ReplaceAllString (actual , "$1 {\n \t $2\n }" )
36+
37+ // The unionfield getters are now wrapped, _except_ for single letter fields.
38+ re = regexp .MustCompile (`(unionfield_.{2,}\(\) +[^ ]+) +{ (.+) }` )
39+ actual = re .ReplaceAllString (actual , "$1 {\n \t $2\n }" )
40+
41+ // In union_union2d, the unionfield_i getter is no longer aligned with unionfield_d,
42+ // which is now wrapped. This does not affect similarly named field in other unions.
43+ re = regexp .MustCompile (`(union_union2d\) unionfield_i\(\) +[^ ]+) +{` )
44+ actual = re .ReplaceAllString (actual , "$1 {" )
45+ re = regexp .MustCompile (`(union_union2d\) unionfield_d\(\) +[^ ]+) +{ (.+) }` )
46+ actual = re .ReplaceAllString (actual , "$1 {\n \t $2\n }" )
47+
48+ return actual
49+ }
50+
2251func TestCGo (t * testing.T ) {
2352 var cflags = []string {"--target=armv6m-none-eabi" }
2453
@@ -74,7 +103,7 @@ func TestCGo(t *testing.T) {
74103 if err != nil {
75104 t .Errorf ("could not write out CGo AST: %v" , err )
76105 }
77- actual := strings . Replace (string (buf .Bytes ()), " \r \n " , " \n " , - 1 )
106+ actual := normalizeResult (string (buf .Bytes ()))
78107
79108 // Read the file with the expected output, to compare against.
80109 outfile := filepath .Join ("testdata" , name + ".out.go" )
0 commit comments