From 649ffe44b3e92cfb477ef67bd8a99f9f609fe699 Mon Sep 17 00:00:00 2001 From: Arik Kfir Date: Sat, 11 May 2024 16:00:46 +0300 Subject: [PATCH] feat(GetRoot): add new API to obtain *testing.T from any given T 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. --- t.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/t.go b/t.go index 7e08c4c..7faaca1 100644 --- a/t.go +++ b/t.go @@ -2,6 +2,7 @@ package justest import ( "fmt" + "testing" ) type T interface { @@ -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)) + } + } +}