diff options
author | Elliott Sales de Andrade <[email protected]> | 2020-02-27 02:12:43 -0500 |
---|---|---|
committer | Ron Evans <[email protected]> | 2020-04-12 18:41:34 +0200 |
commit | 343bb426447c18251302585b094c5ceab776b079 (patch) | |
tree | f2ce21185c848c2554d12cd7276df192a9d1c267 /cgo | |
parent | bb5f7534e5451edfbeb2a2a0fa59ab5a3c77c672 (diff) | |
download | tinygo-343bb426447c18251302585b094c5ceab776b079.tar.gz tinygo-343bb426447c18251302585b094c5ceab776b079.zip |
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.
Diffstat (limited to 'cgo')
-rw-r--r-- | cgo/cgo_test.go | 18 | ||||
-rw-r--r-- | cgo/testdata/types.out.go | 52 |
2 files changed, 56 insertions, 14 deletions
diff --git a/cgo/cgo_test.go b/cgo/cgo_test.go index e9ae2f43e..5673ebad8 100644 --- a/cgo/cgo_test.go +++ b/cgo/cgo_test.go @@ -11,6 +11,7 @@ import ( "go/types" "io/ioutil" "path/filepath" + "regexp" "runtime" "strings" "testing" @@ -19,6 +20,21 @@ import ( // Pass -update to go test to update the output of the test files. var flagUpdate = flag.Bool("update", false, "Update images based on test output.") +// normalizeResult normalizes Go source code that comes out of tests across +// platforms and Go versions. +func normalizeResult(result string) string { + actual := strings.Replace(result, "\r\n", "\n", -1) + + // Make sure all functions are wrapped, even those that would otherwise be + // single-line functions. This is necessary because Go 1.14 changed the way + // such functions are wrapped and it's important to have consistent test + // results. + re := regexp.MustCompile(`func \((.+)\)( .*?) +{ (.+) }`) + actual = re.ReplaceAllString(actual, "func ($1)$2 {\n\t$3\n}") + + return actual +} + func TestCGo(t *testing.T) { var cflags = []string{"--target=armv6m-none-eabi"} @@ -74,7 +90,7 @@ func TestCGo(t *testing.T) { if err != nil { t.Errorf("could not write out CGo AST: %v", err) } - actual := strings.Replace(string(buf.Bytes()), "\r\n", "\n", -1) + actual := normalizeResult(string(buf.Bytes())) // Read the file with the expected output, to compare against. outfile := filepath.Join("testdata", name+".out.go") diff --git a/cgo/testdata/types.out.go b/cgo/testdata/types.out.go index 8a750080b..af638b9e7 100644 --- a/cgo/testdata/types.out.go +++ b/cgo/testdata/types.out.go @@ -62,12 +62,18 @@ type C.union3_t = C.union_1 type C.union_nested_t = C.union_3 type C.unionarray_t = struct{ arr [10]C.uchar } -func (s *C.struct_4) bitfield_a() C.uchar { return s.__bitfield_1 & 0x1f } -func (s *C.struct_4) set_bitfield_a(value C.uchar) { s.__bitfield_1 = s.__bitfield_1&^0x1f | value&0x1f<<0 } +func (s *C.struct_4) bitfield_a() C.uchar { + return s.__bitfield_1 & 0x1f +} +func (s *C.struct_4) set_bitfield_a(value C.uchar) { + s.__bitfield_1 = s.__bitfield_1&^0x1f | value&0x1f<<0 +} func (s *C.struct_4) bitfield_b() C.uchar { return s.__bitfield_1 >> 5 & 0x1 } -func (s *C.struct_4) set_bitfield_b(value C.uchar) { s.__bitfield_1 = s.__bitfield_1&^0x20 | value&0x1<<5 } +func (s *C.struct_4) set_bitfield_b(value C.uchar) { + s.__bitfield_1 = s.__bitfield_1&^0x20 | value&0x1<<5 +} func (s *C.struct_4) bitfield_c() C.uchar { return s.__bitfield_1 >> 6 } @@ -94,25 +100,45 @@ type C.struct_type1 struct { } type C.struct_type2 struct{ _type C.int } -func (union *C.union_1) unionfield_i() *C.int { return (*C.int)(unsafe.Pointer(&union.$union)) } -func (union *C.union_1) unionfield_d() *float64 { return (*float64)(unsafe.Pointer(&union.$union)) } -func (union *C.union_1) unionfield_s() *C.short { return (*C.short)(unsafe.Pointer(&union.$union)) } +func (union *C.union_1) unionfield_i() *C.int { + return (*C.int)(unsafe.Pointer(&union.$union)) +} +func (union *C.union_1) unionfield_d() *float64 { + return (*float64)(unsafe.Pointer(&union.$union)) +} +func (union *C.union_1) unionfield_s() *C.short { + return (*C.short)(unsafe.Pointer(&union.$union)) +} type C.union_1 struct{ $union uint64 } -func (union *C.union_2) unionfield_area() *C.point2d_t { return (*C.point2d_t)(unsafe.Pointer(&union.$union)) } -func (union *C.union_2) unionfield_solid() *C.point3d_t { return (*C.point3d_t)(unsafe.Pointer(&union.$union)) } +func (union *C.union_2) unionfield_area() *C.point2d_t { + return (*C.point2d_t)(unsafe.Pointer(&union.$union)) +} +func (union *C.union_2) unionfield_solid() *C.point3d_t { + return (*C.point3d_t)(unsafe.Pointer(&union.$union)) +} type C.union_2 struct{ $union [3]uint32 } -func (union *C.union_3) unionfield_point() *C.point3d_t { return (*C.point3d_t)(unsafe.Pointer(&union.$union)) } -func (union *C.union_3) unionfield_array() *C.unionarray_t { return (*C.unionarray_t)(unsafe.Pointer(&union.$union)) } -func (union *C.union_3) unionfield_thing() *C.union3_t { return (*C.union3_t)(unsafe.Pointer(&union.$union)) } +func (union *C.union_3) unionfield_point() *C.point3d_t { + return (*C.point3d_t)(unsafe.Pointer(&union.$union)) +} +func (union *C.union_3) unionfield_array() *C.unionarray_t { + return (*C.unionarray_t)(unsafe.Pointer(&union.$union)) +} +func (union *C.union_3) unionfield_thing() *C.union3_t { + return (*C.union3_t)(unsafe.Pointer(&union.$union)) +} type C.union_3 struct{ $union [2]uint64 } -func (union *C.union_union2d) unionfield_i() *C.int { return (*C.int)(unsafe.Pointer(&union.$union)) } -func (union *C.union_union2d) unionfield_d() *[2]float64 { return (*[2]float64)(unsafe.Pointer(&union.$union)) } +func (union *C.union_union2d) unionfield_i() *C.int { + return (*C.int)(unsafe.Pointer(&union.$union)) +} +func (union *C.union_union2d) unionfield_d() *[2]float64 { + return (*[2]float64)(unsafe.Pointer(&union.$union)) +} type C.union_union2d struct{ $union [2]uint64 } type C.enum_option C.int |