-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper-country_test.go
274 lines (253 loc) · 6.55 KB
/
wrapper-country_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// wrapper-country_test.go
package wrappers
import (
"encoding/json"
"testing"
"github.com/biter777/countries"
)
// TestWrapperCountry_Wrap tests the Wrap method of WrapperCountry.
func TestWrapperCountry_Wrap(t *testing.T) {
tests := []struct {
name string
input any
discard bool
want string
wantError bool
wantDiscard bool
}{
{
name: "Wrap valid CountryCode",
input: countries.DE,
discard: false,
want: "Germany",
wantError: false,
wantDiscard: false,
},
{
name: "Wrap valid Country",
input: countries.ByName("Germany"),
discard: false,
want: "Germany",
wantError: false,
wantDiscard: false,
},
{
name: "Wrap valid Country by string",
input: "Canada",
discard: false,
want: "Canada",
wantError: false,
wantDiscard: false,
},
{
name: "Wrap invalid Country",
input: countries.Unknown,
discard: false,
want: "Unknown",
wantError: true,
wantDiscard: true,
},
{
name: "Wrap invalid Country discard",
input: countries.Unknown,
discard: true,
want: "Unknown",
wantError: false,
wantDiscard: true,
},
{
name: "Wrap invalid string",
input: "invalid_country",
discard: false,
want: "Unknown",
wantError: true,
wantDiscard: true,
},
{
name: "Wrap nil",
input: nil,
discard: false,
want: "Unknown",
wantError: true,
wantDiscard: true,
},
{
name: "Input as WrapperProvider with valid value",
input: func() *WrapperCountry {
w := New[*WrapperCountry]()
w.Wrap(countries.US, false)
return w
}(),
discard: false,
want: "United States",
wantError: false,
wantDiscard: false,
},
{
name: "Input as WrapperProvider with invalid value (discarded)",
input: func() *WrapperCountry {
w := New[*WrapperCountry]()
w.Wrap(countries.Unknown, false)
return w
}(),
discard: false,
want: "Unknown",
wantError: false,
wantDiscard: true,
},
{
name: "Unsupported Type without discard",
input: 12345, // int is unsupported
discard: false,
want: "Unknown",
wantError: true,
wantDiscard: true,
},
{
name: "Unsupported Type with discard",
input: 12345, // int is unsupported
discard: true,
want: "Unknown",
wantError: false, // According to Wrap, it should return ErrorType with discard
wantDiscard: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Initialize the country wrapper
countryWrapper := New[*WrapperCountry]()
countryWrapper.Initialize()
// Wrap the input
err := countryWrapper.Wrap(tt.input, tt.discard)
if (err != nil) != tt.wantError {
t.Fatalf("Wrap() error = %v, wantError %v", err, tt.wantError)
}
// Check if wrapper is discarded as expected
if countryWrapper.IsDiscarded() != tt.wantDiscard {
t.Errorf("IsDiscarded() = %v, want %v", countryWrapper.IsDiscarded(), tt.wantDiscard)
}
// If not discarded, check the unwrapped value
if !countryWrapper.IsDiscarded() && countryWrapper.Unwrap() != tt.want {
t.Errorf("Get() = %v, want %v", countryWrapper.Unwrap(), tt.want)
}
})
}
}
// TestWrapperCountry_JSONMarshal tests JSON marshalling of WrapperCountry.
func TestWrapperCountry_JSONMarshal(t *testing.T) {
tests := []struct {
name string
country string
wantError bool
expectedJSON string
}{
{
name: "Valid Country Marshalling",
country: "DE",
wantError: false,
expectedJSON: `{"country":"Germany"}`,
},
{
name: "Invalid Country Marshalling",
country: "invalid_country",
wantError: true,
expectedJSON: `{"country":null}`,
},
}
// Create a struct for testing
type Data struct {
Country *WrapperCountry `json:"country"`
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Initialize the country wrapper
countryWrapper := New[*WrapperCountry]()
countryWrapper.Initialize()
// Wrap the country
err := countryWrapper.Wrap(tt.country, false)
if (err != nil) != tt.wantError {
t.Fatalf("Wrap() error = %v, wantError %v", err, tt.wantError)
}
// Create the data struct
data := Data{
Country: countryWrapper,
}
// Marshal to JSON
jsonData, err := json.Marshal(&data)
if err != nil {
t.Fatalf("Failed to marshal Data: %v", err)
}
// Compare with expected JSON
if string(jsonData) != tt.expectedJSON {
t.Errorf("Marshalled JSON = %v, want %v", string(jsonData), tt.expectedJSON)
}
})
}
}
// TestWrapperCountry_Unmarshal tests JSON unmarshalling of WrapperCountry.
func TestWrapperCountry_Unmarshal(t *testing.T) {
tests := []struct {
name string
jsonInput string
want string
wantError bool
wantDiscard bool
}{
{
name: "Unmarshal valid CountryCode",
jsonInput: `{"country":"DE"}`,
want: "Germany",
wantError: false,
wantDiscard: false,
},
{
name: "Unmarshal invalid CountryCode",
jsonInput: `{"country":"invalid_country"}`,
want: "",
wantError: true,
wantDiscard: true,
},
{
name: "Unmarshal null CountryCode",
jsonInput: `{"country":null}`,
want: "",
wantError: false,
wantDiscard: true,
},
{
name: "Unmarshal unsupported type",
jsonInput: `{"country":123}`,
want: "",
wantError: true,
wantDiscard: true,
},
}
// Create a struct for testing
type Data struct {
Country *WrapperCountry `json:"country"`
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var data Data
// Initialize the country wrapper
data.Country = New[*WrapperCountry]()
data.Country.Initialize()
// Unmarshal the JSON input
err := json.Unmarshal([]byte(tt.jsonInput), &data)
if (err != nil) != tt.wantError {
t.Fatalf("Unmarshal() error = %v, wantError %v", err, tt.wantError)
}
// Make sure that we handle cases where the country is nil
if data.Country != nil {
// Check if wrapper is discarded as expected
if data.Country.IsDiscarded() != tt.wantDiscard {
t.Errorf("IsDiscarded() = %v, want %v", data.Country.IsDiscarded(), tt.wantDiscard)
}
// If not discarded, check the unwrapped value
if !data.Country.IsDiscarded() && data.Country.Unwrap() != tt.want {
t.Errorf("Get() = %v, want %v", data.Country.Get(), tt.want)
}
}
})
}
}