How to use ErrorIs method of td Package

Best Go-testdeep code snippet using td.ErrorIs

td_error_is_test.go

Source:td_error_is_test.go Github

copy

Full Screen

...10 "github.com/maxatome/go-testdeep/internal/dark"11 "github.com/maxatome/go-testdeep/internal/test"12 "github.com/maxatome/go-testdeep/td"13)14func TestErrorIs(t *testing.T) {15 insideErr1 := fmt.Errorf("failure1")16 insideErr2 := fmt.Errorf("failure2: %w", insideErr1)17 insideErr3 := fmt.Errorf("failure3: %w", insideErr2)18 err := fmt.Errorf("failure4: %w", insideErr3)19 checkOK(t, err, td.ErrorIs(err))20 checkOK(t, err, td.ErrorIs(insideErr3))21 checkOK(t, err, td.ErrorIs(insideErr2))22 checkOK(t, err, td.ErrorIs(insideErr1))23 checkOK(t, nil, td.ErrorIs(nil))24 var errNil error25 checkOK(t, &errNil, td.Ptr(td.ErrorIs(nil)))26 checkError(t, nil, td.ErrorIs(insideErr1),27 expectedError{28 Message: mustBe("nil value"),29 Path: mustBe("DATA"),30 Got: mustBe("nil"),31 Expected: mustBe("anything implementing error interface"),32 })33 checkError(t, 45, td.ErrorIs(insideErr1),34 expectedError{35 Message: mustBe("int does not implement error interface"),36 Path: mustBe("DATA"),37 Got: mustBe("45"),38 Expected: mustBe("anything implementing error interface"),39 })40 checkError(t, 45, td.ErrorIs(fmt.Errorf("another")),41 expectedError{42 Message: mustBe("int does not implement error interface"),43 Path: mustBe("DATA"),44 Got: mustBe("45"),45 Expected: mustBe("anything implementing error interface"),46 })47 checkError(t, err, td.ErrorIs(fmt.Errorf("another")),48 expectedError{49 Message: mustBe("is not the error"),50 Path: mustBe("DATA"),51 Got: mustBe(`(*fmt.wrapError) "failure4: failure3: failure2: failure1"`),52 Expected: mustBe(`(*errors.errorString) "another"`),53 })54 checkError(t, err, td.ErrorIs(nil),55 expectedError{56 Message: mustBe("is not the error"),57 Path: mustBe("DATA"),58 Got: mustBe(`(*fmt.wrapError) "failure4: failure3: failure2: failure1"`),59 Expected: mustBe(`nil`),60 })61 type private struct{ err error }62 got := private{err: err}63 for _, expErr := range []error{err, insideErr3} {64 expected := td.Struct(private{}, td.StructFields{"err": td.ErrorIs(expErr)})65 if dark.UnsafeDisabled {66 checkError(t, got, expected,67 expectedError{68 Message: mustBe("cannot compare"),69 Path: mustBe("DATA.err"),70 Summary: mustBe("unexported field that cannot be overridden"),71 })72 } else {73 checkOK(t, got, expected)74 }75 }76 if !dark.UnsafeDisabled {77 got = private{}78 checkOK(t, got, td.Struct(private{}, td.StructFields{"err": td.ErrorIs(nil)}))79 }80 //81 // String82 test.EqualStr(t, td.ErrorIs(insideErr1).String(), "ErrorIs(failure1)")83 test.EqualStr(t, td.ErrorIs(nil).String(), "ErrorIs(nil)")84}85func TestErrorIsTypeBehind(t *testing.T) {86 equalTypes(t, td.ErrorIs(fmt.Errorf("another")), nil)87}...

Full Screen

Full Screen

td_error_is.go

Source:td_error_is.go Github

copy

Full Screen

...11 "github.com/maxatome/go-testdeep/internal/ctxerr"12 "github.com/maxatome/go-testdeep/internal/dark"13 "github.com/maxatome/go-testdeep/internal/types"14)15type tdErrorIs struct {16 baseOKNil17 expected error18}19var _ TestDeep = &tdErrorIs{}20func errorToRawString(err error) types.RawString {21 if err == nil {22 return "nil"23 }24 return types.RawString(fmt.Sprintf("(%[1]T) %[1]q", err))25}26// summary(ErrorIs): checks the data is an error and matches a wrapped error27// input(ErrorIs): if(error)28// ErrorIs operator reports whether any error in an error's chain29// matches expected.30//31// _, err := os.Open("/unknown/file")32// td.Cmp(t, err, os.ErrNotExist) // fails33// td.Cmp(t, err, td.ErrorIs(os.ErrNotExist)) // succeeds34//35// err1 := fmt.Errorf("failure1")36// err2 := fmt.Errorf("failure2: %w", err1)37// err3 := fmt.Errorf("failure3: %w", err2)38// err := fmt.Errorf("failure4: %w", err3)39// td.Cmp(t, err, td.ErrorIs(err)) // succeeds40// td.Cmp(t, err, td.ErrorIs(err1)) // succeeds41// td.Cmp(t, err1, td.ErrorIs(err)) // fails42//43// Behind the scene it uses [errors.Is] function.44//45// Note that like [errors.Is], expected can be nil: in this case the46// comparison succeeds when got is nil too.47//48// See also [CmpError] and [CmpNoError].49func ErrorIs(expected error) TestDeep {50 return &tdErrorIs{51 baseOKNil: newBaseOKNil(3),52 expected: expected,53 }54}55func (e *tdErrorIs) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {56 // nil case57 if !got.IsValid() {58 // Special case59 if e.expected == nil {60 return nil61 }62 if ctx.BooleanError {63 return ctxerr.BooleanError64 }65 return ctx.CollectError(&ctxerr.Error{66 Message: "nil value",67 Got: types.RawString("nil"),68 Expected: types.RawString("anything implementing error interface"),69 })70 }71 gotIf, ok := dark.GetInterface(got, true)72 if !ok {73 return ctx.CollectError(ctx.CannotCompareError())74 }75 gotErr, ok := gotIf.(error)76 if !ok {77 if ctx.BooleanError {78 return ctxerr.BooleanError79 }80 return ctx.CollectError(&ctxerr.Error{81 Message: got.Type().String() + " does not implement error interface",82 Got: gotIf,83 Expected: types.RawString("anything implementing error interface"),84 })85 }86 if errors.Is(gotErr, e.expected) {87 return nil88 }89 if ctx.BooleanError {90 return ctxerr.BooleanError91 }92 return ctx.CollectError(&ctxerr.Error{93 Message: "is not the error",94 Got: errorToRawString(gotErr),95 Expected: errorToRawString(e.expected),96 })97}98func (e *tdErrorIs) String() string {99 if e.expected == nil {100 return "ErrorIs(nil)"101 }102 return "ErrorIs(" + e.expected.Error() + ")"103}...

Full Screen

Full Screen

event_registry_test.go

Source:event_registry_test.go Github

copy

Full Screen

...16func Test_EventRegistry_Register(t *testing.T) {17 t.Run("factory should return pointer", func(t *testing.T) {18 registry := NewEventRegistry()19 err := registry.Register(factoryBadFn)20 assert.ErrorIs(t, err, ErrFactoryShouldReturnValidPointer)21 err = registry.Register(factoryNilFn)22 assert.ErrorIs(t, err, ErrFactoryShouldReturnValidPointer)23 })24 t.Run("factory should return proper type", func(t *testing.T) {25 registry := NewEventRegistry()26 err := registry.Register(factoryFn)27 assert.NoError(t, err)28 assert.Len(t, registry.factories, 1)29 td, err := registry.Create(registry.GetName(&eventRegistryTestData{}))30 assert.NoError(t, err)31 assert.IsType(t, &eventRegistryTestData{}, td)32 })33 t.Run("should not register duplicate factories", func(t *testing.T) {34 registry := NewEventRegistry()35 _ = registry.Register(factoryFn)36 err := registry.Register(factoryFn)37 assert.NoError(t, err)38 assert.Len(t, registry.factories, 1)39 })40}41func Test_EventRegistry_Unregister(t *testing.T) {42 t.Run("should return early if type not registered", func(t *testing.T) {43 registry := NewEventRegistry()44 registry.Unregister(&eventRegistryTestData{})45 assert.Len(t, registry.factories, 0)46 })47 t.Run("should remove factory", func(t *testing.T) {48 registry := NewEventRegistry()49 _ = registry.Register(factoryFn)50 registry.Unregister(factoryFn())51 assert.Len(t, registry.factories, 0)52 })53}54func Test_EventRegistry_Create(t *testing.T) {55 t.Run("should return error when type not registered", func(t *testing.T) {56 registry := NewEventRegistry()57 _, err := registry.Create("poo")58 assert.ErrorIs(t, err, ErrEventDataFactoryNotRegistered)59 })60 t.Run("should return pointer", func(t *testing.T) {61 registry := NewEventRegistry()62 _ = registry.Register(factoryFn)63 ptr, err := registry.Create(registry.GetName(factoryFn()))64 assert.NoError(t, err)65 assert.IsType(t, &eventRegistryTestData{}, ptr)66 })67}68func Test_dn(t *testing.T) {69 n1 := defaultNameFormatter(eventRegistryTestData{})70 n2 := defaultNameFormatter(&eventRegistryTestData{})71 fmt.Println(n1)72 fmt.Println(n2)...

Full Screen

Full Screen

ErrorIs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 err1 := errors.New("Error 1")4 err2 := fmt.Errorf("Error 2: %w", err1)5 err3 := fmt.Errorf("Error 3: %w", err2)6 err4 := fmt.Errorf("Error 4: %w", err3)7 fmt.Println(errors.Is(err4, err1))8 fmt.Println(errors.Is(err4, err2))9 fmt.Println(errors.Is(err4, err3))10 fmt.Println(errors.Is(err4, err4))11}

Full Screen

Full Screen

ErrorIs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 err1 := errors.New("error 1")4 err2 := errors.New("error 2")5 err3 := fmt.Errorf("error 3: %w", err2)6 err4 := fmt.Errorf("error 4: %w", err3)7 err5 := fmt.Errorf("error 5: %w", err1)8 err6 := fmt.Errorf("error 6: %w", err5)9 fmt.Println(errors.Is(err4, err2))10 fmt.Println(errors.Is(err4, err1))11 fmt.Println(errors.Is(err6, err1))12}

Full Screen

Full Screen

ErrorIs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var err1 error = errors.New("Error 1")4 var err2 error = errors.New("Error 2")5 var err3 error = errors.New("Error 3")6 fmt.Println(errors.Is(err1, err2))7 fmt.Println(errors.Is(err1, err1))8 fmt.Println(errors.Is(err1, err3))9}

Full Screen

Full Screen

ErrorIs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 err1 := errors.New("error 1")4 err2 := errors.New("error 2")5 err3 := fmt.Errorf("error 2: %w", err2)6 fmt.Println(errors.Is(err1, err2))7 fmt.Println(errors.Is(err3, err2))8}

Full Screen

Full Screen

ErrorIs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := os.Open("test.txt")4 if err != nil {5 log.Fatal(err)6 }7 fmt.Println(f.Name(), "opened successfully")8}

Full Screen

Full Screen

ErrorIs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 err := errors.New("Error")4 fmt.Println(errors.Is(err, err))5 fmt.Println(errors.Is(err, errors.New("Error")))6 fmt.Println(errors.Is(err, errors.New("Error1")))7}

Full Screen

Full Screen

ErrorIs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 e1 := errors.New("e1")4 e2 := errors.New("e2")5 e3 := errors.New("e3")6 fmt.Println(errors.Is(e1, e1))7 fmt.Println(errors.Is(e1, e2))8 fmt.Println(errors.Is(e1, e3))9}

Full Screen

Full Screen

ErrorIs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 err1 := errors.New("Error1")4 err2 := errors.New("Error2")5 err3 := errors.New("Error3")6 fmt.Println(errors.Is(err1, err2))7 fmt.Println(errors.Is(err1, err1))8 fmt.Println(errors.Is(err2, err3))9}

Full Screen

Full Screen

ErrorIs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var err error = errors.New("Error")4 var err1 error = fmt.Errorf("Error1")5 fmt.Println(errors.Is(err, err1))6}

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