@@ -14,8 +14,11 @@ package tdutil
1414import (
1515 "reflect"
1616 "testing"
17+ "unsafe"
1718)
1819
20+ var testingT = reflect .TypeOf (testing.T {})
21+
1922// T can be used in tests, to test [testing.T] behavior as it overrides
2023// [testing.T.Run] method.
2124type T struct {
@@ -28,7 +31,9 @@ type tFailedNow struct{}
2831// NewT returns a new [*T] instance. name is the string returned by
2932// method Name.
3033func NewT (name string ) * T {
31- return & T {name : name }
34+ t := & T {name : name }
35+ t .prepareForLogs ()
36+ return t
3237}
3338
3439// Run is a simplified version of [testing.T.Run] method, without edge
@@ -70,6 +75,11 @@ func (t *T) Fatalf(format string, args ...any) {
7075 t .FailNow ()
7176}
7277
78+ // Starting go1.18, unsafe.Add can be used instead.
79+ func ptrAdd (p unsafe.Pointer , x uintptr ) unsafe.Pointer {
80+ return unsafe .Pointer (uintptr (p ) + x )
81+ }
82+
7383// CatchFailNow returns true if a [T.FailNow], [T.Fatal] or [T.Fatalf] call
7484// occurred during the execution of fn.
7585func (t * T ) CatchFailNow (fn func ()) (failNowOccurred bool ) {
@@ -85,3 +95,34 @@ func (t *T) CatchFailNow(fn func()) (failNowOccurred bool) {
8595 fn ()
8696 return
8797}
98+
99+ func (t * T ) prepareForLogs () {
100+ if _ , ok := testingT .FieldByName ("output" ); ! ok {
101+ panic ("testing.T.output field not found!" )
102+ }
103+
104+ // o is a new go1.25 testing.common field
105+ if o , ok := testingT .FieldByName ("o" ); ok {
106+ ot := o .Type .Elem ()
107+
108+ // With go1.25 t.T.o needs to be set to feed t.T.output
109+ type outputWriter struct {
110+ c * testing.T
111+ partial []byte //nolint: unused
112+ }
113+ owt := reflect .TypeOf (outputWriter {})
114+
115+ if ot .NumField () != owt .NumField () ||
116+ ot .Field (0 ).Name != owt .Field (0 ).Name || // ot.c is *testing.common
117+ ot .Field (1 ).Type != owt .Field (1 ).Type {
118+ panic ("testing.T.outputWriter changed" )
119+ }
120+
121+ // Use ptrAdd to support go1.16 & go1.17, but starting go1.18 we can use:
122+ // oAddr := (**outputWriter)(unsafe.Add(unsafe.Pointer(&t.T), o.Offset))
123+ oAddr := (* * outputWriter )(ptrAdd (unsafe .Pointer (& t .T ), o .Offset ))
124+ * oAddr = & outputWriter {
125+ c : & t .T ,
126+ }
127+ }
128+ }
0 commit comments