-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo113_test.go
79 lines (64 loc) · 2.64 KB
/
go113_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package errors
import (
stderrors "errors"
"fmt"
"reflect"
"testing"
)
// customErr is a custom error type for testing purposes.
type customErr struct {
msg string
}
// Error returns the error message.
func (c customErr) Error() string { return c.msg }
func TestGo113Compatibility(t *testing.T) {
t.Run("when Wrap is used to wrap a standard error, it should return an error compatible with the standard library Is", func(t *testing.T) {
// First we create an error using the standard library
err := stderrors.New("error that gets wrapped")
// Then we wrap it using our Wrap function
wrapped := Wrap(err, "wrapped up")
// Finally we check if the standard library Is function can handle our wrapped error
if !stderrors.Is(wrapped, err) {
t.Errorf("our Wrap does not support Go 1.13 error chains")
}
})
// Is should be able to handle errors created and wrapped using the standard Go features
t.Run("when Is is used to check if an error is a certain error, it should behave just like the equivalent Is function in the standard library", func(t *testing.T) {
// First we create an error using the standard Go features
err := customErr{msg: "test message"}
wrapped := fmt.Errorf("wrap it: %w", err)
// Then we check if our Is function can handle it
if !Is(wrapped, err) {
t.Error("Is failed")
}
// Finally just to make sure, we check if the standard library Is function can handle it
if !stderrors.Is(wrapped, err) {
t.Error("stderrors.Is failed")
}
})
t.Run("when As is used to check if an error is a certain error, it should behave just like the equivalent As function in the standard library", func(t *testing.T) {
// First we create an error using the standard Go features
err := customErr{msg: "test message"}
wrapped := fmt.Errorf("wrap it: %w", err)
target := new(customErr)
// Then we check if our As function can handle it
if !As(wrapped, target) {
t.Error("As failed")
}
// Finally just to make sure, we check if the standard library As function can handle it
if !stderrors.As(wrapped, target) {
t.Error("stderrors.As failed")
}
})
// Unwrap should be able to handle errors created and wrapped using the standard Go features
t.Run("when Unwrap is used to unwrap an error, it should behave just like the equivalent Unwrap function in the standard library", func(t *testing.T) {
err := customErr{msg: "test message"}
wrapped := fmt.Errorf("wrap it: %w", err)
if unwrappedErr := Unwrap(wrapped); !reflect.DeepEqual(unwrappedErr, err) {
t.Error("Unwrap failed")
}
if unwrappedErr := stderrors.Unwrap(wrapped); !reflect.DeepEqual(unwrappedErr, err) {
t.Error("stderrors.Unwrap failed")
}
})
}