Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

func Assert(something bool) {
if !something {
pc := make([]uintptr, 10)
runtime.Callers(2, pc)
f := runtime.FuncForPC(pc[0])
file, line := f.FileLine(pc[0])
panic(fmt.Sprintf("assertion failed at %s:%d %s\n", file, line, f.Name()))
pc := make([]uintptr, 1)
n := runtime.Callers(2, pc)
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
panic(fmt.Sprintf("assertion failed at %s:%d %s\n", frame.File, frame.Line, frame.Function))
}
}
44 changes: 44 additions & 0 deletions src/assert/assert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package assert

import (
"strings"
"testing"
)

func assertFailure() {
Assert(false)
}

func TestAssertReportsCallerLocation(t *testing.T) {
var got string

func() {
defer func() {
r := recover()
if r == nil {
t.Fatal("expected panic")
}

msg, ok := r.(string)
if !ok {
t.Fatalf("expected panic string, got %T", r)
}

got = msg
}()

assertFailure()
}()

if !strings.Contains(got, "src/assert/assert_test.go:") {
t.Fatalf("expected panic to report assert test file, got %q", got)
}

if !strings.Contains(got, "github.com/envoyproxy/ratelimit/src/assert.assertFailure") {
t.Fatalf("expected panic to report assertFailure function, got %q", got)
}

if strings.Contains(got, "TestAssertReportsCallerLocation") {
t.Fatalf("expected panic to report immediate caller instead of outer test frame, got %q", got)
}
}
Loading