forked from PearsonEducation/Thalassa-Conduit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
77 lines (63 loc) · 2.74 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
package main
import (
"errors"
"fmt"
"testing"
)
// ----------------------------------------------
// ErrorType.String TESTS
// ----------------------------------------------
func Test_ErrorType_String(t *testing.T) {
assert.Equal(t, ErrConflict.String(), "ErrConflict", "ErrorType.String() returned an unexpected value")
assert.Equal(t, ErrNotFound.String(), "ErrNotFound", "ErrorType.String() returned an unexpected value")
assert.Equal(t, ErrBadData.String(), "ErrBadData", "ErrorType.String() returned an unexpected value")
assert.Equal(t, ErrSync.String(), "ErrSync", "ErrorType.String() returned an unexpected value")
assert.Equal(t, ErrOutOfSync.String(), "ErrOutOfSync", "ErrorType.String() returned an unexpected value")
assert.Equal(t, ErrDB.String(), "ErrDB", "ErrorType.String() returned an unexpected value")
assert.Equal(t, ErrUnknown.String(), "ErrUnknown", "ErrorType.String() returned an unexpected value")
var ErrTest ErrorType = 99
assert.Equal(t, ErrTest.String(), "", "ErrorType.String() returned an unexpected value")
}
// ----------------------------------------------
// NewError TESTS
// ----------------------------------------------
func Test_NewError(t *testing.T) {
err := errors.New("test error")
derr := NewError(ErrUnknown, err)
assert.Equal(t, derr.Type, ErrUnknown, "Error.error returned an unexpected value")
assert.Equal(t, derr.error, err, "Error.error returned an unexpected value")
}
// ----------------------------------------------
// NewErrorf TESTS
// ----------------------------------------------
func Test_NewErrorf(t *testing.T) {
err := errors.New("test error")
derr := NewErrorf(ErrUnknown, "test error")
assert.Equal(t, derr.Type, ErrUnknown, "Error.error returned an unexpected value")
assert.Equal(t, derr.error, err, "Error.error returned an unexpected value")
}
func Test_NewErrorf_WithPattern(t *testing.T) {
pat := "test error: %s"
s := "test"
err := fmt.Errorf(pat, s)
derr := NewErrorf(ErrUnknown, pat, s)
assert.Equal(t, derr.Type, ErrUnknown, "Error.error returned an unexpected value")
assert.Equal(t, derr.error, err, "Error.error returned an unexpected value")
}
// Tests that the NewErrorResponse() function behaves properly.
func Test_NewErrorResponse(t *testing.T) {
c := 500
m := "an error occurred"
expected := &ErrorResponse{Code: c, Message: m}
actual := NewErrorResponse(c, m)
assert.Equal(t, *actual, *expected, "NewErrorResponse() return unexpected value")
}
// Tests that the ErrorResponse.String() function behaves properly.
func Test_ErrorResponse_String(t *testing.T) {
c := 500
m := "an error occurred"
e := NewErrorResponse(c, m)
expected := fmt.Sprintf("[%d] %s", c, m)
actual := e.String()
assert.Equal(t, actual, expected, "ErrorResponse.String() return unexpected value")
}