This repository has been archived by the owner on Dec 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
errors_test.go
154 lines (140 loc) · 4.35 KB
/
errors_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package errors
import (
"errors"
"os"
"testing"
)
var (
errOne = errors.New("one")
errTwo = errors.New("two")
errThree = errors.New("three")
errFour = errors.New("four")
)
// TestError checks the Error method of the Error.
func TestError(t *testing.T) {
r := Error{ErrSet: []error{errOne, errTwo}}
if r.Error() != "[one; two]" {
t.Error("got the wrong error output:", r.Error())
}
err := func() error {
return r
}()
if err.Error() != r.Error() {
t.Error("rich error not transcribing well")
}
}
// TestComposeAndExtendErrs checks that two errors get composed and extended
// correctly.
func TestComposeAndExtendErrs(t *testing.T) {
// Try composing one and two.
err := Compose(errOne, errTwo)
if err.Error() != "[one; two]" {
t.Error("errors are being composed incorrectly:", err)
}
if !Contains(err, errOne) {
t.Error("errors components not being checked correctly")
}
if !Contains(err, errTwo) {
t.Error("errors componenets are not being checked correctly")
}
if Contains(errTwo, err) {
// The cmp contains the base, which is not what the function is
// checking.
t.Error("errors components not being checked correctly")
}
// Check when errOne is nil.
err = Compose(nil, errTwo)
if err.Error() != "[two]" {
t.Error("errors are being composed incorrectly:", err)
}
if Contains(err, errOne) {
t.Error("errors components not being checked correctly")
}
if !Contains(err, errTwo) {
t.Error("errors componenets are not being checked correctly")
}
// Check when errTwo is nil.
err = Compose(errOne, nil)
if err.Error() != "[one]" {
t.Error("errors are being composed incorrectly:", err)
}
if !Contains(err, errOne) {
t.Error("errors components not being checked correctly")
}
if Contains(err, errTwo) {
t.Error("errors componenets are not being checked correctly")
}
// Check when both are nil.
err = Compose(nil, nil)
if err != nil {
t.Error("nils are being composed incorrectly")
}
// Check the composition of Errors.
err1 := Compose(errOne)
err2 := Compose(errTwo, errThree)
err3 := Compose(err1, err2)
if err3.Error() != "[[one]; [two; three]]" {
t.Error("error composition is happening incorrectly", err3.Error())
}
if !Contains(err3, errOne) {
t.Error("rich errors are not being composed correctly")
}
if !Contains(err3, errTwo) {
t.Error("rich errors are not being composed correctly")
}
if !Contains(err3, errThree) {
t.Error("rich errors are not being composed correctly")
}
if Contains(err2, errOne) {
t.Error("objects are getting confused")
}
// Extend err3, which should result in a three part error instead of adding
// a new nesting.
err3 = Extend(err3, errFour)
if err3.Error() != "[four; [one]; [two; three]]" {
t.Error("error extensions not working correctly", err3)
}
if IsOSNotExist(err3) {
t.Error("error improperly recognized as os.IsNotExist")
}
// Extend a core error multiple times, make sure the outcome is desired.
fullErr := Extend(Extend(Extend(errOne, errTwo), errThree), errFour)
if fullErr.Error() != "[four; three; two; one]" {
t.Error("iterated extensions are not working correctly", fullErr.Error())
}
if IsOSNotExist(fullErr) {
t.Error("error improperly recognized as os.IsNotExist")
}
}
// TestIsOSNotExist checks that not exist errors are validating correctly even
// when nested deeply.
func TestIsOSNotExist(t *testing.T) {
_, err := os.Open("48718716928376918726591872.txt")
if !IsOSNotExist(err) {
t.Error("baseline os not exist error not recognized")
}
err = Extend(err, errOne)
err = Extend(err, errTwo)
err1 := Compose(errTwo, errThree, errFour)
err = Compose(errOne, err, err1)
err = Compose(nil, nil, nil, err)
err = Compose(err, nil, nil)
if !IsOSNotExist(err) {
t.Error("After being buried, error is not recognized as os.IsNotExist")
}
}
// TestAddContext checks that AddContext behaves as anticipated.
func TestAddContext(t *testing.T) {
originalErr := errors.New("original error")
contextToNil := AddContext(nil, "some context")
if contextToNil != nil {
t.Error("adding context to a nil error should result in nil")
}
contextToErr := AddContext(originalErr, "some context")
if contextToErr.Error() != "[some context; original error]" {
t.Error("unexpected result when adding context:", contextToErr)
}
if !Contains(contextToErr, originalErr) {
t.Error("Contains function not operating correctly on error where context was added")
}
}