From 395a788992f5a151cd542b39c42c8fa76197a2c1 Mon Sep 17 00:00:00 2001 From: Waridley Date: Tue, 8 Jun 2021 18:08:15 -0500 Subject: [PATCH] Add initial serde test --- gdnative-core/Cargo.toml | 6 +- gdnative-core/src/core_types/node_path.rs | 7 -- gdnative-core/src/core_types/variant.rs | 4 +- gdnative-core/src/core_types/variant/serde.rs | 99 +++++++++++++++++-- test/src/lib.rs | 1 + 5 files changed, 100 insertions(+), 17 deletions(-) diff --git a/gdnative-core/Cargo.toml b/gdnative-core/Cargo.toml index 52194b257..a345afce6 100644 --- a/gdnative-core/Cargo.toml +++ b/gdnative-core/Cargo.toml @@ -12,7 +12,7 @@ edition = "2018" [features] default = ["nativescript"] -gd_test = [] +gd_test = ["serde", "ron", "serde_json", ] nativescript = ["bitflags", "parking_lot"] type_tag_fallback = [] @@ -24,7 +24,9 @@ glam = "0.16.0" indexmap = "1.6.0" ahash = "0.7.0" once_cell = "1.7.2" -serde = { version = "1", default_features = false, features = ["derive"], optional = true } +serde = { version = "1", features = ["derive"], optional = true } +ron = { version = "0.6.4", optional = true } +serde_json = { version = "1.0.64", optional = true } gdnative-impl-proc-macros = { path = "../impl/proc_macros", version = "=0.9.3" } diff --git a/gdnative-core/src/core_types/node_path.rs b/gdnative-core/src/core_types/node_path.rs index 25d86b7f2..72be1c204 100644 --- a/gdnative-core/src/core_types/node_path.rs +++ b/gdnative-core/src/core_types/node_path.rs @@ -225,13 +225,6 @@ mod serde { Ok(NodePath::from_str(s)) } - fn visit_string(self, s: String) -> Result - where - E: Error, - { - Ok(NodePath::from_str(&*s)) - } - fn visit_newtype_struct( self, deserializer: D, diff --git a/gdnative-core/src/core_types/variant.rs b/gdnative-core/src/core_types/variant.rs index 076661d29..c35389e5e 100644 --- a/gdnative-core/src/core_types/variant.rs +++ b/gdnative-core/src/core_types/variant.rs @@ -10,7 +10,9 @@ use crate::private::{get_api, ManuallyManagedClassPlaceholder}; use crate::thread_access::*; #[cfg(feature = "serde")] -pub mod serde; +mod serde; +#[cfg(feature = "gd_test")] +pub use self::serde::tests::*; // TODO: implement Debug, PartialEq, etc. diff --git a/gdnative-core/src/core_types/variant/serde.rs b/gdnative-core/src/core_types/variant/serde.rs index 9133b672f..894fbf5ea 100644 --- a/gdnative-core/src/core_types/variant/serde.rs +++ b/gdnative-core/src/core_types/variant/serde.rs @@ -138,7 +138,7 @@ impl Serialize for VariantDispatch { Color(v) => newtype_variant!(VariantType::Color, v), NodePath(v) => newtype_variant!(VariantType::NodePath, v), Rid(v) => newtype_variant!(VariantType::Rid, v), - Object(_) => newtype_variant!(VariantType::Object, &Option::<()>::None), + Object(_) => newtype_variant!(VariantType::Object, &Variant::new()), Dictionary(v) => { newtype_variant!(VariantType::Dictionary, &DictionaryDispatch(v.new_ref())) } @@ -496,13 +496,9 @@ fn int_tagged(key: i64, value: Variant) -> Option { let i = key; if i == value.get_type() as i64 { return Some(value); - } else if (i == VariantType::Object as i64) - && (value.get_type() == ().to_variant().get_type()) - { + } else if (i == VariantType::Object as i64) && (value.get_type() == VariantType::Nil) { return Some(Variant::new()); - } else if (i == VariantType::Rid as i64) - && (value.get_type() == Option::<()>::None.to_variant().get_type()) - { + } else if (i == VariantType::Rid as i64) && (value.get_type() == ().to_variant().get_type()) { return Some(Rid::new().to_variant()); } else if let Some(arr) = value.try_to_array() { if i == VariantType::ByteArray as i64 { @@ -603,3 +599,92 @@ fn f32_field(v: &Variant) -> Option { .map(|f| f as f32) .or_else(|| v.try_to_i64().map(|i| i as f32)) } + +#[cfg(feature = "gd_test")] +#[doc(hidden)] +pub mod tests { + use super::*; + + #[derive(Debug, PartialEq, Serialize, Deserialize)] + struct VariantSerdeTest { + some: Option, + none: Option, + b: Bool, + int: I64, + float: F64, + str: GodotString, + vec2: Vector2, + // rect2: Rect2, //TODO: PartialEq + vec3: Vector3, + // xform_2d: Transform2D, //TODO: PartialEq + plane: Plane, + quat: Quat, + aabb: Aabb, + basis: Basis, + xform: Transform, + color: Color, + path: NodePath, + rid: Rid, + // obj: Object, //TODO: how best to test this? + // dict: Dictionary, //TODO: PartialEq + // v_arr: VariantArray, //TODO: PartialEq + byte_arr: ByteArray, + int_arr: Int32Array, + float_arr: Float32Array, + str_arr: StringArray, + vec2_arr: Vector2Array, + vec3_arr: Vector3Array, + color_arr: ColorArray, + } + + #[cfg(feature = "gd_test")] + impl VariantSerdeTest { + #[doc(hidden)] + fn new() -> Self { + Self { + some: Some(Variant::from_bool(true)), + none: None, + b: false, + int: 1, + float: 2.0, + str: "this is a str".into(), + vec2: Vector2::RIGHT, + vec3: Vector3::BACK, + plane: Plane { + normal: Vector3::ONE.normalized(), + d: 3.0, + }, + quat: Quat::new(4.1, 5.2, 6.3, 7.5), + aabb: Aabb { + position: Default::default(), + size: Default::default(), + }, + basis: Basis::identity().rotated(Vector3::UP, std::f32::consts::TAU / 3.0), + xform: Transform { + basis: Default::default(), + origin: Default::default(), + }, + color: Color::from_rgb(0.549, 0.0, 1.0), + path: "/root/Node".into(), + rid: Rid::new(), + byte_arr: Default::default(), + int_arr: Default::default(), + float_arr: Default::default(), + str_arr: Default::default(), + vec2_arr: Default::default(), + vec3_arr: Default::default(), + color_arr: Default::default(), + } + } + } + + pub fn test_ron_round_trip() -> bool { + let test = VariantSerdeTest::new(); + let test_str = ron::to_string(&test); + if test_str.is_err() { return false } + let mut de = ron::Deserializer::from_str(test_str.as_ref().unwrap()); + if de.is_err() { return false } + VariantSerdeTest::deserialize(de.as_mut().unwrap()) + .map_or(false, |test_again| godot_dbg!(test_again == test)) + } +} diff --git a/test/src/lib.rs b/test/src/lib.rs index eb6931a05..cb364f00c 100644 --- a/test/src/lib.rs +++ b/test/src/lib.rs @@ -55,6 +55,7 @@ pub extern "C" fn run_tests( status &= gdnative::core_types::test_vector2_array_debug(); status &= gdnative::core_types::test_vector3_array_access(); status &= gdnative::core_types::test_vector3_array_debug(); + status &= gdnative::core_types::test_ron_round_trip(); status &= test_underscore_method_binding(); status &= test_rust_class_construction();