How to use OpBad method of ctxerr Package

Best Go-testdeep code snippet using ctxerr.OpBad

td_grep.go

Source:td_grep.go Github

copy

Full Screen

...25 return26 }27 vfilter := reflect.ValueOf(filter)28 if vfilter.Kind() != reflect.Func {29 g.err = ctxerr.OpBad(g.GetLocation().Func,30 "usage: %s%s, FILTER_FUNC must be a function or FILTER_TESTDEEP_OPERATOR a TestDeep operator",31 g.GetLocation().Func, grepUsage)32 return33 }34 filterType := vfilter.Type()35 if filterType.IsVariadic() || filterType.NumIn() != 1 {36 g.err = ctxerr.OpBad(g.GetLocation().Func,37 "usage: %s%s, FILTER_FUNC must take only one non-variadic argument",38 g.GetLocation().Func, grepUsage)39 return40 }41 if filterType.NumOut() != 1 || filterType.Out(0) != types.Bool {42 g.err = ctxerr.OpBad(g.GetLocation().Func,43 "usage: %s%s, FILTER_FUNC must return bool",44 g.GetLocation().Func, grepUsage)45 return46 }47 g.argType = filterType.In(0)48 g.filter = vfilter49}50func (g *tdGrepBase) matchItem(ctx ctxerr.Context, idx int, item reflect.Value) (bool, *ctxerr.Error) {51 if g.argType == nil {52 // g.filter is a TestDeep operator53 return deepValueEqualFinalOK(ctx, item, g.filter), nil54 }55 // item is an interface, but the filter function does not expect an56 // interface, resolve it57 if item.Kind() == reflect.Interface && g.argType.Kind() != reflect.Interface {58 item = item.Elem()59 }60 if !item.Type().AssignableTo(g.argType) {61 if !types.IsConvertible(item, g.argType) {62 if ctx.BooleanError {63 return false, ctxerr.BooleanError64 }65 return false, ctx.AddArrayIndex(idx).CollectError(&ctxerr.Error{66 Message: "incompatible parameter type",67 Got: types.RawString(item.Type().String()),68 Expected: types.RawString(g.argType.String()),69 })70 }71 item = item.Convert(g.argType)72 }73 return g.filter.Call([]reflect.Value{item})[0].Bool(), nil74}75func (g *tdGrepBase) HandleInvalid() bool {76 return true // Knows how to handle untyped nil values (aka invalid values)77}78func (g *tdGrepBase) String() string {79 if g.err != nil {80 return g.stringError()81 }82 if g.argType == nil {83 return S("%s(%s)", g.GetLocation().Func, g.filter.Interface().(TestDeep))84 }85 return S("%s(%s)", g.GetLocation().Func, g.filter.Type())86}87func (g *tdGrepBase) TypeBehind() reflect.Type {88 if g.err != nil {89 return nil90 }91 return g.internalTypeBehind()92}93// sliceTypeBehind is used by First & Last TypeBehind method.94func (g *tdGrepBase) sliceTypeBehind() reflect.Type {95 typ := g.TypeBehind()96 if typ == nil {97 return nil98 }99 return reflect.SliceOf(typ)100}101func (g *tdGrepBase) notFound(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {102 if ctx.BooleanError {103 return ctxerr.BooleanError104 }105 return ctx.CollectError(&ctxerr.Error{106 Message: "item not found",107 Got: got,108 Expected: types.RawString(g.String()),109 })110}111func grepResolvePtr(ctx ctxerr.Context, got *reflect.Value) *ctxerr.Error {112 if got.Kind() == reflect.Ptr {113 gotElem := got.Elem()114 if !gotElem.IsValid() {115 if ctx.BooleanError {116 return ctxerr.BooleanError117 }118 return ctx.CollectError(ctxerr.NilPointer(*got, "non-nil *slice OR *array"))119 }120 switch gotElem.Kind() {121 case reflect.Slice, reflect.Array:122 *got = gotElem123 }124 }125 return nil126}127func grepBadKind(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {128 if ctx.BooleanError {129 return ctxerr.BooleanError130 }131 return ctx.CollectError(ctxerr.BadKind(got, "slice OR array OR *slice OR *array"))132}133type tdGrep struct {134 tdGrepBase135}136var _ TestDeep = &tdGrep{}137// summary(Grep): reduces a slice or an array before comparing its content138// input(Grep): array,slice,ptr(ptr on array/slice)139// Grep is a smuggler operator. It takes an array, a slice or a140// pointer on array/slice. For each item it applies filter, a141// [TestDeep] operator or a function returning a bool, and produces a142// slice consisting of those items for which the filter matched and143// compares it to expectedValue. The filter matches when it is a:144// - [TestDeep] operator and it matches for the item;145// - function receiving the item and it returns true.146//147// expectedValue can be a [TestDeep] operator or a slice (but never an148// array nor a pointer on a slice/array nor any other kind).149//150// got := []int{-3, -2, -1, 0, 1, 2, 3}151// td.Cmp(t, got, td.Grep(td.Gt(0), []int{1, 2, 3})) // succeeds152// td.Cmp(t, got, td.Grep(153// func(x int) bool { return x%2 == 0 },154// []int{-2, 0, 2})) // succeeds155// td.Cmp(t, got, td.Grep(156// func(x int) bool { return x%2 == 0 },157// td.Set(0, 2, -2))) // succeeds158//159// If Grep receives a nil slice or a pointer on a nil slice, it always160// returns a nil slice:161//162// var got []int163// td.Cmp(t, got, td.Grep(td.Gt(0), ([]int)(nil))) // succeeds164// td.Cmp(t, got, td.Grep(td.Gt(0), td.Nil())) // succeeds165// td.Cmp(t, got, td.Grep(td.Gt(0), []int{})) // fails166//167// See also [First] and [Last].168func Grep(filter, expectedValue any) TestDeep {169 g := tdGrep{}170 g.initGrepBase(filter, expectedValue)171 if g.err == nil && !g.isTestDeeper && g.expectedValue.Kind() != reflect.Slice {172 g.err = ctxerr.OpBad("Grep",173 "usage: Grep%s, EXPECTED_VALUE must be a slice not a %s",174 grepUsage, types.KindType(g.expectedValue))175 }176 return &g177}178func (g *tdGrep) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {179 if g.err != nil {180 return ctx.CollectError(g.err)181 }182 if rErr := grepResolvePtr(ctx, &got); rErr != nil {183 return rErr184 }185 switch got.Kind() {186 case reflect.Slice, reflect.Array:...

Full Screen

Full Screen

td_code.go

Source:td_code.go Github

copy

Full Screen

...138 base: newBase(3),139 function: vfn,140 }141 if vfn.Kind() != reflect.Func {142 c.err = ctxerr.OpBadUsage("Code", "(FUNC)", fn, 1, true)143 return &c144 }145 if vfn.IsNil() {146 c.err = ctxerr.OpBad("Code", "Code(FUNC): FUNC cannot be a nil function")147 return &c148 }149 fnType := vfn.Type()150 in := fnType.NumIn()151 // We accept only:152 // func (arg) bool153 // func (arg) error154 // func (arg) (bool, error)155 // func (*td.T, arg) // with arg ≠ *td.T, as it is certainly an error156 // func (assert, require *td.T, arg)157 if fnType.IsVariadic() || in == 0 || in > 3 ||158 (in > 1 && (fnType.In(0) != tType)) ||159 (in >= 2 && (in == 2) == (fnType.In(1) == tType)) {160 c.err = ctxerr.OpBad("Code",161 "Code(FUNC): FUNC must take only one non-variadic argument or (*td.T, arg) or (*td.T, *td.T, arg)")162 return &c163 }164 // func (arg) bool165 // func (arg) error166 // func (arg) (bool, error)167 if in == 1 {168 switch fnType.NumOut() {169 case 2: // (bool, *string*)170 if fnType.Out(1).Kind() != reflect.String {171 break172 }173 fallthrough174 case 1:175 // (*bool*) or (*bool*, string)176 if fnType.Out(0).Kind() == reflect.Bool ||177 // (*error*)178 (fnType.NumOut() == 1 && fnType.Out(0) == types.Error) {179 c.argType = fnType.In(0)180 return &c181 }182 }183 c.err = ctxerr.OpBad("Code",184 "Code(FUNC): FUNC must return bool or (bool, string) or error")185 return &c186 }187 // in == 2 || in == 3188 // func (*td.T, arg) (with arg ≠ *td.T)189 // func (assert, require *td.T, arg)190 if fnType.NumOut() != 0 {191 c.err = ctxerr.OpBad("Code", "Code(FUNC): FUNC must return nothing")192 return &c193 }194 c.tParams = in - 1195 c.argType = fnType.In(c.tParams)196 return &c197}198func (c *tdCode) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {199 if c.err != nil {200 return ctx.CollectError(c.err)201 }202 if !got.Type().AssignableTo(c.argType) {203 if !ctx.BeLax || !types.IsConvertible(got, c.argType) {204 if ctx.BooleanError {205 return ctxerr.BooleanError...

Full Screen

Full Screen

op_error_test.go

Source:op_error_test.go Github

copy

Full Screen

...11 "github.com/maxatome/go-testdeep/internal/ctxerr"12 "github.com/maxatome/go-testdeep/internal/test"13)14const prefix = ": bad usage of Zzz operator\n\t"15func TestOpBadUsage(t *testing.T) {16 defer color.SaveState()()17 test.EqualStr(t,18 ctxerr.OpBadUsage("Zzz", "(STRING)", nil, 1, true).Error(),19 prefix+"usage: Zzz(STRING), but received nil as 1st parameter")20 test.EqualStr(t,21 ctxerr.OpBadUsage("Zzz", "(STRING)", 42, 1, true).Error(),22 prefix+"usage: Zzz(STRING), but received int as 1st parameter")23 test.EqualStr(t,24 ctxerr.OpBadUsage("Zzz", "(STRING)", []int{}, 1, true).Error(),25 prefix+"usage: Zzz(STRING), but received []int (slice) as 1st parameter")26 test.EqualStr(t,27 ctxerr.OpBadUsage("Zzz", "(STRING)", []int{}, 1, false).Error(),28 prefix+"usage: Zzz(STRING), but received []int as 1st parameter")29 test.EqualStr(t,30 ctxerr.OpBadUsage("Zzz", "(STRING)", nil, 1, true).Error(),31 prefix+"usage: Zzz(STRING), but received nil as 1st parameter")32 test.EqualStr(t,33 ctxerr.OpBadUsage("Zzz", "(STRING)", nil, 2, true).Error(),34 prefix+"usage: Zzz(STRING), but received nil as 2nd parameter")35 test.EqualStr(t,36 ctxerr.OpBadUsage("Zzz", "(STRING)", nil, 3, true).Error(),37 prefix+"usage: Zzz(STRING), but received nil as 3rd parameter")38 test.EqualStr(t,39 ctxerr.OpBadUsage("Zzz", "(STRING)", nil, 4, true).Error(),40 prefix+"usage: Zzz(STRING), but received nil as 4th parameter")41}42func TestOpTooManyParams(t *testing.T) {43 defer color.SaveState()()44 test.EqualStr(t, ctxerr.OpTooManyParams("Zzz", "(PARAM)").Error(),45 prefix+"usage: Zzz(PARAM), too many parameters")46}47func TestBad(t *testing.T) {48 defer color.SaveState()()49 test.EqualStr(t,50 ctxerr.OpBad("Zzz", "test").Error(),51 prefix+"test")52 test.EqualStr(t,53 ctxerr.OpBad("Zzz", "test %d", 123).Error(),54 prefix+"test 123")55}56func TestBadKind(t *testing.T) {57 defer color.SaveState()()58 expected := func(got string) string {59 return ": bad kind\n\t got: " + got + "\n\texpected: some kinds"60 }61 test.EqualStr(t,62 ctxerr.BadKind(reflect.ValueOf(42), "some kinds").Error(),63 expected("int"))64 test.EqualStr(t,65 ctxerr.BadKind(reflect.ValueOf(&[]int{}), "some kinds").Error(),66 expected("*slice (*[]int type)"))67 test.EqualStr(t,...

Full Screen

Full Screen

OpBad

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctxerr := golctxerr.New("2.go")4 ctxerr.OpBad("bad op")5 fmt.Println(ctxerr)6}7import (8func main() {9 ctxerr := golctxerr.New("3.go")10 ctxerr.OpBadf("bad op with %s", "params")11 fmt.Println(ctxerr)12}13import (14func main() {15 ctxerr := golctxerr.New("4.go")16 ctxerr.OpBadf("bad op with %s", "params")17 fmt.Println(ctxerr)18}19import (20func main() {21 ctxerr := golctxerr.New("5.go")22 ctxerr.OpBadf("bad op with %s", "params")23 fmt.Println(ctxerr)24}25import (26func main() {27 ctxerr := golctxerr.New("6.go")28 ctxerr.OpBadf("bad op with %s", "params")29 fmt.Println(ctxerr)30}31import (32func main() {33 ctxerr := golctxerr.New("7.go")34 ctxerr.OpBadf("bad op with %s", "params")35 fmt.Println(ctxerr)36}

Full Screen

Full Screen

OpBad

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctxerr := golctxerr.New()4 ctxerr.OpBad("reading file", "file not found", "file.txt")5 fmt.Println(ctxerr)6}72017/02/04 18:10:56 reading file: file not found (file.txt)82017/02/04 18:11:49 reading file: file not found (file.txt)

Full Screen

Full Screen

OpBad

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := ctxerr.New("This is a new context")4 ctx = ctxerr.OpBad(ctx, "error", "this is an error")5 fmt.Println(ctx)6}7import (8func main() {9 ctx := ctxerr.New("This is a new context")10 ctx = ctxerr.OpBadf(ctx, "error", "this is an %s", "error")11 fmt.Println(ctx)12}13import (14func main() {15 ctx := ctxerr.New("This is a new context")16 ctx = ctxerr.OpBad(ctx, "error", "this is an error")17 fmt.Println(ctx)18}19import (20func main() {21 ctx := ctxerr.New("This is a new context")22 ctx = ctxerr.OpBadf(ctx, "error", "this is an %s", "error")23 fmt.Println(ctx)24}

Full Screen

Full Screen

OpBad

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 err := ctxerr.OpBad("OpBad")4 fmt.Println(err)5}63. Implementing Error() method of error interface7import (8func main() {9 err := ctxerr.OpBad("OpBad")10 fmt.Println(err.Error())11}12import (13func main() {14 err := ctxerr.OpBad("OpBad")15 fmt.Println(err)16}17import (18func main() {19 err := ctxerr.OpBad("OpBad")20 fmt.Println(err.Error())21}

Full Screen

Full Screen

OpBad

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

OpBad

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println(err)4}5import "fmt"6func main() {7 fmt.Println(err)8}9import "fmt"10func main() {11 fmt.Println(err)12}13import "fmt"14func main() {15 fmt.Println(err)16}17import "fmt"18func main() {19 fmt.Println(err)20}21import "fmt"22func main() {23 fmt.Println(err)24}25import "fmt"26func main() {27 fmt.Println(err)28}29import "fmt"30func main() {31 fmt.Println(err)32}33import "fmt"34func main() {35 fmt.Println(err)36}

Full Screen

Full Screen

OpBad

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctxerr := ctxerr.New("Some error")4 fmt.Println(ctxerr)5 ctxerr = ctxerr.AddContext("Some more context")6 fmt.Println(ctxerr)7 ctxerr = ctxerr.AddContext("Some more context")8 fmt.Println(ctxerr)9 fmt.Println(ctxerr.Error())10 fmt.Println(ctxerr.ErrorWithContext())

Full Screen

Full Screen

OpBad

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "github.com/krishna-ramaswamy/ctxerr"3func main() {4 ctx := ctxerr.NewContext("2.go", "main")5 err := ctx.OpBad("fmt", "Println", "Hello World")6 fmt.Println(err)7}8import "fmt"9import "github.com/krishna-ramaswamy/ctxerr"10func main() {11 ctx := ctxerr.NewContext("3.go", "main")12 err := ctx.OpBadf("fmt", "Println", "Hello %s", "World")13 fmt.Println(err)14}15import "fmt"16import "github.com/krishna-ramaswamy/ctxerr"17func main() {18 ctx := ctxerr.NewContext("4.go", "main")19 err := ctx.OpBadf("fmt", "Println", "Hello %s", "World")20 fmt.Println(err)21}22import "fmt"23import "github.com/krishna-ramaswamy/ctxerr"24func main() {25 ctx := ctxerr.NewContext("5.go", "main")26 err := ctx.OpBadf("fmt", "Println", "Hello %s", "World")27 fmt.Println(err)28}29import "fmt"30import "github.com/krishna-ramaswamy/ctxerr"31func main() {32 ctx := ctxerr.NewContext("6.go", "main")33 err := ctx.OpBadf("fmt", "Println", "Hello %s", "World")34 fmt.Println(err)35}

Full Screen

Full Screen

OpBad

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := ctxerr.NewCtxErr()4 ctx.OpBad("Error occured in main function")5 fmt.Println(ctx.Error())6}7import (8func main() {9 ctx := ctxerr.NewCtxErr()10 ctx.OpBad("Error occured in main function")11 fmt.Println(ctx.Error())12}13import (14func main() {15 ctx := ctxerr.NewCtxErr()16 ctx.OpBad("Error occured in main function")17 fmt.Println(ctx.Error())18}19import (20func main() {21 ctx := ctxerr.NewCtxErr()22 ctx.OpBad("Error occured in main function")23 fmt.Println(ctx.Error())24}25import (26func main() {27 ctx := ctxerr.NewCtxErr()28 ctx.OpBad("Error occured in main function")29 fmt.Println(ctx.Error())30}31import (32func main() {33 ctx := ctxerr.NewCtxErr()34 ctx.OpBad("Error occured in main function")35 fmt.Println(ctx.Error())36}

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