Skip to content

Commit

Permalink
feat(GetRoot): add new API to obtain *testing.T from any given T (#7)
Browse files Browse the repository at this point in the history
This change adds a new public function called `GetRoot` which, given a
`T` instance, will return the root `*testing.T` instance it bubbles up
to.
  • Loading branch information
arikkfir authored May 11, 2024
1 parent a6741d1 commit da78562
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions t.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package justest

import (
"fmt"
"testing"
)

type T interface {
Expand Down Expand Up @@ -34,3 +35,16 @@ func GetHelper(t T) interface{ Helper() } {
}
return &noOpHelper{}
}

//go:noinline
func GetRoot(t T) *testing.T {
for {
if hp, ok := t.(HasParent); ok {
t = hp.GetParent()
} else if rt, ok := t.(*testing.T); ok {
return rt
} else {
panic(fmt.Sprintf("unsupported T instance: %+v (%T)", t, t))
}
}
}

0 comments on commit da78562

Please sign in to comment.