|
| 1 | +// Copyright (c) 2025, 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 | + |
| 18 | + "github.com/maxatome/go-testdeep/helpers/tdsynctest" |
| 19 | + "github.com/maxatome/go-testdeep/helpers/tdutil" |
| 20 | + "github.com/maxatome/go-testdeep/internal/test" |
| 21 | + "github.com/maxatome/go-testdeep/td" |
| 22 | +) |
| 23 | + |
| 24 | +func TestTest(t *testing.T) { |
| 25 | + t.Run("testing.T required", func(t *testing.T) { |
| 26 | + tt := tdutil.NewT(t.Name()) |
| 27 | + |
| 28 | + run := false |
| 29 | + failed := tt.CatchFailNow(func() { |
| 30 | + assert := td.Assert(tt) |
| 31 | + tdsynctest.Test(assert, func(assert *td.T) { |
| 32 | + run = true |
| 33 | + }) |
| 34 | + }) |
| 35 | + test.IsFalse(t, run) |
| 36 | + test.IsTrue(t, failed) |
| 37 | + test.MatchStr(t, tt.LogBuf(), |
| 38 | + `^\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`) |
| 39 | + }) |
| 40 | + |
| 41 | + t.Run("OK1", func(t *testing.T) { |
| 42 | + run, belax, req := false, false, true |
| 43 | + assert := td.Assert(t).BeLax() |
| 44 | + tdsynctest.Test(assert, func(assert *td.T) { |
| 45 | + go func() { |
| 46 | + run = true |
| 47 | + belax = assert.Config.BeLax |
| 48 | + req = assert.Config.FailureIsFatal |
| 49 | + }() |
| 50 | + tdsynctest.Wait() |
| 51 | + }) |
| 52 | + test.IsTrue(t, run) |
| 53 | + test.IsTrue(t, belax) |
| 54 | + test.IsFalse(t, req) |
| 55 | + }) |
| 56 | + |
| 57 | + t.Run("OK2", func(t *testing.T) { |
| 58 | + run, belax, req := false, true, false |
| 59 | + require := td.Require(t) |
| 60 | + tdsynctest.Test(require, func(require *td.T) { |
| 61 | + go func() { |
| 62 | + run = true |
| 63 | + belax = require.Config.BeLax |
| 64 | + req = require.Config.FailureIsFatal |
| 65 | + }() |
| 66 | + tdsynctest.Wait() |
| 67 | + }) |
| 68 | + test.IsTrue(t, run) |
| 69 | + test.IsFalse(t, belax) |
| 70 | + test.IsTrue(t, req) |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +func TestTestAssertRequire(t *testing.T) { |
| 75 | + t.Run("testing.T required", func(t *testing.T) { |
| 76 | + tt := tdutil.NewT(t.Name()) |
| 77 | + |
| 78 | + run := false |
| 79 | + failed := tt.CatchFailNow(func() { |
| 80 | + assert := td.Assert(tt) |
| 81 | + tdsynctest.TestAssertRequire(assert, func(assert, require *td.T) { |
| 82 | + run = true |
| 83 | + }) |
| 84 | + }) |
| 85 | + test.IsFalse(t, run) |
| 86 | + test.IsTrue(t, failed) |
| 87 | + test.MatchStr(t, tt.LogBuf(), |
| 88 | + `^\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`) |
| 89 | + }) |
| 90 | + |
| 91 | + t.Run("OK1", func(t *testing.T) { |
| 92 | + var run, belaxA, belaxR, ass, req bool |
| 93 | + assert := td.Assert(t).BeLax() |
| 94 | + tdsynctest.TestAssertRequire(assert, func(assert, require *td.T) { |
| 95 | + go func() { |
| 96 | + run = true |
| 97 | + belaxA = assert.Config.BeLax |
| 98 | + ass = !assert.Config.FailureIsFatal |
| 99 | + belaxR = require.Config.BeLax |
| 100 | + req = require.Config.FailureIsFatal |
| 101 | + }() |
| 102 | + tdsynctest.Wait() |
| 103 | + }) |
| 104 | + test.IsTrue(t, run) |
| 105 | + test.IsTrue(t, belaxA) |
| 106 | + test.IsTrue(t, ass) |
| 107 | + test.IsTrue(t, belaxR) |
| 108 | + test.IsTrue(t, req) |
| 109 | + }) |
| 110 | + |
| 111 | + t.Run("OK2", func(t *testing.T) { |
| 112 | + run, belaxA, belaxR, ass, req := false, true, true, false, false |
| 113 | + assert := td.Assert(t) |
| 114 | + tdsynctest.TestAssertRequire(assert, func(assert, require *td.T) { |
| 115 | + go func() { |
| 116 | + run = true |
| 117 | + belaxA = assert.Config.BeLax |
| 118 | + ass = !assert.Config.FailureIsFatal |
| 119 | + belaxR = require.Config.BeLax |
| 120 | + req = require.Config.FailureIsFatal |
| 121 | + }() |
| 122 | + tdsynctest.Wait() |
| 123 | + }) |
| 124 | + test.IsTrue(t, run) |
| 125 | + test.IsFalse(t, belaxA) |
| 126 | + test.IsTrue(t, ass) |
| 127 | + test.IsFalse(t, belaxR) |
| 128 | + test.IsTrue(t, req) |
| 129 | + }) |
| 130 | +} |
0 commit comments