-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert.go
47 lines (39 loc) · 1.13 KB
/
assert.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
package protocmp
import (
"fmt"
"path"
"runtime"
"strings"
"github.com/golang/protobuf/proto"
)
type TestingT interface {
Name() string
Fail()
}
func AssertEqual(t TestingT, expected, actual proto.Message) {
err := Equal(expected, actual)
if err != nil {
frame := getFrame(1)
fmt.Printf(" %s: %s:%d\n %s\n", t.Name(), path.Base(frame.File), frame.Line, strings.ReplaceAll(err.Error(), "\n", "\n "))
t.Fail()
}
}
func getFrame(skipFrames int) runtime.Frame {
// We need the frame at index skipFrames+2, since we never want runtime.Callers and getFrame
targetFrameIndex := skipFrames + 2
// Set size to targetFrameIndex+2 to ensure we have room for one more caller than we need
programCounters := make([]uintptr, targetFrameIndex+2)
n := runtime.Callers(0, programCounters)
frame := runtime.Frame{Function: "unknown"}
if n > 0 {
frames := runtime.CallersFrames(programCounters[:n])
for more, frameIndex := true, 0; more && frameIndex <= targetFrameIndex; frameIndex++ {
var f runtime.Frame
f, more = frames.Next()
if frameIndex == targetFrameIndex {
frame = f
}
}
}
return frame
}