| 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 |
// Until go 1.23 in go.mod |
| 11 |
//go:debug asynctimerchan=0 |
| 12 |
|
| 13 |
package tdsynctest_test |
| 14 |
|
| 15 |
import ( |
| 16 |
"testing" |
| 17 |
"time" |
| 18 |
|
| 19 |
"github.com/maxatome/go-testdeep/helpers/tdsynctest" |
| 20 |
"github.com/maxatome/go-testdeep/helpers/tdutil" |
| 21 |
"github.com/maxatome/go-testdeep/internal/test" |
| 22 |
"github.com/maxatome/go-testdeep/td" |
| 23 |
) |
| 24 |
|
| 25 |
func TestTest(t *testing.T) { |
| 26 |
t.Run("testing.T required", func(t *testing.T) { |
| 27 |
tt := tdutil.NewT(t.Name()) |
| 28 |
|
| 29 |
run := false |
| 30 |
failed := tt.CatchFailNow(func() { |
| 31 |
assert := td.Assert(tt) |
| 32 |
tdsynctest.Test(assert, func(assert *td.T) { |
| 33 |
run = true |
| 34 |
}) |
| 35 |
}) |
| 36 |
test.IsFalse(t, run) |
| 37 |
test.IsTrue(t, failed) |
| 38 |
test.MatchStr(t, tt.LogBuf(), |
| 39 |
`^\s+test_test\.go:\d+: tdsynctest\.Test only works if underlying T\.TB field is a \*testing\.T, so not a \*tdutil\.T\n\z`) |
| 40 |
}) |
| 41 |
|
| 42 |
t.Run("OK1", func(t *testing.T) { |
| 43 |
run, belax, req := false, false, true |
| 44 |
assert := td.Assert(t).BeLax() |
| 45 |
tdsynctest.Test(assert, func(assert *td.T) { |
| 46 |
go func() { |
| 47 |
run = true |
| 48 |
belax = assert.Config.BeLax |
| 49 |
req = assert.Config.FailureIsFatal |
| 50 |
}() |
| 51 |
tdsynctest.Wait() |
| 52 |
}) |
| 53 |
test.IsTrue(t, run) |
| 54 |
test.IsTrue(t, belax) |
| 55 |
test.IsFalse(t, req) |
| 56 |
}) |
| 57 |
|
| 58 |
t.Run("OK2", func(t *testing.T) { |
| 59 |
run, belax, req := false, true, false |
| 60 |
require := td.Require(t) |
| 61 |
tdsynctest.Test(require, func(require *td.T) { |
| 62 |
go func() { |
| 63 |
run = true |
| 64 |
belax = require.Config.BeLax |
| 65 |
req = require.Config.FailureIsFatal |
| 66 |
}() |
| 67 |
tdsynctest.Sleep(time.Nanosecond) |
| 68 |
}) |
| 69 |
test.IsTrue(t, run) |
| 70 |
test.IsFalse(t, belax) |
| 71 |
test.IsTrue(t, req) |
| 72 |
}) |
| 73 |
} |
| 74 |
|
| 75 |
func TestTestAssertRequire(t *testing.T) { |
| 76 |
t.Run("testing.T required", func(t *testing.T) { |
| 77 |
tt := tdutil.NewT(t.Name()) |
| 78 |
|
| 79 |
run := false |
| 80 |
failed := tt.CatchFailNow(func() { |
| 81 |
assert := td.Assert(tt) |
| 82 |
tdsynctest.TestAssertRequire(assert, func(assert, require *td.T) { |
| 83 |
run = true |
| 84 |
}) |
| 85 |
}) |
| 86 |
test.IsFalse(t, run) |
| 87 |
test.IsTrue(t, failed) |
| 88 |
test.MatchStr(t, tt.LogBuf(), |
| 89 |
`^\s+test_test\.go:\d+: tdsynctest\.TestAssertRequire only works if underlying T\.TB field is a \*testing\.T, so not a \*tdutil\.T\n\z`) |
| 90 |
}) |
| 91 |
|
| 92 |
t.Run("OK1", func(t *testing.T) { |
| 93 |
var run, belaxA, belaxR, ass, req bool |
| 94 |
assert := td.Assert(t).BeLax() |
| 95 |
tdsynctest.TestAssertRequire(assert, func(assert, require *td.T) { |
| 96 |
go func() { |
| 97 |
run = true |
| 98 |
belaxA = assert.Config.BeLax |
| 99 |
ass = !assert.Config.FailureIsFatal |
| 100 |
belaxR = require.Config.BeLax |
| 101 |
req = require.Config.FailureIsFatal |
| 102 |
}() |
| 103 |
tdsynctest.Wait() |
| 104 |
}) |
| 105 |
test.IsTrue(t, run) |
| 106 |
test.IsTrue(t, belaxA) |
| 107 |
test.IsTrue(t, ass) |
| 108 |
test.IsTrue(t, belaxR) |
| 109 |
test.IsTrue(t, req) |
| 110 |
}) |
| 111 |
|
| 112 |
t.Run("OK2", func(t *testing.T) { |
| 113 |
run, belaxA, belaxR, ass, req := false, true, true, false, false |
| 114 |
assert := td.Assert(t) |
| 115 |
tdsynctest.TestAssertRequire(assert, func(assert, require *td.T) { |
| 116 |
go func() { |
| 117 |
run = true |
| 118 |
belaxA = assert.Config.BeLax |
| 119 |
ass = !assert.Config.FailureIsFatal |
| 120 |
belaxR = require.Config.BeLax |
| 121 |
req = require.Config.FailureIsFatal |
| 122 |
}() |
| 123 |
tdsynctest.Sleep(time.Nanosecond) |
| 124 |
}) |
| 125 |
test.IsTrue(t, run) |
| 126 |
test.IsFalse(t, belaxA) |
| 127 |
test.IsTrue(t, ass) |
| 128 |
test.IsFalse(t, belaxR) |
| 129 |
test.IsTrue(t, req) |
| 130 |
}) |
| 131 |
} |
| 132 |
|
| 133 |
func TestSubtest(t *testing.T) { |
| 134 |
t.Run("testing.T required", func(t *testing.T) { |
| 135 |
tt := tdutil.NewT(t.Name()) |
| 136 |
|
| 137 |
run := false |
| 138 |
failed := tt.CatchFailNow(func() { |
| 139 |
assert := td.Assert(tt) |
| 140 |
tdsynctest.Subtest(assert, "name", func(t *td.T) { |
| 141 |
run = true |
| 142 |
}) |
| 143 |
}) |
| 144 |
test.IsFalse(t, run) |
| 145 |
test.IsTrue(t, failed) |
| 146 |
test.MatchStr(t, tt.LogBuf(), |
| 147 |
`^\s+test_test\.go:\d+: tdsynctest\.Subtest only works if underlying T\.TB field is a \*testing\.T, so not a \*tdutil\.T\n\z`) |
| 148 |
}) |
| 149 |
|
| 150 |
t.Run("OK1", func(t *testing.T) { |
| 151 |
run, belax, req := false, false, true |
| 152 |
assert := td.Assert(t).BeLax() |
| 153 |
tdsynctest.Subtest(assert, "name", func(assert *td.T) { |
| 154 |
go func() { |
| 155 |
run = true |
| 156 |
belax = assert.Config.BeLax |
| 157 |
req = assert.Config.FailureIsFatal |
| 158 |
}() |
| 159 |
tdsynctest.Wait() |
| 160 |
}) |
| 161 |
test.IsTrue(t, run) |
| 162 |
test.IsTrue(t, belax) |
| 163 |
test.IsFalse(t, req) |
| 164 |
}) |
| 165 |
|
| 166 |
t.Run("OK2", func(t *testing.T) { |
| 167 |
run, belax, req := false, true, false |
| 168 |
require := td.Require(t) |
| 169 |
tdsynctest.Subtest(require, "name", func(require *td.T) { |
| 170 |
go func() { |
| 171 |
run = true |
| 172 |
belax = require.Config.BeLax |
| 173 |
req = require.Config.FailureIsFatal |
| 174 |
}() |
| 175 |
tdsynctest.Sleep(time.Nanosecond) |
| 176 |
}) |
| 177 |
test.IsTrue(t, run) |
| 178 |
test.IsFalse(t, belax) |
| 179 |
test.IsTrue(t, req) |
| 180 |
}) |
| 181 |
} |
| 182 |
|
| 183 |
func TestSubtestAssertRequire(t *testing.T) { |
| 184 |
t.Run("testing.T required", func(t *testing.T) { |
| 185 |
tt := tdutil.NewT(t.Name()) |
| 186 |
|
| 187 |
run := false |
| 188 |
failed := tt.CatchFailNow(func() { |
| 189 |
assert := td.Assert(tt) |
| 190 |
tdsynctest.SubtestAssertRequire(assert, "name", func(assert, require *td.T) { |
| 191 |
run = true |
| 192 |
}) |
| 193 |
}) |
| 194 |
test.IsFalse(t, run) |
| 195 |
test.IsTrue(t, failed) |
| 196 |
test.MatchStr(t, tt.LogBuf(), |
| 197 |
`^\s+test_test\.go:\d+: tdsynctest\.SubtestAssertRequire only works if underlying T\.TB field is a \*testing\.T, so not a \*tdutil\.T\n\z`) |
| 198 |
}) |
| 199 |
|
| 200 |
t.Run("OK1", func(t *testing.T) { |
| 201 |
var run, belaxA, belaxR, ass, req bool |
| 202 |
assert := td.Assert(t).BeLax() |
| 203 |
tdsynctest.SubtestAssertRequire(assert, "name", func(assert, require *td.T) { |
| 204 |
go func() { |
| 205 |
run = true |
| 206 |
belaxA = assert.Config.BeLax |
| 207 |
ass = !assert.Config.FailureIsFatal |
| 208 |
belaxR = require.Config.BeLax |
| 209 |
req = require.Config.FailureIsFatal |
| 210 |
}() |
| 211 |
tdsynctest.Wait() |
| 212 |
}) |
| 213 |
test.IsTrue(t, run) |
| 214 |
test.IsTrue(t, belaxA) |
| 215 |
test.IsTrue(t, ass) |
| 216 |
test.IsTrue(t, belaxR) |
| 217 |
test.IsTrue(t, req) |
| 218 |
}) |
| 219 |
|
| 220 |
t.Run("OK2", func(t *testing.T) { |
| 221 |
run, belaxA, belaxR, ass, req := false, true, true, false, false |
| 222 |
assert := td.Assert(t) |
| 223 |
tdsynctest.SubtestAssertRequire(assert, "name", func(assert, require *td.T) { |
| 224 |
go func() { |
| 225 |
run = true |
| 226 |
belaxA = assert.Config.BeLax |
| 227 |
ass = !assert.Config.FailureIsFatal |
| 228 |
belaxR = require.Config.BeLax |
| 229 |
req = require.Config.FailureIsFatal |
| 230 |
}() |
| 231 |
tdsynctest.Sleep(time.Nanosecond) |
| 232 |
}) |
| 233 |
test.IsTrue(t, run) |
| 234 |
test.IsFalse(t, belaxA) |
| 235 |
test.IsTrue(t, ass) |
| 236 |
test.IsFalse(t, belaxR) |
| 237 |
test.IsTrue(t, req) |
| 238 |
}) |
| 239 |
} |