-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors_test.go
95 lines (76 loc) · 3.03 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package errors
import (
"fmt"
"testing"
)
var errMessage = "User with mail: %s already exists"
var errUserAlreadyExists = New(errMessage)
var userMail = "[email protected]"
var expectedUserAlreadyExists = "User with mail: [email protected] already exists"
func getNewLine() string {
if NewLine {
return "\n"
}
return ""
}
func ExampleError() {
fmt.Print(errUserAlreadyExists.Format(userMail))
// first output first Output line
fmt.Print(errUserAlreadyExists.Format(userMail).Append("Please change your mail addr"))
// second output second and third Output lines
// Output:
// error: User with mail: [email protected] already exists
// error: User with mail: [email protected] already exists
// Please change your mail addr
}
func do(method string, testErr *Error, expectingMsg string, t *testing.T) {
formattedErr := func() error {
return testErr.Format(userMail)
}()
if formattedErr.Error() != expectingMsg {
t.Fatalf("error %s failed, expected:\n%s got:\n%s", method, expectingMsg, formattedErr.Error())
}
}
func TestFormat(t *testing.T) {
expected := Prefix + expectedUserAlreadyExists + getNewLine()
do("Format Test", errUserAlreadyExists, expected, t)
}
func TestAppendErr(t *testing.T) {
NewLine = true
Prefix = "error: "
errChangeMailMsg := "Please change your mail addr"
errChangeMail := fmt.Errorf(errChangeMailMsg) // test go standard error
expectedErrorMessage := errUserAlreadyExists.Format(userMail).Error() + errChangeMailMsg + getNewLine() // first Prefix and last newline lives inside do
errAppended := errUserAlreadyExists.AppendErr(errChangeMail)
do("Append Test Standard error type", &errAppended, expectedErrorMessage, t)
}
func TestAppendError(t *testing.T) {
NewLine = true
Prefix = "error: "
errChangeMailMsg := "Please change your mail addr"
errChangeMail := New(errChangeMailMsg) // test Error struct
expectedErrorMessage := errUserAlreadyExists.Format(userMail).Error() + errChangeMail.Error() + getNewLine() // first Prefix and last newline lives inside do
errAppended := errUserAlreadyExists.AppendErr(errChangeMail)
do("Append Test Error type", &errAppended, expectedErrorMessage, t)
}
func TestAppend(t *testing.T) {
NewLine = true
Prefix = "error: "
errChangeMailMsg := "Please change your mail addr"
expectedErrorMessage := errUserAlreadyExists.Format(userMail).Error() + errChangeMailMsg + getNewLine() // first Prefix and last newline lives inside do
errAppended := errUserAlreadyExists.Append(errChangeMailMsg)
do("Append Test string Message", &errAppended, expectedErrorMessage, t)
}
func TestNewLine(t *testing.T) {
NewLine = false
errNoNewLine := New(errMessage)
expected := Prefix + expectedUserAlreadyExists
do("NewLine Test", errNoNewLine, expected, t)
NewLine = true
}
func TestPrefix(t *testing.T) {
Prefix = "MyPrefix: "
errUpdatedPrefix := New(errMessage)
expected := Prefix + expectedUserAlreadyExists + "\n"
do("Prefix Test with "+Prefix, errUpdatedPrefix, expected, t)
}