Skip to content

Commit 5dcc36e

Browse files
committed
More tests, several failing
1 parent 292053d commit 5dcc36e

File tree

2 files changed

+197
-18
lines changed

2 files changed

+197
-18
lines changed

test/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ approx = "0.5.0"
1919
ron = "0.6.4"
2020
serde = "1"
2121
serde_json = "1.0.64"
22-
bincode = "1.3.3"
22+
bincode = "1.3.3"
23+
serde_cbor = "0.11.1"
24+
serde_yaml = "0.8.17"
25+
toml = "0.5.8"

test/src/test_serde.rs

Lines changed: 193 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ use gdnative::prelude::*;
44
pub(crate) fn run_tests() -> bool {
55
let mut status = true;
66

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
88
status &= test_ron_round_trip();
99
status &= test_json_round_trip();
10+
status &= test_cbor_round_trip();
11+
status &= test_yaml_round_trip();
12+
status &= test_toml_round_trip();
1013

1114
let mut eq_works = true;
1215
eq_works &= test_variant_eq();
@@ -23,7 +26,13 @@ pub(crate) fn run_tests() -> bool {
2326
status &= test_ron_de_disp_as_variant();
2427
status &= test_json_disp_round_trip();
2528
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();
2736

2837
status
2938
}
@@ -194,9 +203,8 @@ fn test_ron_de_disp_as_variant() -> bool {
194203

195204
let ok = std::panic::catch_unwind(|| {
196205
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());
200208
let variant = Variant::deserialize(de.as_mut().unwrap()).unwrap();
201209
let result = Foo::from_variant(&variant).unwrap();
202210
assert_eq!(foo, result)
@@ -215,8 +223,8 @@ fn test_json_round_trip() -> bool {
215223

216224
let ok = std::panic::catch_unwind(|| {
217225
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();
220228
assert_eq!(foo, result)
221229
})
222230
.is_ok();
@@ -233,9 +241,8 @@ fn test_json_disp_round_trip() -> bool {
233241

234242
let ok = std::panic::catch_unwind(|| {
235243
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();
239246
let result = Foo::from_variant(&Variant::from(&disp)).unwrap();
240247
assert_eq!(foo, result)
241248
})
@@ -253,8 +260,8 @@ fn test_json_de_disp_as_variant() -> bool {
253260

254261
let ok = std::panic::catch_unwind(|| {
255262
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();
258265
let result = Foo::from_variant(&variant).unwrap();
259266
assert_eq!(foo, result)
260267
})
@@ -267,21 +274,190 @@ fn test_json_de_disp_as_variant() -> bool {
267274
ok
268275
}
269276

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");
272279

273280
let ok = std::panic::catch_unwind(|| {
274281
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();
277284
let result = Foo::from_variant(&Variant::from(&disp)).unwrap();
278285
assert_eq!(foo, result)
279286
})
280287
.is_ok();
281288

282289
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");
284385
}
386+
387+
ok
388+
}
285389

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+
286406
ok
287407
}
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

Comments
 (0)