Best Go-testdeep code snippet using td.nilHandler
equal.go
Source:equal.go
...38 ctx = ctx.ResetErrors()39 ctx.BooleanError = true40 return deepValueEqualFinal(ctx, got, expected) == nil41}42// nilHandler is called when one of got or expected is nil (but never43// both, it is caller responsibility).44func nilHandler(ctx ctxerr.Context, got, expected reflect.Value) *ctxerr.Error {45 err := ctxerr.Error{}46 if expected.IsValid() { // here: !got.IsValid()47 if expected.Type().Implements(testDeeper) {48 curOperator := dark.MustGetInterface(expected).(TestDeep)49 ctx.CurOperator = curOperator50 if curOperator.HandleInvalid() {51 return curOperator.Match(ctx, got)52 }53 if ctx.BooleanError {54 return ctxerr.BooleanError55 }56 // Special case if expected is a TestDeep operator which does57 // not handle invalid values: the operator is not called, but58 // for the user the error comes from it59 } else if ctx.BooleanError {60 return ctxerr.BooleanError61 }62 err.Expected = expected63 } else { // here: !expected.IsValid() && got.IsValid()64 switch got.Kind() {65 // Special case: got is a nil interface, so consider as equal66 // to expected nil.67 case reflect.Interface:68 if got.IsNil() {69 return nil70 }71 case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice:72 // If BeLax, it is OK: we consider typed nil is equal to (untyped) nil73 if ctx.BeLax && got.IsNil() {74 return nil75 }76 }77 if ctx.BooleanError {78 return ctxerr.BooleanError79 }80 err.Got = got81 }82 err.Message = "values differ"83 return ctx.CollectError(&err)84}85func isCustomEqual(a, b reflect.Value) (bool, bool) {86 aType, bType := a.Type(), b.Type()87 equal, ok := aType.MethodByName("Equal")88 if ok {89 ft := equal.Type90 if !ft.IsVariadic() &&91 ft.NumIn() == 2 &&92 ft.NumOut() == 1 &&93 ft.In(0).AssignableTo(ft.In(1)) &&94 ft.Out(0) == types.Bool &&95 bType.AssignableTo(ft.In(1)) {96 return true, equal.Func.Call([]reflect.Value{a, b})[0].Bool()97 }98 }99 return false, false100}101// resolveAnchor does the same as ctx.Anchors.ResolveAnchor but checks102// whether v is valid and not already a TestDeep operator first.103func resolveAnchor(ctx ctxerr.Context, v reflect.Value) (reflect.Value, bool) {104 if !v.IsValid() || v.Type().Implements(testDeeper) {105 return v, false106 }107 return ctx.Anchors.ResolveAnchor(v)108}109func deepValueEqual(ctx ctxerr.Context, got, expected reflect.Value) (err *ctxerr.Error) {110 // got must not implement testDeeper111 if got.IsValid() && got.Type().Implements(testDeeper) {112 panic(color.Bad("Found a TestDeep operator in got param, " +113 "can only use it in expected one!"))114 }115 // Try to see if a TestDeep operator is anchored in expected116 if op, ok := resolveAnchor(ctx, expected); ok {117 expected = op118 }119 if !got.IsValid() || !expected.IsValid() {120 if got.IsValid() == expected.IsValid() {121 return122 }123 return nilHandler(ctx, got, expected)124 }125 // Check if a Smuggle hook matches got type126 if handled, e := ctx.Hooks.Smuggle(&got); handled {127 if e != nil {128 // ctx.BooleanError is always false here as hooks cannot be set globally129 return ctx.CollectError(&ctxerr.Error{130 Message: e.Error(),131 Got: got,132 Expected: expected,133 })134 }135 }136 // Check if a Cmp hook matches got & expected types137 if handled, e := ctx.Hooks.Cmp(got, expected); handled {138 if e == nil {139 return140 }141 // ctx.BooleanError is always false here as hooks cannot be set globally142 return ctx.CollectError(&ctxerr.Error{143 Message: e.Error(),144 Got: got,145 Expected: expected,146 })147 }148 // Look for an Equal() method149 if ctx.UseEqual || ctx.Hooks.UseEqual(got.Type()) {150 hasEqual, isEqual := isCustomEqual(got, expected)151 if hasEqual {152 if isEqual {153 return154 }155 if ctx.BooleanError {156 return ctxerr.BooleanError157 }158 return ctx.CollectError(&ctxerr.Error{159 Message: "got.Equal(expected) failed",160 Got: got,161 Expected: expected,162 })163 }164 }165 if got.Type() != expected.Type() {166 if expected.Type().Implements(testDeeper) {167 curOperator := dark.MustGetInterface(expected).(TestDeep)168 // Resolve interface169 if got.Kind() == reflect.Interface {170 got = got.Elem()171 if !got.IsValid() {172 return nilHandler(ctx, got, expected)173 }174 }175 ctx.CurOperator = curOperator176 return curOperator.Match(ctx, got)177 }178 // expected is not a TestDeep operator179 if got.Type() == recvKindType || expected.Type() == recvKindType {180 if ctx.BooleanError {181 return ctxerr.BooleanError182 }183 return ctx.CollectError(&ctxerr.Error{184 Message: "values differ",185 Got: got,186 Expected: expected,...
nilHandler
Using AI Code Generation
1td.nilHandler()2td.nilHandler()3td.nilHandler()4td.nilHandler()5td.nilHandler()6td.nilHandler()7td.nilHandler()8td.nilHandler()9td.nilHandler()10td.nilHandler()11td.nilHandler()12td.nilHandler()13td.nilHandler()14td.nilHandler()15td.nilHandler()16td.nilHandler()17td.nilHandler()18td.nilHandler()19td.nilHandler()20td.nilHandler()21td.nilHandler()22td.nilHandler()23td.nilHandler()
nilHandler
Using AI Code Generation
1func main() {2 td.nilHandler()3}4func main() {5 td := new(test)6 td.nilHandler()7}8func main() {9 td = new(test)10 td.nilHandler()11}12func main() {13 td = new(test)14 td.nilHandler()15}16func main() {17 td = new(test)18 td.nilHandler()19}20func main() {21 td = new(test)22 td.nilHandler()23}24func main() {25 td = new(test)26 td.nilHandler()27}28func main() {29 td = new(test)30 td.nilHandler()31}32func main() {33 td = new(test)34 td.nilHandler()35}36func main() {37 td = new(test)38 td.nilHandler()39}40func main() {41 td = new(test)42 td.nilHandler()43}44func main() {45 td = new(test)46 td.nilHandler()47}48func main() {49 td = new(test)50 td.nilHandler()51}
nilHandler
Using AI Code Generation
1import "fmt"2type td struct {3}4func (t *td) nilHandler() {5 fmt.Println("Inside nilHandler")6}7func main() {8 t.nilHandler()9}
nilHandler
Using AI Code Generation
1func main() {2 td()3}4func main() {5 td(nil)6}7func main() {8 td(nil)9}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!