How to use WithSmuggleHooks method of td Package

Best Go-testdeep code snippet using td.WithSmuggleHooks

t_hooks.go

Source:t_hooks.go Github

copy

Full Screen

...67// }68//69// There is no way to add or remove hooks of an existing [*T]70// instance, only to create a new [*T] instance with this method or71// [T.WithSmuggleHooks] to add some.72//73// WithCmpHooks calls t.Fatal if an item of fns is not a function or74// if its signature does not match the expected ones.75//76// See also [T.WithSmuggleHooks].77//78// [UseEqual]: https://pkg.go.dev/github.com/maxatome/go-testdeep/td#ContextConfig.UseEqual79func (t *T) WithCmpHooks(fns ...any) *T {80 t = t.copyWithHooks()81 err := t.Config.hooks.AddCmpHooks(fns)82 if err != nil {83 t.Helper()84 t.Fatal(color.Bad("WithCmpHooks " + err.Error()))85 }86 return t87}88// WithSmuggleHooks returns a new [*T] instance with new Smuggle hooks89// recorded using functions passed in fns.90//91// Each function in fns has to be a function with the following92// possible signatures:93//94// func (got A) B95// func (got A) (B, error)96//97// A cannot be an interface. This restriction can be removed in the98// future, if really needed.99//100// B cannot be an interface. If you have a use case, we can talk about it.101//102// This function is called as soon as possible each time the type A is103// encountered for got.104//105// The B value returned replaces the got value for subsequent tests.106// Smuggle hooks are NOT run again for this returned value to avoid107// easy infinite loop recursion.108//109// When it returns non-nil error (meaning something wrong happened110// during the conversion of A to B), it raises a global error and its111// content is used to tell the reason of the failure.112//113// Smuggle hooks are run just before Cmp hooks.114//115// func TestSmuggleHook(tt *testing.T) {116// t := td.NewT(tt)117//118// // Each encountered int is changed to a bool119// t = t.WithSmuggleHooks(func (got int) bool {120// return got != 0121// })122// t.Cmp(map[string]int{"ok": 1, "no": 0},123// map[string]bool{"ok", true, "no", false}) // succeeds124//125// // Each encountered string is converted to int126// t = t.WithSmuggleHooks(strconv.Atoi)127// t.Cmp("123", 123) // succeeds128//129// // Several hooks can be declared at once130// t = t.WithSmuggleHooks(131// func (got int) bool { return got != 0 },132// strconv.Atoi,133// )134// }135//136// There is no way to add or remove hooks of an existing [*T]137// instance, only create a new [*T] instance with this method or138// [T.WithCmpHooks] to add some.139//140// WithSmuggleHooks calls t.Fatal if an item of fns is not a141// function or if its signature does not match the expected ones.142//143// See also [T.WithCmpHooks].144func (t *T) WithSmuggleHooks(fns ...any) *T {145 t = t.copyWithHooks()146 err := t.Config.hooks.AddSmuggleHooks(fns)147 if err != nil {148 t.Helper()149 t.Fatal(color.Bad("WithSmuggleHooks " + err.Error()))150 }151 return t152}153func (t *T) copyWithHooks() *T {154 nt := NewT(t)155 nt.Config.hooks = t.Config.hooks.Copy()156 return nt157}...

Full Screen

Full Screen

t_hooks_test.go

Source:t_hooks_test.go Github

copy

Full Screen

...115 }116 })117 }118}119func TestWithSmuggleHooks(tt *testing.T) {120 for _, tst := range []struct {121 name string122 cmp any123 got, expected any124 }{125 {126 name: "abs",127 cmp: func(got int) int {128 if got < 0 {129 return -got130 }131 return got132 },133 got: -1234,134 expected: 1234,135 },136 {137 name: "int2bool",138 cmp: func(got int) bool { return got != 0 },139 got: 1,140 expected: true,141 },142 {143 name: "Atoi",144 cmp: strconv.Atoi,145 got: "1234",146 expected: 1234,147 },148 } {149 tt.Run(tst.name, func(tt *testing.T) {150 ttt := test.NewTestingTB(tt.Name())151 t := td.NewT(ttt)152 td.CmpFalse(tt, t.Cmp(tst.got, tst.expected))153 t = t.WithSmuggleHooks(tst.cmp)154 td.CmpTrue(tt, t.Cmp(tst.got, tst.expected))155 })156 }157 tt.Run("Error", func(tt *testing.T) {158 ttt := test.NewTestingTB(tt.Name())159 t := td.NewT(ttt).WithSmuggleHooks(func(got int) (int, error) {160 return 0, errors.New("never equal")161 })162 td.CmpFalse(tt, t.Cmp(1, 1))163 if !strings.Contains(ttt.LastMessage(), "DATA: never equal\n") {164 tt.Errorf(`<%s> does not contain "DATA: never equal\n"`, ttt.LastMessage())165 }166 })167 for _, tst := range []struct {168 name string169 cmp any170 fatal string171 }{172 {173 name: "not a function",174 cmp: "Booh",175 fatal: "WithSmuggleHooks expects a function, not a string",176 },177 {178 name: "wrong signature",179 cmp: func(a []int, b ...int) bool { return false },180 fatal: "WithSmuggleHooks expects: func (A) (B[, error]) not ",181 },182 } {183 tt.Run("panic: "+tst.name, func(tt *testing.T) {184 ttt := test.NewTestingTB(tt.Name())185 t := td.NewT(ttt)186 fatalMesg := ttt.CatchFatal(func() { t.WithSmuggleHooks(tst.cmp) })187 test.IsTrue(tt, ttt.IsFatal)188 if !strings.Contains(fatalMesg, tst.fatal) {189 tt.Errorf(`<%s> does not contain %q`, fatalMesg, tst.fatal)190 }191 })192 }193}...

Full Screen

Full Screen

WithSmuggleHooks

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 td.WithSmuggleHooks(func() {4 fmt.Println("Hello World")5 })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