| 1 |
// Copyright (c) 2025-2026, 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 |
//go:build go1.25 |
| 8 |
// +build go1.25 |
| 9 |
|
| 10 |
package tdsynctest |
| 11 |
|
| 12 |
import ( |
| 13 |
"testing" |
| 14 |
"testing/synctest" |
| 15 |
"time" |
| 16 |
|
| 17 |
"github.com/maxatome/go-testdeep/td" |
| 18 |
) |
| 19 |
|
| 20 |
// Test is a wrapper around [synctest.Test]. |
| 21 |
// |
| 22 |
// The t param of f inherits the configuration of t. |
| 23 |
// |
| 24 |
// t.TB (set by [td.NewT], [td.Assert], [td.Require], …) must be a |
| 25 |
// [*testing.T] instance. |
| 26 |
// |
| 27 |
// See also [TestAssertRequire]. |
| 28 |
func Test(t *td.T, f func(t *td.T)) { |
| 29 |
tt, ok := t.TB.(*testing.T) |
| 30 |
if !ok { |
| 31 |
t.Helper() |
| 32 |
t.Fatalf("tdsynctest.Test only works if underlying T.TB field is a *testing.T, so not a %T", t.TB) |
| 33 |
} |
| 34 |
conf := t.Config |
| 35 |
synctest.Test(tt, func(t *testing.T) { |
| 36 |
f(td.NewT(t, conf)) |
| 37 |
}) |
| 38 |
} |
| 39 |
|
| 40 |
// TestAssertRequire is a wrapper around [synctest.Test]. |
| 41 |
// |
| 42 |
// The assert and require params of f inherit the configuration of t, |
| 43 |
// except that a failure is never fatal using assert and always fatal |
| 44 |
// using require. |
| 45 |
// |
| 46 |
// t.TB (set by [td.NewT], [td.Assert], [td.Require], …) must be a |
| 47 |
// [*testing.T] instance. |
| 48 |
// |
| 49 |
// See also [Test]. |
| 50 |
func TestAssertRequire(t *td.T, f func(assert, require *td.T)) { |
| 51 |
tt, ok := t.TB.(*testing.T) |
| 52 |
if !ok { |
| 53 |
t.Helper() |
| 54 |
t.Fatalf("tdsynctest.TestAssertRequire only works if underlying T.TB field is a *testing.T, so not a %T", t.TB) |
| 55 |
} |
| 56 |
conf := t.Config |
| 57 |
synctest.Test(tt, func(t *testing.T) { |
| 58 |
f(td.AssertRequire(t, conf)) |
| 59 |
}) |
| 60 |
} |
| 61 |
|
| 62 |
// Subtest runs f in a bubble as a subtest of t called name. |
| 63 |
// |
| 64 |
// The t param of f inherits the configuration of t. |
| 65 |
// |
| 66 |
// t.TB (set by [td.NewT], [td.Assert], [td.Require], …) must be a |
| 67 |
// [*testing.T] instance. |
| 68 |
// |
| 69 |
// See also [SubtestAssertRequire]. |
| 70 |
func Subtest(t *td.T, name string, f func(t *td.T)) { |
| 71 |
t.Helper() |
| 72 |
tt, ok := t.TB.(*testing.T) |
| 73 |
if !ok { |
| 74 |
t.Fatalf("tdsynctest.Subtest only works if underlying T.TB field is a *testing.T, so not a %T", t.TB) |
| 75 |
} |
| 76 |
conf := t.Config |
| 77 |
tt.Run(name, func(t *testing.T) { |
| 78 |
synctest.Test(t, func(t *testing.T) { |
| 79 |
f(td.NewT(t, conf)) |
| 80 |
}) |
| 81 |
}) |
| 82 |
} |
| 83 |
|
| 84 |
// SubtestAssertRequire runs f in a bubble as a subtest of t called name. |
| 85 |
// |
| 86 |
// The assert and require params of f inherit the configuration of t, |
| 87 |
// except that a failure is never fatal using assert and always fatal |
| 88 |
// using require. |
| 89 |
// |
| 90 |
// t.TB (set by [td.NewT], [td.Assert], [td.Require], …) must be a |
| 91 |
// [*testing.T] instance. |
| 92 |
// |
| 93 |
// See also [Subtest]. |
| 94 |
func SubtestAssertRequire(t *td.T, name string, f func(assert, require *td.T)) { |
| 95 |
t.Helper() |
| 96 |
tt, ok := t.TB.(*testing.T) |
| 97 |
if !ok { |
| 98 |
t.Fatalf("tdsynctest.SubtestAssertRequire only works if underlying T.TB field is a *testing.T, so not a %T", t.TB) |
| 99 |
} |
| 100 |
conf := t.Config |
| 101 |
tt.Run(name, func(t *testing.T) { |
| 102 |
synctest.Test(t, func(t *testing.T) { |
| 103 |
f(td.AssertRequire(t, conf)) |
| 104 |
}) |
| 105 |
}) |
| 106 |
} |
| 107 |
|
| 108 |
// Wait simply calls [synctest.Wait]. It only exists to avoid to |
| 109 |
// import [testing/synctest]. |
| 110 |
func Wait() { |
| 111 |
synctest.Wait() |
| 112 |
} |
| 113 |
|
| 114 |
// Sleep does the same as go1.27 [synctest.Sleep]. It only exists to avoid to |
| 115 |
// import [testing/synctest] and/or when go version is lesser than 1.27. |
| 116 |
func Sleep(d time.Duration) { |
| 117 |
time.Sleep(d) |
| 118 |
Wait() |
| 119 |
} |