-
Notifications
You must be signed in to change notification settings - Fork 4
/
errors_test.go
54 lines (43 loc) · 1.38 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
package cloudns
import (
"errors"
"github.com/stretchr/testify/assert"
"testing"
)
func TestConstError_Error(t *testing.T) {
// when
err := constError("Hello World")
// then
assert.Error(t, err, "constError() should return error")
assert.Equal(t, "Hello World", err.Error(), "constError() should be described as `Hello World`")
}
func TestWrapError_Error(t *testing.T) {
// given
innerErr := constError("World")
outerErr := constError("Hello")
// when
wrapErr := outerErr.wrap(innerErr)
// then
assert.Error(t, wrapErr, "wrapErr should contain err")
assert.Equal(t, "Hello: World", wrapErr.Error(), "wrapErr should be described as `Hello: World`")
}
func TestWrapError_Error_InnerNil(t *testing.T) {
// given
var innerErr error = nil
outerErr := constError("Hello")
// when
noWrapErr := outerErr.wrap(innerErr)
// then
assert.Error(t, noWrapErr, "noWrapErr should contain err")
assert.Equal(t, "Hello", noWrapErr.Error(), "noWrapErr should be described as `Hello`")
}
func TestWrapError_Unwrap(t *testing.T) {
// given
innerErr := constError("World")
outerErr := constError("Hello")
wrapErr := outerErr.wrap(innerErr)
// then
assert.Error(t, wrapErr, "wrapErr should contain err")
assert.True(t, errors.Is(wrapErr, outerErr), "errors.Is(wrapErr, outerErr) should return true")
assert.True(t, errors.Is(wrapErr, innerErr), "errors.Is(wrapErr, innerErr) should return true")
}