-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_multiple_test.go
123 lines (115 loc) · 2.69 KB
/
error_multiple_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
package i18n
import (
"encoding/json"
"testing"
)
func TestI18nMultipleError_EmptyLocale_MarshalJSON(t *testing.T) {
tests := []struct {
name string
setErr func() *I18nMultipleError
want string
wantErr bool
}{
{
name: "empty i18n error",
setErr: func() *I18nMultipleError {
return &I18nMultipleError{}
},
want: `null`,
},
{
name: "filled i18n multiple error",
setErr: func() *I18nMultipleError {
return &I18nMultipleError{
errors: map[string]*baseError{
"field1": {
section: "user_section1",
key: "error_key1",
},
},
}
},
want: `{"field1":{"user_section1":"error_key1"}}`,
},
{
name: "partial fill i18n error",
setErr: func() *I18nMultipleError {
return &I18nMultipleError{
errors: map[string]*baseError{
"field1": {
section: "user_section",
key: "error_key",
},
},
}
},
want: `{"field1":{"user_section":"error_key"}}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := tt.setErr()
got, err := json.Marshal(e) // e.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("I18nMultipleError.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotStr := string(got); gotStr != tt.want {
t.Errorf("I18nMultipleError.MarshalJSON() = %v, want %v", gotStr, tt.want)
}
})
}
}
type TestStructMultiple struct {
Data string `json:"data"`
Errors *I18nMultipleError `json:"errors"`
}
func TestNestedI18nMultipleError_EmptyLocale_MarshalJSON(t *testing.T) {
tests := []struct {
name string
setErr func() *TestStructMultiple
want string
wantErr bool
}{
{
name: "empty nested i18n error",
setErr: func() *TestStructMultiple {
return &TestStructMultiple{
Data: "test_data",
Errors: nil,
}
},
want: `{"data":"test_data","errors":null}`,
},
{
name: "filled i18n error",
setErr: func() *TestStructMultiple {
return &TestStructMultiple{
Data: "test_data",
Errors: &I18nMultipleError{
errors: map[string]*baseError{
"field1": {
section: "user_section1",
key: "error_key1",
},
},
},
}
},
want: `{"data":"test_data","errors":{"field1":{"user_section1":"error_key1"}}}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := tt.setErr()
got, err := json.Marshal(e)
if (err != nil) != tt.wantErr {
t.Errorf("I18nMultipleError.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotStr := string(got); gotStr != tt.want {
t.Errorf("I18nMultipleError.MarshalJSON() = %v, want %v", gotStr, tt.want)
}
})
}
}