-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
133 lines (114 loc) · 3.29 KB
/
main_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package goapperrors
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Init(t *testing.T) {
t.Run("panic on nil input", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
assert.Fail(t, "expect panic")
}
}()
Init(nil)
})
}
func Test_Add(t *testing.T) {
e1 := errors.New("ErrTokenInvalid")
e2 := Add(e1, &ErrorConfig{})
assert.Equal(t, e1, e2)
t.Run("panic on nil input", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
assert.Fail(t, "expect panic")
}
}()
_ = Add(nil, &ErrorConfig{})
})
}
func Test_Create(t *testing.T) {
e := Create("ErrTokenInvalid", &ErrorConfig{})
assert.Equal(t, "ErrTokenInvalid", e.Error())
t.Run("panic on nil input", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
assert.Fail(t, "expect panic")
}
}()
_ = Create("ErrBadProductSKU", nil)
})
}
func Test_Remove(t *testing.T) {
Remove(nil)
e := Create("ErrBadProductSKU", &ErrorConfig{})
assert.NotNil(t, GetErrorConfig(e))
Remove(e)
assert.Nil(t, GetErrorConfig(e))
}
func Test_Build(t *testing.T) {
t.Run("builds direct app errors", func(t *testing.T) {
initConfig(okConfig)
ae1 := New(errTest1)
ae2 := NewMultiError(New(errTest1), New(errTest2)).
WithCustomConfig(&ErrorConfig{
Status: 1234,
Code: "Err1234",
})
res1 := Build(ae1, LanguageDe)
info1 := res1.ErrorInfo
assert.Equal(t, 0, len(res1.TransMissingKeys))
assert.Equal(t, 500, info1.Status)
assert.Equal(t, "ErrTest1", info1.Code)
assert.Equal(t, "(ErrTest1)-in-de", info1.Message)
res2 := Build(ae2, LanguageFr)
info2 := res2.ErrorInfo
assert.Equal(t, 0, len(res2.TransMissingKeys))
assert.Equal(t, "(Err1234)-in-fr", info2.Message)
assert.Equal(t, 1234, info2.Status)
assert.Equal(t, "Err1234", info2.Code)
assert.Equal(t, 2, len(info2.InnerErrors))
})
t.Run("builds indirect app errors", func(t *testing.T) {
initConfig(okConfig)
ae1 := New(errTest1)
ae2 := NewMultiError(New(errTest1), New(errTest2)).
WithCustomConfig(&ErrorConfig{
Status: 1234,
Code: "Err1234",
})
res1 := Build(Wrap(ae1), LanguageDe)
info1 := res1.ErrorInfo
assert.Equal(t, 0, len(res1.TransMissingKeys))
assert.Equal(t, 500, info1.Status)
assert.Equal(t, "ErrTest1", info1.Code)
assert.Equal(t, "(ErrTest1)-in-de", info1.Message)
res2 := Build(fmt.Errorf("%w", ae2), LanguageFr)
info2 := res2.ErrorInfo
assert.Equal(t, 0, len(res2.TransMissingKeys))
assert.Equal(t, 1234, info2.Status)
assert.Equal(t, "Err1234", info2.Code)
assert.Equal(t, "(Err1234)-in-fr", info2.Message)
assert.Equal(t, 2, len(info2.InnerErrors))
})
t.Run("builds non app error", func(t *testing.T) {
initConfig(okConfig)
defer initErrorMapping(errTest2, &ErrorConfig{
Status: 1234,
Code: "Err1234",
})()
res1 := Build(errTest1, LanguageEn)
info1 := res1.ErrorInfo
assert.Equal(t, 0, len(res1.TransMissingKeys))
assert.Equal(t, 500, info1.Status)
assert.Equal(t, "ErrTest1", info1.Code)
assert.Equal(t, "(ErrTest1)-in-en", info1.Message)
res2 := Build(errTest2, LanguageEn)
info2 := res2.ErrorInfo
assert.Equal(t, 0, len(res2.TransMissingKeys))
assert.Equal(t, 1234, info2.Status)
assert.Equal(t, "Err1234", info2.Code)
assert.Equal(t, "(Err1234)-in-en", info2.Message)
})
}