Skip to content

Commit df09dbd

Browse files
aykevldeadprogram
authored andcommitted
transform: restore previous test behavior
I think it's much nicer to have the test output inline in the source file, that way it's much easier to review any changes. For example, when escape analysis is improved this is visible with removed `// OUT` lines. This is similar to how LLVM writes its tests, and I like that style.
1 parent 134de98 commit df09dbd

4 files changed

Lines changed: 56 additions & 51 deletions

File tree

transform/allocs_test.go

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package transform_test
22

33
import (
4+
"fmt"
45
"go/token"
6+
"os"
57
"regexp"
68
"sort"
79
"strings"
@@ -49,23 +51,37 @@ func TestAllocs2(t *testing.T) {
4951
})
5052
sort.Slice(reports, func(i, j int) bool { return reports[i].pos.Line < reports[j].pos.Line })
5153

52-
// Render every report in each format and diff against its golden file.
53-
for _, format := range []struct {
54-
name string
55-
render func(report) string
56-
}{
57-
{"reason", func(r report) string { return transform.FormatAllocReason(r.pos, r.reason) }},
58-
{"cover", func(r report) string { return transform.FormatAllocCover(r.pos) }},
59-
} {
60-
t.Run(format.name, func(t *testing.T) {
61-
var got strings.Builder
62-
for _, r := range reports {
63-
if line := format.render(r); line != "" {
64-
got.WriteString(line)
65-
got.WriteByte('\n')
66-
}
67-
}
68-
checkGolden(t, goldenFile+"."+format.name, got.String())
69-
})
54+
// Load expected test output (the OUT: lines).
55+
testInput, err := os.ReadFile("./testdata/allocs2.go")
56+
if err != nil {
57+
t.Fatal("could not read test input:", err)
58+
}
59+
var expectedTestOutput strings.Builder
60+
for i, line := range strings.Split(strings.ReplaceAll(string(testInput), "\r\n", "\n"), "\n") {
61+
const prefix = " // OUT: "
62+
if idx := strings.Index(line, prefix); idx > 0 {
63+
msg := line[idx+len(prefix):]
64+
fmt.Fprintf(&expectedTestOutput, "allocs2.go:%d: %s\n", i+1, msg)
65+
}
66+
}
67+
68+
// Check whether the '// OUT' lines in allocs2.go match with the output we
69+
// got from the test.
70+
var actualTestOutput strings.Builder
71+
for _, r := range reports {
72+
fmt.Fprintf(&actualTestOutput, "allocs2.go:%d: %s\n", r.pos.Line, r.reason)
73+
}
74+
if actualTestOutput.String() != expectedTestOutput.String() {
75+
t.Errorf("expected:\n%s\nactual:\n%s", expectedTestOutput.String(), actualTestOutput.String())
76+
}
77+
78+
// Render the cover report and diff it against its golden file.
79+
var got strings.Builder
80+
for _, r := range reports {
81+
if line := transform.FormatAllocCover(r.pos); line != "" {
82+
got.WriteString(line)
83+
got.WriteByte('\n')
84+
}
7085
}
86+
checkGolden(t, goldenFile+".cover", got.String())
7187
}

transform/testdata/allocs2.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,33 @@ func main() {
2121
s3 := make([]int, 3)
2222
returnIntSlice(s3)
2323

24-
useSlice(make([]int, getUnknownNumber()))
24+
useSlice(make([]int, getUnknownNumber())) // OUT: size is not constant
2525

26-
s4 := make([]byte, 300)
26+
s4 := make([]byte, 300) // OUT: object size 300 exceeds maximum stack allocation size 256
2727
readByteSlice(s4)
2828

29-
s5 := make([]int, 4)
29+
s5 := make([]int, 4) // OUT: escapes at line 30
3030
_ = append(s5, 5)
3131

3232
s6 := make([]int, 3)
3333
s7 := []int{1, 2, 3}
3434
copySlice(s6, s7)
3535

36-
c1 := getComplex128()
36+
c1 := getComplex128() // OUT: escapes at line 37
3737
useInterface(c1)
3838

3939
n3 := 5
4040
func() int {
4141
return n3
4242
}()
4343

44-
callVariadic(3, 5, 8)
44+
callVariadic(3, 5, 8) // OUT: escapes at line 44
4545

46-
s8 := []int{3, 5, 8}
46+
s8 := []int{3, 5, 8} // OUT: escapes at line 47
4747
callVariadic(s8...)
4848

49-
n4 := 3
50-
n5 := 7
49+
n4 := 3 // OUT: escapes at line 51
50+
n5 := 7 // OUT: escapes at line 51
5151
func() {
5252
n4 = n5
5353
}()
@@ -104,14 +104,14 @@ func nonEscapingReturnedPointer() vector3 {
104104
var escapedSlice []int
105105

106106
func escapingReturnedSlice() {
107-
s := make([]int, 3)
107+
s := make([]int, 3) // OUT: escapes at line 108
108108
escapedSlice = returnIntSlice(s)
109109
}
110110

111111
var escapedVector3 *vector3
112112

113113
func escapingReturnedPointer() {
114-
b := vector3{4, 5, 6}
114+
b := vector3{4, 5, 6} // OUT: escapes at line 117
115115

116116
c := scaleVector3(&b, 0.5)
117117
escapedVector3 = c
@@ -125,7 +125,7 @@ func recursiveScaleVector3(vec *vector3, n int) *vector3 {
125125
}
126126

127127
func recursiveReturnedPointer() vector3 {
128-
b := vector3{4, 5, 6}
128+
b := vector3{4, 5, 6} // OUT: escapes at unknown line
129129

130130
c := recursiveScaleVector3(&b, 1)
131131
return *c
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
testdata/allocs2.go:24.1,24.43 1 0
2-
testdata/allocs2.go:26.1,26.25 1 0
3-
testdata/allocs2.go:29.1,29.22 1 0
4-
testdata/allocs2.go:36.1,36.23 1 0
5-
testdata/allocs2.go:44.1,44.23 1 0
6-
testdata/allocs2.go:46.1,46.22 1 0
7-
testdata/allocs2.go:49.1,49.9 1 0
8-
testdata/allocs2.go:50.1,50.9 1 0
9-
testdata/allocs2.go:107.1,107.21 1 0
10-
testdata/allocs2.go:114.1,114.23 1 0
11-
testdata/allocs2.go:128.1,128.23 1 0
1+
testdata/allocs2.go:24.1,24.72 1 0
2+
testdata/allocs2.go:26.1,26.91 1 0
3+
testdata/allocs2.go:29.1,29.49 1 0
4+
testdata/allocs2.go:36.1,36.50 1 0
5+
testdata/allocs2.go:44.1,44.50 1 0
6+
testdata/allocs2.go:46.1,46.49 1 0
7+
testdata/allocs2.go:49.1,49.36 1 0
8+
testdata/allocs2.go:50.1,50.36 1 0
9+
testdata/allocs2.go:107.1,107.49 1 0
10+
testdata/allocs2.go:114.1,114.51 1 0
11+
testdata/allocs2.go:128.1,128.55 1 0

transform/testdata/allocs2.out.reason

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)