-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoptional_test.go
More file actions
510 lines (452 loc) · 13.8 KB
/
optional_test.go
File metadata and controls
510 lines (452 loc) · 13.8 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package maxigo
import (
"encoding/json"
"strings"
"testing"
)
// helper is a struct used across multiple Optional tests.
type optionalTestStruct struct {
Name Optional[string] `json:"name,omitzero"`
Flag Optional[bool] `json:"flag,omitzero"`
Count Optional[int64] `json:"count,omitzero"`
}
func TestOptionalSome(t *testing.T) {
s := Some("hello")
if !s.Set {
t.Error("Some: Set = false, want true")
}
if s.Value != "hello" {
t.Errorf("Some: Value = %q, want %q", s.Value, "hello")
}
}
func TestOptionalIsZero(t *testing.T) {
var unset Optional[string]
if !unset.IsZero() {
t.Error("unset Optional: IsZero() = false, want true")
}
set := Some("")
if set.IsZero() {
t.Error("set Optional (zero value): IsZero() = true, want false")
}
}
// TestOptionalStringMarshal verifies three states for Optional[string]:
// - unset → field omitted
// - set to "" → field present as ""
// - set to "hello" → field present as "hello"
func TestOptionalStringMarshal(t *testing.T) {
tests := []struct {
name string
input optionalTestStruct
wantJSON string
}{
{
name: "string unset — field omitted",
input: optionalTestStruct{},
wantJSON: `{}`,
},
{
name: "string set to empty — field present as empty string",
input: optionalTestStruct{Name: Some("")},
wantJSON: `{"name":""}`,
},
{
name: "string set to value — field present with value",
input: optionalTestStruct{Name: Some("hello")},
wantJSON: `{"name":"hello"}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := json.Marshal(tt.input)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
if string(got) != tt.wantJSON {
t.Errorf("Marshal =\n %s\nwant\n %s", got, tt.wantJSON)
}
})
}
}
// TestOptionalBoolMarshal verifies three states for Optional[bool]:
// - unset → field omitted
// - set to false → field present as false
// - set to true → field present as true
func TestOptionalBoolMarshal(t *testing.T) {
tests := []struct {
name string
input optionalTestStruct
wantJSON string
}{
{
name: "bool unset — field omitted",
input: optionalTestStruct{},
wantJSON: `{}`,
},
{
name: "bool set to false — field present as false",
input: optionalTestStruct{Flag: Some(false)},
wantJSON: `{"flag":false}`,
},
{
name: "bool set to true — field present as true",
input: optionalTestStruct{Flag: Some(true)},
wantJSON: `{"flag":true}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := json.Marshal(tt.input)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
if string(got) != tt.wantJSON {
t.Errorf("Marshal =\n %s\nwant\n %s", got, tt.wantJSON)
}
})
}
}
// TestOptionalInt64Marshal verifies three states for Optional[int64]:
// - unset → field omitted
// - set to 0 → field present as 0
// - set to 42 → field present as 42
func TestOptionalInt64Marshal(t *testing.T) {
tests := []struct {
name string
input optionalTestStruct
wantJSON string
}{
{
name: "int64 unset — field omitted",
input: optionalTestStruct{},
wantJSON: `{}`,
},
{
name: "int64 set to 0 — field present as 0",
input: optionalTestStruct{Count: Some(int64(0))},
wantJSON: `{"count":0}`,
},
{
name: "int64 set to 42 — field present as 42",
input: optionalTestStruct{Count: Some(int64(42))},
wantJSON: `{"count":42}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := json.Marshal(tt.input)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
if string(got) != tt.wantJSON {
t.Errorf("Marshal =\n %s\nwant\n %s", got, tt.wantJSON)
}
})
}
}
// TestOptionalAllFieldsMarshal verifies that all three fields set together
// produce the correct combined JSON output.
func TestOptionalAllFieldsMarshal(t *testing.T) {
v := optionalTestStruct{
Name: Some("test"),
Flag: Some(true),
Count: Some(int64(99)),
}
got, err := json.Marshal(v)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
want := `{"name":"test","flag":true,"count":99}`
if string(got) != want {
t.Errorf("Marshal =\n %s\nwant\n %s", got, want)
}
}
// TestOptionalUnmarshalPresent verifies that a field present in JSON
// results in Set=true with the correct value.
func TestOptionalUnmarshalPresent(t *testing.T) {
input := `{"name":"world","flag":true,"count":7}`
var v optionalTestStruct
if err := json.Unmarshal([]byte(input), &v); err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
if !v.Name.Set || v.Name.Value != "world" {
t.Errorf("Name = %+v, want {Value:world Set:true}", v.Name)
}
if !v.Flag.Set || v.Flag.Value != true {
t.Errorf("Flag = %+v, want {Value:true Set:true}", v.Flag)
}
if !v.Count.Set || v.Count.Value != 7 {
t.Errorf("Count = %+v, want {Value:7 Set:true}", v.Count)
}
}
// TestOptionalUnmarshalAbsent verifies that a field absent from JSON
// results in Set=false.
func TestOptionalUnmarshalAbsent(t *testing.T) {
input := `{}`
var v optionalTestStruct
if err := json.Unmarshal([]byte(input), &v); err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
if v.Name.Set {
t.Errorf("Name.Set = true, want false (field was absent)")
}
if v.Flag.Set {
t.Errorf("Flag.Set = true, want false (field was absent)")
}
if v.Count.Set {
t.Errorf("Count.Set = true, want false (field was absent)")
}
}
// TestOptionalUnmarshalNull verifies that JSON null is treated as unset.
// This prevents accidental overwrites when round-tripping API responses
// through request types (e.g. null description would not become "").
func TestOptionalUnmarshalNull(t *testing.T) {
input := `{"name":null,"flag":null,"count":null}`
var v optionalTestStruct
if err := json.Unmarshal([]byte(input), &v); err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
// JSON null should leave Optional as unset (Set=false).
if v.Name.Set {
t.Error("Name.Set = true, want false (null should be unset)")
}
if v.Flag.Set {
t.Error("Flag.Set = true, want false (null should be unset)")
}
if v.Count.Set {
t.Error("Count.Set = true, want false (null should be unset)")
}
}
// TestOptionalUnmarshalZeroValues verifies that zero values in JSON
// are correctly unmarshaled as Set=true with the zero value.
func TestOptionalUnmarshalZeroValues(t *testing.T) {
input := `{"name":"","flag":false,"count":0}`
var v optionalTestStruct
if err := json.Unmarshal([]byte(input), &v); err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
if !v.Name.Set || v.Name.Value != "" {
t.Errorf("Name = %+v, want {Value: Set:true}", v.Name)
}
if !v.Flag.Set || v.Flag.Value != false {
t.Errorf("Flag = %+v, want {Value:false Set:true}", v.Flag)
}
if !v.Count.Set || v.Count.Value != 0 {
t.Errorf("Count = %+v, want {Value:0 Set:true}", v.Count)
}
}
// TestOptionalMarshalUnset verifies that MarshalJSON of an unset Optional
// produces null (used when Optional is marshaled directly, not via omitzero).
func TestOptionalMarshalUnset(t *testing.T) {
var o Optional[string]
got, err := json.Marshal(o)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
if string(got) != "null" {
t.Errorf("Marshal unset = %s, want null", got)
}
}
// TestOptionalNewMessageBodyRoundTrip verifies a full marshal/unmarshal cycle
// with the real NewMessageBody type used by the API.
func TestOptionalNewMessageBodyRoundTrip(t *testing.T) {
original := NewMessageBody{
Text: Some("Hello, Max!"),
Notify: Some(false),
Format: Some(FormatMarkdown),
}
data, err := json.Marshal(original)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
// Verify JSON contains expected fields.
jsonStr := string(data)
for _, want := range []string{`"text":"Hello, Max!"`, `"notify":false`, `"format":"markdown"`} {
if !strings.Contains(jsonStr, want) {
t.Errorf("JSON %s does not contain %s", jsonStr, want)
}
}
// Verify unset fields (attachments, link) are omitted.
for _, absent := range []string{`"attachments"`, `"link"`} {
if strings.Contains(jsonStr, absent) {
t.Errorf("JSON %s should not contain %s (unset field)", jsonStr, absent)
}
}
// Unmarshal back and verify equality.
var decoded NewMessageBody
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
if !decoded.Text.Set || decoded.Text.Value != "Hello, Max!" {
t.Errorf("Text = %+v, want {Value:Hello, Max! Set:true}", decoded.Text)
}
if !decoded.Notify.Set || decoded.Notify.Value != false {
t.Errorf("Notify = %+v, want {Value:false Set:true}", decoded.Notify)
}
if !decoded.Format.Set || decoded.Format.Value != FormatMarkdown {
t.Errorf("Format = %+v, want {Value:markdown Set:true}", decoded.Format)
}
}
// TestOptionalNewMessageBodyTextOnly verifies that a NewMessageBody with only
// text set omits all other optional fields.
func TestOptionalNewMessageBodyTextOnly(t *testing.T) {
body := NewMessageBody{
Text: Some("simple text"),
}
data, err := json.Marshal(body)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
want := `{"text":"simple text"}`
if string(data) != want {
t.Errorf("Marshal =\n %s\nwant\n %s", data, want)
}
}
// TestOptionalNewMessageBodyEmpty verifies that a completely empty NewMessageBody
// marshals to just {}.
func TestOptionalNewMessageBodyEmpty(t *testing.T) {
body := NewMessageBody{}
data, err := json.Marshal(body)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
want := `{}`
if string(data) != want {
t.Errorf("Marshal =\n %s\nwant\n %s", data, want)
}
}
// TestOptionalBotPatchMarshal verifies BotPatch marshal with Optional fields.
func TestOptionalBotPatchMarshal(t *testing.T) {
patch := BotPatch{
FirstName: Some("NewBot"),
Description: Some(""),
}
data, err := json.Marshal(patch)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
jsonStr := string(data)
// first_name should be present.
if !strings.Contains(jsonStr, `"first_name":"NewBot"`) {
t.Errorf("JSON %s missing first_name", jsonStr)
}
// description should be present even as empty string.
if !strings.Contains(jsonStr, `"description":""`) {
t.Errorf("JSON %s missing description (empty string)", jsonStr)
}
// name (unset) should be omitted.
if strings.Contains(jsonStr, `"name"`) {
t.Errorf("JSON %s should not contain unset name field", jsonStr)
}
}
// TestOptionalChatPatchMarshal verifies ChatPatch with mixed Optional states.
func TestOptionalChatPatchMarshal(t *testing.T) {
patch := ChatPatch{
Title: Some("New Title"),
Notify: Some(true),
}
data, err := json.Marshal(patch)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
jsonStr := string(data)
if !strings.Contains(jsonStr, `"title":"New Title"`) {
t.Errorf("JSON %s missing title", jsonStr)
}
if !strings.Contains(jsonStr, `"notify":true`) {
t.Errorf("JSON %s missing notify", jsonStr)
}
// pin (unset) should be omitted.
if strings.Contains(jsonStr, `"pin"`) {
t.Errorf("JSON %s should not contain unset pin field", jsonStr)
}
}
// TestOptionalPinMessageBodyMarshal verifies PinMessageBody with OptBool.
func TestOptionalPinMessageBodyMarshal(t *testing.T) {
t.Run("notify unset", func(t *testing.T) {
body := PinMessageBody{MessageID: "msg-123"}
data, err := json.Marshal(body)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
want := `{"message_id":"msg-123"}`
if string(data) != want {
t.Errorf("Marshal = %s, want %s", data, want)
}
})
t.Run("notify false", func(t *testing.T) {
body := PinMessageBody{MessageID: "msg-123", Notify: Some(false)}
data, err := json.Marshal(body)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
jsonStr := string(data)
if !strings.Contains(jsonStr, `"notify":false`) {
t.Errorf("JSON %s missing notify:false", jsonStr)
}
})
}
// TestOptionalCallbackAnswerMarshal verifies CallbackAnswer with OptString.
func TestOptionalCallbackAnswerMarshal(t *testing.T) {
t.Run("notification unset", func(t *testing.T) {
answer := CallbackAnswer{}
data, err := json.Marshal(answer)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
if strings.Contains(string(data), `"notification"`) {
t.Errorf("JSON %s should not contain unset notification", data)
}
})
t.Run("notification set to empty", func(t *testing.T) {
answer := CallbackAnswer{Notification: Some("")}
data, err := json.Marshal(answer)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
if !strings.Contains(string(data), `"notification":""`) {
t.Errorf("JSON %s missing notification empty string", data)
}
})
}
// TestOptionalButtonMarshal verifies Button with multiple Optional fields.
func TestOptionalButtonMarshal(t *testing.T) {
btn := Button{
Type: "chat",
Text: "Create Chat",
ChatDescription: Some("Welcome!"),
// start_payload and uuid unset — should be omitted.
}
data, err := json.Marshal(btn)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
jsonStr := string(data)
if !strings.Contains(jsonStr, `"chat_description":"Welcome!"`) {
t.Errorf("JSON %s missing chat_description", jsonStr)
}
if strings.Contains(jsonStr, `"start_payload"`) {
t.Errorf("JSON %s should not contain unset start_payload", jsonStr)
}
if strings.Contains(jsonStr, `"uuid"`) {
t.Errorf("JSON %s should not contain unset uuid", jsonStr)
}
}
// TestOptionalTypeAliases verifies that OptString, OptBool, OptInt64 aliases
// work identically to Optional[string], Optional[bool], Optional[int64].
func TestOptionalTypeAliases(t *testing.T) {
s := Some("test")
os := OptString(s)
if !os.Set || os.Value != "test" {
t.Errorf("OptString = %+v, want {Value:test Set:true}", os)
}
b := Some(true)
ob := OptBool(b)
if !ob.Set || ob.Value != true {
t.Errorf("OptBool = %+v, want {Value:true Set:true}", ob)
}
i := Some(int64(42))
oi := OptInt64(i)
if !oi.Set || oi.Value != 42 {
t.Errorf("OptInt64 = %+v, want {Value:42 Set:true}", oi)
}
}