-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators_test.go
100 lines (80 loc) · 2.03 KB
/
operators_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
package errors
import (
"errors"
"testing"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
)
func TestIs(t *testing.T) {
t.Run("is exactly the same error code", func(t *testing.T) {
var derr = New(testCode(true))
assert.True(t, Is(derr, testCode(true)))
})
t.Run("is a similar error code", func(t *testing.T) {
var derr = New(testCode(true))
assert.True(t, Is(derr, similarTestCode(false)))
})
t.Run("is a different error code", func(t *testing.T) {
var derr = New(testCode(true))
assert.False(t, Is(derr, differentTestCode(true)))
})
t.Run("external error", func(t *testing.T) {
var err = errors.New("some error")
assert.False(t, Is(err, testCode(true)))
})
}
func TestGetCode(t *testing.T) {
t.Run("created by this package", func(t *testing.T) {
var (
expc = testCode(true)
err = New(expc)
c, ok = GetCode(err)
)
assert.Equal(t, expc, c)
assert.True(t, ok)
})
t.Run("created by other package", func(t *testing.T) {
var (
err = errors.New("some error")
_, ok = GetCode(err)
)
assert.False(t, ok)
})
}
func TestGetID(t *testing.T) {
t.Run("created by this package", func(t *testing.T) {
var (
err = New(testCode(true))
id, ok = GetID(err)
)
assert.NotEqual(t, uuid.UUID{}, id)
assert.True(t, ok)
})
t.Run("created by other package", func(t *testing.T) {
var (
err = errors.New("some error")
_, ok = GetID(err)
)
assert.False(t, ok)
})
}
// similarTestCode is a silly example of a Code implementation with the only
// purpose of testing the Is method
type similarTestCode bool
func (similarTestCode) String() string {
var tc = testCode(true)
return tc.String()
}
func (similarTestCode) Message() string {
var tc = testCode(true)
return tc.Message()
}
// differentTestCode is a silly example of a Code implementation with the only
// purpose of testing the Is method
type differentTestCode bool
func (differentTestCode) String() string {
return "DifferentError"
}
func (differentTestCode) Message() string {
return "This is a different error"
}