-
Notifications
You must be signed in to change notification settings - Fork 0
/
unknown_test.go
63 lines (48 loc) · 1.3 KB
/
unknown_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
package zerrors
import (
"errors"
"github.com/samber/oops"
"testing"
"github.com/stretchr/testify/assert"
)
func TestToUnknownOnNil(t *testing.T) {
err := ToUnknown(nil, "id")
ok := IsUnknown(err)
assert.False(t, ok)
assert.Equal(t, err, nil)
}
func TestToUnknown(t *testing.T) {
err := ToUnknown(errors.New("not domain"), "id")
assert.True(t, IsUnknown(err))
var e oops.OopsError
ok := errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrUnknown, e.Code())
}
func TestIsUnknownOnNonDomain(t *testing.T) {
err := errors.New("I am found!")
ok := IsUnknown(err)
assert.False(t, ok)
}
func TestThrowUnknown(t *testing.T) {
err := ThrowUnknown("create", "user", "sam")
ok := IsUnknown(err)
assert.True(t, ok)
assert.Equal(t, err.Error(), "cannot create 'sam' of kind 'user'")
}
func TestUnknownf(t *testing.T) {
err := Unknownf("something happened: %s", "reason")
ok := IsUnknown(err)
assert.True(t, ok)
assert.Equal(t, err.Error(), "something happened: reason")
}
func TestToUnknownf(t *testing.T) {
err := ToUnknownf(errors.New("something happened"), "key: %s", "value")
ok := IsUnknown(err)
assert.True(t, ok)
var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrUnknown, e.Code())
assert.Equal(t, err.Error(), "key: value: something happened")
}