Debian Codegraph @

testing/functest/functest.go

raw symbols SCIP
1 /*
2 Copyright 2016 The go4.org Authors
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 // Package functest contains utilities to ease writing table-driven
18 // tests for pure functions and method.
19 //
20 // Example:
21 //
22 // func square(v int) int { return v * v }
23 //
24 // func TestFunc(t *testing.T) {
25 // f := functest.New(square)
26 // f.Test(t,
27 // f.In(0).Want(0),
28 // f.In(1).Want(1),
29 // f.In(2).Want(4),
30 // f.In(3).Want(9),
31 // )
32 // }
33 //
34 // It can test whether things panic:
35 //
36 // f := functest.New(condPanic)
37 // f.Test(t,
38 // f.In(false, nil),
39 // f.In(true, "boom").Check(func(res functest.Result) error {
40 // if res.Panic != "boom" {
41 // return fmt.Errorf("panic = %v; want boom", res.Panic)
42 // }
43 // return nil
44 // }),
45 // f.In(true, nil).Check(func(res functest.Result) error {
46 // if res.Panic != nil || res.Paniked {
47 // return fmt.Errorf("expected panic with nil value, got: %+v", res)
48 // }
49 // return nil
50 // }),
51 // )
52 //
53 // If a test fails, functest does its best to format a useful error message. You can also
54 // name test cases:
55 //
56 // f := functest.New(square)
57 // f.Test(t,
58 // f.In(0).Want(0),
59 // f.In(1).Want(111),
60 // f.In(2).Want(4),
61 // f.Case("three").In(3).Want(999),
62 // )
63 //
64 // Which would fail like:
65 //
66 // --- FAIL: TestSquare (0.00s)
67 // functest.go:304: square(1) = 1; want 111
68 // functest.go:304: three: square(3) = 9; want 999
69 // FAIL
70 //
71 package functestscip-go gomod go4.org . `go4.org/testing/functest`/
72
73 import (
74 "bytesscip-go gomod github.com/golang/go/src go1.13 bytes/"
75 "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/"
76 "reflectscip-go gomod github.com/golang/go/src go1.13 reflect/"
77 "runtimescip-go gomod github.com/golang/go/src go1.13 runtime/"
78 "stringsscip-go gomod github.com/golang/go/src go1.13 strings/"
79 "testingscip-go gomod github.com/golang/go/src go1.13 testing/"
80 )
81
82 // Func is a wrapper around a func to test.
83 // It must be created with New.
84 type Funcscip-go gomod go4.org . `go4.org/testing/functest`/Func# struct {
85 // Name is the name of the function to use in error messages.
86 // In most cases it is initialized by New, unless the function
87 // being tested is an anonymous function.
88 Namescip-go gomod go4.org . `go4.org/testing/functest`/Func#Name. string
89
90 fscip-go gomod go4.org . `go4.org/testing/functest`/Func#f. interface{} // the func
91 fvscip-go gomod go4.org . `go4.org/testing/functest`/Func#fv. reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Valuescip-go gomod github.com/golang/go/src go1.13 reflect/Value# // of f
92 }
93
94 var removePuncscip-go gomod go4.org . `go4.org/testing/functest`/removePunc. = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.NewReplacerscip-go gomod github.com/golang/go/src go1.13 strings/NewReplacer().("(", "", ")", "", "*", "")
95
96 // New wraps a function for testing.
97 // The provided value f must be a function or method.
98 func Newscip-go gomod go4.org . `go4.org/testing/functest`/New().(f interface{}) *Funcscip-go gomod go4.org . `go4.org/testing/functest`/Func# {
99 fv := reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.13 reflect/ValueOf().(f)
100 if fv.Kindscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Funcscip-go gomod github.com/golang/go/src go1.13 reflect/Func. {
101 panic("argument to New must be a func")
102 }
103 var name string
104 rf := runtimescip-go gomod github.com/golang/go/src go1.13 runtime/.FuncForPCscip-go gomod github.com/golang/go/src go1.13 runtime/FuncForPC().(fv.Pointerscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Pointer().())
105 if rf != nil {
106 name = rf.Namescip-go gomod github.com/golang/go/src go1.13 runtime/Func#Name().()
107 if methType := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.13 strings/LastIndex().(name, ".("); methType != -1 {
108 name = removePuncscip-go gomod go4.org . `go4.org/testing/functest`/removePunc..Replacescip-go gomod github.com/golang/go/src go1.13 strings/Replacer#Replace().(name[methType+2:])
109 } else if lastDot := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.13 strings/LastIndex().(name, "."); lastDot != -1 {
110 name = name[lastDot+1:]
111 if stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.13 strings/HasPrefix().(name, "func") {
112 // Looks like some anonymous function. Prefer naming it "f".
113 name = "f"
114 }
115 }
116 } else {
117 name = "f"
118 }
119
120 return &Funcscip-go gomod go4.org . `go4.org/testing/functest`/Func#{
121 fscip-go gomod go4.org . `go4.org/testing/functest`/Func#f.: f,
122 fvscip-go gomod go4.org . `go4.org/testing/functest`/Func#fv.: fv,
123 Namescip-go gomod go4.org . `go4.org/testing/functest`/Func#Name.: name,
124 }
125 }
126
127 // Result is the result of a function call, for use with Check.
128 type Resultscip-go gomod go4.org . `go4.org/testing/functest`/Result# struct {
129 // Result is the return value(s) of the function.
130 Resultscip-go gomod go4.org . `go4.org/testing/functest`/Result#Result. []interface{}
131
132 // Panic is the panic value of the function.
133 Panicscip-go gomod go4.org . `go4.org/testing/functest`/Result#Panic. interface{}
134
135 // Panicked is whether the function paniced.
136 // It can be used to determine whether a function
137 // called panic(nil).
138 Panickedscip-go gomod go4.org . `go4.org/testing/functest`/Result#Panicked. bool
139 }
140
141 // Case is a test case to run.
142 //
143 // Test cases can be either named or unnamed, depending on how they're
144 // created. Naming cases is optional; all failures messages aim to
145 // have useful output and include the input to the function.
146 //
147 // Unless the function's arity is zero, all cases should have their input
148 // set with In.
149 //
150 // The case's expected output can be set with Want and/or Check.
151 type Casescip-go gomod go4.org . `go4.org/testing/functest`/Case# struct {
152 fscip-go gomod go4.org . `go4.org/testing/functest`/Case#f. *Funcscip-go gomod go4.org . `go4.org/testing/functest`/Func#
153 inscip-go gomod go4.org . `go4.org/testing/functest`/Case#in. []interface{}
154 namescip-go gomod go4.org . `go4.org/testing/functest`/Case#name. string // optional
155 wantscip-go gomod go4.org . `go4.org/testing/functest`/Case#want. []interface{} // non-nil if we check args
156 checkResscip-go gomod go4.org . `go4.org/testing/functest`/Case#checkRes. []func(Resultscip-go gomod go4.org . `go4.org/testing/functest`/Result#) error
157 }
158
159 // Case returns a new named case. It should be modified before use.
160 func (f *Funcscip-go gomod go4.org . `go4.org/testing/functest`/Func#) Casescip-go gomod go4.org . `go4.org/testing/functest`/Func#Case().(name string) *Casescip-go gomod go4.org . `go4.org/testing/functest`/Case# {
161 return &Casescip-go gomod go4.org . `go4.org/testing/functest`/Case#{fscip-go gomod go4.org . `go4.org/testing/functest`/Case#f.: f, namescip-go gomod go4.org . `go4.org/testing/functest`/Case#name.: name}
162 }
163
164 // In returns a new unnamed test case. It will be identified by its arguments
165 // only.
166 func (f *Funcscip-go gomod go4.org . `go4.org/testing/functest`/Func#) Inscip-go gomod go4.org . `go4.org/testing/functest`/Func#In().(args ...interface{}) *Casescip-go gomod go4.org . `go4.org/testing/functest`/Case# {
167 return &Casescip-go gomod go4.org . `go4.org/testing/functest`/Case#{fscip-go gomod go4.org . `go4.org/testing/functest`/Case#f.: f, inscip-go gomod go4.org . `go4.org/testing/functest`/Case#in.: args}
168 }
169
170 // In sets the arguments of c used to call f.
171 func (c *Casescip-go gomod go4.org . `go4.org/testing/functest`/Case#) Inscip-go gomod go4.org . `go4.org/testing/functest`/Case#In().(args ...interface{}) *Casescip-go gomod go4.org . `go4.org/testing/functest`/Case# {
172 c.inscip-go gomod go4.org . `go4.org/testing/functest`/Case#in. = args
173 return c
174 }
175
176 // Want sets the expected result values of the test case.
177 // Want modifies and returns c.
178 // Callers my use both Want and Check.
179 func (c *Casescip-go gomod go4.org . `go4.org/testing/functest`/Case#) Wantscip-go gomod go4.org . `go4.org/testing/functest`/Case#Want().(result ...interface{}) *Casescip-go gomod go4.org . `go4.org/testing/functest`/Case# {
180 if c.wantscip-go gomod go4.org . `go4.org/testing/functest`/Case#want. != nil {
181 panic("duplicate Want declared on functest.Case")
182 }
183 c.wantscip-go gomod go4.org . `go4.org/testing/functest`/Case#want. = result
184 numOut := c.fscip-go gomod go4.org . `go4.org/testing/functest`/Case#f..fvscip-go gomod go4.org . `go4.org/testing/functest`/Func#fv..Typescip-go gomod github.com/golang/go/src go1.13 reflect/Value#Type().().NumOutscip-go gomod github.com/golang/go/src go1.13 reflect/Type#NumOut().()
185 if len(result) != numOut {
186 // TODO: let caller providing only interesting result values, or
187 // provide matchers.
188 panic(fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().("Want called with %d values; function returns %d values", len(result), numOut))
189 }
190 return c
191 }
192
193 // Check adds a function to check the result of the case's function
194 // call. It is a low-level function when Want is insufficient.
195 // For instance, it allows checking whether a function panics.
196 // If no checker functions are registered, function panics are considered
197 // a test failure.
198 //
199 // Check modifies and returns c.
200 // Callers my use both Want and Check, and may use Check multiple times.
201 func (c *Casescip-go gomod go4.org . `go4.org/testing/functest`/Case#) Checkscip-go gomod go4.org . `go4.org/testing/functest`/Case#Check().(checker func(Resultscip-go gomod go4.org . `go4.org/testing/functest`/Result#) error) *Casescip-go gomod go4.org . `go4.org/testing/functest`/Case# {
202 c.checkResscip-go gomod go4.org . `go4.org/testing/functest`/Case#checkRes. = append(c.checkResscip-go gomod go4.org . `go4.org/testing/functest`/Case#checkRes., checker)
203 return c
204 }
205
206 // Test runs the provided test cases against f.
207 // If any test cases fail, t.Errorf is called.
208 func (f *Funcscip-go gomod go4.org . `go4.org/testing/functest`/Func#) Testscip-go gomod go4.org . `go4.org/testing/functest`/Func#Test().(t testingscip-go gomod github.com/golang/go/src go1.13 testing/.TBscip-go gomod github.com/golang/go/src go1.13 testing/TB#, cases ...*Casescip-go gomod go4.org . `go4.org/testing/functest`/Case#) {
209 for _, tc := range cases {
210 f.testCasescip-go gomod go4.org . `go4.org/testing/functest`/Func#testCase().(t, tc)
211 }
212 }
213
214 func (f *Funcscip-go gomod go4.org . `go4.org/testing/functest`/Func#) checkCallscip-go gomod go4.org . `go4.org/testing/functest`/Func#checkCall().(in []reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Valuescip-go gomod github.com/golang/go/src go1.13 reflect/Value#) (out []reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Valuescip-go gomod github.com/golang/go/src go1.13 reflect/Value#, didPanic bool, panicValue interface{}) {
215 defer func() { panicValue = recover() }()
216 didPanic = true
217 out = f.fvscip-go gomod go4.org . `go4.org/testing/functest`/Func#fv..Callscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Call().(in)
218 didPanic = false
219 return
220 }
221
222 var nilEmptyInterfacescip-go gomod go4.org . `go4.org/testing/functest`/nilEmptyInterface. = reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Zeroscip-go gomod github.com/golang/go/src go1.13 reflect/Zero().(reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.13 reflect/TypeOf().((*interface{})(nil)).Elemscip-go gomod github.com/golang/go/src go1.13 reflect/Type#Elem().())
223
224 func (f *Funcscip-go gomod go4.org . `go4.org/testing/functest`/Func#) testCasescip-go gomod go4.org . `go4.org/testing/functest`/Func#testCase().(t testingscip-go gomod github.com/golang/go/src go1.13 testing/.TBscip-go gomod github.com/golang/go/src go1.13 testing/TB#, c *Casescip-go gomod go4.org . `go4.org/testing/functest`/Case#) {
225 // Non-variadic:
226 ft := f.fvscip-go gomod go4.org . `go4.org/testing/functest`/Func#fv..Typescip-go gomod github.com/golang/go/src go1.13 reflect/Value#Type().()
227 inReg := ft.NumInscip-go gomod github.com/golang/go/src go1.13 reflect/Type#NumIn().()
228 if ft.IsVariadicscip-go gomod github.com/golang/go/src go1.13 reflect/Type#IsVariadic().() {
229 inReg--
230 if len(c.inscip-go gomod go4.org . `go4.org/testing/functest`/Case#in.) < inReg {
231 c.errorfscip-go gomod go4.org . `go4.org/testing/functest`/Case#errorf().(t, ": input has %d arguments; func requires at least %d", len(c.inscip-go gomod go4.org . `go4.org/testing/functest`/Case#in.), inReg)
232 return
233 }
234 } else if len(c.inscip-go gomod go4.org . `go4.org/testing/functest`/Case#in.) != ft.NumInscip-go gomod github.com/golang/go/src go1.13 reflect/Type#NumIn().() {
235 c.errorfscip-go gomod go4.org . `go4.org/testing/functest`/Case#errorf().(t, ": input has %d arguments; func takes %d", len(c.inscip-go gomod go4.org . `go4.org/testing/functest`/Case#in.), ft.NumInscip-go gomod github.com/golang/go/src go1.13 reflect/Type#NumIn().())
236 return
237 }
238
239 inv := make([]reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Valuescip-go gomod github.com/golang/go/src go1.13 reflect/Value#, len(c.inscip-go gomod go4.org . `go4.org/testing/functest`/Case#in.))
240 for i, v := range c.inscip-go gomod go4.org . `go4.org/testing/functest`/Case#in. {
241 if v == nil {
242 inv[i] = nilEmptyInterfacescip-go gomod go4.org . `go4.org/testing/functest`/nilEmptyInterface.
243 } else {
244 inv[i] = reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.13 reflect/ValueOf().(v)
245 }
246 }
247 got, didPanic, panicValue := f.checkCallscip-go gomod go4.org . `go4.org/testing/functest`/Func#checkCall().(inv)
248
249 var goti []interface{}
250 if !didPanic {
251 goti = make([]interface{}, len(got))
252 for i, rv := range got {
253 goti[i] = rv.Interfacescip-go gomod github.com/golang/go/src go1.13 reflect/Value#Interface().()
254 }
255 }
256
257 if c.wantscip-go gomod go4.org . `go4.org/testing/functest`/Case#want. != nil {
258 if !reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.13 reflect/DeepEqual().(goti, c.wantscip-go gomod go4.org . `go4.org/testing/functest`/Case#want.) {
259 c.errorfscip-go gomod go4.org . `go4.org/testing/functest`/Case#errorf().(t, " = %v; want %v", formatResscip-go gomod go4.org . `go4.org/testing/functest`/formatRes().(goti), formatResscip-go gomod go4.org . `go4.org/testing/functest`/formatRes().(c.wantscip-go gomod go4.org . `go4.org/testing/functest`/Case#want.))
260 }
261 }
262 for _, checkRes := range c.checkResscip-go gomod go4.org . `go4.org/testing/functest`/Case#checkRes. {
263 err := checkRes(Resultscip-go gomod go4.org . `go4.org/testing/functest`/Result#{
264 Resultscip-go gomod go4.org . `go4.org/testing/functest`/Result#Result.: goti,
265 Panicscip-go gomod go4.org . `go4.org/testing/functest`/Result#Panic.: panicValue,
266 Panickedscip-go gomod go4.org . `go4.org/testing/functest`/Result#Panicked.: didPanic,
267 })
268 if err != nil {
269 c.errorfscip-go gomod go4.org . `go4.org/testing/functest`/Case#errorf().(t, ": %v", err)
270 }
271 }
272 if didPanic && (c.checkResscip-go gomod go4.org . `go4.org/testing/functest`/Case#checkRes. == nil) {
273 c.errorfscip-go gomod go4.org . `go4.org/testing/functest`/Case#errorf().(t, ": panicked with %v", panicValue)
274 }
275 }
276
277 func formatResscip-go gomod go4.org . `go4.org/testing/functest`/formatRes().(res []interface{}) string {
278 var buf bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#
279 if len(res) != 1 {
280 buf.WriteBytescip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteByte().('(')
281 }
282 formatValuesscip-go gomod go4.org . `go4.org/testing/functest`/formatValues().(&buf, res)
283 if len(res) != 1 {
284 buf.WriteBytescip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteByte().(')')
285 }
286 return buf.Stringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#String().()
287 }
288
289 func formatValuesscip-go gomod go4.org . `go4.org/testing/functest`/formatValues().(buf *bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#, vals []interface{}) {
290 for i, v := range vals {
291 if i != 0 {
292 buf.WriteStringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteString().(", ")
293 }
294 fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintf().(buf, "%#v", v)
295 }
296 }
297
298 func (c *Casescip-go gomod go4.org . `go4.org/testing/functest`/Case#) errorfscip-go gomod go4.org . `go4.org/testing/functest`/Case#errorf().(t testingscip-go gomod github.com/golang/go/src go1.13 testing/.TBscip-go gomod github.com/golang/go/src go1.13 testing/TB#, format string, args ...interface{}) {
299 var buf bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#
300 if c.namescip-go gomod go4.org . `go4.org/testing/functest`/Case#name. != "" {
301 fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintf().(&buf, "%s: ", c.namescip-go gomod go4.org . `go4.org/testing/functest`/Case#name.)
302 }
303 buf.WriteStringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteString().(c.fscip-go gomod go4.org . `go4.org/testing/functest`/Case#f..Namescip-go gomod go4.org . `go4.org/testing/functest`/Func#Name.)
304 buf.WriteStringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteString().("(")
305 formatValuesscip-go gomod go4.org . `go4.org/testing/functest`/formatValues().(&buf, c.inscip-go gomod go4.org . `go4.org/testing/functest`/Case#in.)
306 buf.WriteStringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteString().(")")
307 fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintf().(&buf, format, args...)
308 t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/TB#Errorf().("%s", buf.Bytesscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#Bytes().())
309 }