forked from golang/playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsandbox_test.go
233 lines (190 loc) · 5.51 KB
/
sandbox_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"go/token"
"os"
"os/exec"
"reflect"
"runtime"
"strings"
"testing"
)
// TestExperiments tests that experiment lines are recognized.
func TestExperiments(t *testing.T) {
var tests = []struct {
src string
exp []string
}{
{"//GOEXPERIMENT=active\n\npackage main", []string{"active"}},
{" // GOEXPERIMENT= active \n\npackage main", []string{"active"}},
{" // GOEXPERIMENT= active \n\npackage main", []string{"active"}},
{" // GOEXPERIMENT = active \n\npackage main", []string{"active"}},
{"//GOEXPERIMENT=foo\n\n// GOEXPERIMENT=bar\n\npackage main", []string{"foo", "bar"}},
{"/* hello world */\n// GOEXPERIMENT=ignored\n", nil},
{"package main\n// GOEXPERIMENT=ignored\n", nil},
}
for _, tt := range tests {
if exp := experiments(tt.src); !reflect.DeepEqual(exp, tt.exp) {
t.Errorf("experiments(%q) = %q, want %q", tt.src, exp, tt.exp)
}
}
}
// TestIsTest verifies that the isTest helper function matches
// exactly (and only) the names of functions recognized as tests.
func TestIsTest(t *testing.T) {
cmd := exec.Command(os.Args[0], "-test.list=.")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, out)
}
t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
isTestFunction := map[string]bool{}
lines := strings.Split(string(out), "\n")
for _, line := range lines {
isTestFunction[strings.TrimSpace(line)] = true
}
for _, tc := range []struct {
prefix string
f interface{}
want bool
}{
{"Test", Test, true},
{"Test", TestIsTest, true},
{"Test", Test1IsATest, true},
{"Test", TestÑIsATest, true},
{"Test", TestisNotATest, false},
{"Example", Example, true},
{"Example", ExampleTest, true},
{"Example", Example_isAnExample, true},
{"Example", ExampleTest_isAnExample, true},
// Example_noOutput has a valid example function name but lacks an output
// declaration, but the isTest function operates only on the test name
// so it cannot detect that the function is not a test.
{"Example", Example1IsAnExample, true},
{"Example", ExampleisNotAnExample, false},
{"Benchmark", Benchmark, true},
{"Benchmark", BenchmarkNop, true},
{"Benchmark", Benchmark1IsABenchmark, true},
{"Benchmark", BenchmarkisNotABenchmark, false},
{"Fuzz", Fuzz, true},
{"Fuzz", Fuzz1IsAFuzz, true},
{"Fuzz", FuzzÑIsAFuzz, true},
{"Fuzz", FuzzisNotAFuzz, false},
} {
name := nameOf(t, tc.f)
t.Run(name, func(t *testing.T) {
if tc.want != isTestFunction[name] {
t.Fatalf(".want (%v) is inconsistent with -test.list", tc.want)
}
if !strings.HasPrefix(name, tc.prefix) {
t.Fatalf("%q is not a prefix of %v", tc.prefix, name)
}
got := isTest(name, tc.prefix)
if got != tc.want {
t.Errorf(`isTest(%q, %q) = %v; want %v`, name, tc.prefix, got, tc.want)
}
})
}
}
// nameOf returns the runtime-reported name of function f.
func nameOf(t *testing.T, f interface{}) string {
t.Helper()
v := reflect.ValueOf(f)
if v.Kind() != reflect.Func {
t.Fatalf("%v is not a function", f)
}
rf := runtime.FuncForPC(v.Pointer())
if rf == nil {
t.Fatalf("%v.Pointer() is not a known function", f)
}
fullName := rf.Name()
parts := strings.Split(fullName, ".")
name := parts[len(parts)-1]
if !token.IsIdentifier(name) {
t.Fatalf("%q is not a valid identifier", name)
}
return name
}
// TestisNotATest is not a test function, despite appearances.
//
// Please ignore any lint or vet warnings for this function.
func TestisNotATest(t *testing.T) {
panic("This is not a valid test function.")
}
// Test1IsATest is a valid test function.
func Test1IsATest(t *testing.T) {
}
// Test is a test with a minimal name.
func Test(t *testing.T) {
}
// TestÑIsATest is a test with an interesting Unicode name.
func TestÑIsATest(t *testing.T) {
}
func Example() {
// Output:
}
func ExampleTest() {
// This is an example for the function Test.
// ❤ recursion.
Test(nil)
// Output:
}
func Example1IsAnExample() {
// Output:
}
// ExampleisNotAnExample is not an example function, despite appearances.
//
// Please ignore any lint or vet warnings for this function.
func ExampleisNotAnExample() {
panic("This is not a valid example function.")
// Output:
// None. (This is not really an example function.)
}
func Example_isAnExample() {
// Output:
}
func ExampleTest_isAnExample() {
Test(nil)
// Output:
}
func Example_noOutput() {
// No output declared: should be compiled but not run.
}
func Benchmark(b *testing.B) {
for i := 0; i < b.N; i++ {
}
}
func BenchmarkNop(b *testing.B) {
for i := 0; i < b.N; i++ {
}
}
func Benchmark1IsABenchmark(b *testing.B) {
for i := 0; i < b.N; i++ {
}
}
// BenchmarkisNotABenchmark is not a benchmark function, despite appearances.
//
// Please ignore any lint or vet warnings for this function.
func BenchmarkisNotABenchmark(b *testing.B) {
panic("This is not a valid benchmark function.")
}
// FuzzisNotAFuzz is not a fuzz test function, despite appearances.
//
// Please ignore any lint or vet warnings for this function.
func FuzzisNotAFuzz(f *testing.F) {
panic("This is not a valid fuzzing function.")
}
// Fuzz1IsAFuzz is a valid fuzz function.
func Fuzz1IsAFuzz(f *testing.F) {
f.Skip()
}
// Fuzz is a fuzz with a minimal name.
func Fuzz(f *testing.F) {
f.Skip()
}
// FuzzÑIsAFuzz is a fuzz with an interesting Unicode name.
func FuzzÑIsAFuzz(f *testing.F) {
f.Skip()
}