How to use BeLax method of td Package

Best Go-testdeep code snippet using td.BeLax

td_between.go

Source:td_between.go Github

copy

Full Screen

...58// func (a T) Less(b T) bool // returns true if a < b59// func (a T) Compare(b T) int // returns -1 if a < b, 1 if a > b, 0 if a == b60//61// from and to must be the same type as the compared value, except62// if BeLax config flag is true. [time.Duration] type is accepted as63// to when from is [time.Time] or convertible. bounds allows to64// specify whether bounds are included or not:65// - [BoundsInIn] (default): between from and to both included66// - [BoundsInOut]: between from included and to excluded67// - [BoundsOutIn]: between from excluded and to included68// - [BoundsOutOut]: between from and to both excluded69//70// If bounds is missing, it defaults to [BoundsInIn].71//72// tc.Cmp(t, 17, td.Between(17, 20)) // succeeds, BoundsInIn by default73// tc.Cmp(t, 17, td.Between(10, 17, BoundsInOut)) // fails74// tc.Cmp(t, 17, td.Between(10, 17, BoundsOutIn)) // succeeds75// tc.Cmp(t, 17, td.Between(17, 20, BoundsOutOut)) // fails76// tc.Cmp(t, // succeeds77// netip.MustParse("127.0.0.1"),78// td.Between(netip.MustParse("127.0.0.0"), netip.MustParse("127.255.255.255")))79//80// TypeBehind method returns the [reflect.Type] of from.81func Between(from, to any, bounds ...BoundsKind) TestDeep {82 b := tdBetween{83 base: newBase(3),84 expectedMin: reflect.ValueOf(from),85 expectedMax: reflect.ValueOf(to),86 }87 const usage = "(NUM|STRING|TIME, NUM|STRING|TIME/DURATION[, BOUNDS_KIND])"88 if len(bounds) > 0 {89 if len(bounds) > 1 {90 b.err = ctxerr.OpTooManyParams("Between", usage)91 return &b92 }93 if bounds[0] == BoundsInIn || bounds[0] == BoundsInOut {94 b.minBound = boundIn95 } else {96 b.minBound = boundOut97 }98 if bounds[0] == BoundsInIn || bounds[0] == BoundsOutIn {99 b.maxBound = boundIn100 } else {101 b.maxBound = boundOut102 }103 } else {104 b.minBound = boundIn105 b.maxBound = boundIn106 }107 if b.expectedMax.Type() == b.expectedMin.Type() {108 return b.initBetween(usage)109 }110 // Special case for (TIME, DURATION)111 ok, convertible := types.IsTypeOrConvertible(b.expectedMin, types.Time)112 if ok {113 if d, ok := to.(time.Duration); ok {114 if convertible {115 b.expectedMax = reflect.ValueOf(116 b.expectedMin.117 Convert(types.Time).118 Interface().(time.Time).119 Add(d)).120 Convert(b.expectedMin.Type())121 } else {122 b.expectedMax = reflect.ValueOf(from.(time.Time).Add(d))123 }124 return b.initBetween(usage)125 }126 b.err = ctxerr.OpBad("Between",127 "Between(FROM, TO): when FROM type is %[1]s, TO must have the same type or time.Duration: %[2]s ≠ %[1]s|time.Duration",128 b.expectedMin.Type(),129 b.expectedMax.Type(),130 )131 return &b132 }133 b.err = ctxerr.OpBad("Between",134 "Between(FROM, TO): FROM and TO must have the same type: %s ≠ %s",135 b.expectedMin.Type(),136 b.expectedMax.Type(),137 )138 return &b139}140func (b *tdBetween) initBetween(usage string) TestDeep {141 if !b.expectedMax.IsValid() {142 b.expectedMax = b.expectedMin143 }144 // Is any of:145 // (T) Compare(T) int146 // or147 // (T) Less(T) bool148 // available?149 if cmp := types.NewOrder(b.expectedMin.Type()); cmp != nil {150 if order := cmp(b.expectedMin, b.expectedMax); order > 0 {151 b.expectedMin, b.expectedMax = b.expectedMax, b.expectedMin152 }153 return &tdBetweenCmp{154 tdBetween: *b,155 expectedType: b.expectedMin.Type(),156 cmp: cmp,157 }158 }159 switch b.expectedMin.Kind() {160 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:161 if b.expectedMin.Int() > b.expectedMax.Int() {162 b.expectedMin, b.expectedMax = b.expectedMax, b.expectedMin163 }164 return b165 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:166 if b.expectedMin.Uint() > b.expectedMax.Uint() {167 b.expectedMin, b.expectedMax = b.expectedMax, b.expectedMin168 }169 return b170 case reflect.Float32, reflect.Float64:171 if b.expectedMin.Float() > b.expectedMax.Float() {172 b.expectedMin, b.expectedMax = b.expectedMax, b.expectedMin173 }174 return b175 case reflect.String:176 if b.expectedMin.String() > b.expectedMax.String() {177 b.expectedMin, b.expectedMax = b.expectedMax, b.expectedMin178 }179 return b180 case reflect.Struct:181 ok, convertible := types.IsTypeOrConvertible(b.expectedMin, types.Time)182 if !ok {183 break184 }185 var bt tdBetweenTime186 if convertible {187 bt = tdBetweenTime{188 tdBetween: *b,189 expectedType: b.expectedMin.Type(),190 mustConvert: true,191 }192 bt.expectedMin = b.expectedMin.Convert(types.Time)193 bt.expectedMax = b.expectedMax.Convert(types.Time)194 } else {195 bt = tdBetweenTime{196 tdBetween: *b,197 expectedType: types.Time,198 }199 }200 if bt.expectedMin.Interface().(time.Time).201 After(bt.expectedMax.Interface().(time.Time)) {202 bt.expectedMin, bt.expectedMax = bt.expectedMax, bt.expectedMin203 }204 return &bt205 }206 b.err = ctxerr.OpBadUsage(b.GetLocation().Func,207 usage, b.expectedMin.Interface(), 1, true)208 return b209}210func (b *tdBetween) nInt(tolerance reflect.Value) {211 if diff := tolerance.Int(); diff != 0 {212 expectedBase := b.expectedMin.Int()213 max := expectedBase + diff214 if max < expectedBase {215 max = math.MaxInt64216 }217 min := expectedBase - diff218 if min > expectedBase {219 min = math.MinInt64220 }221 b.expectedMin = reflect.New(tolerance.Type()).Elem()222 b.expectedMin.SetInt(min)223 b.expectedMax = reflect.New(tolerance.Type()).Elem()224 b.expectedMax.SetInt(max)225 }226}227func (b *tdBetween) nUint(tolerance reflect.Value) {228 if diff := tolerance.Uint(); diff != 0 {229 base := b.expectedMin.Uint()230 max := base + diff231 if max < base {232 max = math.MaxUint64233 }234 min := base - diff235 if min > base {236 min = 0237 }238 b.expectedMin = reflect.New(tolerance.Type()).Elem()239 b.expectedMin.SetUint(min)240 b.expectedMax = reflect.New(tolerance.Type()).Elem()241 b.expectedMax.SetUint(max)242 }243}244func (b *tdBetween) nFloat(tolerance reflect.Value) {245 if diff := tolerance.Float(); diff != 0 {246 base := b.expectedMin.Float()247 b.expectedMin = reflect.New(tolerance.Type()).Elem()248 b.expectedMin.SetFloat(base - diff)249 b.expectedMax = reflect.New(tolerance.Type()).Elem()250 b.expectedMax.SetFloat(base + diff)251 }252}253// summary(N): compares a number with a tolerance value254// input(N): int,float,cplx(todo)255// N operator compares a numeric data against num ± tolerance. If256// tolerance is missing, it defaults to 0. num and tolerance257// must be the same type as the compared value, except if BeLax config258// flag is true.259//260// td.Cmp(t, 12.2, td.N(12., 0.3)) // succeeds261// td.Cmp(t, 12.2, td.N(12., 0.1)) // fails262//263// TypeBehind method returns the [reflect.Type] of num.264func N(num any, tolerance ...any) TestDeep {265 n := tdBetween{266 base: newBase(3),267 expectedMin: reflect.ValueOf(num),268 minBound: boundIn,269 maxBound: boundIn,270 }271 const usage = "({,U}INT{,8,16,32,64}|FLOAT{32,64}[, TOLERANCE])"272 switch n.expectedMin.Kind() {273 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,274 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,275 reflect.Float32, reflect.Float64:276 default:277 n.err = ctxerr.OpBadUsage("N", usage, num, 1, true)278 return &n279 }280 n.expectedMax = n.expectedMin281 if len(tolerance) > 0 {282 if len(tolerance) > 1 {283 n.err = ctxerr.OpTooManyParams("N", usage)284 return &n285 }286 tol := reflect.ValueOf(tolerance[0])287 if tol.Type() != n.expectedMin.Type() {288 n.err = ctxerr.OpBad("N",289 "N(NUM, TOLERANCE): NUM and TOLERANCE must have the same type: %s ≠ %s",290 n.expectedMin.Type(), tol.Type())291 return &n292 }293 switch tol.Kind() {294 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:295 n.nInt(tol)296 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,297 reflect.Uint64:298 n.nUint(tol)299 default: // case reflect.Float32, reflect.Float64:300 n.nFloat(tol)301 }302 }303 return &n304}305// summary(Gt): checks that a number, string or time.Time is306// greater than a value307// input(Gt): str,int,float,cplx(todo),struct(time.Time)308// Gt operator checks that data is greater than309// minExpectedValue. minExpectedValue can be any numeric, string,310// [time.Time] (or assignable) value or implements at least one of the311// two following methods:312//313// func (a T) Less(b T) bool // returns true if a < b314// func (a T) Compare(b T) int // returns -1 if a < b, 1 if a > b, 0 if a == b315//316// minExpectedValue must be the same type as the compared value,317// except if BeLax config flag is true.318//319// td.Cmp(t, 17, td.Gt(15))320// before := time.Now()321// td.Cmp(t, time.Now(), td.Gt(before))322//323// TypeBehind method returns the [reflect.Type] of minExpectedValue.324func Gt(minExpectedValue any) TestDeep {325 b := &tdBetween{326 base: newBase(3),327 expectedMin: reflect.ValueOf(minExpectedValue),328 minBound: boundOut,329 }330 return b.initBetween("(NUM|STRING|TIME)")331}332// summary(Gte): checks that a number, string or time.Time is333// greater or equal than a value334// input(Gte): str,int,float,cplx(todo),struct(time.Time)335// Gte operator checks that data is greater or equal than336// minExpectedValue. minExpectedValue can be any numeric, string,337// [time.Time] (or assignable) value or implements at least one of the338// two following methods:339//340// func (a T) Less(b T) bool // returns true if a < b341// func (a T) Compare(b T) int // returns -1 if a < b, 1 if a > b, 0 if a == b342//343// minExpectedValue must be the same type as the compared value,344// except if BeLax config flag is true.345//346// td.Cmp(t, 17, td.Gte(17))347// before := time.Now()348// td.Cmp(t, time.Now(), td.Gte(before))349//350// TypeBehind method returns the [reflect.Type] of minExpectedValue.351func Gte(minExpectedValue any) TestDeep {352 b := &tdBetween{353 base: newBase(3),354 expectedMin: reflect.ValueOf(minExpectedValue),355 minBound: boundIn,356 }357 return b.initBetween("(NUM|STRING|TIME)")358}359// summary(Lt): checks that a number, string or time.Time is360// lesser than a value361// input(Lt): str,int,float,cplx(todo),struct(time.Time)362// Lt operator checks that data is lesser than363// maxExpectedValue. maxExpectedValue can be any numeric, string,364// [time.Time] (or assignable) value or implements at least one of the365// two following methods:366//367// func (a T) Less(b T) bool // returns true if a < b368// func (a T) Compare(b T) int // returns -1 if a < b, 1 if a > b, 0 if a == b369//370// maxExpectedValue must be the same type as the compared value,371// except if BeLax config flag is true.372//373// td.Cmp(t, 17, td.Lt(19))374// before := time.Now()375// td.Cmp(t, before, td.Lt(time.Now()))376//377// TypeBehind method returns the [reflect.Type] of maxExpectedValue.378func Lt(maxExpectedValue any) TestDeep {379 b := &tdBetween{380 base: newBase(3),381 expectedMin: reflect.ValueOf(maxExpectedValue),382 maxBound: boundOut,383 }384 return b.initBetween("(NUM|STRING|TIME)")385}386// summary(Lte): checks that a number, string or time.Time is387// lesser or equal than a value388// input(Lte): str,int,float,cplx(todo),struct(time.Time)389// Lte operator checks that data is lesser or equal than390// maxExpectedValue. maxExpectedValue can be any numeric, string,391// [time.Time] (or assignable) value or implements at least one of the392// two following methods:393//394// func (a T) Less(b T) bool // returns true if a < b395// func (a T) Compare(b T) int // returns -1 if a < b, 1 if a > b, 0 if a == b396//397// maxExpectedValue must be the same type as the compared value,398// except if BeLax config flag is true.399//400// td.Cmp(t, 17, td.Lte(17))401// before := time.Now()402// td.Cmp(t, before, td.Lt(time.Now()))403//404// TypeBehind method returns the [reflect.Type] of maxExpectedValue.405func Lte(maxExpectedValue any) TestDeep {406 b := &tdBetween{407 base: newBase(3),408 expectedMin: reflect.ValueOf(maxExpectedValue),409 maxBound: boundIn,410 }411 return b.initBetween("(NUM|STRING|TIME)")412}413func (b *tdBetween) matchInt(got reflect.Value) (ok bool) {414 switch b.minBound {415 case boundIn:416 ok = got.Int() >= b.expectedMin.Int()417 case boundOut:418 ok = got.Int() > b.expectedMin.Int()419 default:420 ok = true421 }422 if ok {423 switch b.maxBound {424 case boundIn:425 ok = got.Int() <= b.expectedMax.Int()426 case boundOut:427 ok = got.Int() < b.expectedMax.Int()428 default:429 ok = true430 }431 }432 return433}434func (b *tdBetween) matchUint(got reflect.Value) (ok bool) {435 switch b.minBound {436 case boundIn:437 ok = got.Uint() >= b.expectedMin.Uint()438 case boundOut:439 ok = got.Uint() > b.expectedMin.Uint()440 default:441 ok = true442 }443 if ok {444 switch b.maxBound {445 case boundIn:446 ok = got.Uint() <= b.expectedMax.Uint()447 case boundOut:448 ok = got.Uint() < b.expectedMax.Uint()449 default:450 ok = true451 }452 }453 return454}455func (b *tdBetween) matchFloat(got reflect.Value) (ok bool) {456 switch b.minBound {457 case boundIn:458 ok = got.Float() >= b.expectedMin.Float()459 case boundOut:460 ok = got.Float() > b.expectedMin.Float()461 default:462 ok = true463 }464 if ok {465 switch b.maxBound {466 case boundIn:467 ok = got.Float() <= b.expectedMax.Float()468 case boundOut:469 ok = got.Float() < b.expectedMax.Float()470 default:471 ok = true472 }473 }474 return475}476func (b *tdBetween) matchString(got reflect.Value) (ok bool) {477 switch b.minBound {478 case boundIn:479 ok = got.String() >= b.expectedMin.String()480 case boundOut:481 ok = got.String() > b.expectedMin.String()482 default:483 ok = true484 }485 if ok {486 switch b.maxBound {487 case boundIn:488 ok = got.String() <= b.expectedMax.String()489 case boundOut:490 ok = got.String() < b.expectedMax.String()491 default:492 ok = true493 }494 }495 return496}497func (b *tdBetween) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {498 if b.err != nil {499 return ctx.CollectError(b.err)500 }501 if got.Type() != b.expectedMin.Type() {502 if ctx.BeLax && types.IsConvertible(b.expectedMin, got.Type()) {503 nb := *b504 nb.expectedMin = b.expectedMin.Convert(got.Type())505 nb.expectedMax = b.expectedMax.Convert(got.Type())506 b = &nb507 } else {508 if ctx.BooleanError {509 return ctxerr.BooleanError510 }511 return ctx.CollectError(ctxerr.TypeMismatch(got.Type(), b.expectedMin.Type()))512 }513 }514 var ok bool515 switch got.Kind() {516 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:517 ok = b.matchInt(got)518 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:519 ok = b.matchUint(got)520 case reflect.Float32, reflect.Float64:521 ok = b.matchFloat(got)522 case reflect.String:523 ok = b.matchString(got)524 }525 if ok {526 return nil527 }528 if ctx.BooleanError {529 return ctxerr.BooleanError530 }531 return ctx.CollectError(&ctxerr.Error{532 Message: "values differ",533 Got: got,534 Expected: types.RawString(b.String()),535 })536}537func (b *tdBetween) String() string {538 if b.err != nil {539 return b.stringError()540 }541 var (542 min, max any543 minStr, maxStr string544 )545 if b.minBound != boundNone {546 min = b.expectedMin.Interface()547 minStr = util.ToString(min)548 }549 if b.maxBound != boundNone {550 max = b.expectedMax.Interface()551 maxStr = util.ToString(max)552 }553 if min != nil {554 if max != nil {555 return fmt.Sprintf("%s %c got %c %s",556 minStr,557 util.TernRune(b.minBound == boundIn, '≤', '<'),558 util.TernRune(b.maxBound == boundIn, '≤', '<'),559 maxStr)560 }561 return fmt.Sprintf("%c %s",562 util.TernRune(b.minBound == boundIn, '≥', '>'), minStr)563 }564 return fmt.Sprintf("%c %s",565 util.TernRune(b.maxBound == boundIn, '≤', '<'), maxStr)566}567func (b *tdBetween) TypeBehind() reflect.Type {568 if b.err != nil {569 return nil570 }571 return b.expectedMin.Type()572}573var _ TestDeep = &tdBetweenTime{}574func (b *tdBetweenTime) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {575 // b.err != nil is not possible here, as when a *tdBetweenTime is576 // built, there is never an error577 if got.Type() != b.expectedType {578 if ctx.BeLax && types.IsConvertible(got, b.expectedType) {579 got = got.Convert(b.expectedType)580 } else {581 if ctx.BooleanError {582 return ctxerr.BooleanError583 }584 return ctx.CollectError(ctxerr.TypeMismatch(got.Type(), b.expectedType))585 }586 }587 cmpGot, err := getTime(ctx, got, b.mustConvert)588 if err != nil {589 return ctx.CollectError(err)590 }591 var ok bool592 if b.minBound != boundNone {593 min := b.expectedMin.Interface().(time.Time)594 if b.minBound == boundIn {595 ok = !min.After(cmpGot)596 } else {597 ok = cmpGot.After(min)598 }599 } else {600 ok = true601 }602 if ok && b.maxBound != boundNone {603 max := b.expectedMax.Interface().(time.Time)604 if b.maxBound == boundIn {605 ok = !max.Before(cmpGot)606 } else {607 ok = cmpGot.Before(max)608 }609 }610 if ok {611 return nil612 }613 if ctx.BooleanError {614 return ctxerr.BooleanError615 }616 return ctx.CollectError(&ctxerr.Error{617 Message: "values differ",618 Got: got,619 Expected: types.RawString(b.String()),620 })621}622func (b *tdBetweenTime) TypeBehind() reflect.Type {623 // b.err != nil is not possible here, as when a *tdBetweenTime is624 // built, there is never an error625 return b.expectedType626}627var _ TestDeep = &tdBetweenCmp{}628func (b *tdBetweenCmp) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {629 // b.err != nil is not possible here, as when a *tdBetweenCmp is630 // built, there is never an error631 if got.Type() != b.expectedType {632 if ctx.BeLax && types.IsConvertible(got, b.expectedType) {633 got = got.Convert(b.expectedType)634 } else {635 if ctx.BooleanError {636 return ctxerr.BooleanError637 }638 return ctx.CollectError(ctxerr.TypeMismatch(got.Type(), b.expectedType))639 }640 }641 var ok bool642 if b.minBound != boundNone {643 order := b.cmp(got, b.expectedMin)644 if b.minBound == boundIn {645 ok = order >= 0646 } else {...

Full Screen

Full Screen

config.go

Source:config.go Github

copy

Full Screen

...53 //54 // See (*T).UseEqual method to only apply this property to some55 // specific types.56 UseEqual bool57 // BeLax allows to compare different but convertible types. If set58 // to false (default), got and expected types must be the same. If59 // set to true and expected type is convertible to got one, expected60 // is first converted to go type before its comparison. See CmpLax61 // function/method and Lax operator to set this flag without62 // providing a specific configuration.63 BeLax bool64 // IgnoreUnexported allows to ignore unexported struct fields. Be65 // careful about structs entirely composed of unexported fields66 // (like time.Time for example). With this flag set to true, they67 // are all equal. In such case it is advised to set UseEqual flag,68 // to use (*T).UseEqual method or to add a Cmp hook using69 // (*T).WithCmpHooks method.70 //71 // See (*T).IgnoreUnexported method to only apply this property to some72 // specific types.73 IgnoreUnexported bool74}75// Equal returns true if both c and o are equal. Only public fields76// are taken into account to check equality.77func (c ContextConfig) Equal(o ContextConfig) bool {78 return c.RootName == o.RootName &&79 c.MaxErrors == o.MaxErrors &&80 c.FailureIsFatal == o.FailureIsFatal &&81 c.UseEqual == o.UseEqual &&82 c.BeLax == o.BeLax &&83 c.IgnoreUnexported == o.IgnoreUnexported84}85// OriginalPath returns the current path when the [ContextConfig] has86// been built. It always returns ContextConfig.RootName except if c87// has been built by [Code] operator. See [Code] documentation for an88// example of use.89func (c ContextConfig) OriginalPath() string {90 if c.forkedFromCtx == nil {91 return c.RootName92 }93 return c.forkedFromCtx.Path.String()94}95const (96 contextDefaultRootName = "DATA"97 contextPanicRootName = "FUNCTION"98 envMaxErrors = "TESTDEEP_MAX_ERRORS"99)100func getMaxErrorsFromEnv() int {101 env := os.Getenv(envMaxErrors)102 if env != "" {103 n, err := strconv.Atoi(env)104 if err == nil {105 return n106 }107 }108 return 10109}110// DefaultContextConfig is the default configuration used to render111// tests failures. If overridden, new settings will impact all Cmp*112// functions and [*T] methods (if not specifically configured.)113var DefaultContextConfig = ContextConfig{114 RootName: contextDefaultRootName,115 MaxErrors: getMaxErrorsFromEnv(),116 FailureIsFatal: false,117 UseEqual: false,118 BeLax: false,119 IgnoreUnexported: false,120}121func (c *ContextConfig) sanitize() {122 if c.RootName == "" {123 c.RootName = DefaultContextConfig.RootName124 }125 if c.MaxErrors == 0 {126 c.MaxErrors = DefaultContextConfig.MaxErrors127 }128}129// newContext creates a new ctxerr.Context using DefaultContextConfig130// configuration.131func newContext(t TestingT) ctxerr.Context {132 if tt, ok := t.(*T); ok {133 return newContextWithConfig(tt, tt.Config)134 }135 tb, _ := t.(testing.TB)136 return newContextWithConfig(tb, DefaultContextConfig)137}138// newContextWithConfig creates a new ctxerr.Context using a specific139// configuration.140func newContextWithConfig(tb testing.TB, config ContextConfig) (ctx ctxerr.Context) {141 config.sanitize()142 ctx = ctxerr.Context{143 Path: ctxerr.NewPath(config.RootName),144 Visited: visited.NewVisited(),145 MaxErrors: config.MaxErrors,146 Anchors: config.anchors,147 Hooks: config.hooks,148 OriginalTB: tb,149 FailureIsFatal: config.FailureIsFatal,150 UseEqual: config.UseEqual,151 BeLax: config.BeLax,152 IgnoreUnexported: config.IgnoreUnexported,153 }154 ctx.InitErrors()155 return156}157// newBooleanContext creates a new boolean ctxerr.Context.158func newBooleanContext() ctxerr.Context {159 return ctxerr.Context{160 Visited: visited.NewVisited(),161 BooleanError: true,162 UseEqual: DefaultContextConfig.UseEqual,163 BeLax: DefaultContextConfig.BeLax,164 IgnoreUnexported: DefaultContextConfig.IgnoreUnexported,165 }166}...

Full Screen

Full Screen

td_lax.go

Source:td_lax.go Github

copy

Full Screen

...12type tdLax struct {13 tdSmugglerBase14}15var _ TestDeep = &tdLax{}16// summary(Lax): temporarily enables [`BeLax` config flag]17// input(Lax): all18// Lax is a smuggler operator, it temporarily enables the BeLax config19// flag before letting the comparison process continue its course.20//21// It is more commonly used as [CmpLax] function than as an operator. It22// could be used when, for example, an operator is constructed once23// but applied to different, but compatible types as in:24//25// bw := td.Between(20, 30)26// intValue := 2127// floatValue := 21.8928// td.Cmp(t, intValue, bw) // no need to be lax here: same int types29// td.Cmp(t, floatValue, td.Lax(bw)) // be lax please, as float64 ≠ int30//31// Note that in the latter case, [CmpLax] could be used as well:32//33// td.CmpLax(t, floatValue, bw)34//35// TypeBehind method returns the greatest convertible or more common36// [reflect.Type] of expectedValue if it is a base type (bool, int*,37// uint*, float*, complex*, string), the [reflect.Type] of38// expectedValue otherwise, except if expectedValue is a [TestDeep]39// operator. In this case, it delegates TypeBehind() to the operator.40func Lax(expectedValue any) TestDeep {41 c := tdLax{42 tdSmugglerBase: newSmugglerBase(expectedValue),43 }44 if !c.isTestDeeper {45 c.expectedValue = reflect.ValueOf(expectedValue)46 }47 return &c48}49func (l *tdLax) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {50 ctx.BeLax = true51 return deepValueEqual(ctx, got, l.expectedValue)52}53func (l *tdLax) HandleInvalid() bool {54 return true // Knows how to handle untyped nil values (aka invalid values)55}56func (l *tdLax) String() string {57 return "Lax(" + util.ToString(l.expectedValue) + ")"58}59func (l *tdLax) TypeBehind() reflect.Type {60 // If the expected value is a TestDeep operator, delegate TypeBehind to it61 if l.isTestDeeper {62 return l.expectedValue.Interface().(TestDeep).TypeBehind()63 }64 // For base types, returns the greatest convertible or more common one...

Full Screen

Full Screen

BeLax

Using AI Code Generation

copy

Full Screen

1import (2func init() {3 tpl = template.Must(template.ParseFiles("index.gohtml"))4}5func main() {6 http.HandleFunc("/", index)7 http.HandleFunc("/process", process)8 http.Handle("/favicon.ico", http.NotFoundHandler())9 http.ListenAndServe(":8080", nil)10}11func index(res http.ResponseWriter, req *http.Request) {12 err := tpl.Execute(res, nil)13 if err != nil {14 log.Fatalln(err)15 }16}17func process(res http.ResponseWriter, req *http.Request) {18 err := req.ParseForm()19 if err != nil {20 log.Fatalln(err)21 }22 data := struct {23 }{24 }25 err = tpl.Execute(res, data)26 if err != nil {27 log.Fatalln(err)28 }29}30import (31func init() {32 tpl = template.Must(template.ParseFiles("index.gohtml"))33}34func main() {35 http.HandleFunc("/", index)36 http.HandleFunc("/process", process)37 http.Handle("/favicon.ico", http.NotFoundHandler())38 http.ListenAndServe(":8080", nil)39}40func index(res http.ResponseWriter, req *http.Request) {41 err := tpl.Execute(res, nil)42 if err != nil {43 log.Fatalln(err)44 }45}46func process(res http.ResponseWriter, req *http.Request) {47 err := req.ParseForm()48 if err != nil {49 log.Fatalln(err)50 }51 data := struct {

Full Screen

Full Screen

BeLax

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/html", html.Minify)6 m.AddFunc("text/javascript", js.Minify)7 m.AddFunc("application/json", json.Minify)8 m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)9 m.AddFunc("image/svg+xml", svg.Minify)10 files, err := ioutil.ReadDir("./")11 if err != nil {12 log.Fatal(err)13 }14 for _, f := range files {15 if strings.HasSuffix(f.Name(), ".html") {16 file, err := os.Open(f.Name())17 if err != nil {18 log.Fatal(err)19 }20 defer file.Close()21 minified, err := m.Bytes("text/html", file)22 if err != nil {23 log.Fatal(err)24 }25 err = ioutil.WriteFile(f.Name(), minified, 0644)26 if err != nil {27 log.Fatal(err)28 }29 }30 }31}32import (33func main() {34 m := minify.New()35 m.AddFunc("text/css", css.Minify)

Full Screen

Full Screen

BeLax

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 xlFile, err := xlsx.OpenFile(excelFileName)4 if err != nil {5 fmt.Println(err)6 }7 for _, row := range sheet.Rows {8 for _, cell := range row.Cells {9 text := cell.String()10 fmt.Printf("%s\n", text)11 }12 }13}14import (15func main() {16 xlFile, err := xlsx.OpenFile(excelFileName)17 if err != nil {18 fmt.Println(err)19 }20 for _, row := range sheet.Rows {21 for _, cell := range row.Cells {22 text := cell.String()23 fmt.Printf("%s\n", text)24 }25 }26}27import (28func main() {29 xlFile, err := xlsx.OpenFile(excelFileName)30 if err != nil {31 fmt.Println(err)32 }33 for _, row := range sheet.Rows {34 for _, cell := range row.Cells {35 text := cell.String()36 fmt.Printf("%s\n", text)37 }38 }39}40import (41func main() {

Full Screen

Full Screen

BeLax

Using AI Code Generation

copy

Full Screen

1import (2func init() {3 tpl = template.Must(template.ParseGlob("templates/*.gohtml"))4}5func main() {6 http.HandleFunc("/", index)7 http.HandleFunc("/about", about)8 http.HandleFunc("/contact", contact)9 http.HandleFunc("/apply", apply)10 http.Handle("/favicon.ico", http.NotFoundHandler())11 http.ListenAndServe(":8080", nil)12}13func index(w http.ResponseWriter, req *http.Request) {14 err := tpl.ExecuteTemplate(w, "index.gohtml", nil)15 if err != nil {16 log.Fatalln(err)17 }18}19func about(w http.ResponseWriter, req *http.Request) {20 err := tpl.ExecuteTemplate(w, "about.gohtml", nil)21 if err != nil {22 log.Fatalln(err)23 }24}25func contact(w http.ResponseWriter, req *http.Request) {26 err := tpl.ExecuteTemplate(w, "contact.gohtml", nil)27 if err != nil {28 log.Fatalln(err)29 }30}31func apply(w http.ResponseWriter, req *http.Request) {32 if req.Method == http.MethodPost {33 err := req.ParseForm()34 if err != nil {35 log.Fatalln(err)36 }37 data := struct {38 }{39 req.FormValue("fname"),40 req.FormValue("lname"),41 }42 err = tpl.ExecuteTemplate(w, "applyProcess.gohtml", data)43 if err != nil {44 log.Fatalln(err)45 }46 }47 err := tpl.ExecuteTemplate(w, "apply.gohtml", nil)48 if err != nil {49 log.Fatalln(err)50 }51}52import (53func init() {54 tpl = template.Must(template.ParseGlob("templates/*.gohtml"))55}56func main() {57 http.HandleFunc("/", index)58 http.HandleFunc("/about", about)59 http.HandleFunc("/contact", contact)60 http.HandleFunc("/apply", apply)61 http.Handle("/favicon.ico", http.NotFoundHandler())62 http.ListenAndServe(":8080", nil)63}64func index(w http

Full Screen

Full Screen

BeLax

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 xlFile, _ := xlsx.OpenFile("1.xlsx")4 for _, row := range sheet.Rows {5 for _, cell := range row.Cells {6 text := cell.String()7 fmt.Printf("%s\n", text)8 }9 }10}11import (12func main() {13 xlFile, _ := xlsx.OpenFile("1.xlsx")14 for _, row := range sheet.Rows {15 for _, cell := range row.Cells {16 text := cell.String()17 fmt.Printf("%s\n", text)18 }19 }20}21import (22func main() {23 xlFile, _ := xlsx.OpenFile("1.xlsx")24 for _, row := range sheet.Rows {25 for _, cell := range row.Cells {26 text := cell.String()27 fmt.Printf("%s\n", text)28 }29 }30}31import (32func main() {33 xlFile, _ := xlsx.OpenFile("1.xlsx")34 for _, row := range sheet.Rows {35 for _, cell := range row.Cells {36 text := cell.String()37 fmt.Printf("%s\n", text)38 }39 }40}41import (42func main() {43 xlFile, _ := xlsx.OpenFile("1.xlsx")44 for _, row := range sheet.Rows {45 for _, cell := range row.Cells {46 text := cell.String()

Full Screen

Full Screen

BeLax

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 http.HandleFunc("/", index)4 http.ListenAndServe(":8080", nil)5}6func index(w http.ResponseWriter, r *http.Request) {7 tpl := template.Must(template.ParseFiles("index.gohtml"))8 err := tpl.ExecuteTemplate(w, "index.gohtml", "Hello World")9 if err != nil {10 http.Error(w, err.Error(), 500)11 fmt.Println(err)12 }13}14 <p>{{.}}</p>15 <p>{{. | html}}</p>16 <p>{{. | html | safeHTML}}</p>17import (18func main() {19 http.HandleFunc("/", index)20 http.ListenAndServe(":8080", nil)21}22func index(w http.ResponseWriter, r *http.Request) {23 tpl := template.Must(template.ParseFiles("index.gohtml"))24 err := tpl.ExecuteTemplate(w, "index.gohtml", "Hello World")25 if err != nil {26 http.Error(w, err.Error(), 500)27 fmt.Println(err)28 }29}30 <p>{{.}}</p>31 <p>{{. | html}}</p>32 <p>{{. | html | safeHTML}}</p>33import (34func main() {35 http.HandleFunc("/", index)36 http.ListenAndServe(":8080

Full Screen

Full Screen

BeLax

Using AI Code Generation

copy

Full Screen

1func main() {2 td := tdlib.New()3 td.BeLax()4}5func main() {6 td := tdlib.New()7 td.BeStrict()8}9func main() {10 td := tdlib.New()11 td.BeLax()12}13func main() {14 td := tdlib.New()15 td.BeStrict()16}17func main() {18 td := tdlib.New()19 td.BeLax()20}21func main() {22 td := tdlib.New()23 td.BeStrict()24}25func main() {26 td := tdlib.New()27 td.BeLax()28}29func main() {30 td := tdlib.New()31 td.BeStrict()32}33func main() {34 td := tdlib.New()35 td.BeLax()36}37func main() {38 td := tdlib.New()39 td.BeStrict()40}41func main() {42 td := tdlib.New()43 td.BeLax()44}45func main() {46 td := tdlib.New()47 td.BeStrict()48}49func 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