Skip to content

Commit df02bbc

Browse files
authored
test: increase test coverage for slackerror (#413)
* test: increase test coverage for slackerror * test: fix naming and ordering of new slackerror tests Rename Test_AppendMessage to Test_Error_AppendMessage to follow the Test_StructName_FunctionName convention for struct methods. Reorder Test_Error_AppendMessage, Test_Is, and Test_IsErrorType to their correct alphabetical positions in the file. * chore: add .worktrees to .gitignore
1 parent 11917c5 commit df02bbc

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ environments.yaml
1919
# macOS
2020
.DS_Store
2121

22-
# For GoLand users
22+
# Git
23+
.worktrees/
24+
25+
# Goland project files
2326
.idea/
2427

2528
# goreleaser

internal/slackerror/error_test.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,86 @@ func Test_Error(t *testing.T) {
265265
}
266266
}
267267

268+
func Test_Error_AppendMessage(t *testing.T) {
269+
tests := map[string]struct {
270+
initialMsg string
271+
msgToAppend string
272+
expected string
273+
}{
274+
"previously empty error message": {
275+
initialMsg: "",
276+
msgToAppend: "hello world",
277+
expected: "hello world",
278+
},
279+
"previously non-empty messages": {
280+
initialMsg: "hello",
281+
msgToAppend: "world",
282+
expected: "hello\nworld",
283+
},
284+
"appending an empty string": {
285+
initialMsg: "hello world",
286+
msgToAppend: "",
287+
expected: "hello world",
288+
},
289+
}
290+
for name, tc := range tests {
291+
t.Run(name, func(t *testing.T) {
292+
err := New("").WithMessage("%s", tc.initialMsg).AppendMessage(tc.msgToAppend)
293+
require.Equal(t, tc.expected, err.Message)
294+
})
295+
}
296+
}
297+
298+
func Test_Is(t *testing.T) {
299+
tests := map[string]struct {
300+
err error
301+
errorCode string
302+
expected bool
303+
}{
304+
"returns true when error contains the code": {
305+
err: New(ErrAccessDenied),
306+
errorCode: ErrAccessDenied,
307+
expected: true,
308+
},
309+
"returns false when error does not contain the code": {
310+
err: New("some other error"),
311+
errorCode: ErrAccessDenied,
312+
expected: false,
313+
},
314+
}
315+
for name, tc := range tests {
316+
t.Run(name, func(t *testing.T) {
317+
result := Is(tc.err, tc.errorCode)
318+
require.Equal(t, tc.expected, result)
319+
})
320+
}
321+
}
322+
323+
func Test_IsErrorType(t *testing.T) {
324+
tests := map[string]struct {
325+
err error
326+
code string
327+
expected bool
328+
}{
329+
"matching error type returns true": {
330+
err: New(ErrAuthToken),
331+
code: ErrAuthToken,
332+
expected: true,
333+
},
334+
"non-matching error type returns false": {
335+
err: errors.New("plain error"),
336+
code: ErrAuthToken,
337+
expected: false,
338+
},
339+
}
340+
for name, tc := range tests {
341+
t.Run(name, func(t *testing.T) {
342+
result := IsErrorType(tc.err, tc.code)
343+
require.Equal(t, tc.expected, result)
344+
})
345+
}
346+
}
347+
268348
func Test_recursiveUnwrap(t *testing.T) {
269349
err1 := Error{Code: "error1"}
270350
err2 := Error{Code: "error2", Cause: &err1}
@@ -495,6 +575,66 @@ func Test_WithRootCause(t *testing.T) {
495575
}
496576
}
497577

578+
func Test_Wrap(t *testing.T) {
579+
tests := map[string]struct {
580+
cause error
581+
message string
582+
}{
583+
"wraps a known error": {
584+
cause: New("inner error"),
585+
message: ErrAccessDenied,
586+
},
587+
"wraps an unknown error": {
588+
cause: errors.New("some standard error"),
589+
message: "outer message",
590+
},
591+
"wraps with nil cause": {
592+
cause: nil,
593+
message: "no cause",
594+
},
595+
}
596+
for name, tc := range tests {
597+
t.Run(name, func(t *testing.T) {
598+
err := Wrap(tc.cause, tc.message)
599+
require.NotNil(t, err)
600+
require.Equal(t, tc.cause, err.Cause)
601+
})
602+
}
603+
}
604+
605+
func Test_Wrapf(t *testing.T) {
606+
tests := map[string]struct {
607+
err error
608+
format string
609+
args []any
610+
expected bool
611+
}{
612+
"wraps non-nil error with formatted message": {
613+
err: errors.New("root cause"),
614+
format: "failed to %s: %d",
615+
args: []any{"deploy", 42},
616+
expected: true,
617+
},
618+
"returns nil for nil error": {
619+
err: nil,
620+
format: "should not appear",
621+
expected: false,
622+
},
623+
}
624+
for name, tc := range tests {
625+
t.Run(name, func(t *testing.T) {
626+
result := Wrapf(tc.err, tc.format, tc.args...)
627+
if tc.expected {
628+
require.NotNil(t, result)
629+
slackErr := result.(*Error)
630+
require.Equal(t, tc.err, slackErr.Cause)
631+
} else {
632+
require.Nil(t, result)
633+
}
634+
})
635+
}
636+
}
637+
498638
func Test_nil_error(t *testing.T) {
499639
var testErr *Error = nil
500640
require.Nil(t, testErr.WithCode("test"))

0 commit comments

Comments
 (0)