-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode_test.go
57 lines (50 loc) · 1.09 KB
/
code_test.go
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
package exitcode_test
import (
"errors"
"flag"
"fmt"
"testing"
"github.com/carlmjohnson/exitcode"
)
func TestGet(t *testing.T) {
base := exitcode.Set(errors.New(""), 4)
wrapped := fmt.Errorf("wrapping: %w", base)
testCases := map[string]struct {
error
int
}{
"nil": {nil, 0},
"default": {errors.New(""), 1},
"help": {flag.ErrHelp, 0},
"set": {exitcode.Set(errors.New(""), 3), 3},
"wrapped": {wrapped, 4},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
err := tc.error
want := tc.int
got := exitcode.Get(err)
if got != want {
t.Errorf("%v: %d != %d", err, got, want)
}
})
}
}
func TestSet(t *testing.T) {
t.Run("same-message", func(t *testing.T) {
err := errors.New("hello")
coder := exitcode.Set(err, 2)
got := err.Error()
want := coder.Error()
if got != want {
t.Errorf("error message %q != %q", got, want)
}
})
t.Run("keep-chain", func(t *testing.T) {
err := errors.New("hello")
coder := exitcode.Set(err, 3)
if !errors.Is(coder, err) {
t.Errorf("broken chain: %v is not %v", coder, err)
}
})
}