-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
89 lines (73 loc) · 1.94 KB
/
errors_test.go
File metadata and controls
89 lines (73 loc) · 1.94 KB
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
package main
import "testing"
func TestErrorTypes(t *testing.T) {
tests := []struct {
name string
err error
want string
}{
{
name: "ErrEmptyTaskName",
err: ErrEmptyTaskName,
want: "task name cannot be empty",
},
{
name: "ErrTaskNameTooLong",
err: ErrTaskNameTooLong,
want: "task name too long",
},
{
name: "ErrInvalidTaskName",
err: ErrInvalidTaskName,
want: "task name contains invalid characters",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.err.Error() != tt.want {
t.Errorf("%s.Error() = %q; want %q", tt.name, tt.err.Error(), tt.want)
}
})
}
}
func TestErrorIsComparable(t *testing.T) {
// Test that errors can be compared
if ErrEmptyTaskName != ErrEmptyTaskName {
t.Error("ErrEmptyTaskName != ErrEmptyTaskName; want equal")
}
if ErrEmptyTaskName == ErrTaskNameTooLong {
t.Error("ErrEmptyTaskName == ErrTaskNameTooLong; want not equal")
}
if ErrTaskNameTooLong != ErrTaskNameTooLong {
t.Error("ErrTaskNameTooLong != ErrTaskNameTooLong; want equal")
}
if ErrInvalidTaskName != ErrInvalidTaskName {
t.Error("ErrInvalidTaskName != ErrInvalidTaskName; want equal")
}
}
func TestErrorWrapping(t *testing.T) {
// Test that errors can be wrapped with fmt.Errorf
err := ErrEmptyTaskName
wrapped := wrapError("test context", err)
if wrapped == nil {
t.Error("wrapError() = nil; want non-nil")
}
// Verify the wrapped error contains the original error message
if !containsString(wrapped.Error(), err.Error()) {
t.Errorf("wrapError() = %q; want error containing %q", wrapped.Error(), err.Error())
}
}
// Helper function to wrap errors (simulating fmt.Errorf usage)
func wrapError(msg string, err error) error {
return &wrappedError{msg: msg, err: err}
}
type wrappedError struct {
msg string
err error
}
func (e *wrappedError) Error() string {
return e.msg + ": " + e.err.Error()
}
func (e *wrappedError) Unwrap() error {
return e.err
}