@@ -4,9 +4,12 @@ use gdnative::prelude::*;
4
4
pub ( crate ) fn run_tests ( ) -> bool {
5
5
let mut status = true ;
6
6
7
- //These [de]serialize each field individually , instead of going through ToVariant/FromVariant
7
+ //These [de]serialize Foo via the derived impl , instead of going through ToVariant/FromVariant
8
8
status &= test_ron_round_trip ( ) ;
9
9
status &= test_json_round_trip ( ) ;
10
+ status &= test_cbor_round_trip ( ) ;
11
+ status &= test_yaml_round_trip ( ) ;
12
+ status &= test_toml_round_trip ( ) ;
10
13
11
14
let mut eq_works = true ;
12
15
eq_works &= test_variant_eq ( ) ;
@@ -23,7 +26,13 @@ pub(crate) fn run_tests() -> bool {
23
26
status &= test_ron_de_disp_as_variant ( ) ;
24
27
status &= test_json_disp_round_trip ( ) ;
25
28
status &= test_json_de_disp_as_variant ( ) ;
26
- status &= test_bincode_round_trip ( ) ;
29
+ status &= test_bincode_disp_round_trip ( ) ;
30
+ status &= test_cbor_variant_round_trip ( ) ;
31
+ status &= test_cbor_disp_round_trip ( ) ;
32
+ status &= test_yaml_variant_round_trip ( ) ;
33
+ status &= test_yaml_disp_round_trip ( ) ;
34
+ status &= test_toml_variant_round_trip ( ) ;
35
+ status &= test_toml_disp_round_trip ( ) ;
27
36
28
37
status
29
38
}
@@ -194,9 +203,8 @@ fn test_ron_de_disp_as_variant() -> bool {
194
203
195
204
let ok = std:: panic:: catch_unwind ( || {
196
205
let foo = Foo :: new ( ) ;
197
- let test_str = ron:: to_string ( & foo. to_variant ( ) . dispatch ( ) ) ;
198
- godot_dbg ! ( & test_str) ;
199
- let mut de = ron:: Deserializer :: from_str ( test_str. as_ref ( ) . unwrap ( ) ) ;
206
+ let s = ron:: to_string ( & foo. to_variant ( ) . dispatch ( ) ) ;
207
+ let mut de = ron:: Deserializer :: from_str ( s. as_ref ( ) . unwrap ( ) ) ;
200
208
let variant = Variant :: deserialize ( de. as_mut ( ) . unwrap ( ) ) . unwrap ( ) ;
201
209
let result = Foo :: from_variant ( & variant) . unwrap ( ) ;
202
210
assert_eq ! ( foo, result)
@@ -215,8 +223,8 @@ fn test_json_round_trip() -> bool {
215
223
216
224
let ok = std:: panic:: catch_unwind ( || {
217
225
let foo = Foo :: new ( ) ;
218
- let test_str = serde_json:: to_string ( & foo) ;
219
- let result = serde_json:: from_str :: < Foo > ( test_str . as_ref ( ) . unwrap ( ) ) . unwrap ( ) ;
226
+ let s = serde_json:: to_string ( & foo) ;
227
+ let result = serde_json:: from_str :: < Foo > ( s . as_ref ( ) . unwrap ( ) ) . unwrap ( ) ;
220
228
assert_eq ! ( foo, result)
221
229
} )
222
230
. is_ok ( ) ;
@@ -233,9 +241,8 @@ fn test_json_disp_round_trip() -> bool {
233
241
234
242
let ok = std:: panic:: catch_unwind ( || {
235
243
let foo = Foo :: new ( ) ;
236
- let test_str = serde_json:: to_string ( & foo. to_variant ( ) . dispatch ( ) ) ;
237
- godot_dbg ! ( & test_str) ;
238
- let disp = serde_json:: from_str :: < VariantDispatch > ( test_str. as_ref ( ) . unwrap ( ) ) . unwrap ( ) ;
244
+ let s = serde_json:: to_string ( & foo. to_variant ( ) . dispatch ( ) ) ;
245
+ let disp = serde_json:: from_str :: < VariantDispatch > ( s. as_ref ( ) . unwrap ( ) ) . unwrap ( ) ;
239
246
let result = Foo :: from_variant ( & Variant :: from ( & disp) ) . unwrap ( ) ;
240
247
assert_eq ! ( foo, result)
241
248
} )
@@ -253,8 +260,8 @@ fn test_json_de_disp_as_variant() -> bool {
253
260
254
261
let ok = std:: panic:: catch_unwind ( || {
255
262
let foo = Foo :: new ( ) ;
256
- let test_str = serde_json:: to_string ( & foo. to_variant ( ) . dispatch ( ) ) ;
257
- let variant = serde_json:: from_str :: < Variant > ( test_str . as_ref ( ) . unwrap ( ) ) . unwrap ( ) ;
263
+ let s = serde_json:: to_string ( & foo. to_variant ( ) . dispatch ( ) ) ;
264
+ let variant = serde_json:: from_str :: < Variant > ( s . as_ref ( ) . unwrap ( ) ) . unwrap ( ) ;
258
265
let result = Foo :: from_variant ( & variant) . unwrap ( ) ;
259
266
assert_eq ! ( foo, result)
260
267
} )
@@ -267,21 +274,190 @@ fn test_json_de_disp_as_variant() -> bool {
267
274
ok
268
275
}
269
276
270
- fn test_bincode_round_trip ( ) -> bool {
271
- println ! ( " -- test_bincode_round_trip " ) ;
277
+ fn test_bincode_disp_round_trip ( ) -> bool {
278
+ println ! ( " -- test_bincode_disp_round_trip " ) ;
272
279
273
280
let ok = std:: panic:: catch_unwind ( || {
274
281
let foo = Foo :: new ( ) ;
275
- let test_bytes = bincode:: serialize ( & foo. to_variant ( ) . dispatch ( ) ) ;
276
- let disp = bincode:: deserialize :: < VariantDispatch > ( test_bytes . as_ref ( ) . unwrap ( ) ) . unwrap ( ) ;
282
+ let bytes = bincode:: serialize ( & foo. to_variant ( ) . dispatch ( ) ) ;
283
+ let disp = bincode:: deserialize :: < VariantDispatch > ( bytes . as_ref ( ) . unwrap ( ) ) . unwrap ( ) ;
277
284
let result = Foo :: from_variant ( & Variant :: from ( & disp) ) . unwrap ( ) ;
278
285
assert_eq ! ( foo, result)
279
286
} )
280
287
. is_ok ( ) ;
281
288
282
289
if !ok {
283
- gdnative:: godot_error!( " !! Test test_bincode_round_trip failed" ) ;
290
+ gdnative:: godot_error!( " !! Test test_bincode_disp_round_trip failed" ) ;
291
+ }
292
+
293
+ ok
294
+ }
295
+
296
+ fn test_cbor_round_trip ( ) -> bool {
297
+ println ! ( " -- test_cbor_round_trip" ) ;
298
+
299
+ let ok = std:: panic:: catch_unwind ( || {
300
+ let foo = Foo :: new ( ) ;
301
+ let bytes = serde_cbor:: to_vec ( & foo) . unwrap ( ) ;
302
+ let result = serde_cbor:: from_slice :: < Foo > ( & bytes) . unwrap ( ) ;
303
+ assert_eq ! ( foo, result)
304
+ } )
305
+ . is_ok ( ) ;
306
+
307
+ if !ok {
308
+ gdnative:: godot_error!( " !! Test test_cbor_round_trip failed" ) ;
309
+ }
310
+
311
+ ok
312
+ }
313
+
314
+
315
+ fn test_cbor_variant_round_trip ( ) -> bool {
316
+ println ! ( " -- test_cbor_variant_round_trip" ) ;
317
+
318
+ let ok = std:: panic:: catch_unwind ( || {
319
+ let foo = Foo :: new ( ) ;
320
+ let bytes = serde_cbor:: to_vec ( & foo. to_variant ( ) ) . unwrap ( ) ;
321
+ let disp = serde_cbor:: from_slice :: < Variant > ( & bytes) . unwrap ( ) ;
322
+ let result = Foo :: from_variant ( & disp) . unwrap ( ) ;
323
+ assert_eq ! ( foo, result)
324
+ } )
325
+ . is_ok ( ) ;
326
+
327
+ if !ok {
328
+ gdnative:: godot_error!( " !! Test test_cbor_variant_round_trip failed" ) ;
329
+ }
330
+
331
+ ok
332
+ }
333
+
334
+ fn test_cbor_disp_round_trip ( ) -> bool {
335
+ println ! ( " -- test_cbor_disp_round_trip" ) ;
336
+
337
+ let ok = std:: panic:: catch_unwind ( || {
338
+ let foo = Foo :: new ( ) ;
339
+ let bytes = serde_cbor:: to_vec ( & foo. to_variant ( ) . dispatch ( ) ) . unwrap ( ) ;
340
+ let disp = serde_cbor:: from_slice :: < VariantDispatch > ( & bytes) . unwrap ( ) ;
341
+ let result = Foo :: from_variant ( & Variant :: from ( & disp) ) . unwrap ( ) ;
342
+ assert_eq ! ( foo, result)
343
+ } )
344
+ . is_ok ( ) ;
345
+
346
+ if !ok {
347
+ gdnative:: godot_error!( " !! Test test_cbor_disp_round_trip failed" ) ;
348
+ }
349
+
350
+ ok
351
+ }
352
+
353
+ fn test_yaml_round_trip ( ) -> bool {
354
+ println ! ( " -- test_yaml_round_trip" ) ;
355
+
356
+ let ok = std:: panic:: catch_unwind ( || {
357
+ let foo = Foo :: new ( ) ;
358
+ let s = serde_yaml:: to_string ( & foo) . unwrap ( ) ;
359
+ let result = serde_yaml:: from_str :: < Foo > ( & s) . unwrap ( ) ;
360
+ assert_eq ! ( foo, result)
361
+ } )
362
+ . is_ok ( ) ;
363
+
364
+ if !ok {
365
+ gdnative:: godot_error!( " !! Test test_yaml_round_trip failed" ) ;
366
+ }
367
+
368
+ ok
369
+ }
370
+
371
+ fn test_yaml_variant_round_trip ( ) -> bool {
372
+ println ! ( " -- test_yaml_variant_round_trip" ) ;
373
+
374
+ let ok = std:: panic:: catch_unwind ( || {
375
+ let foo = Foo :: new ( ) ;
376
+ let s = serde_yaml:: to_string ( & foo. to_variant ( ) ) . unwrap ( ) ;
377
+ let variant = serde_yaml:: from_str :: < Variant > ( & s) . unwrap ( ) ;
378
+ let result = Foo :: from_variant ( & variant) . unwrap ( ) ;
379
+ assert_eq ! ( foo, result)
380
+ } )
381
+ . is_ok ( ) ;
382
+
383
+ if !ok {
384
+ gdnative:: godot_error!( " !! Test test_yaml_variant_round_trip failed" ) ;
284
385
}
386
+
387
+ ok
388
+ }
285
389
390
+ fn test_yaml_disp_round_trip ( ) -> bool {
391
+ println ! ( " -- test_yaml_disp_round_trip" ) ;
392
+
393
+ let ok = std:: panic:: catch_unwind ( || {
394
+ let foo = Foo :: new ( ) ;
395
+ let s = serde_yaml:: to_string ( & foo. to_variant ( ) . dispatch ( ) ) . unwrap ( ) ;
396
+ let disp = serde_yaml:: from_str :: < VariantDispatch > ( & s) . unwrap ( ) ;
397
+ let result= Foo :: from_variant ( & Variant :: from ( & disp) ) . unwrap ( ) ;
398
+ assert_eq ! ( foo, result)
399
+ } )
400
+ . is_ok ( ) ;
401
+
402
+ if !ok {
403
+ gdnative:: godot_error!( " !! Test test_yaml_disp_round_trip failed" ) ;
404
+ }
405
+
286
406
ok
287
407
}
408
+
409
+ fn test_toml_round_trip ( ) -> bool {
410
+ println ! ( " -- test_toml_round_trip" ) ;
411
+
412
+ let ok = std:: panic:: catch_unwind ( || {
413
+ let foo = Foo :: new ( ) ;
414
+ let s = toml:: to_string ( & foo) . unwrap ( ) ;
415
+ let result = toml:: from_str :: < Foo > ( & s) . unwrap ( ) ;
416
+ assert_eq ! ( foo, result)
417
+ } )
418
+ . is_ok ( ) ;
419
+
420
+ if !ok {
421
+ gdnative:: godot_error!( " !! Test test_toml_round_trip failed" ) ;
422
+ }
423
+
424
+ ok
425
+ }
426
+
427
+ fn test_toml_variant_round_trip ( ) -> bool {
428
+ println ! ( " -- test_toml_variant_round_trip" ) ;
429
+
430
+ let ok = std:: panic:: catch_unwind ( || {
431
+ let foo = Foo :: new ( ) ;
432
+ let s = toml:: to_string ( & foo. to_variant ( ) ) . unwrap ( ) ;
433
+ let variant = toml:: from_str :: < Variant > ( & s) . unwrap ( ) ;
434
+ let result = Foo :: from_variant ( & variant) . unwrap ( ) ;
435
+ assert_eq ! ( foo, result)
436
+ } )
437
+ . is_ok ( ) ;
438
+
439
+ if !ok {
440
+ gdnative:: godot_error!( " !! Test test_toml_variant_round_trip failed" ) ;
441
+ }
442
+
443
+ ok
444
+ }
445
+
446
+ fn test_toml_disp_round_trip ( ) -> bool {
447
+ println ! ( " -- test_toml_disp_round_trip" ) ;
448
+
449
+ let ok = std:: panic:: catch_unwind ( || {
450
+ let foo = Foo :: new ( ) ;
451
+ let s = toml:: to_string ( & foo. to_variant ( ) . dispatch ( ) ) . unwrap ( ) ;
452
+ let disp = toml:: from_str :: < VariantDispatch > ( & s) . unwrap ( ) ;
453
+ let result= Foo :: from_variant ( & Variant :: from ( & disp) ) . unwrap ( ) ;
454
+ assert_eq ! ( foo, result)
455
+ } )
456
+ . is_ok ( ) ;
457
+
458
+ if !ok {
459
+ gdnative:: godot_error!( " !! Test test_toml_disp_round_trip failed" ) ;
460
+ }
461
+
462
+ ok
463
+ }
0 commit comments