-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatalog_test.go
127 lines (97 loc) · 2.95 KB
/
catalog_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
package customerror
import (
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewCatalog(t *testing.T) {
type args struct {
name string
}
tests := []struct {
name string
args args
want *Catalog
wantErr bool
}{
{
name: "Should work",
args: args{
name: "myapp",
},
want: &Catalog{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewCatalog(tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("NewCatalog() error = %v, wantErr %v", err, tt.wantErr)
return
}
// Example of adding a new language (pt-BR) to an existing error
// code.
ec, err := got.Set("INVALID_REQUEST_BODY", "invalid request body", WithTranslation("pt-BR", "corpo da solicitação inválido"))
if err != nil {
t.Errorf("NewCatalog() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.NotEmpty(t, ec)
// Example of adding a new language (es-ES) to an existing error
// code.
ec1, err := got.Set("E1010", "invalid response", WithTranslation("es-ES", "resposta inválida"))
if err != nil {
t.Errorf("NewCatalog() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.NotEmpty(t, ec1)
cEInvalidRequestBodyErr, err := got.Get("INVALID_REQUEST_BODY")
if err != nil {
t.Errorf("NewCatalog() error = %v, wantErr %v", err, tt.wantErr)
return
}
// Example of throwing the error in pt-BR, also wrapping another
// error.
cEInvalidRequestBody := cEInvalidRequestBodyErr.New(WithLanguage("pt-BR"), WithError(errors.New("some error")))
if cEInvalidRequestBody.Error() != "corpo da solicitação inválido. Original Error: some error" {
t.Errorf("NewCatalog() error = %v, wantErr %v", err, tt.wantErr)
return
}
cEE1010Err, err := got.Get("E1010")
if err != nil {
t.Errorf("NewCatalog() error = %v, wantErr %v", err, tt.wantErr)
return
}
cEE1010 := cEE1010Err.New(WithLanguage("es-ES"))
if cEE1010.Error() != "resposta inválida" {
t.Errorf("NewCatalog() error = %v, wantErr %v", err, "resposta inválida")
return
}
})
}
}
func TestNewCatalog_2(t *testing.T) {
cE := Factory("insert id", WithTranslation("pt-BR", "id"))
x1 := cE.NewFailedToError(WithLanguage("pt-BR"))
if x1.Error() != "falhou id" {
t.Fatalf("Got %s Expected %s", x1, "falhou id")
}
x2 := cE.NewInvalidError(WithLanguage("pt-BR"))
if x2.Error() != "id é inválido" {
t.Fatalf("Got %s Expected %s", x2, "id é inválido")
}
x3 := cE.NewMissingError(WithLanguage("pt-BR"))
if x3.Error() != "faltando id" {
t.Fatalf("Got %s Expected %s", x3, "faltando id")
}
x4 := cE.NewRequiredError(WithLanguage("pt-BR"))
if x4.Error() != "id necessário" {
t.Fatalf("Got %s Expected %s", x4, "id necessário")
}
x5 := cE.NewHTTPError(http.StatusNoContent, WithLanguage("pt-BR"))
if x5.Error() != "no content" {
t.Fatalf("Got %s Expected %s", x5, "no content")
}
}