Skip to content

Commit 343bb42

Browse files
QuLogicdeadprogram
authored andcommitted
cgo: normalize test results
This makes the result consistent across Go versions, by running a regex on the CGo output that wraps all single-line functions in a consistent way. Originally written by Elliott Sales de Andrade and modified by Ayke van Laethem.
1 parent bb5f753 commit 343bb42

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

cgo/cgo_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,21 @@ import (
1920
// Pass -update to go test to update the output of the test files.
2021
var flagUpdate = flag.Bool("update", false, "Update images based on test output.")
2122

23+
// normalizeResult normalizes Go source code that comes out of tests across
24+
// platforms and Go versions.
25+
func normalizeResult(result string) string {
26+
actual := strings.Replace(result, "\r\n", "\n", -1)
27+
28+
// Make sure all functions are wrapped, even those that would otherwise be
29+
// single-line functions. This is necessary because Go 1.14 changed the way
30+
// such functions are wrapped and it's important to have consistent test
31+
// results.
32+
re := regexp.MustCompile(`func \((.+)\)( .*?) +{ (.+) }`)
33+
actual = re.ReplaceAllString(actual, "func ($1)$2 {\n\t$3\n}")
34+
35+
return actual
36+
}
37+
2238
func TestCGo(t *testing.T) {
2339
var cflags = []string{"--target=armv6m-none-eabi"}
2440

@@ -74,7 +90,7 @@ func TestCGo(t *testing.T) {
7490
if err != nil {
7591
t.Errorf("could not write out CGo AST: %v", err)
7692
}
77-
actual := strings.Replace(string(buf.Bytes()), "\r\n", "\n", -1)
93+
actual := normalizeResult(string(buf.Bytes()))
7894

7995
// Read the file with the expected output, to compare against.
8096
outfile := filepath.Join("testdata", name+".out.go")

cgo/testdata/types.out.go

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,18 @@ type C.union3_t = C.union_1
6262
type C.union_nested_t = C.union_3
6363
type C.unionarray_t = struct{ arr [10]C.uchar }
6464

65-
func (s *C.struct_4) bitfield_a() C.uchar { return s.__bitfield_1 & 0x1f }
66-
func (s *C.struct_4) set_bitfield_a(value C.uchar) { s.__bitfield_1 = s.__bitfield_1&^0x1f | value&0x1f<<0 }
65+
func (s *C.struct_4) bitfield_a() C.uchar {
66+
return s.__bitfield_1 & 0x1f
67+
}
68+
func (s *C.struct_4) set_bitfield_a(value C.uchar) {
69+
s.__bitfield_1 = s.__bitfield_1&^0x1f | value&0x1f<<0
70+
}
6771
func (s *C.struct_4) bitfield_b() C.uchar {
6872
return s.__bitfield_1 >> 5 & 0x1
6973
}
70-
func (s *C.struct_4) set_bitfield_b(value C.uchar) { s.__bitfield_1 = s.__bitfield_1&^0x20 | value&0x1<<5 }
74+
func (s *C.struct_4) set_bitfield_b(value C.uchar) {
75+
s.__bitfield_1 = s.__bitfield_1&^0x20 | value&0x1<<5
76+
}
7177
func (s *C.struct_4) bitfield_c() C.uchar {
7278
return s.__bitfield_1 >> 6
7379
}
@@ -94,25 +100,45 @@ type C.struct_type1 struct {
94100
}
95101
type C.struct_type2 struct{ _type C.int }
96102

97-
func (union *C.union_1) unionfield_i() *C.int { return (*C.int)(unsafe.Pointer(&union.$union)) }
98-
func (union *C.union_1) unionfield_d() *float64 { return (*float64)(unsafe.Pointer(&union.$union)) }
99-
func (union *C.union_1) unionfield_s() *C.short { return (*C.short)(unsafe.Pointer(&union.$union)) }
103+
func (union *C.union_1) unionfield_i() *C.int {
104+
return (*C.int)(unsafe.Pointer(&union.$union))
105+
}
106+
func (union *C.union_1) unionfield_d() *float64 {
107+
return (*float64)(unsafe.Pointer(&union.$union))
108+
}
109+
func (union *C.union_1) unionfield_s() *C.short {
110+
return (*C.short)(unsafe.Pointer(&union.$union))
111+
}
100112

101113
type C.union_1 struct{ $union uint64 }
102114

103-
func (union *C.union_2) unionfield_area() *C.point2d_t { return (*C.point2d_t)(unsafe.Pointer(&union.$union)) }
104-
func (union *C.union_2) unionfield_solid() *C.point3d_t { return (*C.point3d_t)(unsafe.Pointer(&union.$union)) }
115+
func (union *C.union_2) unionfield_area() *C.point2d_t {
116+
return (*C.point2d_t)(unsafe.Pointer(&union.$union))
117+
}
118+
func (union *C.union_2) unionfield_solid() *C.point3d_t {
119+
return (*C.point3d_t)(unsafe.Pointer(&union.$union))
120+
}
105121

106122
type C.union_2 struct{ $union [3]uint32 }
107123

108-
func (union *C.union_3) unionfield_point() *C.point3d_t { return (*C.point3d_t)(unsafe.Pointer(&union.$union)) }
109-
func (union *C.union_3) unionfield_array() *C.unionarray_t { return (*C.unionarray_t)(unsafe.Pointer(&union.$union)) }
110-
func (union *C.union_3) unionfield_thing() *C.union3_t { return (*C.union3_t)(unsafe.Pointer(&union.$union)) }
124+
func (union *C.union_3) unionfield_point() *C.point3d_t {
125+
return (*C.point3d_t)(unsafe.Pointer(&union.$union))
126+
}
127+
func (union *C.union_3) unionfield_array() *C.unionarray_t {
128+
return (*C.unionarray_t)(unsafe.Pointer(&union.$union))
129+
}
130+
func (union *C.union_3) unionfield_thing() *C.union3_t {
131+
return (*C.union3_t)(unsafe.Pointer(&union.$union))
132+
}
111133

112134
type C.union_3 struct{ $union [2]uint64 }
113135

114-
func (union *C.union_union2d) unionfield_i() *C.int { return (*C.int)(unsafe.Pointer(&union.$union)) }
115-
func (union *C.union_union2d) unionfield_d() *[2]float64 { return (*[2]float64)(unsafe.Pointer(&union.$union)) }
136+
func (union *C.union_union2d) unionfield_i() *C.int {
137+
return (*C.int)(unsafe.Pointer(&union.$union))
138+
}
139+
func (union *C.union_union2d) unionfield_d() *[2]float64 {
140+
return (*[2]float64)(unsafe.Pointer(&union.$union))
141+
}
116142

117143
type C.union_union2d struct{ $union [2]uint64 }
118144
type C.enum_option C.int

0 commit comments

Comments
 (0)