| 1 |
// Copyright (c) 2018-2021, 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 td_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"regexp" |
| •11 |
"runtime" |
| 12 |
"strings" |
| 13 |
"sync" |
| 14 |
"testing" |
| 15 |
"time" |
| 16 |
|
| 17 |
"github.com/maxatome/go-testdeep/internal/test" |
| 18 |
"github.com/maxatome/go-testdeep/internal/trace" |
| 19 |
"github.com/maxatome/go-testdeep/td" |
| 20 |
) |
| 21 |
|
| 22 |
func TestT(tt *testing.T) { |
| 23 |
// We don't want to include "anchors" field in comparison |
| 24 |
cmp := func(tt *testing.T, got, expected td.ContextConfig) { |
| 25 |
tt.Helper() |
| 26 |
td.Cmp(tt, got, |
| 27 |
td.SStruct(expected, td.StructFields{ |
| 28 |
"anchors": td.Ignore(), |
| 29 |
"hooks": td.Ignore(), |
| 30 |
}), |
| 31 |
) |
| 32 |
} |
| 33 |
|
| 34 |
tt.Run("without config", func(tt *testing.T) { |
| 35 |
t := td.NewT(tt) |
| 36 |
cmp(tt, t.Config, td.DefaultContextConfig) |
| 37 |
|
| 38 |
tDup := td.NewT(t) |
| 39 |
cmp(tt, tDup.Config, td.DefaultContextConfig) |
| 40 |
}) |
| 41 |
|
| 42 |
tt.Run("explicit default config", func(tt *testing.T) { |
| 43 |
t := td.NewT(tt, td.ContextConfig{}) |
| 44 |
cmp(tt, t.Config, td.DefaultContextConfig) |
| 45 |
|
| 46 |
tDup := td.NewT(t) |
| 47 |
cmp(tt, tDup.Config, td.DefaultContextConfig) |
| 48 |
}) |
| 49 |
|
| 50 |
tt.Run("specific config", func(tt *testing.T) { |
| 51 |
conf := td.ContextConfig{ |
| 52 |
RootName: "TEST", |
| 53 |
MaxErrors: 33, |
| 54 |
} |
| 55 |
t := td.NewT(tt, conf) |
| 56 |
cmp(tt, t.Config, conf) |
| 57 |
|
| 58 |
tDup := td.NewT(t) |
| 59 |
cmp(tt, tDup.Config, conf) |
| 60 |
|
| 61 |
newConf := conf |
| 62 |
newConf.MaxErrors = 34 |
| 63 |
tDup = td.NewT(t, newConf) |
| 64 |
cmp(tt, tDup.Config, newConf) |
| 65 |
|
| 66 |
t2 := t.RootName("T2") |
| 67 |
cmp(tt, t.Config, conf) |
| 68 |
cmp(tt, t2.Config, td.ContextConfig{ |
| 69 |
RootName: "T2", |
| 70 |
MaxErrors: 33, |
| 71 |
}) |
| 72 |
|
| 73 |
t3 := t.RootName("") |
| 74 |
cmp(tt, t3.Config, td.ContextConfig{ |
| 75 |
RootName: "DATA", |
| 76 |
MaxErrors: 33, |
| 77 |
}) |
| 78 |
}) |
| 79 |
|
| 80 |
// |
| 81 |
// Bad usages |
| 82 |
ttb := test.NewTestingTB("usage params") |
| 83 |
ttb.CatchFatal(func() { |
| 84 |
td.NewT(ttb, td.ContextConfig{}, td.ContextConfig{}) |
| 85 |
}) |
| 86 |
test.IsTrue(tt, ttb.IsFatal) |
| 87 |
test.IsTrue(tt, strings.Contains(ttb.Messages[0], "usage: NewT(")) |
| 88 |
|
| 89 |
test.CheckPanic(tt, func() { td.NewT(nil) }, "usage: NewT") |
| 90 |
} |
| 91 |
|
| 92 |
func TestTCmp(tt *testing.T) { |
| 93 |
ttt := test.NewTestingTB(tt.Name()) |
| 94 |
t := td.NewT(ttt) |
| 95 |
test.IsTrue(tt, t.Cmp(1, 1)) |
| 96 |
test.IsFalse(tt, ttt.Failed()) |
| 97 |
|
| 98 |
ttt = test.NewTestingTB(tt.Name()) |
| 99 |
t = td.NewT(ttt) |
| 100 |
test.IsFalse(tt, t.Cmp(1, 2)) |
| 101 |
test.IsTrue(tt, ttt.Failed()) |
| 102 |
} |
| 103 |
|
| 104 |
func TestTCmpDeeply(tt *testing.T) { |
| 105 |
ttt := test.NewTestingTB(tt.Name()) |
| 106 |
t := td.NewT(ttt) |
| 107 |
test.IsTrue(tt, t.CmpDeeply(1, 1)) |
| 108 |
test.IsFalse(tt, ttt.Failed()) |
| 109 |
|
| 110 |
ttt = test.NewTestingTB(tt.Name()) |
| 111 |
t = td.NewT(ttt) |
| 112 |
test.IsFalse(tt, t.CmpDeeply(1, 2)) |
| 113 |
test.IsTrue(tt, ttt.Failed()) |
| 114 |
} |
| 115 |
|
| 116 |
func TestParallel(t *testing.T) { |
| 117 |
t.Run("without Parallel", func(tt *testing.T) { |
| 118 |
ttt := test.NewTestingTB(tt.Name()) |
| 119 |
|
| 120 |
t := td.NewT(ttt) |
| 121 |
t.Parallel() |
| 122 |
|
| 123 |
// has no effect |
| 124 |
}) |
| 125 |
|
| 126 |
t.Run("with Parallel", func(tt *testing.T) { |
| 127 |
ttt := test.NewParallelTestingTB(tt.Name()) |
| 128 |
|
| 129 |
t := td.NewT(ttt) |
| 130 |
t.Parallel() |
| 131 |
|
| 132 |
test.IsTrue(tt, ttt.IsParallel) |
| 133 |
}) |
| 134 |
|
| 135 |
t.Run("Run with Parallel", func(tt *testing.T) { |
| 136 |
// This test verifies that subtests with t.Parallel() are run |
| 137 |
// in parallel. We use a WaitGroup to make both subtests block |
| 138 |
// until they're both ready. This test will block forever if |
| 139 |
// the tests are not run together. |
| •140 |
if runtime.GOMAXPROCS(0) < 2 { |
| 141 |
tt.Skip("not enough CPUs to run parallel tests") |
| 142 |
} |
| 143 |
|
| 144 |
var ready sync.WaitGroup |
| 145 |
ready.Add(2) |
| 146 |
|
| 147 |
t := td.NewT(tt) |
| 148 |
|
| 149 |
t.Run("level 1", func(t *td.T) { |
| 150 |
t.Parallel() |
| 151 |
|
| 152 |
ready.Done() // I'm ready. |
| 153 |
ready.Wait() // Are you? |
| 154 |
}) |
| 155 |
|
| 156 |
t.Run("level 2", func(t *td.T) { |
| 157 |
t.Parallel() |
| 158 |
|
| 159 |
ready.Done() // I'm ready. |
| 160 |
ready.Wait() // Are you? |
| 161 |
}) |
| 162 |
}) |
| 163 |
} |
| 164 |
|
| 165 |
func TestRun(t *testing.T) { |
| 166 |
t.Run("test.TB with Run", func(tt *testing.T) { |
| 167 |
t := td.NewT(tt) |
| 168 |
|
| 169 |
runPassed := false |
| 170 |
nestedFailureIsFatal := false |
| 171 |
|
| 172 |
ok := t.Run("Test level1", |
| 173 |
func(t *td.T) { |
| 174 |
ok := t.FailureIsFatal().Run("Test level2", |
| 175 |
func(t *td.T) { |
| 176 |
runPassed = t.True(true) // test succeeds! |
| 177 |
|
| 178 |
// Check we inherit config from caller |
| 179 |
nestedFailureIsFatal = t.Config.FailureIsFatal |
| 180 |
}) |
| 181 |
|
| 182 |
t.True(ok) |
| 183 |
}) |
| 184 |
|
| 185 |
test.IsTrue(tt, ok) |
| 186 |
test.IsTrue(tt, runPassed) |
| 187 |
test.IsTrue(tt, nestedFailureIsFatal) |
| 188 |
}) |
| 189 |
|
| 190 |
t.Run("test.TB without Run", func(tt *testing.T) { |
| 191 |
t := td.NewT(test.NewTestingTB("gg")) |
| 192 |
|
| 193 |
runPassed := false |
| 194 |
|
| 195 |
ok := t.Run("Test level1", |
| 196 |
func(t *td.T) { |
| 197 |
ok := t.Run("Test level2", |
| 198 |
func(t *td.T) { |
| 199 |
runPassed = t.True(true) // test succeeds! |
| 200 |
}) |
| 201 |
|
| 202 |
t.True(ok) |
| 203 |
}) |
| 204 |
|
| 205 |
t.True(ok) |
| 206 |
t.True(runPassed) |
| 207 |
}) |
| 208 |
} |
| 209 |
|
| 210 |
func TestRunAssertRequire(t *testing.T) { |
| 211 |
t.Run("test.TB with Run", func(tt *testing.T) { |
| 212 |
t := td.NewT(tt) |
| 213 |
|
| 214 |
runPassed := false |
| 215 |
assertIsFatal := true |
| 216 |
requireIsFatal := false |
| 217 |
|
| 218 |
ok := t.RunAssertRequire("Test level1", |
| 219 |
func(assert, require *td.T) { |
| 220 |
assertIsFatal = assert.Config.FailureIsFatal |
| 221 |
requireIsFatal = require.Config.FailureIsFatal |
| 222 |
|
| 223 |
ok := assert.RunAssertRequire("Test level2", |
| 224 |
func(assert, require *td.T) { |
| 225 |
runPassed = assert.True(true) // test succeeds! |
| 226 |
runPassed = runPassed && require.True(true) // test succeeds! |
| 227 |
|
| 228 |
assertIsFatal = assertIsFatal || assert.Config.FailureIsFatal |
| 229 |
requireIsFatal = requireIsFatal && require.Config.FailureIsFatal |
| 230 |
}) |
| 231 |
assert.True(ok) |
| 232 |
require.True(ok) |
| 233 |
|
| 234 |
ok = require.RunAssertRequire("Test level2", |
| 235 |
func(assert, require *td.T) { |
| 236 |
runPassed = runPassed && assert.True(true) // test succeeds! |
| 237 |
runPassed = runPassed && require.True(true) // test succeeds! |
| 238 |
|
| 239 |
assertIsFatal = assertIsFatal || assert.Config.FailureIsFatal |
| 240 |
requireIsFatal = requireIsFatal && require.Config.FailureIsFatal |
| 241 |
}) |
| 242 |
assert.True(ok) |
| 243 |
require.True(ok) |
| 244 |
}) |
| 245 |
|
| 246 |
test.IsTrue(tt, ok) |
| 247 |
test.IsTrue(tt, runPassed) |
| 248 |
test.IsFalse(tt, assertIsFatal) |
| 249 |
test.IsTrue(tt, requireIsFatal) |
| 250 |
}) |
| 251 |
|
| 252 |
t.Run("test.TB without Run", func(tt *testing.T) { |
| 253 |
t := td.NewT(test.NewTestingTB("gg")) |
| 254 |
|
| 255 |
runPassed := false |
| 256 |
assertIsFatal := true |
| 257 |
requireIsFatal := false |
| 258 |
|
| 259 |
ok := t.RunAssertRequire("Test level1", |
| 260 |
func(assert, require *td.T) { |
| 261 |
assertIsFatal = assert.Config.FailureIsFatal |
| 262 |
requireIsFatal = require.Config.FailureIsFatal |
| 263 |
|
| 264 |
ok := assert.RunAssertRequire("Test level2", |
| 265 |
func(assert, require *td.T) { |
| 266 |
runPassed = assert.True(true) // test succeeds! |
| 267 |
runPassed = runPassed && require.True(true) // test succeeds! |
| 268 |
|
| 269 |
assertIsFatal = assertIsFatal || assert.Config.FailureIsFatal |
| 270 |
requireIsFatal = requireIsFatal && require.Config.FailureIsFatal |
| 271 |
}) |
| 272 |
assert.True(ok) |
| 273 |
require.True(ok) |
| 274 |
|
| 275 |
ok = require.RunAssertRequire("Test level2", |
| 276 |
func(assert, require *td.T) { |
| 277 |
runPassed = runPassed && assert.True(true) // test succeeds! |
| 278 |
runPassed = runPassed && require.True(true) // test succeeds! |
| 279 |
|
| 280 |
assertIsFatal = assertIsFatal || assert.Config.FailureIsFatal |
| 281 |
requireIsFatal = requireIsFatal && require.Config.FailureIsFatal |
| 282 |
}) |
| 283 |
assert.True(ok) |
| 284 |
require.True(ok) |
| 285 |
}) |
| 286 |
|
| 287 |
test.IsTrue(tt, ok) |
| 288 |
test.IsTrue(tt, runPassed) |
| 289 |
test.IsFalse(tt, assertIsFatal) |
| 290 |
test.IsTrue(tt, requireIsFatal) |
| 291 |
}) |
| 292 |
} |
| 293 |
|
| 294 |
// Deprecated RunT. |
| 295 |
func TestRunT(t *testing.T) { |
| 296 |
t.Run("test.TB with Run", func(tt *testing.T) { |
| 297 |
t := td.NewT(tt) |
| 298 |
|
| 299 |
runPassed := false |
| 300 |
|
| 301 |
ok := t.RunT("Test level1", //nolint: staticcheck |
| 302 |
func(t *td.T) { |
| 303 |
ok := t.RunT("Test level2", //nolint: staticcheck |
| 304 |
func(t *td.T) { |
| 305 |
runPassed = t.True(true) // test succeeds! |
| 306 |
}) |
| 307 |
|
| 308 |
t.True(ok) |
| 309 |
}) |
| 310 |
|
| 311 |
test.IsTrue(tt, ok) |
| 312 |
test.IsTrue(tt, runPassed) |
| 313 |
}) |
| 314 |
|
| 315 |
t.Run("test.TB without Run", func(tt *testing.T) { |
| 316 |
t := td.NewT(test.NewTestingTB("gg")) |
| 317 |
|
| 318 |
runPassed := false |
| 319 |
|
| 320 |
ok := t.RunT("Test level1", //nolint: staticcheck |
| 321 |
func(t *td.T) { |
| 322 |
ok := t.RunT("Test level2", //nolint: staticcheck |
| 323 |
func(t *td.T) { |
| 324 |
runPassed = t.True(true) // test succeeds! |
| 325 |
}) |
| 326 |
|
| 327 |
t.True(ok) |
| 328 |
}) |
| 329 |
|
| 330 |
test.IsTrue(tt, ok) |
| 331 |
test.IsTrue(tt, runPassed) |
| 332 |
}) |
| 333 |
} |
| 334 |
|
| 335 |
func TestFailureIsFatal(tt *testing.T) { |
| 336 |
// All t.True(false) tests of course fail |
| 337 |
|
| 338 |
// Using default config |
| 339 |
ttt := test.NewTestingTB(tt.Name()) |
| 340 |
t := td.NewT(ttt) |
| 341 |
t.True(false) // failure |
| 342 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 343 |
test.IsFalse(tt, ttt.IsFatal, "by default it is not fatal") |
| 344 |
|
| 345 |
// Using specific config |
| 346 |
ttt = test.NewTestingTB(tt.Name()) |
| 347 |
t = td.NewT(ttt, td.ContextConfig{FailureIsFatal: true}) |
| 348 |
ttt.CatchFatal(func() { t.True(false) }) // failure |
| 349 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 350 |
test.IsTrue(tt, ttt.IsFatal, "it must be fatal") |
| 351 |
|
| 352 |
// Using FailureIsFatal() |
| 353 |
ttt = test.NewTestingTB(tt.Name()) |
| 354 |
t = td.NewT(ttt).FailureIsFatal() |
| 355 |
ttt.CatchFatal(func() { t.True(false) }) // failure |
| 356 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 357 |
test.IsTrue(tt, ttt.IsFatal, "it must be fatal") |
| 358 |
|
| 359 |
// Using FailureIsFatal(true) |
| 360 |
ttt = test.NewTestingTB(tt.Name()) |
| 361 |
t = td.NewT(ttt).FailureIsFatal(true) |
| 362 |
ttt.CatchFatal(func() { t.True(false) }) // failure |
| 363 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 364 |
test.IsTrue(tt, ttt.IsFatal, "it must be fatal") |
| 365 |
|
| 366 |
// Using T.Assert() |
| 367 |
ttt = test.NewTestingTB(tt.Name()) |
| 368 |
t = td.NewT(ttt, td.ContextConfig{FailureIsFatal: true}).Assert() |
| 369 |
t.True(false) // failure |
| 370 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 371 |
test.IsFalse(tt, ttt.IsFatal, "by default it is not fatal") |
| 372 |
|
| 373 |
// Using T.Require() |
| 374 |
ttt = test.NewTestingTB(tt.Name()) |
| 375 |
t = td.NewT(ttt).Require() |
| 376 |
ttt.CatchFatal(func() { t.True(false) }) // failure |
| 377 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 378 |
test.IsTrue(tt, ttt.IsFatal, "it must be fatal") |
| 379 |
|
| 380 |
// Using Require() |
| 381 |
ttt = test.NewTestingTB(tt.Name()) |
| 382 |
t = td.Require(ttt) |
| 383 |
ttt.CatchFatal(func() { t.True(false) }) // failure |
| 384 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 385 |
test.IsTrue(tt, ttt.IsFatal, "it must be fatal") |
| 386 |
|
| 387 |
// Using Require() with specific config (cannot override FailureIsFatal) |
| 388 |
ttt = test.NewTestingTB(tt.Name()) |
| 389 |
t = td.Require(ttt, td.ContextConfig{FailureIsFatal: false}) |
| 390 |
ttt.CatchFatal(func() { t.True(false) }) // failure |
| 391 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 392 |
test.IsTrue(tt, ttt.IsFatal, "it must be fatal") |
| 393 |
|
| 394 |
// Canceling specific config |
| 395 |
ttt = test.NewTestingTB(tt.Name()) |
| 396 |
t = td.NewT(ttt, td.ContextConfig{FailureIsFatal: false}). |
| 397 |
FailureIsFatal(false) |
| 398 |
t.True(false) // failure |
| 399 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 400 |
test.IsFalse(tt, ttt.IsFatal, "it must be not fatal") |
| 401 |
|
| 402 |
// Using Assert() |
| 403 |
ttt = test.NewTestingTB(tt.Name()) |
| 404 |
t = td.Assert(ttt) |
| 405 |
t.True(false) // failure |
| 406 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 407 |
test.IsFalse(tt, ttt.IsFatal, "it must be not fatal") |
| 408 |
|
| 409 |
// Using Assert() with specific config (cannot override FailureIsFatal) |
| 410 |
ttt = test.NewTestingTB(tt.Name()) |
| 411 |
t = td.Assert(ttt, td.ContextConfig{FailureIsFatal: true}) |
| 412 |
t.True(false) // failure |
| 413 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 414 |
test.IsFalse(tt, ttt.IsFatal, "it must be not fatal") |
| 415 |
|
| 416 |
// AssertRequire() / assert |
| 417 |
ttt = test.NewTestingTB(tt.Name()) |
| 418 |
t, _ = td.AssertRequire(ttt) |
| 419 |
t.True(false) // failure |
| 420 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 421 |
test.IsFalse(tt, ttt.IsFatal, "it must be not fatal") |
| 422 |
|
| 423 |
// Using AssertRequire() / assert with specific config (cannot |
| 424 |
// override FailureIsFatal) |
| 425 |
ttt = test.NewTestingTB(tt.Name()) |
| 426 |
t, _ = td.AssertRequire(ttt, td.ContextConfig{FailureIsFatal: true}) |
| 427 |
t.True(false) // failure |
| 428 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 429 |
test.IsFalse(tt, ttt.IsFatal, "it must be not fatal") |
| 430 |
|
| 431 |
// AssertRequire() / require |
| 432 |
ttt = test.NewTestingTB(tt.Name()) |
| 433 |
_, t = td.AssertRequire(ttt) |
| 434 |
ttt.CatchFatal(func() { t.True(false) }) // failure |
| 435 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 436 |
test.IsTrue(tt, ttt.IsFatal, "it must be fatal") |
| 437 |
|
| 438 |
// Using AssertRequire() / require with specific config (cannot |
| 439 |
// override FailureIsFatal) |
| 440 |
ttt = test.NewTestingTB(tt.Name()) |
| 441 |
_, t = td.AssertRequire(ttt, td.ContextConfig{FailureIsFatal: true}) |
| 442 |
ttt.CatchFatal(func() { t.True(false) }) // failure |
| 443 |
test.IsTrue(tt, ttt.LastMessage() != "") |
| 444 |
test.IsTrue(tt, ttt.IsFatal, "it must be fatal") |
| 445 |
} |
| 446 |
|
| 447 |
func TestUseEqual(tt *testing.T) { |
| 448 |
ttt := test.NewTestingTB(tt.Name()) |
| 449 |
|
| 450 |
var time1, time2 time.Time |
| 451 |
for { |
| 452 |
time1 = time.Now() |
| 453 |
time2 = time1.Truncate(0) |
| 454 |
if !time1.Equal(time2) { |
| 455 |
tt.Fatal("time.Equal() does not work as expected") |
| 456 |
} |
| 457 |
if time1 != time2 { // to avoid the bad luck case where time1.wall=0 |
| 458 |
break |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
// Using default config |
| 463 |
t := td.NewT(ttt) |
| 464 |
test.IsFalse(tt, t.Cmp(time1, time2)) |
| 465 |
|
| 466 |
// UseEqual |
| 467 |
t = td.NewT(ttt).UseEqual() // enable globally |
| 468 |
test.IsTrue(tt, t.Cmp(time1, time2)) |
| 469 |
|
| 470 |
t = td.NewT(ttt).UseEqual(true) // enable globally |
| 471 |
test.IsTrue(tt, t.Cmp(time1, time2)) |
| 472 |
|
| 473 |
t = td.NewT(ttt).UseEqual(false) // disable globally |
| 474 |
test.IsFalse(tt, t.Cmp(time1, time2)) |
| 475 |
|
| 476 |
t = td.NewT(ttt).UseEqual(time.Time{}) // enable only for time.Time |
| 477 |
test.IsTrue(tt, t.Cmp(time1, time2)) |
| 478 |
|
| 479 |
t = t.UseEqual().UseEqual(false) // enable then disable globally |
| 480 |
test.IsTrue(tt, t.Cmp(time1, time2)) // Equal() still used |
| 481 |
|
| 482 |
test.EqualStr(tt, |
| 483 |
ttt.CatchFatal(func() { td.NewT(ttt).UseEqual(42) }), |
| 484 |
"UseEqual expects type int owns an Equal method (@0)") |
| 485 |
} |
| 486 |
|
| 487 |
func TestBeLax(tt *testing.T) { |
| 488 |
ttt := test.NewTestingTB(tt.Name()) |
| 489 |
|
| 490 |
// Using default config |
| 491 |
t := td.NewT(ttt) |
| 492 |
test.IsFalse(tt, t.Cmp(int64(123), 123)) |
| 493 |
|
| 494 |
// BeLax |
| 495 |
t = td.NewT(ttt).BeLax() |
| 496 |
test.IsTrue(tt, t.Cmp(int64(123), 123)) |
| 497 |
|
| 498 |
t = td.NewT(ttt).BeLax(true) |
| 499 |
test.IsTrue(tt, t.Cmp(int64(123), 123)) |
| 500 |
|
| 501 |
t = td.NewT(ttt).BeLax(false) |
| 502 |
test.IsFalse(tt, t.Cmp(int64(123), 123)) |
| 503 |
} |
| 504 |
|
| 505 |
func TestIgnoreUnexported(tt *testing.T) { |
| 506 |
ttt := test.NewTestingTB(tt.Name()) |
| 507 |
|
| 508 |
type SType1 struct { |
| 509 |
Public int |
| 510 |
private string |
| 511 |
} |
| 512 |
a1, b1 := SType1{Public: 42, private: "test"}, SType1{Public: 42} |
| 513 |
|
| 514 |
type SType2 struct { |
| 515 |
Public int |
| 516 |
private string |
| 517 |
} |
| 518 |
a2, b2 := SType2{Public: 42, private: "test"}, SType2{Public: 42} |
| 519 |
|
| 520 |
// Using default config |
| 521 |
t := td.NewT(ttt) |
| 522 |
test.IsFalse(tt, t.Cmp(a1, b1)) |
| 523 |
|
| 524 |
// IgnoreUnexported |
| 525 |
t = td.NewT(ttt).IgnoreUnexported() // ignore unexported globally |
| 526 |
test.IsTrue(tt, t.Cmp(a1, b1)) |
| 527 |
test.IsTrue(tt, t.Cmp(a2, b2)) |
| 528 |
|
| 529 |
t = td.NewT(ttt).IgnoreUnexported(true) // ignore unexported globally |
| 530 |
test.IsTrue(tt, t.Cmp(a1, b1)) |
| 531 |
test.IsTrue(tt, t.Cmp(a2, b2)) |
| 532 |
|
| 533 |
t = td.NewT(ttt).IgnoreUnexported(false) // handle unexported globally |
| 534 |
test.IsFalse(tt, t.Cmp(a1, b1)) |
| 535 |
test.IsFalse(tt, t.Cmp(a2, b2)) |
| 536 |
|
| 537 |
t = td.NewT(ttt).IgnoreUnexported(SType1{}) // ignore only for SType1 |
| 538 |
test.IsTrue(tt, t.Cmp(a1, b1)) |
| 539 |
test.IsFalse(tt, t.Cmp(a2, b2)) |
| 540 |
|
| 541 |
t = t.UseEqual().UseEqual(false) // enable then disable globally |
| 542 |
test.IsTrue(tt, t.Cmp(a1, b1)) |
| 543 |
test.IsFalse(tt, t.Cmp(a2, b2)) |
| 544 |
|
| 545 |
t = td.NewT(ttt).IgnoreUnexported(SType1{}, SType2{}) // enable for both |
| 546 |
test.IsTrue(tt, t.Cmp(a1, b1)) |
| 547 |
test.IsTrue(tt, t.Cmp(a2, b2)) |
| 548 |
|
| 549 |
test.EqualStr(tt, |
| 550 |
ttt.CatchFatal(func() { td.NewT(ttt).IgnoreUnexported(42) }), |
| 551 |
"IgnoreUnexported expects type int be a struct, not a int (@0)") |
| 552 |
} |
| 553 |
|
| 554 |
func TestTestDeepInGotOK(tt *testing.T) { |
| 555 |
ttt := test.NewTestingTB(tt.Name()) |
| 556 |
|
| 557 |
var t *td.T |
| 558 |
cmp := func() bool { return t.Cmp(td.Ignore(), td.Ignore()) } |
| 559 |
|
| 560 |
// Using default config |
| 561 |
t = td.NewT(ttt) |
| 562 |
test.CheckPanic(tt, func() { cmp() }, |
| 563 |
"Found a TestDeep operator in got param, can only use it in expected one!") |
| 564 |
|
| 565 |
t = td.NewT(ttt).TestDeepInGotOK() |
| 566 |
test.IsTrue(tt, cmp()) |
| 567 |
|
| 568 |
t = t.TestDeepInGotOK(false) |
| 569 |
test.CheckPanic(tt, func() { cmp() }, |
| 570 |
"Found a TestDeep operator in got param, can only use it in expected one!") |
| 571 |
|
| 572 |
t = t.TestDeepInGotOK(true) |
| 573 |
test.IsTrue(tt, cmp()) |
| 574 |
} |
| 575 |
|
| 576 |
func TestLogTrace(tt *testing.T) { |
| 577 |
ttt := test.NewTestingTB(tt.Name()) |
| 578 |
|
| 579 |
t := td.NewT(ttt) |
| 580 |
|
| 581 |
//line /t_struct_test.go:100 |
| 582 |
t.LogTrace() |
| 583 |
test.EqualStr(tt, ttt.LastMessage(), `Stack trace: |
| 584 |
TestLogTrace() /t_struct_test.go:100`) |
| 585 |
test.IsFalse(tt, ttt.HasFailed) |
| 586 |
test.IsFalse(tt, ttt.IsFatal) |
| 587 |
ttt.ResetMessages() |
| 588 |
|
| 589 |
//line /t_struct_test.go:110 |
| 590 |
t.LogTrace("This is the %s:", "stack") |
| 591 |
test.EqualStr(tt, ttt.LastMessage(), `This is the stack: |
| 592 |
TestLogTrace() /t_struct_test.go:110`) |
| 593 |
ttt.ResetMessages() |
| 594 |
|
| 595 |
//line /t_struct_test.go:120 |
| 596 |
t.LogTrace("This is the %s:\n", "stack") |
| 597 |
test.EqualStr(tt, ttt.LastMessage(), `This is the stack: |
| 598 |
TestLogTrace() /t_struct_test.go:120`) |
| 599 |
ttt.ResetMessages() |
| 600 |
|
| 601 |
//line /t_struct_test.go:130 |
| 602 |
t.LogTrace("This is the ", "stack") |
| 603 |
test.EqualStr(tt, ttt.LastMessage(), `This is the stack |
| 604 |
TestLogTrace() /t_struct_test.go:130`) |
| 605 |
ttt.ResetMessages() |
| 606 |
|
| 607 |
trace.IgnorePackage() |
| 608 |
defer trace.UnignorePackage() |
| 609 |
//line /t_struct_test.go:140 |
| 610 |
t.LogTrace("Stack:\n") |
| 611 |
test.EqualStr(tt, ttt.LastMessage(), `Stack: |
| 612 |
Empty stack trace`) |
| 613 |
} |
| 614 |
|
| 615 |
func TestErrorTrace(tt *testing.T) { |
| 616 |
ttt := test.NewTestingTB(tt.Name()) |
| 617 |
|
| 618 |
t := td.NewT(ttt) |
| 619 |
|
| 620 |
//line /t_struct_test.go:200 |
| 621 |
t.ErrorTrace() |
| 622 |
test.EqualStr(tt, ttt.LastMessage(), `Stack trace: |
| 623 |
TestErrorTrace() /t_struct_test.go:200`) |
| 624 |
test.IsTrue(tt, ttt.HasFailed) |
| 625 |
test.IsFalse(tt, ttt.IsFatal) |
| 626 |
ttt.ResetMessages() |
| 627 |
|
| 628 |
//line /t_struct_test.go:210 |
| 629 |
t.ErrorTrace("This is the %s:", "stack") |
| 630 |
test.EqualStr(tt, ttt.LastMessage(), `This is the stack: |
| 631 |
TestErrorTrace() /t_struct_test.go:210`) |
| 632 |
ttt.ResetMessages() |
| 633 |
|
| 634 |
//line /t_struct_test.go:220 |
| 635 |
t.ErrorTrace("This is the %s:\n", "stack") |
| 636 |
test.EqualStr(tt, ttt.LastMessage(), `This is the stack: |
| 637 |
TestErrorTrace() /t_struct_test.go:220`) |
| 638 |
ttt.ResetMessages() |
| 639 |
|
| 640 |
//line /t_struct_test.go:230 |
| 641 |
t.ErrorTrace("This is the ", "stack") |
| 642 |
test.EqualStr(tt, ttt.LastMessage(), `This is the stack |
| 643 |
TestErrorTrace() /t_struct_test.go:230`) |
| 644 |
ttt.ResetMessages() |
| 645 |
|
| 646 |
trace.IgnorePackage() |
| 647 |
defer trace.UnignorePackage() |
| 648 |
//line /t_struct_test.go:240 |
| 649 |
t.ErrorTrace("Stack:\n") |
| 650 |
test.EqualStr(tt, ttt.LastMessage(), `Stack: |
| 651 |
Empty stack trace`) |
| 652 |
} |
| 653 |
|
| 654 |
func TestFatalTrace(tt *testing.T) { |
| 655 |
ttt := test.NewTestingTB(tt.Name()) |
| 656 |
|
| 657 |
t := td.NewT(ttt) |
| 658 |
|
| 659 |
match := func(got, expectedRe string) { |
| 660 |
tt.Helper() |
| 661 |
re := regexp.MustCompile(expectedRe) |
| 662 |
if !re.MatchString(got) { |
| 663 |
test.EqualErrorMessage(tt, got, expectedRe) |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
//line /t_struct_test.go:300 |
| 668 |
match(ttt.CatchFatal(func() { t.FatalTrace() }), `Stack trace: |
| 669 |
TestFatalTrace\.func\d\(\) /t_struct_test\.go:300 |
| 670 |
\(\*TestingT\)\.CatchFatal\(\) internal/test/types\.go:\d+ |
| 671 |
TestFatalTrace\(\) /t_struct_test\.go:300`) |
| 672 |
test.IsTrue(tt, ttt.HasFailed) |
| 673 |
test.IsTrue(tt, ttt.IsFatal) |
| 674 |
ttt.ResetMessages() |
| 675 |
|
| 676 |
//line /t_struct_test.go:310 |
| 677 |
match(ttt.CatchFatal(func() { t.FatalTrace("This is the %s:", "stack") }), |
| 678 |
`This is the stack: |
| 679 |
TestFatalTrace\.func\d\(\) /t_struct_test\.go:310 |
| 680 |
\(\*TestingT\)\.CatchFatal\(\) internal/test/types\.go:\d+ |
| 681 |
TestFatalTrace\(\) /t_struct_test\.go:310`) |
| 682 |
ttt.ResetMessages() |
| 683 |
|
| 684 |
//line /t_struct_test.go:320 |
| 685 |
match(ttt.CatchFatal(func() { t.FatalTrace("This is the %s:\n", "stack") }), |
| 686 |
`This is the stack: |
| 687 |
TestFatalTrace\.func\d\(\) /t_struct_test\.go:320 |
| 688 |
\(\*TestingT\)\.CatchFatal\(\) internal/test/types\.go:\d+ |
| 689 |
TestFatalTrace\(\) /t_struct_test\.go:320`) |
| 690 |
ttt.ResetMessages() |
| 691 |
|
| 692 |
//line /t_struct_test.go:330 |
| 693 |
match(ttt.CatchFatal(func() { t.FatalTrace("This is the ", "stack") }), |
| 694 |
`This is the stack |
| 695 |
TestFatalTrace\.func\d\(\) /t_struct_test\.go:330 |
| 696 |
\(\*TestingT\)\.CatchFatal\(\) internal/test/types\.go:\d+ |
| 697 |
TestFatalTrace\(\) /t_struct_test\.go:330`) |
| 698 |
ttt.ResetMessages() |
| 699 |
|
| 700 |
trace.IgnorePackage() |
| 701 |
defer trace.UnignorePackage() |
| 702 |
//line /t_struct_test.go:340 |
| 703 |
test.EqualStr(tt, ttt.CatchFatal(func() { t.FatalTrace("Stack:\n") }), |
| 704 |
`Stack: |
| 705 |
Empty stack trace`) |
| 706 |
} |