Best Go-testdeep code snippet using td.deepValueEqualFinal
equal.go
Source:equal.go
...22 return "nil"23 }24 return "not nil"25}26func deepValueEqualFinal(ctx ctxerr.Context, got, expected reflect.Value) (err *ctxerr.Error) {27 err = deepValueEqual(ctx, got, expected)28 if err == nil {29 // Try to merge pending errors30 errMerge := ctx.MergeErrors()31 if errMerge != nil {32 return errMerge33 }34 }35 return36}37func deepValueEqualFinalOK(ctx ctxerr.Context, got, expected reflect.Value) bool {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,187 })188 }189 if ctx.BeLax && types.IsConvertible(expected, got.Type()) {190 return deepValueEqual(ctx, got, expected.Convert(got.Type()))191 }192 // If got is an interface, try to see what is behind before failing193 // Used by Set/Bag Match method in such cases:194 // []any{123, "foo"} â Bag("foo", 123)195 // Interface kind -^-----^ but String-^ and ^- Int kinds196 if got.Kind() == reflect.Interface {197 return deepValueEqual(ctx, got.Elem(), expected)198 }199 if ctx.BooleanError {200 return ctxerr.BooleanError201 }202 return ctx.CollectError(ctxerr.TypeMismatch(got.Type(), expected.Type()))203 }204 // if ctx.Depth > 10 { panic("deepValueEqual") } // for debugging205 // Avoid looping forever on cyclic references206 if ctx.Visited.Record(got, expected) {207 return208 }209 switch got.Kind() {210 case reflect.Array:211 for i, l := 0, got.Len(); i < l; i++ {212 err = deepValueEqual(ctx.AddArrayIndex(i),213 got.Index(i), expected.Index(i))214 if err != nil {215 return216 }217 }218 return219 case reflect.Slice:220 if got.IsNil() != expected.IsNil() {221 if ctx.BooleanError {222 return ctxerr.BooleanError223 }224 return ctx.CollectError(&ctxerr.Error{225 Message: "nil slice",226 Got: isNilStr(got.IsNil()),227 Expected: isNilStr(expected.IsNil()),228 })229 }230 var (231 gotLen = got.Len()232 expectedLen = expected.Len()233 )234 if gotLen != expectedLen {235 // Shortcut in boolean context236 if ctx.BooleanError {237 return ctxerr.BooleanError238 }239 } else {240 if got.Pointer() == expected.Pointer() {241 return242 }243 }244 var maxLen int245 if gotLen >= expectedLen {246 maxLen = expectedLen247 } else {248 maxLen = gotLen249 }250 // Special case for internal tuple type: it is clearer to read251 // TUPLE instead of DATA when an error occurs when using this type252 if got.Type() == tupleType &&253 ctx.Path.Len() == 1 && ctx.Path.String() == contextDefaultRootName {254 ctx = ctx.ResetPath("TUPLE")255 }256 for i := 0; i < maxLen; i++ {257 err = deepValueEqual(ctx.AddArrayIndex(i),258 got.Index(i), expected.Index(i))259 if err != nil {260 return261 }262 }263 if gotLen != expectedLen {264 res := tdSetResult{265 Kind: itemsSetResult,266 // do not sort Extra/Mising here267 }268 if gotLen > expectedLen {269 res.Extra = make([]reflect.Value, gotLen-expectedLen)270 for i := expectedLen; i < gotLen; i++ {271 res.Extra[i-expectedLen] = got.Index(i)272 }273 } else {274 res.Missing = make([]reflect.Value, expectedLen-gotLen)275 for i := gotLen; i < expectedLen; i++ {276 res.Missing[i-gotLen] = expected.Index(i)277 }278 }279 return ctx.CollectError(&ctxerr.Error{280 Message: fmt.Sprintf("comparing slices, from index #%d", maxLen),281 Summary: res.Summary(),282 })283 }284 return285 case reflect.Interface:286 return deepValueEqual(ctx, got.Elem(), expected.Elem())287 case reflect.Ptr:288 if got.Pointer() == expected.Pointer() {289 return290 }291 return deepValueEqual(ctx.AddPtr(1), got.Elem(), expected.Elem())292 case reflect.Struct:293 sType := got.Type()294 ignoreUnexported := ctx.IgnoreUnexported || ctx.Hooks.IgnoreUnexported(sType)295 for i, n := 0, got.NumField(); i < n; i++ {296 field := sType.Field(i)297 if ignoreUnexported && field.PkgPath != "" {298 continue299 }300 err = deepValueEqual(ctx.AddField(field.Name),301 got.Field(i), expected.Field(i))302 if err != nil {303 return304 }305 }306 return307 case reflect.Map:308 if got.IsNil() != expected.IsNil() {309 if ctx.BooleanError {310 return ctxerr.BooleanError311 }312 return ctx.CollectError(&ctxerr.Error{313 Message: "nil map",314 Got: isNilStr(got.IsNil()),315 Expected: isNilStr(expected.IsNil()),316 })317 }318 // Shortcut in boolean context319 if ctx.BooleanError && got.Len() != expected.Len() {320 return ctxerr.BooleanError321 }322 if got.Pointer() == expected.Pointer() {323 return324 }325 var notFoundKeys []reflect.Value326 foundKeys := map[any]bool{}327 for _, vkey := range tdutil.MapSortedKeys(expected) {328 gotValue := got.MapIndex(vkey)329 if !gotValue.IsValid() {330 notFoundKeys = append(notFoundKeys, vkey)331 continue332 }333 err = deepValueEqual(ctx.AddMapKey(vkey),334 gotValue, expected.MapIndex(vkey))335 if err != nil {336 return337 }338 foundKeys[dark.MustGetInterface(vkey)] = true339 }340 if got.Len() == len(foundKeys) {341 if len(notFoundKeys) == 0 {342 return343 }344 return ctx.CollectError(&ctxerr.Error{345 Message: "comparing map",346 Summary: (tdSetResult{347 Kind: keysSetResult,348 Missing: notFoundKeys,349 Sort: true,350 }).Summary(),351 })352 }353 if ctx.BooleanError {354 return ctxerr.BooleanError355 }356 // Retrieve extra keys357 res := tdSetResult{358 Kind: keysSetResult,359 Missing: notFoundKeys,360 Extra: make([]reflect.Value, 0, got.Len()-len(foundKeys)),361 Sort: true,362 }363 for _, vkey := range tdutil.MapSortedKeys(got) {364 if !foundKeys[dark.MustGetInterface(vkey)] {365 res.Extra = append(res.Extra, vkey)366 }367 }368 return ctx.CollectError(&ctxerr.Error{369 Message: "comparing map",370 Summary: res.Summary(),371 })372 case reflect.Func:373 if got.IsNil() && expected.IsNil() {374 return375 }376 if ctx.BooleanError {377 return ctxerr.BooleanError378 }379 // Can't do better than this:380 return ctx.CollectError(&ctxerr.Error{381 Message: "functions mismatch",382 Summary: ctxerr.NewSummary("<can not be compared>"),383 })384 default:385 // Normal equality suffices386 if dark.MustGetInterface(got) == dark.MustGetInterface(expected) {387 return388 }389 if ctx.BooleanError {390 return ctxerr.BooleanError391 }392 return ctx.CollectError(&ctxerr.Error{393 Message: "values differ",394 Got: got,395 Expected: expected,396 })397 }398}399func deepValueEqualOK(got, expected reflect.Value) bool {400 return deepValueEqualFinal(newBooleanContext(), got, expected) == nil401}402// EqDeeply returns true if got matches expected. expected can403// be the same type as got is, or contains some [TestDeep] operators.404//405// got := "foobar"406// td.EqDeeply(got, "foobar") // returns true407// td.EqDeeply(got, td.HasPrefix("foo")) // returns true408func EqDeeply(got, expected any) bool {409 return deepValueEqualOK(reflect.ValueOf(got), reflect.ValueOf(expected))410}411// EqDeeplyError returns nil if got matches expected. expected can be412// the same type as got is, or contains some [TestDeep] operators. If413// got does not match expected, the returned [*ctxerr.Error] contains414// the reason of the first mismatch detected.415//416// got := "foobar"417// if err := td.EqDeeplyError(got, "foobar"); err != nil {418// // â¦419// }420// if err := td.EqDeeplyError(got, td.HasPrefix("foo")); err != nil {421// // â¦422// }423func EqDeeplyError(got, expected any) error {424 err := deepValueEqualFinal(newContext(nil),425 reflect.ValueOf(got), reflect.ValueOf(expected))426 if err == nil {427 return nil428 }429 return err430}...
cmp_deeply.go
Source:cmp_deeply.go
...126}127func cmpDeeply(ctx ctxerr.Context, t TestingT, got, expected any,128 args ...any,129) bool {130 err := deepValueEqualFinal(ctx,131 reflect.ValueOf(got), reflect.ValueOf(expected))132 if err == nil {133 return true134 }135 t.Helper()136 formatError(t, ctx.FailureIsFatal, err, args...)137 return false138}139// S returns a string based on args as Cmp* functions do with their140// own args parameter to name their test. So behind the scene,141// [tdutil.BuildTestName] is used.142//143// If len(args)Â >Â 1 and the first item of args is a string and144// contains a '%' rune then [fmt.Fprintf] is used to compose the...
td_all.go
Source:td_all.go
...58}59func (a *tdAll) Match(ctx ctxerr.Context, got reflect.Value) (err *ctxerr.Error) {60 var origErr *ctxerr.Error61 for idx, item := range a.items {62 // Use deepValueEqualFinal here instead of deepValueEqual as we63 // want to know whether an error occurred or not, we do not want64 // to accumulate it silently65 origErr = deepValueEqualFinal(66 ctx.ResetErrors().67 AddCustomLevel(fmt.Sprintf("<All#%d/%d>", idx+1, len(a.items))),68 got, item)69 if origErr != nil {70 if ctx.BooleanError {71 return ctxerr.BooleanError72 }73 err := &ctxerr.Error{74 Message: fmt.Sprintf("compared (part %d of %d)", idx+1, len(a.items)),75 Got: got,76 Expected: item,77 }78 if item.IsValid() && item.Type().Implements(testDeeper) {79 err.Origin = origErr...
deepValueEqualFinal
Using AI Code Generation
1import (2type td struct {3}4func (t td) deepValueEqualFinal(x, y reflect.Value) bool {5 if !x.IsValid() || !y.IsValid() {6 return x.IsValid() == y.IsValid()7 }8 if x.Type() != y.Type() {9 }10 switch x.Kind() {11 return x.Bool() == y.Bool()12 return x.Int() == y.Int()13 return x.Uint() == y.Uint()14 return x.Float() == y.Float()15 return x.Complex() == y.Complex()16 return x.String() == y.String()17 return x.Pointer() == y.Pointer()18 return t.deepValueEqualFinal(x.Elem(), y.Elem())19 for i := 0; i < x.Len(); i++ {20 if !t.deepValueEqualFinal(x.Index(i), y.Index(i)) {21 }22 }23 if x.IsNil() || y.IsNil() {24 return x.IsNil() == y.IsNil()25 }26 if x.Len() != y.Len() {27 }28 for i := 0; i < x.Len(); i++ {29 if !t.deepValueEqualFinal(x.Index(i), y.Index(i)) {30 }31 }32 for i := 0; i < x.NumField(); i++ {33 if !t.deepValueEqualFinal(x.Field(i), y.Field(i)) {34 }35 }36 if x.IsNil() || y.IsNil() {37 return x.IsNil() == y.IsNil()38 }39 if x.Len() != y.Len() {40 }41 for _, k := range x.MapKeys() {
deepValueEqualFinal
Using AI Code Generation
1import (2func main() {3 var td = td{}4 fmt.Println(td.deepValueEqualFinal(reflect.ValueOf(x), reflect.ValueOf(y)))5}6import (7func main() {8 var td = td{}9 fmt.Println(td.deepValueEqual(reflect.ValueOf(x), reflect.ValueOf(y)))10}11import (12func main() {13 var td = td{}14 fmt.Println(td.deepValueEqualFinal(reflect.ValueOf(x), reflect.ValueOf(y)))15}16import (17func main() {18 var td = td{}19 fmt.Println(td.deepValueEqual(reflect.ValueOf(x), reflect.ValueOf(y)))20}21import (22func main() {23 var td = td{}24 fmt.Println(td.deepValueEqualFinal(reflect.ValueOf(x), reflect.ValueOf(y)))25}26import (27func main() {28 var td = td{}29 fmt.Println(td.deepValueEqual(reflect.ValueOf(x), reflect.ValueOf(y)))30}31import (32func main() {33 var td = td{}34 fmt.Println(td.deepValueEqualFinal(reflect.ValueOf(x), reflect.ValueOf(y)))35}
deepValueEqualFinal
Using AI Code Generation
1import (2func main() {3 td := new(td)4 fmt.Println(td.deepValueEqualFinal(1, 1))5 fmt.Println(td.deepValueEqualFinal(1, 2))6 fmt.Println(td.deepValueEqualFinal(1, 1.0))7 fmt.Println(td.deepValueEqualFinal(1, 1.1))8 fmt.Println(td.deepValueEqualFinal(1, "1"))9 fmt.Println(td.deepValueEqualFinal(1, "2"))10 fmt.Println(td.deepValueEqualFinal("1", "1"))11 fmt.Println(td.deepValueEqualFinal("1", "2"))12 fmt.Println(td.deepValueEqualFinal(1, []int{1}))13 fmt.Println(td.deepValueEqualFinal([]int{1}, []int{1}))14 fmt.Println(td.deepValueEqualFinal([]int{1}, []int{2}))15 fmt.Println(td.deepValueEqualFinal([]int{1, 2}, []int{1, 2}))16 fmt.Println(td.deepValueEqualFinal([]int{1, 2}, []int{2, 1}))17 fmt.Println(td.deepValueEqualFinal([]int{1, 2}, []int{1, 2, 3}))18 fmt.Println(td.deepValueEqualFinal([]int{1, 2, 3}, []int{1, 2}))19 fmt.Println(td.deepValueEqualFinal([]int{1}, 1))20 fmt.Println(td.deepValueEqualFinal([]int{1, 2}, 1))21 fmt.Println(td.deepValueEqualFinal([]int{1, 2}, 2))22 fmt.Println(td.deepValueEqualFinal([]int{1, 2}, 3))23 fmt.Println(td.deepValueEqualFinal([]int{1, 2},
deepValueEqualFinal
Using AI Code Generation
1import "fmt"2type td struct {3}4func main() {5 var t1 = td{1, "abc"}6 var t2 = td{1, "abc"}7 var t3 = td{1, "abcd"}8 fmt.Println("t1==t2", t1.deepValueEqualFinal(t2))9 fmt.Println("t1==t3", t1.deepValueEqualFinal(t3))10}11func (t1 td) deepValueEqualFinal(t2 td) bool {12 if t1.a == t2.a && t1.b == t2.b {13 }14}15import (16type td struct {17}18func main() {19 var t1 = td{1, "abc"}20 var t2 = td{1, "abc"}21 var t3 = td{1, "abcd"}22 fmt.Println("t1==t2", reflect.DeepEqual(t1, t2))23 fmt.Println("t1==t3", reflect.DeepEqual(t1, t3))24}25func (t1 td) deepValueEqualFinal(t2 td) bool {26 if t1.a == t2.a && t1.b == t2.b {27 }28}
deepValueEqualFinal
Using AI Code Generation
1import (2type td struct {3}4func deepValueEqualFinal(a, b reflect.Value) bool {5 if a.Type() != b.Type() {6 }7 switch a.Kind() {8 return a.Bool() == b.Bool()9 return a.Int() == b.Int()10 return a.Uint() == b.Uint()11 return a.Float() == b.Float()12 return a.Complex() == b.Complex()13 return a.String() == b.String()14 return a.Pointer() == b.Pointer()15 if a.IsNil() || b.IsNil() {16 return a.IsNil() == b.IsNil()17 }18 if a.Len() != b.Len() {19 }20 for i := 0; i < a.Len(); i++ {21 if !deepValueEqualFinal(a.Index(i), b.Index(i)) {22 }23 }24 if a.NumField() != b.NumField() {25 }26 for i := 0; i < a.NumField(); i++ {27 if !deepValueEqualFinal(a.Field(i), b.Field(i)) {28 }29 }30 if a.Len() != b.Len() {31 }32 for _, k := range a.MapKeys() {33 if !deepValueEqualFinal(a.MapIndex(k), b.MapIndex(k)) {34 }35 }36 }37 panic("unreachable")38}39func main() {40 t1 := td{
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!!