-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorWithContext_test.go
More file actions
114 lines (100 loc) · 3.46 KB
/
errorWithContext_test.go
File metadata and controls
114 lines (100 loc) · 3.46 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package errorcontext_test
import (
"context"
"errors"
"fmt"
"testing"
. "github.com/blugnu/test"
"github.com/blugnu/errorcontext"
)
func TestErrorWithContextError(t *testing.T) {
With(t)
type testcase struct {
sut error
result string
}
Run(Testcases(
ForEach(func(tc testcase) {
got := tc.sut.Error()
Expect(got).To(Equal(tc.result))
}),
Cases([]testcase{
{sut: errorcontext.ErrorWithContext{}, result: "unspecified error with context"},
{sut: errorcontext.Wrap(context.TODO(), errors.New("wrapped")), result: "wrapped"},
{sut: errorcontext.Wrap(context.TODO(), fmt.Errorf("%w: %w", errors.New("wrapped"), errors.New("cause"))), result: "wrapped: cause"},
{sut: errorcontext.Wrap(context.TODO(), errors.Join(errors.New("wrapped"), errors.New("cause"))), result: "wrapped\ncause"},
}),
))
}
func TestErrorsIs(t *testing.T) {
With(t)
// arrange
ea := errors.New("a")
eb := errors.New("b")
type testcase struct {
name string
sut error
target error
result bool
}
Run(Testcases(
ForEach(func(tc testcase) {
got := errors.Is(tc.sut, tc.target)
Expect(got).To(Equal(tc.result))
}),
Cases([]testcase{
{name: "ErrorWithContext{}:ErrorWithContext{}", sut: errorcontext.ErrorWithContext{}, target: errorcontext.ErrorWithContext{}, result: true},
{name: "ErrorWithContext{}:<some error>", sut: errorcontext.ErrorWithContext{}, target: errors.New(""), result: false},
{name: "ErrorWithContext{error:a}:ErrorWithContext{error:nil}", sut: errorcontext.Wrap(context.TODO(), ea), target: errorcontext.ErrorWithContext{}, result: true},
{name: "ErrorWithContext{error:a}:ErrorWithContext{error:a}", sut: errorcontext.Wrap(context.TODO(), ea), target: errorcontext.Wrap(context.TODO(), ea), result: true},
{name: "ErrorWithContext{error:a}:ErrorWithContext{error:b}", sut: errorcontext.Wrap(context.TODO(), ea), target: errorcontext.Wrap(context.TODO(), eb), result: false},
{name: "ErrorWithContext{error:a}:error(a)", sut: errorcontext.Wrap(context.TODO(), ea), target: ea, result: true},
{name: "ErrorWithContext{error:a}:error(b)", sut: errorcontext.Wrap(context.TODO(), ea), target: eb, result: false},
}),
))
}
func TestErrorsAs(t *testing.T) {
With(t)
// arrange
ctx := context.Background()
err := errors.New("error")
type testcase struct {
name string
sut error
target any
result bool
}
Run(Testcases(
ForEach(func(tc testcase) {
got := errors.As(tc.sut, &tc.target)
Expect(got).To(Equal(tc.result))
}),
Cases([]testcase{
{name: "ErrorWithContext{}->ErrorWithContext{}", sut: errorcontext.Wrap(ctx, err), target: errorcontext.ErrorWithContext{}, result: true},
{name: "ErrorWithContext{}->&ErrorWithContext{}", sut: errorcontext.Wrap(ctx, err), target: &errorcontext.ErrorWithContext{}, result: true},
}),
))
}
func TestErrorWithContextContext(t *testing.T) {
With(t)
// arrange
type keytype int
const key keytype = 1
ctxa := context.Background()
ctxb := context.WithValue(ctxa, key, "value")
type testcase struct {
name string
sut error
result context.Context
}
Run(Testcases(
ForEach(func(tc testcase) {
got := tc.sut.(errorcontext.ErrorWithContext).Context()
Expect(got).To(Equal(tc.result))
}),
Cases([]testcase{
{name: "wraps error with context", sut: errorcontext.Wrap(ctxa, errorcontext.Wrap(ctxb, errors.New("error"))), result: ctxb},
{name: "does not wrap error with context", sut: errorcontext.Wrap(ctxa, errors.New("error")), result: ctxa},
}),
))
}