Skip to content

Commit

Permalink
Add initial serde test
Browse files Browse the repository at this point in the history
  • Loading branch information
Waridley committed Jun 9, 2021
1 parent beea410 commit 395a788
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 17 deletions.
6 changes: 4 additions & 2 deletions gdnative-core/Cargo.toml
Expand Up @@ -12,7 +12,7 @@ edition = "2018"

[features]
default = ["nativescript"]
gd_test = []
gd_test = ["serde", "ron", "serde_json", ]
nativescript = ["bitflags", "parking_lot"]
type_tag_fallback = []

Expand All @@ -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" }

Expand Down
7 changes: 0 additions & 7 deletions gdnative-core/src/core_types/node_path.rs
Expand Up @@ -225,13 +225,6 @@ mod serde {
Ok(NodePath::from_str(s))
}

fn visit_string<E>(self, s: String) -> Result<Self::Value, E>
where
E: Error,
{
Ok(NodePath::from_str(&*s))
}

fn visit_newtype_struct<D>(
self,
deserializer: D,
Expand Down
4 changes: 3 additions & 1 deletion gdnative-core/src/core_types/variant.rs
Expand Up @@ -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.

Expand Down
99 changes: 92 additions & 7 deletions gdnative-core/src/core_types/variant/serde.rs
Expand Up @@ -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()))
}
Expand Down Expand Up @@ -496,13 +496,9 @@ fn int_tagged(key: i64, value: Variant) -> Option<Variant> {
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 {
Expand Down Expand Up @@ -603,3 +599,92 @@ fn f32_field(v: &Variant) -> Option<f32> {
.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<Variant>,
none: Option<Variant>,
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))
}
}
1 change: 1 addition & 0 deletions test/src/lib.rs
Expand Up @@ -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();
Expand Down

0 comments on commit 395a788

Please sign in to comment.