Add to_dynamic test for Option.

This commit is contained in:
Stephen Chung 2022-01-13 19:02:37 +08:00
parent a3a527923a
commit 5dae2b07f7

View File

@ -751,7 +751,7 @@ fn test_serde_json() -> serde_json::Result<()> {
#[test] #[test]
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
fn test_serde_optional() -> Result<(), Box<EvalAltResult>> { fn test_serde_optional() -> Result<(), Box<EvalAltResult>> {
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct TestStruct { struct TestStruct {
foo: Option<char>, foo: Option<char>,
} }
@ -774,6 +774,24 @@ fn test_serde_optional() -> Result<(), Box<EvalAltResult>> {
assert_eq!(from_dynamic::<TestStruct>(&r)?, TestStruct { foo: None }); assert_eq!(from_dynamic::<TestStruct>(&r)?, TestStruct { foo: None });
let ts = TestStruct { foo: Some('a') };
let r = to_dynamic(&ts)?;
let map = r.cast::<Map>();
assert_eq!(map.len(), 1);
assert_eq!(map.get("foo").unwrap().as_char().unwrap(), 'a');
let ts = TestStruct { foo: None };
let r = to_dynamic(&ts)?;
let map = r.cast::<Map>();
assert_eq!(map.len(), 1);
assert_eq!(map.get("foo").unwrap().as_unit().unwrap(), ());
Ok(()) Ok(())
} }