| 1 |
// Copyright (c) 2018, 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 test |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"runtime" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/maxatome/go-testdeep/internal/trace" |
| 16 |
) |
| 17 |
|
| 18 |
// TestingT is a type implementing td.TestingT intended to be used in |
| 19 |
// tests. |
| 20 |
type TestingT struct { |
| 21 |
Messages []string |
| 22 |
IsFatal bool |
| 23 |
HasFailed bool |
| 24 |
} |
| 25 |
|
| 26 |
type testingFatal string |
| 27 |
|
| 28 |
// NewTestingT returns a new instance of [*TestingT]. |
| 29 |
func NewTestingT() *TestingT { |
| 30 |
return &TestingT{} |
| 31 |
} |
| 32 |
|
| 33 |
// Error mocks [testing.T] Error method. |
| 34 |
func (t *TestingT) Error(args ...any) { |
| 35 |
t.Messages = append(t.Messages, fmt.Sprint(args...)) |
| 36 |
t.IsFatal = false |
| 37 |
t.HasFailed = true |
| 38 |
} |
| 39 |
|
| 40 |
// Fatal mocks [testing.T.Fatal] method. |
| 41 |
func (t *TestingT) Fatal(args ...any) { |
| 42 |
t.Messages = append(t.Messages, fmt.Sprint(args...)) |
| 43 |
t.IsFatal = true |
| 44 |
t.HasFailed = true |
| 45 |
|
| 46 |
panic(testingFatal(t.Messages[len(t.Messages)-1])) |
| 47 |
} |
| 48 |
|
| 49 |
func (t *TestingT) CatchFatal(fn func()) (fatalStr string) { |
| 50 |
panicked := true |
| 51 |
trace.IgnorePackage() |
| 52 |
defer func() { |
| 53 |
trace.UnignorePackage() |
| 54 |
if panicked { |
| 55 |
if x := recover(); x != nil { |
| 56 |
if str, ok := x.(testingFatal); ok { |
| 57 |
fatalStr = string(str) |
| 58 |
} else { |
| 59 |
panic(x) // rethrow |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
}() |
| 64 |
|
| 65 |
fn() |
| 66 |
panicked = false |
| 67 |
return |
| 68 |
} |
| 69 |
|
| 70 |
// ContainsMessages checks expectedMsgs are all present in Messages, in |
| 71 |
// this order. It stops when a message is not found and returns the |
| 72 |
// remaining messages. |
| 73 |
func (t *TestingT) ContainsMessages(expectedMsgs ...string) []string { |
| 74 |
curExp := 0 |
| 75 |
for _, msg := range t.Messages { |
| 76 |
for { |
| 77 |
if curExp == len(expectedMsgs) { |
| 78 |
return nil |
| 79 |
} |
| 80 |
pos := strings.Index(msg, expectedMsgs[curExp]) |
| 81 |
if pos < 0 { |
| 82 |
break |
| 83 |
} |
| 84 |
msg = msg[pos+len(expectedMsgs[curExp]):] |
| 85 |
curExp++ |
| 86 |
} |
| 87 |
} |
| 88 |
return expectedMsgs[curExp:] |
| 89 |
} |
| 90 |
|
| 91 |
// Helper mocks [testing.T.Helper] method. |
| 92 |
func (t *TestingT) Helper() { |
| 93 |
// Do nothing |
| 94 |
} |
| 95 |
|
| 96 |
// LastMessage returns the last message. |
| 97 |
func (t *TestingT) LastMessage() string { |
| 98 |
if len(t.Messages) == 0 { |
| 99 |
return "" |
| 100 |
} |
| 101 |
return t.Messages[len(t.Messages)-1] |
| 102 |
} |
| 103 |
|
| 104 |
// ResetMessages resets the messages. |
| 105 |
func (t *TestingT) ResetMessages() { |
| 106 |
t.Messages = t.Messages[:0] |
| 107 |
} |
| 108 |
|
| 109 |
// TestingTB is a type implementing [testing.TB] intended to be used in |
| 110 |
// tests. |
| 111 |
type TestingTB struct { |
| 112 |
TestingT |
| 113 |
name string |
| 114 |
testing.TB |
| 115 |
cleanup func() |
| 116 |
} |
| 117 |
|
| 118 |
// NewTestingTB returns a new instance of [*TestingTB]. |
| 119 |
func NewTestingTB(name string) *TestingTB { |
| 120 |
return &TestingTB{name: name} |
| 121 |
} |
| 122 |
|
| 123 |
// Cleanup mocks [testing.T.Cleanup] method. Not thread-safe but we |
| 124 |
// don't care in tests. |
| 125 |
func (t *TestingTB) Cleanup(fn func()) { |
| 126 |
old := t.cleanup |
| 127 |
t.cleanup = func() { |
| 128 |
if old != nil { |
| 129 |
defer old() |
| 130 |
} |
| 131 |
fn() |
| 132 |
} |
| 133 |
runtime.SetFinalizer(t, func(t *TestingTB) { t.cleanup() }) |
| 134 |
} |
| 135 |
|
| 136 |
// Fatal mocks [testing.T.Error] method. |
| 137 |
func (t *TestingTB) Error(args ...any) { |
| 138 |
t.TestingT.Error(args...) |
| 139 |
} |
| 140 |
|
| 141 |
// Errorf mocks [testing.T.Errorf] method. |
| 142 |
func (t *TestingTB) Errorf(format string, args ...any) { |
| 143 |
t.TestingT.Error(fmt.Sprintf(format, args...)) |
| 144 |
} |
| 145 |
|
| 146 |
// Fail mocks [testing.T.Fail] method. |
| 147 |
func (t *TestingTB) Fail() { |
| 148 |
t.HasFailed = true |
| 149 |
} |
| 150 |
|
| 151 |
// FailNow mocks [testing.T.FailNow] method. |
| 152 |
func (t *TestingTB) FailNow() { |
| 153 |
t.HasFailed = true |
| 154 |
t.IsFatal = true |
| 155 |
} |
| 156 |
|
| 157 |
// Failed mocks [testing.T.Failed] method. |
| 158 |
func (t *TestingTB) Failed() bool { |
| 159 |
return t.HasFailed |
| 160 |
} |
| 161 |
|
| 162 |
// Fatal mocks [testing.T.Fatal] method. |
| 163 |
func (t *TestingTB) Fatal(args ...any) { |
| 164 |
t.TestingT.Fatal(args...) |
| 165 |
} |
| 166 |
|
| 167 |
// Fatalf mocks [testing.T.Fatalf] method. |
| 168 |
func (t *TestingTB) Fatalf(format string, args ...any) { |
| 169 |
t.TestingT.Fatal(fmt.Sprintf(format, args...)) |
| 170 |
} |
| 171 |
|
| 172 |
// Helper mocks [testing.T.Helper] method. |
| 173 |
func (t *TestingTB) Helper() { |
| 174 |
// Do nothing |
| 175 |
} |
| 176 |
|
| 177 |
// Log mocks [testing.T.Log] method. |
| 178 |
func (t *TestingTB) Log(args ...any) { |
| 179 |
t.Messages = append(t.Messages, fmt.Sprint(args...)) |
| 180 |
} |
| 181 |
|
| 182 |
// Logf mocks [testing.T.Logf] method. |
| 183 |
func (t *TestingTB) Logf(format string, args ...any) { |
| 184 |
t.Log(fmt.Sprintf(format, args...)) |
| 185 |
} |
| 186 |
|
| 187 |
// Name mocks [testing.T.Name] method. |
| 188 |
func (t *TestingTB) Name() string { |
| 189 |
return t.name |
| 190 |
} |
| 191 |
|
| 192 |
// Skip mocks [testing.T.Skip] method. |
| 193 |
func (t *TestingTB) Skip(args ...any) {} |
| 194 |
|
| 195 |
// SkipNow mocks [testing.T.SkipNow] method. |
| 196 |
func (t *TestingTB) SkipNow() {} |
| 197 |
|
| 198 |
// Skipf mocks [testing.T.Skipf] method. |
| 199 |
func (t *TestingTB) Skipf(format string, args ...any) {} |
| 200 |
|
| 201 |
// Skipped mocks [testing.T.Skipped] method. |
| 202 |
func (t *TestingTB) Skipped() bool { |
| 203 |
return false |
| 204 |
} |
| 205 |
|
| 206 |
// ParallelTestingTB is a type implementing [testing.TB] and a |
| 207 |
// Parallel() method intended to be used in tests. |
| 208 |
type ParallelTestingTB struct { |
| 209 |
IsParallel bool |
| 210 |
*TestingTB |
| 211 |
} |
| 212 |
|
| 213 |
// NewParallelTestingTB returns a new instance of [*ParallelTestingTB]. |
| 214 |
func NewParallelTestingTB(name string) *ParallelTestingTB { |
| 215 |
return &ParallelTestingTB{TestingTB: NewTestingTB(name)} |
| 216 |
} |
| 217 |
|
| 218 |
// Parallel mocks the [testing.T.Parallel] method. Not thread-safe. |
| 219 |
func (t *ParallelTestingTB) Parallel() { |
| 220 |
if t.IsParallel { |
| 221 |
// testing.T.Parallel() panics if called multiple times for the |
| 222 |
// same test. |
| 223 |
panic("testing: t.Parallel called multiple times") |
| 224 |
} |
| 225 |
t.IsParallel = true |
| 226 |
} |