MCPcopy
hub / github.com/stretchr/testify / CallerInfo

Function CallerInfo

assert/assertions.go:212–287  ·  assert/assertions.go::CallerInfo

* CallerInfo is necessary because the assert functions use the testing object internally, causing it to print the file:line of the assert method, rather than where the problem actually occurred in calling code.*/ CallerInfo returns an array of strings containing the file and line number of each stac

()

Source from the content-addressed store, hash-verified

210// of each stack frame leading from the current test to the assert call that
211// failed.
212func CallerInfo() []string {
213 var pc uintptr
214 var file string
215 var line int
216 var name string
217
218 const stackFrameBufferSize = 10
219 pcs := make([]uintptr, stackFrameBufferSize)
220
221 callers := []string{}
222 offset := 1
223
224 for {
225 n := runtime.Callers(offset, pcs)
226
227 if n == 0 {
228 break
229 }
230
231 frames := runtime.CallersFrames(pcs[:n])
232
233 for {
234 frame, more := frames.Next()
235 pc = frame.PC
236 file = frame.File
237 line = frame.Line
238
239 // This is a huge edge case, but it will panic if this is the case, see #180
240 if file == "<autogenerated>" {
241 break
242 }
243
244 f := runtime.FuncForPC(pc)
245 if f == nil {
246 break
247 }
248 name = f.Name()
249
250 // testing.tRunner is the standard library function that calls
251 // tests. Subtests are called directly by tRunner, without going through
252 // the Test/Benchmark/Example function that contains the t.Run calls, so
253 // with subtests we should break when we hit tRunner, without adding it
254 // to the list of callers.
255 if name == "testing.tRunner" {
256 break
257 }
258
259 parts := strings.Split(file, "/")
260 if len(parts) > 1 {
261 filename := parts[len(parts)-1]
262 dir := parts[len(parts)-2]
263 if (dir != "assert" && dir != "mock" && dir != "require") || filename == "mock_test.go" {
264 callers = append(callers, fmt.Sprintf("%s:%d", file, line))
265 }
266 }
267
268 // Drop the package
269 dotPos := strings.LastIndexByte(name, '.')

Callers 6

OnMethod · 0.92
MethodCalledMethod · 0.92
AssertExpectationsMethod · 0.92
AssertMethod · 0.92
FailFunction · 0.85

Calls 2

isTestFunction · 0.85
NameMethod · 0.65

Tested by 1