How to use AddCustomLevel method of ctxerr Package

Best Go-testdeep code snippet using ctxerr.AddCustomLevel

td_grep.go

Source:td_grep.go Github

copy

Full Screen

...186 case reflect.Slice, reflect.Array:187 const grepped = "<grepped>"188 if got.Kind() == reflect.Slice && got.IsNil() {189 return deepValueEqual(190 ctx.AddCustomLevel(grepped),191 reflect.New(got.Type()).Elem(),192 g.expectedValue,193 )194 }195 l := got.Len()196 out := reflect.MakeSlice(reflect.SliceOf(got.Type().Elem()), 0, l)197 for idx := 0; idx < l; idx++ {198 item := got.Index(idx)199 ok, rErr := g.matchItem(ctx, idx, item)200 if rErr != nil {201 return rErr202 }203 if ok {204 out = reflect.Append(out, item)205 }206 }207 return deepValueEqual(ctx.AddCustomLevel(grepped), out, g.expectedValue)208 }209 return grepBadKind(ctx, got)210}211type tdFirst struct {212 tdGrepBase213}214var _ TestDeep = &tdFirst{}215// summary(First): find the first matching item of a slice or an array216// then compare its content217// input(First): array,slice,ptr(ptr on array/slice)218// First is a smuggler operator. It takes an array, a slice or a219// pointer on array/slice. For each item it applies filter, a220// [TestDeep] operator or a function returning a bool. It takes the221// first item for which the filter matched and compares it to222// expectedValue. The filter matches when it is a:223// - [TestDeep] operator and it matches for the item;224// - function receiving the item and it returns true.225//226// expectedValue can of course be a [TestDeep] operator.227//228// got := []int{-3, -2, -1, 0, 1, 2, 3}229// td.Cmp(t, got, td.First(td.Gt(0), 1)) // succeeds230// td.Cmp(t, got, td.First(func(x int) bool { return x%2 == 0 }, -2)) // succeeds231// td.Cmp(t, got, td.First(func(x int) bool { return x%2 == 0 }, td.Lt(0))) // succeeds232//233// If the input is empty (and/or nil for a slice), an "item not found"234// error is raised before comparing to expectedValue.235//236// var got []int237// td.Cmp(t, got, td.First(td.Gt(0), td.Gt(0))) // fails238// td.Cmp(t, []int{}, td.First(td.Gt(0), td.Gt(0))) // fails239// td.Cmp(t, [0]int{}, td.First(td.Gt(0), td.Gt(0))) // fails240//241// See also [Last] and [Grep].242func First(filter, expectedValue any) TestDeep {243 g := tdFirst{}244 g.initGrepBase(filter, expectedValue)245 return &g246}247func (g *tdFirst) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {248 if g.err != nil {249 return ctx.CollectError(g.err)250 }251 if rErr := grepResolvePtr(ctx, &got); rErr != nil {252 return rErr253 }254 switch got.Kind() {255 case reflect.Slice, reflect.Array:256 for idx, l := 0, got.Len(); idx < l; idx++ {257 item := got.Index(idx)258 ok, rErr := g.matchItem(ctx, idx, item)259 if rErr != nil {260 return rErr261 }262 if ok {263 return deepValueEqual(264 ctx.AddCustomLevel(S("<first#%d>", idx)),265 item,266 g.expectedValue,267 )268 }269 }270 return g.notFound(ctx, got)271 }272 return grepBadKind(ctx, got)273}274func (g *tdFirst) TypeBehind() reflect.Type {275 return g.sliceTypeBehind()276}277type tdLast struct {278 tdGrepBase279}280var _ TestDeep = &tdLast{}281// summary(Last): find the last matching item of a slice or an array282// then compare its content283// input(Last): array,slice,ptr(ptr on array/slice)284// Last is a smuggler operator. It takes an array, a slice or a285// pointer on array/slice. For each item it applies filter, a286// [TestDeep] operator or a function returning a bool. It takes the287// last item for which the filter matched and compares it to288// expectedValue. The filter matches when it is a:289// - [TestDeep] operator and it matches for the item;290// - function receiving the item and it returns true.291//292// expectedValue can of course be a [TestDeep] operator.293//294// got := []int{-3, -2, -1, 0, 1, 2, 3}295// td.Cmp(t, got, td.Last(td.Lt(0), -1)) // succeeds296// td.Cmp(t, got, td.Last(func(x int) bool { return x%2 == 0 }, 2)) // succeeds297// td.Cmp(t, got, td.Last(func(x int) bool { return x%2 == 0 }, td.Gt(0))) // succeeds298//299// If the input is empty (and/or nil for a slice), an "item not found"300// error is raised before comparing to expectedValue.301//302// var got []int303// td.Cmp(t, got, td.Last(td.Gt(0), td.Gt(0))) // fails304// td.Cmp(t, []int{}, td.Last(td.Gt(0), td.Gt(0))) // fails305// td.Cmp(t, [0]int{}, td.Last(td.Gt(0), td.Gt(0))) // fails306//307// See also [First] and [Grep].308func Last(filter, expectedValue any) TestDeep {309 g := tdLast{}310 g.initGrepBase(filter, expectedValue)311 return &g312}313func (g *tdLast) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {314 if g.err != nil {315 return ctx.CollectError(g.err)316 }317 if rErr := grepResolvePtr(ctx, &got); rErr != nil {318 return rErr319 }320 switch got.Kind() {321 case reflect.Slice, reflect.Array:322 for idx := got.Len() - 1; idx >= 0; idx-- {323 item := got.Index(idx)324 ok, rErr := g.matchItem(ctx, idx, item)325 if rErr != nil {326 return rErr327 }328 if ok {329 return deepValueEqual(330 ctx.AddCustomLevel(S("<last#%d>", idx)),331 item,332 g.expectedValue,333 )334 }335 }336 return g.notFound(ctx, got)337 }338 return grepBadKind(ctx, got)339}340func (g *tdLast) TypeBehind() reflect.Type {341 return g.sliceTypeBehind()342}...

Full Screen

Full Screen

error_test.go

Source:error_test.go Github

copy

Full Screen

...75 Line: 23,76 },77 Origin: &ctxerr.Error{78 Context: ctxerr.Context{79 Path: ctxerr.NewPath("DATA").AddArrayIndex(12).AddField("Field").AddCustomLevel("<All#1/2>"),80 },81 Message: "origin error message",82 Summary: ctxerr.NewSummary("42"),83 Location: location.Location{84 File: "file2.go",85 Func: "SubOperator",86 Line: 236,87 },88 },89 }90 test.EqualStr(t, err.Error(),91 `DATA[12].Field: error message92 66693Originates from following error:94 DATA[12].Field<All#1/2>: origin error message95 4296 [under operator SubOperator at file2.go:236]97[under operator Operator at file.go:23]`)98 test.EqualStr(t, err.GotString(), "")99 test.EqualStr(t, err.ExpectedString(), "")100 test.EqualStr(t, err.SummaryString(), "666")101 err = ctxerr.Error{102 Context: ctxerr.Context{103 Path: ctxerr.NewPath("DATA").AddArrayIndex(12).AddField("Field"),104 },105 Message: "error message",106 Summary: ctxerr.NewSummary("666"),107 Location: location.Location{108 File: "file.go",109 Func: "Operator",110 Line: 23,111 },112 Origin: &ctxerr.Error{113 Context: ctxerr.Context{114 Path: ctxerr.NewPath("DATA").AddArrayIndex(12).AddField("Field").AddCustomLevel("<All#1/2>"),115 },116 Message: "origin error message",117 Summary: ctxerr.NewSummary("42"),118 Location: location.Location{119 File: "file2.go",120 Func: "SubOperator",121 Line: 236,122 },123 },124 // Next error at same location125 Next: &ctxerr.Error{126 Context: ctxerr.Context{127 Path: ctxerr.NewPath("DATA").AddArrayIndex(13).AddField("Field"),128 },129 Message: "error message",130 Summary: ctxerr.NewSummary("888"),131 Location: location.Location{132 File: "file.go",133 Func: "Operator",134 Line: 23,135 },136 },137 }138 test.EqualStr(t, err.Error(),139 `DATA[12].Field: error message140 666141Originates from following error:142 DATA[12].Field<All#1/2>: origin error message143 42144 [under operator SubOperator at file2.go:236]145DATA[13].Field: error message146 888147[under operator Operator at file.go:23]`)148 err = ctxerr.Error{149 Context: ctxerr.Context{Path: ctxerr.NewPath("DATA").AddArrayIndex(12).AddField("Field")},150 Message: "error message",151 Summary: ctxerr.NewSummary("666"),152 Location: location.Location{153 File: "file.go",154 Func: "Operator",155 Line: 23,156 },157 Origin: &ctxerr.Error{158 Context: ctxerr.Context{159 Path: ctxerr.NewPath("DATA").AddArrayIndex(12).AddField("Field").AddCustomLevel("<All#1/2>"),160 },161 Message: "origin error message",162 Summary: ctxerr.NewSummary("42"),163 Location: location.Location{164 File: "file2.go",165 Func: "SubOperator",166 Line: 236,167 },168 },169 // Next error at different location170 Next: &ctxerr.Error{171 Context: ctxerr.Context{172 Path: ctxerr.NewPath("DATA").AddArrayIndex(13).AddField("Field"),173 },...

Full Screen

Full Screen

path_test.go

Source:path_test.go Github

copy

Full Screen

...143 AddPtr(3).144 AddFunctionCall("FUNC").145 AddArrayIndex(42).146 AddMapKey("key").147 AddCustomLevel("→panic"),148 Expected: `FUNC(***(*DATA).field1)[42]["key"]→panic`,149 },150 {151 Path: ctxerr.NewPath("DATA").152 AddPtr(2).153 AddField("field1").154 AddPtr(3).155 AddFunctionCall("FUNC").156 AddPtr(2).157 AddArrayIndex(42).158 AddMapKey("key").159 AddCustomLevel("→panic"),160 Expected: `(**FUNC(***(*DATA).field1))[42]["key"]→panic`,161 },162 } {163 test.EqualStr(t,164 testCase.Path.String(), testCase.Expected,165 "test case #%d", i)166 }167 var nilPath ctxerr.Path168 for i, newPath := range []ctxerr.Path{169 nilPath.Copy(),170 nilPath.AddField("foo"),171 nilPath.AddArrayIndex(42),172 nilPath.AddMapKey("bar"),173 nilPath.AddPtr(12),174 nilPath.AddFunctionCall("zip"),175 nilPath.AddCustomLevel("custom"),176 } {177 if newPath != nil {178 t.Errorf("at #%d, got=%p expected=nil", i, newPath)179 }180 }181}182func TestEqual(t *testing.T) {183 path := ctxerr.NewPath("DATA").184 AddPtr(2).185 AddField("field1")186 test.EqualInt(t, path.Len(), 2)187 test.IsTrue(t, path.Equal(ctxerr.NewPath("DATA").AddPtr(1).AddPtr(1).AddField("field1")))188 test.IsFalse(t, path.Equal(ctxerr.NewPath("DATA")))189 test.IsFalse(t, path.Equal(ctxerr.NewPath("DATA").AddPtr(2).AddField("field2")))...

Full Screen

Full Screen

AddCustomLevel

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 h := fasthttpadaptor.NewFastHTTPHandlerFunc(handler)4 log.Fatal(fasthttp.ListenAndServe(":8080", h))5}6func handler(ctx *fasthttp.RequestCtx) {

Full Screen

Full Screen

AddCustomLevel

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := ctxerr.New()4 ctx.AddCustomLevel("debug", 5)5 ctx.SetLevel("debug")6 ctx.SetFormatter(&logrus.TextFormatter{})7 ctx.SetOutput(os.Stdout)8 ctx.Debug("This is a debug message")9}

Full Screen

Full Screen

AddCustomLevel

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctxerr.AddCustomLevel("INFO", "INFO", 1)4 ctxerr.AddCustomLevel("WARNING", "WARNING", 2)5 ctxerr.AddCustomLevel("ERROR", "ERROR", 3)6 ctxerr.AddCustomLevel("FATAL", "FATAL", 4)7 fmt.Println("INFO:", ctxerr.INFO)8 fmt.Println("WARNING:", ctxerr.WARNING)9 fmt.Println("ERROR:", ctxerr.ERROR)10 fmt.Println("FATAL:", ctxerr.FATAL)11}12import (13func main() {14 ctxerr.AddCustomLevel("INFO", "INFO", 1)15 ctxerr.AddCustomLevel("WARNING", "WARNING", 2)16 ctxerr.AddCustomLevel("ERROR", "ERROR", 3)17 ctxerr.AddCustomLevel("FATAL", "FATAL", 4)18 fmt.Println("INFO:", ctxerr.INFO)19 fmt.Println("WARNING:", ctxerr.WARNING)20 fmt.Println("ERROR:", ctxerr.ERROR)21 fmt.Println("FATAL:", ctxerr.FATAL)22}23import (

Full Screen

Full Screen

AddCustomLevel

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 err := errors.New("some error")4 cErr := ctxerr.New("some context", err)5 cErr.AddCustomLevel("level1", "some more context")6 fmt.Println(cErr.Error())7}8import (9func main() {10 err := errors.New("some error")11 cErr := ctxerr.New("some context", err)12 cErr.AddCustomLevel("level1", "some more context")13 cErr.AddCustomLevel("level2", "some more context")14 fmt.Println(cErr.Error())15}16import (17func main() {18 err := errors.New("some error")19 cErr := ctxerr.New("some context", err)20 cErr.AddCustomLevel("level1", "some more context")21 cErr.AddCustomLevel("level2", "some more context")22 cErr.AddCustomLevel("level3", "some more context")23 fmt.Println(cErr.Error())24}25import (26func main() {27 err := errors.New("some error")28 cErr := ctxerr.New("some context", err)29 cErr.AddCustomLevel("level1", "some more context")30 cErr.AddCustomLevel("level2", "some more context")31 cErr.AddCustomLevel("level3", "some more context")32 cErr.AddCustomLevel("

Full Screen

Full Screen

AddCustomLevel

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctxerr.AddCustomLevel("CUSTOM", 5)4 err := ctxerr.New("CUSTOM", "This is a custom error")5 fmt.Println(err)6}

Full Screen

Full Screen

AddCustomLevel

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 customLevel := ctxerr.NewCustomLevel("CUSTOMLEVEL", 5)4 ctxerr.AddCustomLevel(customLevel)5 err := ctxerr.NewError("Error message", "Error code", ctxerr.CUSTOMLEVEL)6 fmt.Println(err.Level)7 fmt.Println(err.Message)8 fmt.Println(err.Code)9 fmt.Println(err.LevelName)10}11import (12func main() {13 customLevel1 := ctxerr.NewCustomLevel("CUSTOMLEVEL1", 5)14 customLevel2 := ctxerr.NewCustomLevel("CUSTOMLEVEL2", 6)15 customLevel3 := ctxerr.NewCustomLevel("CUSTOMLEVEL3", 7)16 customLevel4 := ctxerr.NewCustomLevel("CUSTOMLEVEL4", 8)17 customLevel5 := ctxerr.NewCustomLevel("CUSTOMLEVEL5", 9)18 ctxerr.AddCustomLevels(customLevel1, customLevel2, customLevel3, customLevel4, customLevel5)19 err := ctxerr.NewError("Error message", "Error code", ctxerr.CUSTOMLEVEL4)20 fmt.Println(err.Level)21 fmt.Println(err.Message)22 fmt.Println(err.Code)23 fmt.Println(err.LevelName)24}

Full Screen

Full Screen

AddCustomLevel

Using AI Code Generation

copy

Full Screen

1func main() {2 ctxerr.AddCustomLevel("test", 2)3 err := ctxerr.New("error", ctxerr.Level("test"))4 if ctxerr.Level("test").Check(err) {5 fmt.Println("error is of level \"test\"")6 }7}

Full Screen

Full Screen

AddCustomLevel

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctxerr.AddCustomLevel("custom1", 100)4 ctxerr.AddCustomLevel("custom2", 200)5 ctxerr.AddCustomLevel("custom3", 300)6 ctxerr.AddCustomLevel("custom4", 400)7 ctxerr.AddCustomLevel("custom5", 500)8 ctxerr.AddCustomLevel("custom6", 600)9 err1 := ctxerr.New("error1", "Custom Error1", 100)10 err2 := ctxerr.New("error2", "Custom Error2", 200, err1)11 err3 := ctxerr.New("error3", "Custom Error3", 300, err2, map[string]string{"key1": "value1"})12 err4 := ctxerr.New("error4", "Custom Error4", 400, err3, map[string]string{"key2": "value2"}, true)13 err5 := ctxerr.New("error5", "Custom Error5", 500, err4, map[string]string{"key3": "value3"}, true, "custom1")14 err6 := ctxerr.New("error6", "Custom Error6", 600, err5, map[string]string{"key4": "value4"}, true, "custom2", "Custom Message6")15 fmt.Println(err6)16}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful