-
Notifications
You must be signed in to change notification settings - Fork 0
/
superr_test.go
69 lines (57 loc) · 2.08 KB
/
superr_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
58
59
60
61
62
63
64
65
66
67
68
69
package superr_test
import (
"errors"
"fmt"
"strings"
"testing"
"github.com/goware/superr"
)
var (
ErrFail = errors.New("fail")
ErrAppOpps = errors.New("oops")
ErrDeclined = errors.New("declined")
)
func TestVanilla(t *testing.T) {
err := fmt.Errorf("some error: %w", ErrFail)
assert(t, errors.Is(err, ErrFail), "expecting err is ErrFail")
}
func TestBasic(t *testing.T) {
err := superr.New(ErrFail, ErrAppOpps, ErrDeclined, fmt.Errorf("nooo.."))
assert(t, errors.Is(err, ErrFail), "expecting err is ErrFail")
assert(t, errors.Is(err, ErrAppOpps), "expecting err is ErrAppOpps")
assert(t, errors.Is(err, ErrDeclined), "expecting err is ErrDeclined")
assert(t, strings.Contains(err.Error(), "nooo"), "expecting err string to contain 'nooo'")
}
func TestDisjointed(t *testing.T) {
err1 := superr.New(ErrFail, ErrAppOpps)
err2 := errors.New("something happened")
err3 := fmt.Errorf("auth fail: %w", ErrDeclined)
err := superr.New(err1, err2, err3)
assert(t, errors.Is(err1, ErrFail), "expecting err is ErrFail")
assert(t, errors.Is(err1, ErrAppOpps), "expecting err is ErrAppOpps")
assert(t, errors.Is(err, ErrFail), "expecting err is ErrFail")
assert(t, errors.Is(err, ErrAppOpps), "expecting err is ErrAppOpps")
assert(t, errors.Is(err, ErrDeclined), "expecting err is ErrDeclined")
assert(t, errors.Is(err, err2), "expecting err is err2")
assert(t, strings.Contains(err.Error(), "auth fail"), "expecting err string to contain 'auth fail'")
fmt.Println("==> example string output:", err)
}
func TestGetErrorStack(t *testing.T) {
err1 := superr.New(ErrFail, ErrAppOpps)
err2 := errors.New("something happened")
err3 := fmt.Errorf("auth fail: %w", ErrDeclined)
err := superr.New(err1, err2, err3)
errstack := superr.GetErrorStack(err)
for _, e := range errstack {
fmt.Println("=> e", e)
}
assert(t, errors.Is(errstack[0], ErrFail), "err0")
assert(t, strings.Contains(errstack[1].Error(), "something happened"), "err1")
assert(t, errors.Is(errstack[2], ErrDeclined), "err2")
}
func assert(t *testing.T, cond bool, msg string) {
if !cond {
t.Error(msg)
t.FailNow()
}
}