| 1 |
package check |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
"slices" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/powerman/check/internal/contextx" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
type checks struct { |
| 33 |
tb testing.TB |
| 34 |
|
| 35 |
todo bool |
| 36 |
must bool |
| 37 |
ctx context.Context |
| 38 |
} |
| 39 |
|
| 40 |
func (c *checks) withTODO() *checks { |
| 41 |
d := *c |
| 42 |
d.todo = true |
| 43 |
return &d |
| 44 |
} |
| 45 |
|
| 46 |
func (c *checks) withMustAll() *checks { |
| 47 |
d := *c |
| 48 |
d.must = true |
| 49 |
return &d |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
func (c *checks) context() context.Context { |
| 55 |
if c.ctx != nil { |
| 56 |
return c.ctx |
| 57 |
} |
| 58 |
return c.tb.Context() |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
func (c *checks) mergeContext(ctx context.Context) (*checks, context.CancelFunc) { |
| 69 |
merged, cancel := contextx.Merge(ctx, c.context()) |
| 70 |
d := *c |
| 71 |
d.ctx = merged |
| 72 |
return &d, cancel |
| 73 |
} |
| 74 |
|
| 75 |
func (c *checks) pass() { |
| 76 |
statsMu.Lock() |
| 77 |
defer statsMu.Unlock() |
| 78 |
|
| 79 |
if stats[c.tb] == nil { |
| 80 |
stats[c.tb] = newTestStat(c.tb.Name(), false) |
| 81 |
} |
| 82 |
if c.todo { |
| 83 |
stats[c.tb].forged.value++ |
| 84 |
} else { |
| 85 |
stats[c.tb].passed.value++ |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func (c *checks) fail() { |
| 90 |
statsMu.Lock() |
| 91 |
defer statsMu.Unlock() |
| 92 |
|
| 93 |
if stats[c.tb] == nil { |
| 94 |
stats[c.tb] = newTestStat(c.tb.Name(), false) |
| 95 |
} |
| 96 |
stats[c.tb].failed.value++ |
| 97 |
} |
| 98 |
|
| 99 |
func (c *checks) report(ok bool, msg []any, checker string, name []string, args []any) bool { |
| 100 |
c.tb.Helper() |
| 101 |
|
| 102 |
if ok != c.todo { |
| 103 |
c.pass() |
| 104 |
return ok |
| 105 |
} |
| 106 |
|
| 107 |
if c.todo { |
| 108 |
checker = "TODO " + checker |
| 109 |
} |
| 110 |
|
| 111 |
dump := make([]dump, 0, len(args)) |
| 112 |
for _, arg := range args { |
| 113 |
dump = append(dump, newDump(arg)) |
| 114 |
} |
| 115 |
|
| 116 |
failure := new(bytes.Buffer) |
| 117 |
fmt.Fprintf(failure, "%s\nChecker: %s%s%s\n", |
| 118 |
format(msg...), |
| 119 |
ansiYellow, checker, ansiReset, |
| 120 |
) |
| 121 |
|
| 122 |
for i, v := range slices.Backward(dump) { |
| 123 |
fmt.Fprintf(failure, "%-10s", name[i]+":") |
| 124 |
switch name[i] { |
| 125 |
case nameActual: |
| 126 |
fmt.Fprint(failure, ansiRed) |
| 127 |
default: |
| 128 |
fmt.Fprint(failure, ansiGreen) |
| 129 |
} |
| 130 |
fmt.Fprintf(failure, "%s%s", v, ansiReset) |
| 131 |
} |
| 132 |
|
| 133 |
wantDiff := len(dump) == 2 && name[0] == nameActual && name[1] == nameExpected |
| 134 |
if wantDiff { |
| 135 |
fmt.Fprintf(failure, "\n%s", colouredDiff(dump[0].diff(dump[1]))) |
| 136 |
} |
| 137 |
c.tb.Errorf("%s\n", failure) |
| 138 |
|
| 139 |
c.fail() |
| 140 |
|
| 141 |
if c.must { |
| 142 |
c.tb.FailNow() |
| 143 |
} |
| 144 |
return ok |
| 145 |
} |
| 146 |
|
| 147 |
func (c *checks) reportShould1(funcName string, actual any, msg []any, ok bool) bool { |
| 148 |
c.tb.Helper() |
| 149 |
return c.report(ok, msg, |
| 150 |
"Should "+funcName, |
| 151 |
[]string{nameActual}, |
| 152 |
[]any{actual}) |
| 153 |
} |
| 154 |
|
| 155 |
func (c *checks) reportShould2(funcName string, actual, expected any, msg []any, ok bool) bool { |
| 156 |
c.tb.Helper() |
| 157 |
return c.report(ok, msg, |
| 158 |
"Should "+funcName, |
| 159 |
[]string{nameActual, nameExpected}, |
| 160 |
[]any{actual, expected}) |
| 161 |
} |
| 162 |
|
| 163 |
func (c *checks) report0(msg []any, ok bool) bool { |
| 164 |
c.tb.Helper() |
| 165 |
return c.report(ok, msg, |
| 166 |
callerFuncName(1), |
| 167 |
[]string{}, |
| 168 |
[]any{}) |
| 169 |
} |
| 170 |
|
| 171 |
func (c *checks) report1(actual any, msg []any, ok bool) bool { |
| 172 |
c.tb.Helper() |
| 173 |
return c.report(ok, msg, |
| 174 |
callerFuncName(1), |
| 175 |
[]string{nameActual}, |
| 176 |
[]any{actual}) |
| 177 |
} |
| 178 |
|
| 179 |
func (c *checks) report2(actual, expected any, msg []any, ok bool) bool { |
| 180 |
c.tb.Helper() |
| 181 |
checker, arg2Name := callerFuncName(1), nameExpected |
| 182 |
if strings.Contains(checker, "Match") { |
| 183 |
arg2Name = "Regex" |
| 184 |
} |
| 185 |
return c.report(ok, msg, |
| 186 |
checker, |
| 187 |
[]string{nameActual, arg2Name}, |
| 188 |
[]any{actual, expected}) |
| 189 |
} |
| 190 |
|
| 191 |
func (c *checks) report3(actual, expected1, expected2 any, msg []any, ok bool) bool { |
| 192 |
c.tb.Helper() |
| 193 |
checker, arg2Name, arg3Name := callerFuncName(1), "arg1", "arg2" |
| 194 |
switch { |
| 195 |
case strings.Contains(checker, "Between"): |
| 196 |
arg2Name, arg3Name = "Min", "Max" |
| 197 |
case strings.Contains(checker, "Delta"): |
| 198 |
arg2Name, arg3Name = nameExpected, "Delta" |
| 199 |
case strings.Contains(checker, "SMAPE"): |
| 200 |
arg2Name, arg3Name = nameExpected, "SMAPE" |
| 201 |
} |
| 202 |
return c.report(ok, msg, |
| 203 |
checker, |
| 204 |
[]string{nameActual, arg2Name, arg3Name}, |
| 205 |
[]any{actual, expected1, expected2}) |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
func (c *checks) Must(continueTest bool, msg ...any) { |
| 214 |
c.tb.Helper() |
| 215 |
c.report0(msg, continueTest) |
| 216 |
if !continueTest { |
| 217 |
c.tb.FailNow() |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
type TB struct { |
| 227 |
testing.TB |
| 228 |
*checks |
| 229 |
} |
| 230 |
|
| 231 |
var _ testing.TB = (*TB)(nil) |
| 232 |
|
| 233 |
type ( |
| 234 |
|
| 235 |
ShouldFunc1 func(t *TB, actual any) bool |
| 236 |
|
| 237 |
ShouldFunc2 func(t *TB, actual, expected any) bool |
| 238 |
) |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
func (t *TB) Should(anyShouldFunc any, args ...any) bool { |
| 260 |
t.Helper() |
| 261 |
switch f := anyShouldFunc.(type) { |
| 262 |
case func(t *TB, actual any) bool: |
| 263 |
return t.should1(f, args...) |
| 264 |
case func(t *TB, actual, expected any) bool: |
| 265 |
return t.should2(f, args...) |
| 266 |
default: |
| 267 |
panic("anyShouldFunc is not a ShouldFunc1 or ShouldFunc2") |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
func (t *TB) should1(f ShouldFunc1, args ...any) bool { |
| 272 |
t.Helper() |
| 273 |
if len(args) < 1 { |
| 274 |
panic("not enough params for " + funcName(f)) |
| 275 |
} |
| 276 |
actual, msg := args[0], args[1:] |
| 277 |
return t.reportShould1(funcName(f), actual, msg, |
| 278 |
f(t, actual)) |
| 279 |
} |
| 280 |
|
| 281 |
func (t *TB) should2(f ShouldFunc2, args ...any) bool { |
| 282 |
t.Helper() |
| 283 |
const minArgs = 2 |
| 284 |
if len(args) < minArgs { |
| 285 |
panic("not enough params for " + funcName(f)) |
| 286 |
} |
| 287 |
actual, expected, msg := args[0], args[1], args[2:] |
| 288 |
return t.reportShould2(funcName(f), actual, expected, msg, |
| 289 |
f(t, actual, expected)) |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
func New(tb testing.TB) *TB { |
| 302 |
return &TB{TB: tb, checks: &checks{tb: tb}} |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
func Must(tb testing.TB) *TB { |
| 310 |
return &TB{TB: tb, checks: &checks{tb: tb, must: true}} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
func (t *TB) TODO() *TB { |
| 325 |
return &TB{TB: t.TB, checks: t.withTODO()} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
func (t *TB) MustAll() *TB { |
| 334 |
return &TB{TB: t.TB, checks: t.withMustAll()} |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
func (t *TB) Context() context.Context { |
| 341 |
return t.context() |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
func (t *TB) MergeContext(ctx context.Context) *TB { |
| 352 |
merged, cancel := t.mergeContext(ctx) |
| 353 |
t.Cleanup(cancel) |
| 354 |
return &TB{TB: t.TB, checks: merged} |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
func (t *TB) Error(args ...any) { |
| 361 |
t.Helper() |
| 362 |
t.report0(args, false) |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
func (t *TB) Errorf(format string, args ...any) { |
| 369 |
t.Helper() |
| 370 |
t.report0(append([]any{format}, args...), false) |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
func (t *TB) Fatal(args ...any) { |
| 377 |
t.Helper() |
| 378 |
t.report0(args, false) |
| 379 |
if !t.todo { |
| 380 |
t.TB.FailNow() |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
func (t *TB) Fatalf(format string, args ...any) { |
| 388 |
t.Helper() |
| 389 |
t.report0(append([]any{format}, args...), false) |
| 390 |
if !t.todo { |
| 391 |
t.TB.FailNow() |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
func (t *TB) Fail() { |
| 400 |
t.fail() |
| 401 |
t.TB.Fail() |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
func (t *TB) FailNow() { |
| 409 |
t.fail() |
| 410 |
t.TB.FailNow() |
| 411 |
} |