-
Notifications
You must be signed in to change notification settings - Fork 15
/
trace_test.go
117 lines (95 loc) · 1.99 KB
/
trace_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"fmt"
"log"
"regexp"
"testing"
)
func init() {
setup = fmt.Sprintf(setup, prefix, formatLength)
var err error
filter, err = regexp.Compile(filterFlag)
if err != nil {
log.Fatal(err)
}
}
var (
testSrc1 = []byte(`package none
func testFunc1(a, b string) string {
return a + b
}`)
testSrc2 = []byte(`package none
func testFunc2(a, _ string) int {
return 1
}`)
testSrc3 = []byte(`package none
func testFunc3(a ...interface{}) bool {
return true
}`)
testSrc4 = []byte(`package none
func testFunc4(a, b int) {return false}`)
testSrc5 = []byte(`package none
func testFunc5(a, b int) {
func(a int) {
a = b
}(a)
}`)
)
func annotateTest(source []byte, t *testing.T) []byte {
processed, err := annotate("test.go", source)
if err != nil {
fmt.Println(string(processed))
t.Fatal(err)
}
t.Log(string(processed))
return processed
}
func returnsOn() func() {
prev := showReturn
showReturn = true
return func() {
showReturn = prev
}
}
func timingOn() func() {
prevT := timing
timing = true
prevR := showReturn
showReturn = true
return func() {
timing = prevT
showReturn = prevR
}
}
// TODO: Use go/types to further check the output, since we don't execute the
// new source.
// I'll add that once go/types is moved into the main repo
// Check the syntax on a basic function
func TestBasic(t *testing.T) {
annotateTest(testSrc1, t)
}
// Check that we don't fail on an un-named argument
func TestUnderscore(t *testing.T) {
annotateTest(testSrc2, t)
}
// We should handle variadic just fine
func TestVariadic(t *testing.T) {
annotateTest(testSrc3, t)
}
// Make sure we can handle improperly formatted source
func TestUnFmted(t *testing.T) {
annotateTest(testSrc4, t)
}
// test output with return logging
func TestReturns(t *testing.T) {
defer returnsOn()()
annotateTest(testSrc1, t)
}
func TestTiming(t *testing.T) {
defer timingOn()()
annotateTest(testSrc4, t)
}
func TestEmbedded(t *testing.T) {
defer timingOn()()
annotateTest(testSrc5, t)
}