How to use resolveAnchor method of td Package

Best Go-testdeep code snippet using td.resolveAnchor

equal.go

Source:equal.go Github

copy

Full Screen

...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(),...

Full Screen

Full Screen

anchor.go

Source:anchor.go Github

copy

Full Screen

1// Copyright (c) 2020, Maxime Soulé2// All rights reserved.3//4// This source code is licensed under the BSD-style license found in the5// LICENSE file in the root directory of this source tree.6package anchors7import (8 "errors"9 "fmt"10 "math"11 "reflect"12 "sync"13)14type anchor struct {15 Anchor reflect.Value // Anchor is the generated value used as anchor16 Operator reflect.Value // Operator is a td.TestDeep behind17}18// Info gathers all anchors information.19type Info struct {20 sync.Mutex21 index int22 persist bool23 anchors map[any]anchor24}25// NewInfo returns a new instance of [*Info].26func NewInfo() *Info {27 return &Info{28 anchors: map[any]anchor{},29 }30}31// AddAnchor anchors a new operator op, with type typ then returns the32// anchor value.33func (i *Info) AddAnchor(typ reflect.Type, op reflect.Value) (reflect.Value, error) {34 i.Lock()35 defer i.Unlock()36 anc, key, err := i.build(typ)37 if err != nil {38 return reflect.Value{}, err39 }40 if i.anchors == nil {41 i.anchors = map[any]anchor{}42 }43 i.anchors[key] = anchor{44 Anchor: anc,45 Operator: op,46 }47 return anc, nil48}49// DoAnchorsPersist returns true if anchors are persistent across tests.50func (i *Info) DoAnchorsPersist() bool {51 i.Lock()52 defer i.Unlock()53 return i.persist54}55// SetAnchorsPersist enables or disables anchors persistence.56func (i *Info) SetAnchorsPersist(persist bool) {57 i.Lock()58 defer i.Unlock()59 i.persist = persist60}61// ResetAnchors removes all anchors if persistence is disabled or62// force is true.63func (i *Info) ResetAnchors(force bool) {64 i.Lock()65 defer i.Unlock()66 if !i.persist || force {67 for k := range i.anchors {68 delete(i.anchors, k)69 }70 i.index = 071 }72}73func (i *Info) nextIndex() (n int) {74 n = i.index75 i.index++76 return77}78// ResolveAnchor checks whether the passed value matches an anchored79// operator or not. If yes, this operator is returned with true. If80// no, the value is returned as is with false.81func (i *Info) ResolveAnchor(v reflect.Value) (reflect.Value, bool) {82 if i == nil || !v.CanInterface() {83 return v, false84 }85 // Shortcut86 i.Lock()87 la := len(i.anchors)88 i.Unlock()89 if la == 0 {90 return v, false91 }92 var key any93sw:94 switch v.Kind() {95 case reflect.Int,96 reflect.Int8,97 reflect.Int16,98 reflect.Int32,99 reflect.Int64,100 reflect.Uint,101 reflect.Uint8,102 reflect.Uint16,103 reflect.Uint32,104 reflect.Uint64,105 reflect.Uintptr,106 reflect.Float32,107 reflect.Float64,108 reflect.Complex64,109 reflect.Complex128,110 reflect.String:111 key = v.Interface()112 case reflect.Chan,113 reflect.Map,114 reflect.Slice,115 reflect.Ptr:116 key = v.Pointer()117 case reflect.Struct:118 typ := v.Type()119 if typ.Comparable() {120 // Check for anchorable types. No need of 2 passes here.121 for _, at := range AnchorableTypes {122 if typ == at.typ || at.typ.ConvertibleTo(typ) { // 1.17 ok as struct here123 key = v.Interface()124 break sw125 }126 }127 }128 fallthrough129 default:130 return v, false131 }132 i.Lock()133 defer i.Unlock()134 if anchor, ok := i.anchors[key]; ok {135 return anchor.Operator, true136 }137 return v, false138}139func (i *Info) setInt(typ reflect.Type, min int64) (reflect.Value, any) {140 nvm := reflect.New(typ).Elem()141 nvm.SetInt(min + int64(i.nextIndex()))142 return nvm, nvm.Interface()143}144func (i *Info) setUint(typ reflect.Type, max uint64) (reflect.Value, any) {145 nvm := reflect.New(typ).Elem()146 nvm.SetUint(max - uint64(i.nextIndex()))147 return nvm, nvm.Interface()148}149func (i *Info) setFloat(typ reflect.Type, min float64) (reflect.Value, any) {150 nvm := reflect.New(typ).Elem()151 nvm.SetFloat(min + float64(i.nextIndex()))152 return nvm, nvm.Interface()153}154func (i *Info) setComplex(typ reflect.Type, min float64) (reflect.Value, any) {155 nvm := reflect.New(typ).Elem()156 min += float64(i.nextIndex())157 nvm.SetComplex(complex(min, min))158 return nvm, nvm.Interface()159}160// build builds a new value of type "typ" and returns it under two161// forms:162// - the new value itself as a reflect.Value;163// - an any usable as a key in an AnchorsSet map.164//165// It returns an error if "typ" kind is not recognized or if it is a166// non-anchorable struct.167func (i *Info) build(typ reflect.Type) (reflect.Value, any, error) {168 // For each numeric type, anchor the operator on a number close to169 // the limit of this type, but not at the extreme limit to avoid170 // edge cases where these limits are used in real world and so avoid171 // collisions172 switch typ.Kind() {173 case reflect.Int:174 nvm, iface := i.setInt(typ, int64(^int(^uint(0)>>1))+1004293)175 return nvm, iface, nil176 case reflect.Int8:177 nvm, iface := i.setInt(typ, math.MinInt8+13)178 return nvm, iface, nil179 case reflect.Int16:180 nvm, iface := i.setInt(typ, math.MinInt16+1049)181 return nvm, iface, nil182 case reflect.Int32:183 nvm, iface := i.setInt(typ, math.MinInt32+1004293)184 return nvm, iface, nil185 case reflect.Int64:186 nvm, iface := i.setInt(typ, math.MinInt64+1000424443)187 return nvm, iface, nil188 case reflect.Uint:189 nvm, iface := i.setUint(typ, uint64(^uint(0))-1004293)190 return nvm, iface, nil191 case reflect.Uint8:192 nvm, iface := i.setUint(typ, math.MaxUint8-29)193 return nvm, iface, nil194 case reflect.Uint16:195 nvm, iface := i.setUint(typ, math.MaxUint16-2099)196 return nvm, iface, nil197 case reflect.Uint32:198 nvm, iface := i.setUint(typ, math.MaxUint32-2008571)199 return nvm, iface, nil200 case reflect.Uint64:201 nvm, iface := i.setUint(typ, math.MaxUint64-2000848901)202 return nvm, iface, nil203 case reflect.Uintptr:204 nvm, iface := i.setUint(typ, uint64(^uintptr(0))-2000848901)205 return nvm, iface, nil206 case reflect.Float32:207 nvm, iface := i.setFloat(typ, -(1<<24)+104243)208 return nvm, iface, nil209 case reflect.Float64:210 nvm, iface := i.setFloat(typ, -(1<<53)+100004243)211 return nvm, iface, nil212 case reflect.Complex64:213 nvm, iface := i.setComplex(typ, -(1<<24)+104243)214 return nvm, iface, nil215 case reflect.Complex128:216 nvm, iface := i.setComplex(typ, -(1<<53)+100004243)217 return nvm, iface, nil218 case reflect.String:219 nvm := reflect.New(typ).Elem()220 nvm.SetString(fmt.Sprintf("<testdeep@anchor#%d>", i.nextIndex()))221 return nvm, nvm.Interface(), nil222 case reflect.Chan:223 nvm := reflect.MakeChan(typ, 0)224 return nvm, nvm.Pointer(), nil225 case reflect.Map:226 nvm := reflect.MakeMap(typ)227 return nvm, nvm.Pointer(), nil228 case reflect.Slice:229 nvm := reflect.MakeSlice(typ, 0, 1) // cap=1 to avoid same ptr below230 return nvm, nvm.Pointer(), nil231 case reflect.Ptr:232 nvm := reflect.New(typ.Elem())233 return nvm, nvm.Pointer(), nil234 case reflect.Struct:235 // First pass for the exact type236 for _, at := range AnchorableTypes {237 if typ == at.typ {238 nvm := at.builder.Call([]reflect.Value{reflect.ValueOf(i.nextIndex())})[0]239 return nvm, nvm.Interface(), nil240 }241 }242 // Second pass for convertible type243 for _, at := range AnchorableTypes {244 if at.typ.ConvertibleTo(typ) {245 nvm := at.builder.Call([]reflect.Value{reflect.ValueOf(i.nextIndex())})[0].246 Convert(typ)247 return nvm, nvm.Interface(), nil248 }249 }250 return reflect.Value{}, nil,251 errors.New(typ.String() + " struct type is not supported as an anchor. Try AddAnchorableStructType")252 default:253 return reflect.Value{}, nil,254 errors.New(typ.Kind().String() + " kind is not supported as an anchor")255 }256}...

Full Screen

Full Screen

resolveAnchor

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 m := minify.New()4 m.AddFunc("text/css", css.Minify)5 m.AddFunc("text/javascript", js.Minify)6 m.AddFunc("text/html", html.Minify)7 m.Add("text/html", &html.Minifier{8 })9 m.Add("text/css", &css.Minifier{10 })11 m.Add("text/javascript", &js.Minifier{12 })

Full Screen

Full Screen

resolveAnchor

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(td.resolveAnchor())4}5import (6func main() {7 fmt.Println(td.resolveAnchor())8}9import (10func main() {11 fmt.Println(td.resolveAnchor())12}13import (14func main() {15 fmt.Println(td.resolveAnchor())16}17import (18func main() {19 fmt.Println(td.resolveAnchor())20}21import (22func main() {23 fmt.Println(td.resolveAnchor())24}25import (26func main() {

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