Skip to content

Commit

Permalink
fixed test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Hedzr Yeh <hedzrz@gmail.com>
  • Loading branch information
hedzr committed Feb 26, 2023
1 parent 88a4b74 commit abaef36
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 88 deletions.
89 changes: 2 additions & 87 deletions causes_test.go
Original file line number Diff line number Diff line change
@@ -1,99 +1,14 @@
package errors

import (
"errors"
"fmt"
"io"
"testing"
)

type DivisionError struct {
IntA int
IntB int
Msg string
}

func (e *DivisionError) Error() string {
return e.Msg
}

func Divide(a, b int) (int, error) {
if b == 0 {
return 0, &DivisionError{
Msg: fmt.Sprintf("cannot divide '%d' by zero", a),
IntA: a, IntB: b,
}
}
return a / b, nil
}

func dummy(t *testing.T) error {
a, b := 10, 0
result, err := Divide(a, b)
if err != nil {
var divErr *DivisionError
switch {
case errors.As(err, &divErr):
fmt.Printf("%d / %d is not mathematically valid: %s\n",
divErr.IntA, divErr.IntB, divErr.Error())
default:
fmt.Printf("unexpected division error: %s\n", err)
t.Fail()
}
return err
}

fmt.Printf("%d / %d = %d\n", a, b, result)
return err
}

func dummyV3(t *testing.T) error {
a, b := 10, 0
result, err := Divide(a, b)
if err != nil {
var divErr *DivisionError
switch {
case As(err, &divErr):
fmt.Printf("%d / %d is not mathematically valid: %s\n",
divErr.IntA, divErr.IntB, divErr.Error())
default:
fmt.Printf("unexpected division error: %s\n", err)
t.Fail()
}
return err
}

fmt.Printf("%d / %d = %d\n", a, b, result)
return err
}

func TestCauses2_errors(t *testing.T) {
err := io.EOF

if !errors.Is(err, io.EOF) {
t.Fail()
}

err = dummy(t)
err = fmt.Errorf("wrapped: %w", err)
t.Logf("divide: %v", err)
t.Logf("Unwrap: %v", errors.Unwrap(err))
}

func TestCauses2_errorsV3(t *testing.T) {
err := io.EOF

if !Is(err, io.EOF) {
t.Fail()
}

err = dummyV3(t)
err = fmt.Errorf("wrapped: %w", err)
t.Logf("divide: %v", err)
t.Logf("Unwrap: %v", Unwrap(err))

func TestAs_e1(t *testing.T) {
// As() on our error wrappers
err = New("xx")
err := New("xx")
var e1 *causes2
if As(err, &e1) {
t.Logf("e1: %v", e1)
Expand Down
90 changes: 89 additions & 1 deletion join_go1.13_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
package errors_test

import (
"errors"
"fmt"
"io"
"testing"

"gopkg.in/hedzr/errors.v3"
v3 "gopkg.in/hedzr/errors.v3"
)

func TestJoinErrorsStdFormat(t *testing.T) {
Expand Down Expand Up @@ -41,3 +43,89 @@ func TestJoinErrorsStdFormat(t *testing.T) {
t.Fatal("expecting err3 is err1")
}
}

type DivisionError struct {
IntA int
IntB int
Msg string
}

func (e *DivisionError) Error() string {
return e.Msg
}

func Divide(a, b int) (int, error) {
if b == 0 {
return 0, &DivisionError{
Msg: fmt.Sprintf("cannot divide '%d' by zero", a),
IntA: a, IntB: b,
}
}
return a / b, nil
}

func dummy(t *testing.T) error {
a, b := 10, 0
result, err := Divide(a, b)
if err != nil {
var divErr *DivisionError
switch {
case errors.As(err, &divErr):
fmt.Printf("%d / %d is not mathematically valid: %s\n",
divErr.IntA, divErr.IntB, divErr.Error())
default:
fmt.Printf("unexpected division error: %s\n", err)
t.Fail()
}
return err
}

fmt.Printf("%d / %d = %d\n", a, b, result)
return err
}

func dummyV3(t *testing.T) error {
a, b := 10, 0
result, err := Divide(a, b)
if err != nil {
var divErr *DivisionError
switch {
case v3.As(err, &divErr):
fmt.Printf("%d / %d is not mathematically valid: %s\n",
divErr.IntA, divErr.IntB, divErr.Error())
default:
fmt.Printf("unexpected division error: %s\n", err)
t.Fail()
}
return err
}

fmt.Printf("%d / %d = %d\n", a, b, result)
return err
}

func TestCauses2_errors(t *testing.T) {
err := io.EOF

if !errors.Is(err, io.EOF) {
t.Fail()
}

err = dummy(t)
err = fmt.Errorf("wrapped: %w", err)
t.Logf("divide: %v", err)
t.Logf("Unwrap: %v", errors.Unwrap(err))
}

func TestCauses2_errorsV3(t *testing.T) {
err := io.EOF

if !v3.Is(err, io.EOF) {
t.Fail()
}

err = dummyV3(t)
err = fmt.Errorf("wrapped: %w", err)
t.Logf("divide: %v", err)
t.Logf("Unwrap: %v", v3.Unwrap(err))
}

0 comments on commit abaef36

Please sign in to comment.