-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrtag_test.go
More file actions
71 lines (53 loc) · 1.53 KB
/
errtag_test.go
File metadata and controls
71 lines (53 loc) · 1.53 KB
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
package et
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
// Example namespace
type porridgeErrs struct {
Namespace
}
type errTooCold struct {
Member[porridgeErrs]
}
type errTooHot struct {
Member[porridgeErrs]
}
type chairErrs struct {
Namespace
}
type errTooBig struct {
Member[chairErrs]
}
type errTooSmall struct {
Member[chairErrs]
}
type bedErrs struct {
Namespace
}
type errTooHard struct {
Member[bedErrs]
}
type errTooSoft struct {
Member[bedErrs]
}
func TestErrtag(t *testing.T) {
errA := Errorf[errTooHot]("ouch my tongue")
assert.Equal(t, "porridgeErrs::errTooHot: ouch my tongue", errA.Error())
errB := Errorf[errTooSmall]("cannot fit")
assert.Equal(t, "chairErrs::errTooSmall: cannot fit", errB.Error())
errC := Errorf[errTooSoft]("floof")
assert.Equal(t, "bedErrs::errTooSoft: floof", errC.Error())
papaBearIssues := Wrap[errTooHard](Wrap[errTooBig](Wrap[errTooHot](errors.New("not for me"))))
assert.Equal(t, "bedErrs::errTooHard: chairErrs::errTooBig: porridgeErrs::errTooHot: not for me", papaBearIssues.Error())
assert.ErrorIs(t, papaBearIssues, OfType[errTooHard]())
assert.ErrorIs(t, papaBearIssues, OfType[errTooBig]())
assert.ErrorIs(t, papaBearIssues, OfType[errTooHot]())
assert.ErrorIs(t, papaBearIssues, OfKind[porridgeErrs]())
assert.ErrorIs(t, papaBearIssues, OfKind[chairErrs]())
assert.ErrorIs(t, papaBearIssues, OfKind[bedErrs]())
porridgeErr := AsKind[porridgeErrs]()
assert.ErrorAs(t, papaBearIssues, &porridgeErr)
assert.ErrorIs(t, porridgeErr, OfType[errTooHot]())
}