MCPcopy
hub / github.com/grafana/dskit / TestMultiError_As

Function TestMultiError_As

multierror/multierror_test.go:110–151  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

108}
109
110func TestMultiError_As(t *testing.T) {
111 tE1 := testError{"error cause 1"}
112 tE2 := testError{"error cause 2"}
113 var target testError
114 testCases := map[string]struct {
115 sourceErrors []error
116 target error
117 as bool
118 }{
119 "MultiError containing only a testError can be cast to that testError": {
120 sourceErrors: []error{tE1},
121 target: tE1,
122 as: true,
123 },
124 "MultiError containing multiple testErrors can be cast to the first testError added": {
125 sourceErrors: []error{tE1, tE2},
126 target: tE1,
127 as: true,
128 },
129 "MultiError containing multiple errors can be cast to the first testError added": {
130 sourceErrors: []error{context.Canceled, tE1, context.DeadlineExceeded, tE2},
131 target: tE1,
132 as: true,
133 },
134 "MultiError non containing a testError cannot be cast to a testError": {
135 sourceErrors: []error{context.Canceled, context.DeadlineExceeded},
136 as: false,
137 },
138 }
139
140 for testName, testCase := range testCases {
141 t.Run(testName, func(t *testing.T) {
142 mErr := New(testCase.sourceErrors...).Err()
143 if testCase.as {
144 require.True(t, errors.As(mErr, &target))
145 require.Equal(t, testCase.target, target)
146 } else {
147 require.False(t, errors.As(mErr, &target))
148 }
149 })
150 }
151}
152
153type testError struct {
154 cause string

Callers

nothing calls this directly

Calls 4

RunMethod · 0.80
NewFunction · 0.70
ErrMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected