-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresult_test.go
More file actions
457 lines (407 loc) · 10.2 KB
/
result_test.go
File metadata and controls
457 lines (407 loc) · 10.2 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
package flyt_test
import (
"encoding/json"
"testing"
"github.com/mark3labs/flyt"
)
func TestResultString(t *testing.T) {
tests := []struct {
name string
result flyt.Result
wantStr string
wantOk bool
wantOr string
orDefault string
}{
{
name: "valid string",
result: flyt.R("hello"),
wantStr: "hello",
wantOk: true,
wantOr: "hello",
orDefault: "default",
},
{
name: "nil value",
result: flyt.R(nil),
wantStr: "",
wantOk: false,
wantOr: "default",
orDefault: "default",
},
{
name: "non-string value",
result: flyt.R(123),
wantStr: "",
wantOk: false,
wantOr: "default",
orDefault: "default",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test AsString
str, ok := tt.result.AsString()
if str != tt.wantStr || ok != tt.wantOk {
t.Errorf("AsString() = (%q, %v), want (%q, %v)", str, ok, tt.wantStr, tt.wantOk)
}
// Test AsStringOr
strOr := tt.result.AsStringOr(tt.orDefault)
if strOr != tt.wantOr {
t.Errorf("AsStringOr(%q) = %q, want %q", tt.orDefault, strOr, tt.wantOr)
}
// Test MustString for valid strings
if tt.wantOk {
mustStr := tt.result.MustString()
if mustStr != tt.wantStr {
t.Errorf("MustString() = %q, want %q", mustStr, tt.wantStr)
}
} else {
// Should panic for non-strings
defer func() {
if r := recover(); r == nil {
t.Error("MustString() should have panicked")
}
}()
tt.result.MustString()
}
})
}
}
func TestResultInt(t *testing.T) {
tests := []struct {
name string
result flyt.Result
wantInt int
wantOk bool
}{
{"int", flyt.R(42), 42, true},
{"int8", flyt.R(int8(42)), 42, true},
{"int16", flyt.R(int16(42)), 42, true},
{"int32", flyt.R(int32(42)), 42, true},
{"int64", flyt.R(int64(42)), 42, true},
{"uint", flyt.R(uint(42)), 42, true},
{"uint8", flyt.R(uint8(42)), 42, true},
{"uint16", flyt.R(uint16(42)), 42, true},
{"uint32", flyt.R(uint32(42)), 42, true},
{"uint64", flyt.R(uint64(42)), 42, true},
{"float32", flyt.R(float32(42.0)), 42, true},
{"float64", flyt.R(float64(42.0)), 42, true},
{"nil", flyt.R(nil), 0, false},
{"string", flyt.R("42"), 0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i, ok := tt.result.AsInt()
if i != tt.wantInt || ok != tt.wantOk {
t.Errorf("AsInt() = (%d, %v), want (%d, %v)", i, ok, tt.wantInt, tt.wantOk)
}
iOr := tt.result.AsIntOr(99)
wantOr := tt.wantInt
if !tt.wantOk {
wantOr = 99
}
if iOr != wantOr {
t.Errorf("AsIntOr(99) = %d, want %d", iOr, wantOr)
}
})
}
}
func TestResultFloat64(t *testing.T) {
tests := []struct {
name string
result flyt.Result
wantVal float64
wantOk bool
}{
{"float64", flyt.R(42.5), 42.5, true},
{"float32", flyt.R(float32(42.5)), 42.5, true},
{"int", flyt.R(42), 42.0, true},
{"uint", flyt.R(uint(42)), 42.0, true},
{"nil", flyt.R(nil), 0.0, false},
{"string", flyt.R("42.5"), 0.0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, ok := tt.result.AsFloat64()
if f != tt.wantVal || ok != tt.wantOk {
t.Errorf("AsFloat64() = (%f, %v), want (%f, %v)", f, ok, tt.wantVal, tt.wantOk)
}
})
}
}
func TestResultBool(t *testing.T) {
tests := []struct {
name string
result flyt.Result
wantVal bool
wantOk bool
}{
{"true", flyt.R(true), true, true},
{"false", flyt.R(false), false, true},
{"nil", flyt.R(nil), false, false},
{"string", flyt.R("true"), false, false},
{"int", flyt.R(1), false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b, ok := tt.result.AsBool()
if b != tt.wantVal || ok != tt.wantOk {
t.Errorf("AsBool() = (%v, %v), want (%v, %v)", b, ok, tt.wantVal, tt.wantOk)
}
})
}
}
func TestResultSlice(t *testing.T) {
tests := []struct {
name string
result flyt.Result
wantLen int
wantOk bool
}{
{"[]any", flyt.R([]any{1, 2, 3}), 3, true},
{"[]string", flyt.R([]string{"a", "b", "c"}), 3, true},
{"[]int", flyt.R([]int{1, 2, 3}), 3, true},
{"nil", flyt.R(nil), 0, false},
{"non-slice", flyt.R("not a slice"), 0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, ok := tt.result.AsSlice()
if ok != tt.wantOk {
t.Errorf("AsSlice() ok = %v, want %v", ok, tt.wantOk)
}
if ok && len(s) != tt.wantLen {
t.Errorf("AsSlice() len = %d, want %d", len(s), tt.wantLen)
}
})
}
}
func TestResultMap(t *testing.T) {
tests := []struct {
name string
result flyt.Result
wantOk bool
}{
{"map[string]any", flyt.R(map[string]any{"key": "value"}), true},
{"nil", flyt.R(nil), false},
{"non-map", flyt.R("not a map"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, ok := tt.result.AsMap()
if ok != tt.wantOk {
t.Errorf("AsMap() ok = %v, want %v", ok, tt.wantOk)
}
})
}
}
func TestResultBind(t *testing.T) {
type User struct {
ID int `json:"id"`
Name string `json:"name"`
}
tests := []struct {
name string
result flyt.Result
wantErr bool
}{
{
name: "valid map",
result: flyt.R(map[string]any{"id": 123, "name": "Alice"}),
wantErr: false,
},
{
name: "valid struct",
result: flyt.R(User{ID: 456, Name: "Bob"}),
wantErr: false,
},
{
name: "nil value",
result: flyt.R(nil),
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var user User
err := tt.result.Bind(&user)
if (err != nil) != tt.wantErr {
t.Errorf("Bind() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr && user.ID == 0 {
t.Error("Bind() didn't populate the struct")
}
})
}
// Test Bind with non-pointer
t.Run("non-pointer dest", func(t *testing.T) {
var user User
result := flyt.R(map[string]any{"id": 123, "name": "Alice"})
err := result.Bind(user) // Pass by value, not pointer
if err == nil {
t.Error("Bind() should error with non-pointer destination")
}
})
}
func TestResultMustBind(t *testing.T) {
type Config struct {
Timeout int `json:"timeout"`
}
// Test successful bind
result := flyt.R(map[string]any{"timeout": 30})
var config Config
result.MustBind(&config)
if config.Timeout != 30 {
t.Errorf("MustBind() didn't populate struct correctly: %+v", config)
}
// Test panic on failure
defer func() {
if r := recover(); r == nil {
t.Error("MustBind() should have panicked with nil value")
}
}()
nilResult := flyt.R(nil)
nilResult.MustBind(&config)
}
func TestResultHelpers(t *testing.T) {
// Test IsNil
nilResult := flyt.R(nil)
if !nilResult.IsNil() {
t.Error("IsNil() should return true for nil value")
}
nonNilResult := flyt.R("value")
if nonNilResult.IsNil() {
t.Error("IsNil() should return false for non-nil value")
}
// Test Type
if nilResult.Type() != "nil" {
t.Errorf("Type() = %q, want 'nil'", nilResult.Type())
}
stringResult := flyt.R("hello")
expectedType := "string"
if stringResult.Type() != expectedType {
t.Errorf("Type() = %q, want %q", stringResult.Type(), expectedType)
}
// Test Value
val := stringResult.Value()
if val != "hello" {
t.Errorf("Value() = %v, want 'hello'", val)
}
}
func TestResultAs(t *testing.T) {
type CustomType struct {
Value string
}
custom := &CustomType{Value: "test"}
result := flyt.R(custom)
// Test successful type assertion
if typed, ok := flyt.As[*CustomType](result); !ok || typed.Value != "test" {
t.Errorf("As[*CustomType]() = (%v, %v), want (%v, true)", typed, ok, custom)
}
// Test failed type assertion
if _, ok := flyt.As[string](result); ok {
t.Error("As[string]() should have failed for *CustomType")
}
// Test with nil
nilResult := flyt.R(nil)
if _, ok := flyt.As[*CustomType](nilResult); ok {
t.Error("As[*CustomType]() should have failed for nil value")
}
}
func TestResultMustAs(t *testing.T) {
result := flyt.R("hello")
// Test successful assertion
str := flyt.MustAs[string](result)
if str != "hello" {
t.Errorf("MustAs[string]() = %q, want 'hello'", str)
}
// Test panic on failure
defer func() {
if r := recover(); r == nil {
t.Error("MustAs[int]() should have panicked")
}
}()
flyt.MustAs[int](result)
}
func TestResultIntegration(t *testing.T) {
// Simulate a complex result from a node
complexData := map[string]any{
"user": map[string]any{
"id": 123,
"name": "Alice",
},
"items": []any{"item1", "item2", "item3"},
"config": map[string]any{
"enabled": true,
"timeout": 30,
},
}
result := flyt.R(complexData)
// Test various accessors
m, ok := result.AsMap()
if !ok {
t.Fatal("AsMap() failed")
}
// Access nested data
if user, ok := m["user"].(map[string]any); ok {
if user["name"] != "Alice" {
t.Errorf("user name = %v, want 'Alice'", user["name"])
}
} else {
t.Error("Failed to access user map")
}
// Bind to struct
type Response struct {
User struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"user"`
Items []string `json:"items"`
Config struct {
Enabled bool `json:"enabled"`
Timeout int `json:"timeout"`
} `json:"config"`
}
var response Response
err := result.Bind(&response)
if err != nil {
t.Fatalf("Bind() failed: %v", err)
}
if response.User.ID != 123 {
t.Errorf("User.ID = %d, want 123", response.User.ID)
}
if len(response.Items) != 3 {
t.Errorf("len(Items) = %d, want 3", len(response.Items))
}
if !response.Config.Enabled {
t.Error("Config.Enabled should be true")
}
}
func TestResultWithJSON(t *testing.T) {
// Test that Result works with JSON marshaling/unmarshaling
original := map[string]any{
"key": "value",
"num": 42,
}
result := flyt.R(original)
// Marshal the underlying value
data, err := json.Marshal(result.Value())
if err != nil {
t.Fatalf("json.Marshal failed: %v", err)
}
// Unmarshal back
var decoded map[string]any
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatalf("json.Unmarshal failed: %v", err)
}
// Create new Result and verify
newResult := flyt.R(decoded)
m, ok := newResult.AsMap()
if !ok {
t.Fatal("AsMap() failed after JSON round-trip")
}
if m["key"] != "value" || m["num"].(float64) != 42 {
t.Errorf("JSON round-trip changed data: %v", m)
}
}