Fix serde test.

This commit is contained in:
Stephen Chung 2020-10-10 14:15:23 +08:00
parent dd9f58f323
commit c463e42987

View File

@ -1,6 +1,9 @@
#![cfg(feature = "serde")]
use rhai::{de::from_dynamic, ser::to_dynamic, Dynamic, Engine, EvalAltResult, INT};
use rhai::{
serde::{from_dynamic, to_dynamic},
Dynamic, Engine, EvalAltResult, INT,
};
use serde::{Deserialize, Serialize};
#[cfg(not(feature = "no_index"))]
@ -139,12 +142,10 @@ fn test_serde_ser_externally_tagged_enum() -> Result<(), Box<EvalAltResult>> {
assert!(content.is_empty());
}
{
let mut map = to_dynamic(MyEnum::VariantNewtype(123))?.cast::<Map>();
let content = map.remove("VariantNewtype").unwrap();
assert!(map.is_empty());
assert_eq!(Ok(123), content.as_int());
}
#[cfg(not(feature = "no_index"))]
{
@ -156,20 +157,16 @@ fn test_serde_ser_externally_tagged_enum() -> Result<(), Box<EvalAltResult>> {
assert_eq!(Ok(456), content[1].as_int());
}
{
let mut map = to_dynamic(MyEnum::VariantEmptyStruct {})?.cast::<Map>();
let map_inner = map.remove("VariantEmptyStruct").unwrap().cast::<Map>();
assert!(map.is_empty());
assert!(map_inner.is_empty());
}
{
let mut map = to_dynamic(MyEnum::VariantStruct { a: 123 })?.cast::<Map>();
let mut map_inner = map.remove("VariantStruct").unwrap().cast::<Map>();
assert!(map.is_empty());
assert_eq!(Ok(123), map_inner.remove("a").unwrap().as_int());
assert!(map_inner.is_empty());
}
Ok(())
}
@ -217,11 +214,9 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
},
}
{
let mut map = to_dynamic(MyEnum::VariantUnit)?.cast::<Map>();
assert_eq!(Ok("VariantUnit"), map.remove("tag").unwrap().as_str());
assert!(map.is_empty());
}
#[cfg(not(feature = "no_index"))]
{
@ -232,13 +227,11 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
assert!(content.is_empty());
}
{
let mut map = to_dynamic(MyEnum::VariantNewtype(123))?.cast::<Map>();
assert_eq!(Ok("VariantNewtype"), map.remove("tag").unwrap().as_str());
let content = map.remove("content").unwrap();
assert!(map.is_empty());
assert_eq!(Ok(123), content.as_int());
}
#[cfg(not(feature = "no_index"))]
{
@ -251,7 +244,6 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
assert_eq!(Ok(456), content[1].as_int());
}
{
let mut map = to_dynamic(MyEnum::VariantEmptyStruct {})?.cast::<Map>();
assert_eq!(
Ok("VariantEmptyStruct"),
@ -260,16 +252,13 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
let map_inner = map.remove("content").unwrap().cast::<Map>();
assert!(map.is_empty());
assert!(map_inner.is_empty());
}
{
let mut map = to_dynamic(MyEnum::VariantStruct { a: 123 })?.cast::<Map>();
assert_eq!(Ok("VariantStruct"), map.remove("tag").unwrap().as_str());
let mut map_inner = map.remove("content").unwrap().cast::<Map>();
assert!(map.is_empty());
assert_eq!(Ok(123), map_inner.remove("a").unwrap().as_int());
assert!(map_inner.is_empty());
}
Ok(())
}
@ -285,22 +274,16 @@ fn test_serde_ser_untagged_enum() -> Result<(), Box<EvalAltResult>> {
VariantStruct2 { b: i32 },
}
{
let map = to_dynamic(MyEnum::VariantEmptyStruct {})?.cast::<Map>();
assert!(map.is_empty());
}
{
let mut map = to_dynamic(MyEnum::VariantStruct1 { a: 123 })?.cast::<Map>();
assert_eq!(Ok(123), map.remove("a").unwrap().as_int());
assert!(map.is_empty());
}
{
let mut map = to_dynamic(MyEnum::VariantStruct2 { b: 123 })?.cast::<Map>();
assert_eq!(Ok(123), map.remove("b").unwrap().as_int());
assert!(map.is_empty());
}
Ok(())
}
@ -433,15 +416,11 @@ fn test_serde_de_unit_enum() -> Result<(), Box<EvalAltResult>> {
VariantBar,
}
{
let d = Dynamic::from("VariantFoo".to_string());
assert_eq!(MyEnum::VariantFoo, from_dynamic(&d)?);
}
{
let d = Dynamic::from("VariantBar".to_string());
assert_eq!(MyEnum::VariantBar, from_dynamic(&d)?);
}
Ok(())
}
@ -464,10 +443,8 @@ fn test_serde_de_externally_tagged_enum() -> Result<(), Box<EvalAltResult>> {
},
}
{
let d = Dynamic::from("VariantUnit".to_string());
assert_eq!(MyEnum::VariantUnit, from_dynamic(&d).unwrap());
}
#[cfg(not(feature = "no_index"))]
{
@ -480,14 +457,12 @@ fn test_serde_de_externally_tagged_enum() -> Result<(), Box<EvalAltResult>> {
);
}
{
let mut map_outer = Map::new();
map_outer.insert("VariantNewtype".into(), (123 as INT).into());
assert_eq!(
MyEnum::VariantNewtype(123),
from_dynamic(&map_outer.into()).unwrap()
);
}
#[cfg(not(feature = "no_index"))]
{
@ -500,7 +475,6 @@ fn test_serde_de_externally_tagged_enum() -> Result<(), Box<EvalAltResult>> {
);
}
{
let map_inner = Map::new();
let mut map_outer = Map::new();
map_outer.insert("VariantEmptyStruct".into(), map_inner.into());
@ -508,9 +482,7 @@ fn test_serde_de_externally_tagged_enum() -> Result<(), Box<EvalAltResult>> {
MyEnum::VariantEmptyStruct {},
from_dynamic(&map_outer.into()).unwrap()
);
}
{
let mut map_inner = Map::new();
map_inner.insert("a".into(), (123 as INT).into());
let mut map_outer = Map::new();
@ -519,7 +491,6 @@ fn test_serde_de_externally_tagged_enum() -> Result<(), Box<EvalAltResult>> {
MyEnum::VariantStruct { a: 123 },
from_dynamic(&map_outer.into()).unwrap()
);
}
Ok(())
}
@ -534,7 +505,6 @@ fn test_serde_de_internally_tagged_enum() -> Result<(), Box<EvalAltResult>> {
VariantStruct { a: i32 },
}
{
let mut map = Map::new();
map.insert("tag".into(), "VariantStruct".into());
map.insert("a".into(), (123 as INT).into());
@ -542,16 +512,13 @@ fn test_serde_de_internally_tagged_enum() -> Result<(), Box<EvalAltResult>> {
MyEnum::VariantStruct { a: 123 },
from_dynamic(&map.into()).unwrap()
);
}
{
let mut map = Map::new();
map.insert("tag".into(), "VariantEmptyStruct".into());
assert_eq!(
MyEnum::VariantEmptyStruct {},
from_dynamic(&map.into()).unwrap()
);
}
Ok(())
}
@ -574,14 +541,12 @@ fn test_serde_de_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
},
}
{
let mut map_outer = Map::new();
map_outer.insert("tag".into(), "VariantUnit".into());
assert_eq!(
MyEnum::VariantUnit,
from_dynamic(&map_outer.into()).unwrap()
);
}
#[cfg(not(feature = "no_index"))]
{
@ -595,7 +560,6 @@ fn test_serde_de_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
);
}
{
let mut map_outer = Map::new();
map_outer.insert("tag".into(), "VariantNewtype".into());
map_outer.insert("content".into(), (123 as INT).into());
@ -603,7 +567,6 @@ fn test_serde_de_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
MyEnum::VariantNewtype(123),
from_dynamic(&map_outer.into()).unwrap()
);
}
#[cfg(not(feature = "no_index"))]
{
@ -617,7 +580,6 @@ fn test_serde_de_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
);
}
{
let map_inner = Map::new();
let mut map_outer = Map::new();
map_outer.insert("tag".into(), "VariantEmptyStruct".into());
@ -626,9 +588,7 @@ fn test_serde_de_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
MyEnum::VariantEmptyStruct {},
from_dynamic(&map_outer.into()).unwrap()
);
}
{
let mut map_inner = Map::new();
map_inner.insert("a".into(), (123 as INT).into());
let mut map_outer = Map::new();
@ -638,7 +598,6 @@ fn test_serde_de_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
MyEnum::VariantStruct { a: 123 },
from_dynamic(&map_outer.into()).unwrap()
);
}
Ok(())
}
@ -654,31 +613,25 @@ fn test_serde_de_untagged_enum() -> Result<(), Box<EvalAltResult>> {
VariantStruct2 { b: i32 },
}
{
let map = Map::new();
assert_eq!(
MyEnum::VariantEmptyStruct {},
from_dynamic(&map.into()).unwrap()
);
}
{
let mut map = Map::new();
map.insert("a".into(), (123 as INT).into());
assert_eq!(
MyEnum::VariantStruct1 { a: 123 },
from_dynamic(&map.into()).unwrap()
);
}
{
let mut map = Map::new();
map.insert("b".into(), (123 as INT).into());
assert_eq!(
MyEnum::VariantStruct2 { b: 123 },
from_dynamic(&map.into()).unwrap()
);
}
Ok(())
}