Skip to content

Commit 395a788

Browse files
committed
Add initial serde test
1 parent beea410 commit 395a788

File tree

5 files changed

+100
-17
lines changed

5 files changed

+100
-17
lines changed

gdnative-core/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ edition = "2018"
1212

1313
[features]
1414
default = ["nativescript"]
15-
gd_test = []
15+
gd_test = ["serde", "ron", "serde_json", ]
1616
nativescript = ["bitflags", "parking_lot"]
1717
type_tag_fallback = []
1818

@@ -24,7 +24,9 @@ glam = "0.16.0"
2424
indexmap = "1.6.0"
2525
ahash = "0.7.0"
2626
once_cell = "1.7.2"
27-
serde = { version = "1", default_features = false, features = ["derive"], optional = true }
27+
serde = { version = "1", features = ["derive"], optional = true }
28+
ron = { version = "0.6.4", optional = true }
29+
serde_json = { version = "1.0.64", optional = true }
2830

2931
gdnative-impl-proc-macros = { path = "../impl/proc_macros", version = "=0.9.3" }
3032

gdnative-core/src/core_types/node_path.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,6 @@ mod serde {
225225
Ok(NodePath::from_str(s))
226226
}
227227

228-
fn visit_string<E>(self, s: String) -> Result<Self::Value, E>
229-
where
230-
E: Error,
231-
{
232-
Ok(NodePath::from_str(&*s))
233-
}
234-
235228
fn visit_newtype_struct<D>(
236229
self,
237230
deserializer: D,

gdnative-core/src/core_types/variant.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use crate::private::{get_api, ManuallyManagedClassPlaceholder};
1010
use crate::thread_access::*;
1111

1212
#[cfg(feature = "serde")]
13-
pub mod serde;
13+
mod serde;
14+
#[cfg(feature = "gd_test")]
15+
pub use self::serde::tests::*;
1416

1517
// TODO: implement Debug, PartialEq, etc.
1618

gdnative-core/src/core_types/variant/serde.rs

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl Serialize for VariantDispatch {
138138
Color(v) => newtype_variant!(VariantType::Color, v),
139139
NodePath(v) => newtype_variant!(VariantType::NodePath, v),
140140
Rid(v) => newtype_variant!(VariantType::Rid, v),
141-
Object(_) => newtype_variant!(VariantType::Object, &Option::<()>::None),
141+
Object(_) => newtype_variant!(VariantType::Object, &Variant::new()),
142142
Dictionary(v) => {
143143
newtype_variant!(VariantType::Dictionary, &DictionaryDispatch(v.new_ref()))
144144
}
@@ -496,13 +496,9 @@ fn int_tagged(key: i64, value: Variant) -> Option<Variant> {
496496
let i = key;
497497
if i == value.get_type() as i64 {
498498
return Some(value);
499-
} else if (i == VariantType::Object as i64)
500-
&& (value.get_type() == ().to_variant().get_type())
501-
{
499+
} else if (i == VariantType::Object as i64) && (value.get_type() == VariantType::Nil) {
502500
return Some(Variant::new());
503-
} else if (i == VariantType::Rid as i64)
504-
&& (value.get_type() == Option::<()>::None.to_variant().get_type())
505-
{
501+
} else if (i == VariantType::Rid as i64) && (value.get_type() == ().to_variant().get_type()) {
506502
return Some(Rid::new().to_variant());
507503
} else if let Some(arr) = value.try_to_array() {
508504
if i == VariantType::ByteArray as i64 {
@@ -603,3 +599,92 @@ fn f32_field(v: &Variant) -> Option<f32> {
603599
.map(|f| f as f32)
604600
.or_else(|| v.try_to_i64().map(|i| i as f32))
605601
}
602+
603+
#[cfg(feature = "gd_test")]
604+
#[doc(hidden)]
605+
pub mod tests {
606+
use super::*;
607+
608+
#[derive(Debug, PartialEq, Serialize, Deserialize)]
609+
struct VariantSerdeTest {
610+
some: Option<Variant>,
611+
none: Option<Variant>,
612+
b: Bool,
613+
int: I64,
614+
float: F64,
615+
str: GodotString,
616+
vec2: Vector2,
617+
// rect2: Rect2, //TODO: PartialEq
618+
vec3: Vector3,
619+
// xform_2d: Transform2D, //TODO: PartialEq
620+
plane: Plane,
621+
quat: Quat,
622+
aabb: Aabb,
623+
basis: Basis,
624+
xform: Transform,
625+
color: Color,
626+
path: NodePath,
627+
rid: Rid,
628+
// obj: Object, //TODO: how best to test this?
629+
// dict: Dictionary, //TODO: PartialEq
630+
// v_arr: VariantArray, //TODO: PartialEq
631+
byte_arr: ByteArray,
632+
int_arr: Int32Array,
633+
float_arr: Float32Array,
634+
str_arr: StringArray,
635+
vec2_arr: Vector2Array,
636+
vec3_arr: Vector3Array,
637+
color_arr: ColorArray,
638+
}
639+
640+
#[cfg(feature = "gd_test")]
641+
impl VariantSerdeTest {
642+
#[doc(hidden)]
643+
fn new() -> Self {
644+
Self {
645+
some: Some(Variant::from_bool(true)),
646+
none: None,
647+
b: false,
648+
int: 1,
649+
float: 2.0,
650+
str: "this is a str".into(),
651+
vec2: Vector2::RIGHT,
652+
vec3: Vector3::BACK,
653+
plane: Plane {
654+
normal: Vector3::ONE.normalized(),
655+
d: 3.0,
656+
},
657+
quat: Quat::new(4.1, 5.2, 6.3, 7.5),
658+
aabb: Aabb {
659+
position: Default::default(),
660+
size: Default::default(),
661+
},
662+
basis: Basis::identity().rotated(Vector3::UP, std::f32::consts::TAU / 3.0),
663+
xform: Transform {
664+
basis: Default::default(),
665+
origin: Default::default(),
666+
},
667+
color: Color::from_rgb(0.549, 0.0, 1.0),
668+
path: "/root/Node".into(),
669+
rid: Rid::new(),
670+
byte_arr: Default::default(),
671+
int_arr: Default::default(),
672+
float_arr: Default::default(),
673+
str_arr: Default::default(),
674+
vec2_arr: Default::default(),
675+
vec3_arr: Default::default(),
676+
color_arr: Default::default(),
677+
}
678+
}
679+
}
680+
681+
pub fn test_ron_round_trip() -> bool {
682+
let test = VariantSerdeTest::new();
683+
let test_str = ron::to_string(&test);
684+
if test_str.is_err() { return false }
685+
let mut de = ron::Deserializer::from_str(test_str.as_ref().unwrap());
686+
if de.is_err() { return false }
687+
VariantSerdeTest::deserialize(de.as_mut().unwrap())
688+
.map_or(false, |test_again| godot_dbg!(test_again == test))
689+
}
690+
}

test/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub extern "C" fn run_tests(
5555
status &= gdnative::core_types::test_vector2_array_debug();
5656
status &= gdnative::core_types::test_vector3_array_access();
5757
status &= gdnative::core_types::test_vector3_array_debug();
58+
status &= gdnative::core_types::test_ron_round_trip();
5859

5960
status &= test_underscore_method_binding();
6061
status &= test_rust_class_construction();

0 commit comments

Comments
 (0)