How to use TestCodeCustom method of td_test Package

Best Go-testdeep code snippet using td_test.TestCodeCustom

td_code_test.go

Source:td_code_test.go Github

copy

Full Screen

...243 "Code(func(int) (td_test.MyBool, td_test.MyString))")244 // Erroneous op245 test.EqualStr(t, td.Code(nil).String(), "Code(<ERROR>)")246}247func TestCodeCustom(t *testing.T) {248 // Specific _checkOK func as td.Code(FUNC) with FUNC(t,arg) or249 // FUNC(assert,require,arg) works in non-boolean context but cannot250 // work in boolean context as there is no initial testing.TB instance251 _customCheckOK := func(t *testing.T, got, expected any, args ...any) bool {252 t.Helper()253 if !td.Cmp(t, got, expected, args...) {254 return false255 }256 // Should always fail in boolean context as no original testing.TB available257 err := td.EqDeeplyError(got, expected)258 if err == nil {259 t.Error(`Boolean context succeeded and it shouldn't`)260 return false261 }262 expErr := expectedError{263 Message: mustBe("cannot build *td.T instance"),264 Path: mustBe("DATA"),265 Summary: mustBe("original testing.TB instance is missing"),266 }267 if !strings.HasPrefix(expected.(fmt.Stringer).String(), "Code") {268 expErr = ifaceExpectedError(t, expErr)269 }270 if !matchError(t, err.(*ctxerr.Error), expErr, true, args...) {271 return false272 }273 if td.EqDeeply(got, expected) {274 t.Error(`Boolean context succeeded and it shouldn't`)275 return false276 }277 return true278 }279 customCheckOK(t, _customCheckOK, 123, td.Code(func(t *td.T, n int) {280 t.Cmp(t.Config.FailureIsFatal, false)281 t.Cmp(n, 123)282 }))283 customCheckOK(t, _customCheckOK, 123, td.Code(func(assert, require *td.T, n int) {284 assert.Cmp(assert.Config.FailureIsFatal, false)285 assert.Cmp(require.Config.FailureIsFatal, true)286 assert.Cmp(n, 123)287 require.Cmp(n, 123)288 }))289 got := map[string]int{"foo": 123}290 t.Run("Simple success", func(t *testing.T) {291 mockT := test.NewTestingTB("TestCodeCustom")292 td.Cmp(mockT, got, td.Map(map[string]int{}, td.MapEntries{293 "foo": td.Code(func(t *td.T, n int) {294 t.Cmp(n, 123)295 }),296 }))297 test.EqualInt(t, len(mockT.Messages), 0)298 })299 t.Run("Simple failure", func(t *testing.T) {300 mockT := test.NewTestingTB("TestCodeCustom")301 td.NewT(mockT).302 RootName("PIPO").303 Cmp(got, td.Map(map[string]int{}, td.MapEntries{304 "foo": td.Code(func(t *td.T, n int) {305 t.Cmp(n, 124) // inherit only RootName306 t.RootName(t.Config.OriginalPath()).Cmp(n, 125) // recover current path307 t.RootName("").Cmp(n, 126) // undo RootName inheritance308 }),309 }))310 test.IsTrue(t, mockT.HasFailed)311 test.IsFalse(t, mockT.IsFatal)312 missing := mockT.ContainsMessages(313 `PIPO: values differ`,314 ` got: 123`,315 `expected: 124`,316 `PIPO["foo"]: values differ`,317 ` got: 123`,318 `expected: 125`,319 `DATA: values differ`,320 ` got: 123`,321 `expected: 126`,322 )323 if len(missing) != 0 {324 t.Error("Following expected messages are not found:\n-", strings.Join(missing, "\n- "))325 t.Error("================================ in:")326 t.Error(strings.Join(mockT.Messages, "\n"))327 t.Error("====================================")328 }329 })330 t.Run("AssertRequire success", func(t *testing.T) {331 mockT := test.NewTestingTB("TestCodeCustom")332 td.Cmp(mockT, got, td.Map(map[string]int{}, td.MapEntries{333 "foo": td.Code(func(assert, require *td.T, n int) {334 assert.Cmp(n, 123)335 require.Cmp(n, 123)336 }),337 }))338 test.EqualInt(t, len(mockT.Messages), 0)339 })340 t.Run("AssertRequire failure", func(t *testing.T) {341 mockT := test.NewTestingTB("TestCodeCustom")342 td.NewT(mockT).343 RootName("PIPO").344 Cmp(got, td.Map(map[string]int{}, td.MapEntries{345 "foo": td.Code(func(assert, require *td.T, n int) {346 assert.Cmp(n, 124) // inherit only RootName347 assert.RootName(assert.Config.OriginalPath()).Cmp(n, 125) // recover current path348 assert.RootName(require.Config.OriginalPath()).Cmp(n, 126) // recover current path349 assert.RootName("").Cmp(n, 127) // undo RootName inheritance350 }),351 }))352 test.IsTrue(t, mockT.HasFailed)353 test.IsFalse(t, mockT.IsFatal)354 missing := mockT.ContainsMessages(355 `PIPO: values differ`,356 ` got: 123`,357 `expected: 124`,358 `PIPO["foo"]: values differ`,359 ` got: 123`,360 `expected: 125`,361 `PIPO["foo"]: values differ`,362 ` got: 123`,363 `expected: 126`,364 `DATA: values differ`,365 ` got: 123`,366 `expected: 127`,367 )368 if len(missing) != 0 {369 t.Error("Following expected messages are not found:\n-", strings.Join(missing, "\n- "))370 t.Error("================================ in:")371 t.Error(strings.Join(mockT.Messages, "\n"))372 t.Error("====================================")373 }374 })375 t.Run("AssertRequire fatalfailure", func(t *testing.T) {376 mockT := test.NewTestingTB("TestCodeCustom")377 td.NewT(mockT).378 RootName("PIPO").379 Cmp(got, td.Map(map[string]int{}, td.MapEntries{380 "foo": td.Code(func(assert, require *td.T, n int) {381 mockT.CatchFatal(func() {382 assert.RootName("FIRST").Cmp(n, 124)383 require.RootName("SECOND").Cmp(n, 125)384 assert.RootName("THIRD").Cmp(n, 126)385 })386 }),387 }))388 test.IsTrue(t, mockT.HasFailed)389 test.IsTrue(t, mockT.IsFatal)390 missing := mockT.ContainsMessages(...

Full Screen

Full Screen

TestCodeCustom

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Starting TestCodeCustom")4 td_test.TestCodeCustom()5 fmt.Println("Completed TestCodeCustom")6}

Full Screen

Full Screen

TestCodeCustom

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Starting TestCodeCustom")4 td_test.TestCodeCustom()5 fmt.Println("Completed TestCodeCustom")6}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Go-testdeep automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful