Debian Codegraph @

helpers/tdutil/t.go

raw
1 // Copyright (c) 2019, 2020, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 // Package tdutil allows to write unit tests for go-testdeep helpers
8 // and so provides some helpful functions.
9 //
10 // It is not intended to be used in tests outside go-testdeep and its
11 // helpers perimeter.
12 package tdutil
13
14 import (
15 "reflect"
16 "testing"
17 "unsafe"
18
19 "github.com/maxatome/go-testdeep/internal/or"
20 )
21
22 var testingT = reflect.TypeOf(testing.T{})
23
24 // T can be used in tests, to test [testing.T] behavior as it overrides
25 // [testing.T.Run] method.
26 type T struct {
27 testing.T
28 name string
29 }
30
31 type tFailedNow struct{}
32
33 // NewT returns a new [*T] instance. name is the string returned by
34 // method Name.
35 func NewT(name string) *T {
36 t := &T{name: name}
37 t.prepareForLogs()
38 return t
39 }
40
41 // Run is a simplified version of [testing.T.Run] method, without edge
42 // cases.
43 func (t *T) Run(name string, f func(*testing.T)) bool {
44 t.CatchFailNow(func() { f(&t.T) })
45 return !t.Failed()
46 }
47
48 // Name returns the name of the running test (in fact the one set by [NewT]).
49 func (t *T) Name() string {
50 return t.name
51 }
52
53 // LogBuf is an ugly hack allowing to access internal [testing.T] log
54 // buffer. Keep cool, it is only used for internal unit tests.
55 func (t *T) LogBuf() string {
56 return string(reflect.ValueOf(t.T).FieldByName("output").Bytes()) //nolint: govet
57 }
58
59 // FailNow simulates the original [testing.T.FailNow] using
60 // panic. [T.CatchFailNow] should be used to properly intercept it.
61 func (t *T) FailNow() {
62 t.Fail()
63 panic(tFailedNow{})
64 }
65
66 // Fatal simulates the original [testing.T.Fatal].
67 func (t *T) Fatal(args ...any) {
68 t.Helper()
69 t.Error(args...)
70 t.FailNow()
71 }
72
73 // Fatal simulates the original [testing.T.Fatalf].
74 func (t *T) Fatalf(format string, args ...any) {
75 t.Helper()
76 t.Errorf(format, args...)
77 t.FailNow()
78 }
79
80 // Starting go1.18, unsafe.Add can be used instead.
81 func ptrAdd(p unsafe.Pointer, x uintptr) unsafe.Pointer {
82 return unsafe.Pointer(uintptr(p) + x)
83 }
84
85 // CatchFailNow returns true if a [T.FailNow], [T.Fatal] or [T.Fatalf] call
86 // occurred during the execution of fn.
87 func (t *T) CatchFailNow(fn func()) (failNowOccurred bool) {
88 defer func() {
89 if x := recover(); x != nil {
90 _, failNowOccurred = x.(tFailedNow)
91 if !failNowOccurred {
92 panic(x) // rethrow
93 }
94 }
95 }()
96
97 fn()
98 return
99 }
100
101 func (t *T) prepareForLogs() {
102 _, ok := testingT.FieldByName("output")
103 or.Panic(ok, "testing.T.output field not found!")
104
105 // o is a new go1.25 testing.common field
106 if o, ok := testingT.FieldByName("o"); ok {
107 ot := o.Type.Elem()
108
109 // With go1.25 t.T.o needs to be set to feed t.T.output
110 type outputWriter struct {
111 c *testing.T
112 partial []byte //nolint: unused
113 }
114 owt := reflect.TypeOf(outputWriter{})
115
116 or.Panic(
117 ot.NumField() == owt.NumField() &&
118 ot.Field(0).Name == owt.Field(0).Name && // ot.c is *testing.common
119 ot.Field(1).Type == owt.Field(1).Type,
120 "testing.T.outputWriter changed")
121
122 // Use ptrAdd to support go1.16 & go1.17, but starting go1.18 we can use:
123 // oAddr := (**outputWriter)(unsafe.Add(unsafe.Pointer(&t.T), o.Offset))
124 oAddr := (**outputWriter)(ptrAdd(unsafe.Pointer(&t.T), o.Offset))
125 *oAddr = &outputWriter{
126 c: &t.T,
127 }
128 }
129 }