| 1 |
// Copyright 2023 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build goexperiment.jsonv2 |
| 6 |
|
| 7 |
package jsontest |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"path" |
| 12 |
"runtime" |
| 13 |
) |
| 14 |
|
| 15 |
// TODO(https://go.dev/issue/52751): Replace with native testing support. |
| 16 |
|
| 17 |
// CaseName is a case name annotated with a file and line. |
| 18 |
type CaseName struct { |
| 19 |
Name string |
| 20 |
Where CasePos |
| 21 |
} |
| 22 |
|
| 23 |
// Name annotates a case name with the file and line of the caller. |
| 24 |
func Name(s string) (c CaseName) { |
| 25 |
c.Name = s |
| 26 |
runtime.Callers(2, c.Where.pc[:]) |
| 27 |
return c |
| 28 |
} |
| 29 |
|
| 30 |
// CasePos represents a file and line number. |
| 31 |
type CasePos struct{ pc [1]uintptr } |
| 32 |
|
| 33 |
func (pos CasePos) String() string { |
| 34 |
frames := runtime.CallersFrames(pos.pc[:]) |
| 35 |
frame, _ := frames.Next() |
| 36 |
return fmt.Sprintf("%s:%d", path.Base(frame.File), frame.Line) |
| 37 |
} |