How to use Smuggle method of hooks Package

Best Go-testdeep code snippet using hooks.Smuggle

hooks_test.go

Source:hooks_test.go Github

copy

Full Screen

...117 }118 test.IsTrue(t, handled)119 })120}121func TestSmuggle(t *testing.T) {122 var i *hooks.Info123 got := reflect.ValueOf(123)124 handled, err := i.Smuggle(&got)125 test.NoError(t, err)126 test.IsFalse(t, handled)127 i = hooks.NewInfo()128 err = i.AddSmuggleHooks([]any{func(a int) bool { return a != 0 }})129 test.NoError(t, err)130 got = reflect.ValueOf(123)131 handled, err = i.Smuggle(&got)132 test.NoError(t, err)133 test.IsTrue(t, handled)134 if test.EqualInt(t, int(got.Kind()), int(reflect.Bool)) {135 test.IsTrue(t, got.Bool())136 }137 got = reflect.ValueOf("biz")138 handled, err = i.Smuggle(&got)139 test.NoError(t, err)140 test.IsFalse(t, handled)141 test.EqualStr(t, got.String(), "biz")142 err = i.AddSmuggleHooks([]any{strconv.Atoi})143 test.NoError(t, err)144 got = reflect.ValueOf("123")145 handled, err = i.Smuggle(&got)146 test.NoError(t, err)147 test.IsTrue(t, handled)148 if test.EqualInt(t, int(got.Kind()), int(reflect.Int)) {149 test.EqualInt(t, int(got.Int()), 123)150 }151 got = reflect.ValueOf("NotANumber")152 handled, err = i.Smuggle(&got)153 test.Error(t, err)154 test.IsTrue(t, handled)155}156func TestAddSmuggleHooks(t *testing.T) {157 for _, tst := range []struct {158 name string159 smuggle any160 err string161 }{162 {163 name: "not a function",164 smuggle: "zip",165 err: "expects a function, not a string (@1)",166 },167 {168 name: "no variadic",169 smuggle: func(a ...byte) bool { return true },170 err: "expects: func (A) (B[, error]) not func(...uint8) bool (@1)",171 },172 {173 name: "in",174 smuggle: func(a, b int) bool { return true },175 err: "expects: func (A) (B[, error]) not func(int, int) bool (@1)",176 },177 {178 name: "interface",179 smuggle: func(a any) bool { return true },180 err: "expects: func (A) (B[, error]) not func(interface {}) bool (@1)",181 },182 {183 name: "out",184 smuggle: func(a int) {},185 err: "expects: func (A) (B[, error]) not func(int) (@1)",186 },187 {188 name: "bad return",189 smuggle: func(a int) (int, int) { return 0, 0 },190 err: "expects: func (A) (B[, error]) not func(int) (int, int) (@1)",191 },192 {193 name: "return interface",194 smuggle: func(a int) any { return 0 },195 err: "expects: func (A) (B[, error]) not func(int) interface {} (@1)",196 },197 {198 name: "return interface, error",199 smuggle: func(a int) (any, error) { return 0, nil },200 err: "expects: func (A) (B[, error]) not func(int) (interface {}, error) (@1)",201 },202 } {203 i := hooks.NewInfo()204 err := i.AddSmuggleHooks([]any{205 func(a int) bool { return true },206 tst.smuggle,207 })208 if test.Error(t, err, tst.name) {209 if !strings.Contains(err.Error(), tst.err) {210 t.Errorf("<%s> does not contain <%s> for %s", err, tst.err, tst.name)211 }212 }213 }214}215func TestUseEqual(t *testing.T) {216 var i *hooks.Info217 test.IsFalse(t, i.UseEqual(reflect.TypeOf(42)))218 i = hooks.NewInfo()219 test.IsFalse(t, i.UseEqual(reflect.TypeOf(42)))220 test.NoError(t, i.AddUseEqual([]any{}))221 test.NoError(t, i.AddUseEqual([]any{time.Time{}, net.IP{}}))222 test.IsTrue(t, i.UseEqual(reflect.TypeOf(time.Time{})))223 test.IsTrue(t, i.UseEqual(reflect.TypeOf(net.IP{})))224}225func TestAddUseEqual(t *testing.T) {226 for _, tst := range []struct {227 name string228 typ any229 err string230 }{231 {232 name: "no Equal() method",233 typ: &testing.T{},234 err: "expects type *testing.T owns an Equal method (@1)",235 },236 {237 name: "variadic Equal() method",238 typ: badEqualVariadic{},239 err: "expects type hooks_test.badEqualVariadic Equal method signature be Equal(hooks_test.badEqualVariadic) bool (@1)",240 },241 {242 name: "bad NumIn Equal() method",243 typ: badEqualNumIn{},244 err: "expects type hooks_test.badEqualNumIn Equal method signature be Equal(hooks_test.badEqualNumIn) bool (@1)",245 },246 {247 name: "bad NumOut Equal() method",248 typ: badEqualNumOut{},249 err: "expects type hooks_test.badEqualNumOut Equal method signature be Equal(hooks_test.badEqualNumOut) bool (@1)",250 },251 {252 name: "In(0) not assignable to In(1) Equal() method",253 typ: badEqualInAssign{},254 err: "expects type hooks_test.badEqualInAssign Equal method signature be Equal(hooks_test.badEqualInAssign) bool (@1)",255 },256 {257 name: "Equal() method don't return bool",258 typ: badEqualOutType{},259 err: "expects type hooks_test.badEqualOutType Equal method signature be Equal(hooks_test.badEqualOutType) bool (@1)",260 },261 } {262 i := hooks.NewInfo()263 err := i.AddUseEqual([]any{time.Time{}, tst.typ})264 if test.Error(t, err, tst.name) {265 if !strings.Contains(err.Error(), tst.err) {266 t.Errorf("<%s> does not contain <%s> for %s", err, tst.err, tst.name)267 }268 }269 }270}271func TestIgnoreUnexported(t *testing.T) {272 var i *hooks.Info273 test.IsFalse(t, i.IgnoreUnexported(reflect.TypeOf(struct{}{})))274 i = hooks.NewInfo()275 test.IsFalse(t, i.IgnoreUnexported(reflect.TypeOf(struct{}{})))276 test.NoError(t, i.AddIgnoreUnexported([]any{}))277 test.NoError(t, i.AddIgnoreUnexported([]any{testing.T{}, time.Time{}}))278 test.IsTrue(t, i.IgnoreUnexported(reflect.TypeOf(time.Time{})))279 test.IsTrue(t, i.IgnoreUnexported(reflect.TypeOf(testing.T{})))280}281func TestAddIgnoreUnexported(t *testing.T) {282 i := hooks.NewInfo()283 err := i.AddIgnoreUnexported([]any{time.Time{}, 0})284 if test.Error(t, err) {285 test.EqualStr(t, err.Error(), "expects type int be a struct, not a int (@1)")286 }287}288func TestCopy(t *testing.T) {289 var orig *hooks.Info290 ni := orig.Copy()291 if ni == nil {292 t.Errorf("Copy should never return nil, even for a nil instance")293 }294 orig = hooks.NewInfo()295 copy1 := orig.Copy()296 if copy1 == nil {297 t.Errorf("Copy should never return nil")298 }299 hookedBool := false300 test.NoError(t, copy1.AddSmuggleHooks([]any{301 func(in bool) bool { hookedBool = true; return in },302 }))303 gotBool := reflect.ValueOf(true)304 // orig instance does not have any hook305 handled, _ := orig.Smuggle(&gotBool)306 test.IsFalse(t, hookedBool)307 test.IsFalse(t, handled)308 // new bool smuggle hook OK309 hookedBool = false310 handled, _ = copy1.Smuggle(&gotBool)311 test.IsTrue(t, hookedBool)312 test.IsTrue(t, handled)313 copy2 := copy1.Copy()314 if copy2 == nil {315 t.Errorf("Copy should never return nil")316 }317 hookedInt := false318 test.NoError(t, copy2.AddSmuggleHooks([]any{319 func(in int) int { hookedInt = true; return in },320 }))321 // bool smuggle hook inherited from copy1322 hookedBool = false323 handled, _ = copy2.Smuggle(&gotBool)324 test.IsTrue(t, hookedBool)325 test.IsTrue(t, handled)326 gotInt := reflect.ValueOf(123)327 // new int smuggle hook not available in copy1 instance328 hookedInt = false329 handled, _ = copy1.Smuggle(&gotInt)330 test.IsFalse(t, hookedInt)331 test.IsFalse(t, handled)332 // new int smuggle hook OK333 hookedInt = false334 handled, _ = copy2.Smuggle(&gotInt)335 test.IsTrue(t, hookedInt)336 test.IsTrue(t, handled)337 test.IsTrue(t, handled)338}339type badEqualVariadic struct{}340func (badEqualVariadic) Equal(a ...badEqualVariadic) bool { return false }341type badEqualNumIn struct{}342func (badEqualNumIn) Equal(a badEqualNumIn, b badEqualNumIn) bool { return false }343type badEqualNumOut struct{}344func (badEqualNumOut) Equal(a badEqualNumOut) {}345type badEqualInAssign struct{}346func (badEqualInAssign) Equal(a int) bool { return false }347type badEqualOutType struct{}348func (badEqualOutType) Equal(a badEqualOutType) int { return 42 }...

Full Screen

Full Screen

hooks.go

Source:hooks.go Github

copy

Full Screen

...116 }117 err, _ := res.Interface().(error)118 return true, err119}120// AddSmuggleHooks records new Smuggle hooks using functions contained121// in fns.122//123// Each function in fns has to be a function with the following124// possible signatures:125//126// func (got A) B127// func (got A) (B, error)128//129// A cannot be an interface. This retriction can be removed in the130// future, if really needed.131//132// B can be an interface.133//134// It returns an error if an item of fns is not a function or if its135// signature does not match the expected ones.136func (i *Info) AddSmuggleHooks(fns []any) error {137 for n, fn := range fns {138 vfn := reflect.ValueOf(fn)139 if vfn.Kind() != reflect.Func {140 return fmt.Errorf("expects a function, not a %s (@%d)", vfn.Kind(), n)141 }142 ft := vfn.Type()143 if !ft.IsVariadic() &&144 ft.NumIn() == 1 &&145 ft.In(0).Kind() != reflect.Interface &&146 (ft.NumOut() == 1 || (ft.NumOut() == 2 && ft.Out(1) == types.Error)) &&147 ft.Out(0).Kind() != reflect.Interface {148 i.Lock()149 prop := i.props[ft.In(0)]150 prop.smuggle = vfn151 i.props[ft.In(0)] = prop152 i.Unlock()153 continue154 }155 return fmt.Errorf("expects: func (A) (B[, error]) not %s (@%d)", ft, n)156 }157 return nil158}159// Smuggle checks if a Smuggle hook exists matching *got type.160//161// If no, it returns (false, nil)162//163// If yes, it calls it and returns (true, nil) if it succeeds,164// (true, <an error>) if it fails.165func (i *Info) Smuggle(got *reflect.Value) (bool, error) {166 if i == nil {167 return false, nil168 }169 tg := got.Type()170 i.Lock()171 prop, ok := i.props[tg]172 i.Unlock()173 if !ok || !prop.smuggle.IsValid() {174 return false, nil175 }176 res := prop.smuggle.Call([]reflect.Value{*got})177 if len(res) == 2 {178 if err, _ := res[1].Interface().(error); err != nil {179 return true, err...

Full Screen

Full Screen

t_hooks.go

Source:t_hooks.go Github

copy

Full Screen

...30// content is used to tell the reason of the failure.31//32// Cmp hooks are checked before [UseEqual] feature.33//34// Cmp hooks are run just after [Smuggle] hooks.35//36// func TestCmpHook(tt *testing.T) {37// t := td.NewT(tt)38//39// // Test reflect.Value contents instead of default field/field40// t = t.WithCmpHooks(func (got, expected reflect.Value) bool {41// return td.EqDeeply(got.Interface(), expected.Interface())42// })43// a, b := 1, 144// t.Cmp(reflect.ValueOf(&a), reflect.ValueOf(&b)) // succeeds45//46// // Test reflect.Type correctly instead of default field/field47// t = t.WithCmpHooks(func (got, expected reflect.Type) bool {48// return got == expected49// })50//51// // Test time.Time via its Equal() method instead of default52// // field/field (note it bypasses the UseEqual flag)53// t = t.WithCmpHooks((time.Time).Equal)54// date, _ := time.Parse(time.RFC3339, "2020-09-08T22:13:54+02:00")55// t.Cmp(date, date.UTC()) // succeeds56//57// // Several hooks can be declared at once58// t = t.WithCmpHooks(59// func (got, expected reflect.Value) bool {60// return td.EqDeeply(got.Interface(), expected.Interface())61// },62// func (got, expected reflect.Type) bool {63// return got == expected64// },65// (time.Time).Equal,66// )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

Smuggle

Using AI Code Generation

copy

Full Screen

1import (2type Hooks struct {3}4func (h *Hooks) Before() {5 fmt.Println("Before")6}7func (h *Hooks) After() {8 fmt.Println("After")9}10func (h *Hooks) Smuggle() {11 fmt.Println("Smuggle")12}13func (h *Hooks) Smuggle1() {14 fmt.Println("Smuggle1")15}16func (h *Hooks) Smuggle2() {17 fmt.Println("Smuggle2")18}19func (h *Hooks) Smuggle3() {20 fmt.Println("Smuggle3")21}22func (h *Hooks) Smuggle4() {23 fmt.Println("Smuggle4")24}25func Smuggle(hook interface{}, method string) {26 valueOfHook := reflect.ValueOf(hook)27 valueOfMethod := valueOfHook.MethodByName(method)28 valueOfMethod.Call([]reflect.Value{})29}30func main() {31 hooks := Hooks{}32 Smuggle(&hooks, "Before")33 Smuggle(&hooks, "After")34 Smuggle(&hooks, "Smuggle")35 Smuggle(&hooks, "Smuggle1")36 Smuggle(&hooks, "Smuggle2")37 Smuggle(&hooks, "Smuggle3")38 Smuggle(&hooks, "Smuggle4")39}

Full Screen

Full Screen

Smuggle

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var f func(int) int4 f = func(a int) int {5 }6 f = func(a int) int {7 }8 f = func(a int) int {9 }10 f = func(a int) int {11 }12}13import (14func main() {15 var f func(int) int16 f = func(a int) int {17 }18 f = func(a int) int {19 }20 f = func(a int) int {21 }22 f = func(a int) int {23 }24}25import (26func main() {27 var f func(int) int28 f = func(a int) int {29 }30 f = func(a int) int {31 }

Full Screen

Full Screen

Smuggle

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Enter the flag:")4 os.Stdin.Read(buf[:])5 if strings.Contains(string(buf[:]), "flag{") {6 fmt.Println("You got it!")7 } else {8 fmt.Println("Nope!")9 }10 syscall.Exit(0)11}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful