From 9dd0f1ec0aa796b225acefc6aa03bda0c4616091 Mon Sep 17 00:00:00 2001 From: Mossa Date: Sat, 23 Mar 2024 21:49:59 +0100 Subject: [PATCH] serde: this test fails, so it needs to be fixed somehow --- extendr-api/tests/serde_tests.rs | 86 ++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 extendr-api/tests/serde_tests.rs diff --git a/extendr-api/tests/serde_tests.rs b/extendr-api/tests/serde_tests.rs new file mode 100644 index 0000000000..efd17999ea --- /dev/null +++ b/extendr-api/tests/serde_tests.rs @@ -0,0 +1,86 @@ +#![cfg(feature = "serde")] +use extendr_api::deserializer::from_robj; +use extendr_api::prelude::*; +use extendr_api::serializer::to_robj; +use serde::{Deserialize, Serialize}; + +// Enums are mapped to named lists of lists. +#[derive(Debug, PartialEq, Serialize, Deserialize)] +enum Enum { + Unit, + Newtype(u32), + Tuple(u32, u32), + Struct { a: u32 }, + AnOption(Option), +} + +#[derive(Debug, Serialize, Deserialize, PartialEq)] +struct Test { + int: i32, + // FIXME: something with serialization + // seq: Vec<&'a str>, + option: Option, + option_rint: Option, +} + +/// +#[test] +fn test_back_to_back() -> std::result::Result<(), Box> { + test! { + // region: enum ser-de + + let expected = Enum::Unit; + assert_eq!(expected, from_robj(&to_robj(&expected)?)?); + + let expected = Enum::Newtype(1); + assert_eq!(expected, from_robj(&to_robj(&expected)?)?); + + let expected = Enum::Tuple(1, 2); + assert_eq!(expected, from_robj(&to_robj(&expected)?)?); + + let expected = Enum::Struct { a: 1 }; + assert_eq!(expected, from_robj(&to_robj(&expected)?)?); + + let expected = Enum::AnOption(Some(1)); + assert_eq!(expected, from_robj(&to_robj(&expected)?)?); + + let expected = Enum::AnOption(None); + assert_eq!(expected, from_robj(&to_robj(&expected)?)?); + + // endregion + + // region: struct ser-de + + let test01 = Test { + int: 1, + // seq: vec!["a", "b"], + option: Some(42_i32), + option_rint: Some(Rint::new(21)), + }; + let test02 = Test { + int: 1, + // seq: vec!["a", "b"], + option: None, + option_rint: Some(Rint::na()), + }; + let test03 = Test { + int: 1, + // seq: vec!["a", "b"], + option: None, + option_rint: None, + }; + + let expected = test01; + assert_eq!(expected, from_robj(&to_robj(&expected)?)?); + + let expected = test02; + assert_eq!(expected, from_robj(&to_robj(&expected)?)?); + + let expected = test03; + assert_eq!(expected, from_robj(&to_robj(&expected)?)?); + + // endregion + // Ok(()) + }; + Ok(()) +}