-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy patherror_test.go
118 lines (101 loc) · 2.9 KB
/
error_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package pipe
import (
"errors"
"fmt"
"os"
"reflect"
"testing"
)
func TestCheckError(t *testing.T) {
t.Parallel()
someErr := fmt.Errorf("some error")
if CheckError(true, someErr) == nil {
t.Error("Expected an error when cond is true")
}
if CheckError(false, someErr) != nil {
t.Error("Expected no error when cond is false")
}
}
func ExampleCheckError() {
isPositive := New(Options{}).
Append(func(args []interface{}) (int, error) {
in, ok := args[0].(int)
return in + 1, CheckError(!ok, fmt.Errorf("not an int"))
}).
Append(func(x int) bool {
return x > 0
})
_, err := isPositive.Do([]interface{}{"some string"})
fmt.Println(err)
// Output: pipe: not an int
}
func TestCheckErrorf(t *testing.T) {
t.Parallel()
err := CheckErrorf(true, "some error %d", 1)
if err == nil {
t.Error("Expected an error when cond is true")
}
if err.Error() != "some error 1" {
t.Error("Unexpected error:", err)
}
if CheckErrorf(false, "some error %d", 1) != nil {
t.Fatal("Expected no error when cond is false")
}
}
func TestError(t *testing.T) {
t.Parallel()
e := Error{errs: []error{
&os.PathError{Op: "create", Path: "foo", Err: os.ErrExist},
}}
if e.Error() != "pipe: create foo: file already exists" {
t.Error("Unexpected single error message:", e)
}
if !errors.Is(e, e.errs[0]) {
t.Error("errors.Is() must match the first error")
}
if !errors.Is(e, os.ErrExist) {
t.Error("errors.Is(e, target) must be true when matching child error")
}
if errors.Is(e, os.ErrClosed) {
t.Error("errors.Is(e, target) must be false when no matching child error")
}
var linkErr *os.LinkError
if errors.As(e, &linkErr) {
t.Error("errors.As(e, target) must be false when no matching child error")
}
var pathErr *os.PathError
if !errors.As(e, &pathErr) {
t.Error("errors.As(e, target) must be true when matching child error")
}
if !reflect.DeepEqual(pathErr, e.errs[0]) {
t.Error("Path error from errors.As() should be set")
}
}
func TestErrorMulti(t *testing.T) {
t.Parallel()
e := Error{errs: []error{
fmt.Errorf("error 1"),
fmt.Errorf("error 2"),
&os.PathError{Op: "create", Path: "foo", Err: os.ErrExist},
}}
if expect := "pipe: multiple errors: error 1; error 2; create foo: file already exists"; e.Error() != expect {
t.Errorf("Error() must equal %q, but found: %q", expect, e.Error())
}
if errors.Unwrap(e) != nil {
t.Error("errors.Unwrap() must return nil for multiple errors")
}
if errors.Is(e, os.ErrExist) {
t.Error("errors.Is(e, target) must be false when matching more than 1 error")
}
if errors.Is(e, os.ErrClosed) {
t.Error("errors.Is(e, target) must be false when matching more than 1 error")
}
var linkErr *os.LinkError
if errors.As(e, &linkErr) {
t.Error("errors.As(e, target) must be false when matching more than 1 error")
}
var pathErr *os.PathError
if errors.As(e, &pathErr) {
t.Error("errors.As(e, target) false when matching more than 1 error")
}
}