1package domain
2
3import (
4 "errors"
5 "fmt"
6 "testing"
7)
8
9func TestNoticeCodeString(t *testing.T) {
10 for code := range noticeCodeLimit {
11 if got := code.String(); got == "unknown" {
12 t.Errorf("NoticeCode(%d).String() = unknown, want declared code slug", code)
13 }
14 }
15 if got := NoticeCode(9999).String(); got != "unknown" {
16 t.Errorf("NoticeCode(9999).String() = %q, want unknown", got)
17 }
18}
19
20func TestUserError(t *testing.T) {
21 cause := errors.New("boom")
22 tests := []struct {
23 name string
24 err *UserError
25 wantMsg string
26 wantSev NoticeSeverity
27 wantWrap error
28 }{
29 {"err with cause", UserErr(NoticePaneSpawn, "couldn't open pane", cause), "couldn't open pane: boom", NoticeError, cause},
30 {"err without cause", UserErr(NoticeSessionUnavailable, "session is unavailable", nil), "session is unavailable", NoticeError, nil},
31 {"warn", UserWarn(NoticeResizeFailed, "pane resize failed", cause), "pane resize failed: boom", NoticeWarn, cause},
32 }
33 for _, tt := range tests {
34 t.Run(tt.name, func(t *testing.T) {
35 if got := tt.err.Error(); got != tt.wantMsg {
36 t.Errorf("Error() = %q, want %q", got, tt.wantMsg)
37 }
38 if tt.err.Severity != tt.wantSev {
39 t.Errorf("Severity = %d, want %d", tt.err.Severity, tt.wantSev)
40 }
41 if !errors.Is(tt.err, tt.wantWrap) && tt.wantWrap != nil {
42 t.Errorf("errors.Is(err, cause) = false, want true")
43 }
44 var ue *UserError
45 if !errors.As(fmt.Errorf("wrapped: %w", tt.err), &ue) {
46 t.Errorf("errors.As through wrapping = false, want true")
47 }
48 })
49 }
50}