From a2c0554f3c5556a2ca4f8f004c4fb46929b2a7d3 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 6 Jul 2020 21:52:07 +0800 Subject: [PATCH] Make consistent style. --- tests/serde.rs | 125 +++++++++++++++++-------------------------------- 1 file changed, 42 insertions(+), 83 deletions(-) diff --git a/tests/serde.rs b/tests/serde.rs index 7b0a4c5f..005e4e8c 100644 --- a/tests/serde.rs +++ b/tests/serde.rs @@ -10,57 +10,33 @@ use rhai::Map; #[test] fn test_serde_ser_primary_types() -> Result<(), Box> { - assert_eq!( - to_dynamic(42_u64)?.type_name(), - std::any::type_name::() - ); - assert_eq!(to_dynamic(u64::MAX)?.type_name(), "u64"); - assert_eq!( - to_dynamic(42 as INT)?.type_name(), - std::any::type_name::() - ); - assert_eq!(to_dynamic(true)?.type_name(), "bool"); - assert_eq!(to_dynamic(())?.type_name(), "()"); + assert!(to_dynamic(42_u64)?.is::()); + assert!(to_dynamic(u64::MAX)?.is::()); + assert!(to_dynamic(42 as INT)?.is::()); + assert!(to_dynamic(true)?.is::()); + assert!(to_dynamic(())?.is::<()>()); #[cfg(not(feature = "no_float"))] { - assert_eq!(to_dynamic(123.456_f64)?.type_name(), "f64"); - assert_eq!(to_dynamic(123.456_f32)?.type_name(), "f32"); + assert!(to_dynamic(123.456_f64)?.is::()); + assert!(to_dynamic(123.456_f32)?.is::()); } - assert_eq!(to_dynamic("hello".to_string())?.type_name(), "string"); + assert!(to_dynamic("hello".to_string())?.is::()); Ok(()) } #[test] fn test_serde_ser_integer_types() -> Result<(), Box> { - assert_eq!(to_dynamic(42_i8)?.type_name(), std::any::type_name::()); - assert_eq!( - to_dynamic(42_i16)?.type_name(), - std::any::type_name::() - ); - assert_eq!( - to_dynamic(42_i32)?.type_name(), - std::any::type_name::() - ); - assert_eq!( - to_dynamic(42_i64)?.type_name(), - std::any::type_name::() - ); - assert_eq!(to_dynamic(42_u8)?.type_name(), std::any::type_name::()); - assert_eq!( - to_dynamic(42_u16)?.type_name(), - std::any::type_name::() - ); - assert_eq!( - to_dynamic(42_u32)?.type_name(), - std::any::type_name::() - ); - assert_eq!( - to_dynamic(42_u64)?.type_name(), - std::any::type_name::() - ); + assert!(to_dynamic(42_i8)?.is::()); + assert!(to_dynamic(42_i16)?.is::()); + assert!(to_dynamic(42_i32)?.is::()); + assert!(to_dynamic(42_i64)?.is::()); + assert!(to_dynamic(42_u8)?.is::()); + assert!(to_dynamic(42_u16)?.is::()); + assert!(to_dynamic(42_u32)?.is::()); + assert!(to_dynamic(42_u64)?.is::()); Ok(()) } @@ -72,7 +48,7 @@ fn test_serde_ser_array() -> Result<(), Box> { let d = to_dynamic(arr)?; assert!(d.is::()); - assert_eq!(d.cast::().len(), 4); + assert_eq!(4, d.cast::().len()); Ok(()) } @@ -105,14 +81,14 @@ fn test_serde_ser_struct() -> Result<(), Box> { assert!(d.is::()); let mut map = d.cast::(); - let mut obj = map.remove("obj").unwrap().cast::(); - let mut seq = map.remove("seq").unwrap().cast::(); + let obj = map.remove("obj").unwrap().cast::(); + let seq = map.remove("seq").unwrap().cast::(); - assert_eq!(obj.remove("a").unwrap().cast::(), 123); - assert!(obj.remove("b").unwrap().cast::()); - assert_eq!(map.remove("int").unwrap().cast::(), 42); + assert_eq!(Ok(123), obj["a"].as_int()); + assert!(obj["b"].as_bool().unwrap()); + assert_eq!(Ok(42), map["int"].as_int()); assert_eq!(seq.len(), 3); - assert_eq!(seq.remove(1).cast::(), "kitty"); + assert_eq!(Ok("kitty"), seq[1].as_str()); Ok(()) } @@ -126,10 +102,10 @@ fn test_serde_ser_unit_enum() -> Result<(), Box> { } let d = to_dynamic(MyEnum::VariantFoo)?; - assert_eq!("VariantFoo", d.as_str().unwrap()); + assert_eq!(Ok("VariantFoo"), d.as_str()); let d = to_dynamic(MyEnum::VariantBar)?; - assert_eq!("VariantBar", d.as_str().unwrap()); + assert_eq!(Ok("VariantBar"), d.as_str()); Ok(()) } @@ -152,22 +128,19 @@ fn test_serde_ser_externally_tagged_enum() -> Result<(), Box> { } { - let d = to_dynamic(MyEnum::VariantUnit)?; - assert_eq!("VariantUnit", d.as_str().unwrap()); + assert_eq!(Ok("VariantUnit"), to_dynamic(MyEnum::VariantUnit)?.as_str()); } #[cfg(not(feature = "no_index"))] { - let d = to_dynamic(MyEnum::VariantUnitTuple())?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantUnitTuple())?.cast::(); let content = map.remove("VariantUnitTuple").unwrap().cast::(); assert!(map.is_empty()); assert!(content.is_empty()); } { - let d = to_dynamic(MyEnum::VariantNewtype(123))?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantNewtype(123))?.cast::(); let content = map.remove("VariantNewtype").unwrap(); assert!(map.is_empty()); assert_eq!(Ok(123), content.as_int()); @@ -175,8 +148,7 @@ fn test_serde_ser_externally_tagged_enum() -> Result<(), Box> { #[cfg(not(feature = "no_index"))] { - let d = to_dynamic(MyEnum::VariantTuple(123, 456))?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantTuple(123, 456))?.cast::(); let content = map.remove("VariantTuple").unwrap().cast::(); assert!(map.is_empty()); assert_eq!(2, content.len()); @@ -185,16 +157,14 @@ fn test_serde_ser_externally_tagged_enum() -> Result<(), Box> { } { - let d = to_dynamic(MyEnum::VariantEmptyStruct {})?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantEmptyStruct {})?.cast::(); let map_inner = map.remove("VariantEmptyStruct").unwrap().cast::(); assert!(map.is_empty()); assert!(map_inner.is_empty()); } { - let d = to_dynamic(MyEnum::VariantStruct { a: 123 })?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantStruct { a: 123 })?.cast::(); let mut map_inner = map.remove("VariantStruct").unwrap().cast::(); assert!(map.is_empty()); assert_eq!(Ok(123), map_inner.remove("a").unwrap().as_int()); @@ -214,16 +184,14 @@ fn test_serde_ser_internally_tagged_enum() -> Result<(), Box> { VariantStruct { a: i32 }, } - let d = to_dynamic(MyEnum::VariantEmptyStruct {})?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantEmptyStruct {})?.cast::(); assert_eq!( Ok("VariantEmptyStruct"), map.remove("tag").unwrap().as_str() ); assert!(map.is_empty()); - let d = to_dynamic(MyEnum::VariantStruct { a: 123 })?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantStruct { a: 123 })?.cast::(); assert_eq!(Ok("VariantStruct"), map.remove("tag").unwrap().as_str()); assert_eq!(Ok(123), map.remove("a").unwrap().as_int()); assert!(map.is_empty()); @@ -250,16 +218,14 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box> { } { - let d = to_dynamic(MyEnum::VariantUnit)?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantUnit)?.cast::(); assert_eq!(Ok("VariantUnit"), map.remove("tag").unwrap().as_str()); assert!(map.is_empty()); } #[cfg(not(feature = "no_index"))] { - let d = to_dynamic(MyEnum::VariantUnitTuple())?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantUnitTuple())?.cast::(); assert_eq!(Ok("VariantUnitTuple"), map.remove("tag").unwrap().as_str()); let content = map.remove("content").unwrap().cast::(); assert!(map.is_empty()); @@ -267,8 +233,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box> { } { - let d = to_dynamic(MyEnum::VariantNewtype(123))?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantNewtype(123))?.cast::(); assert_eq!(Ok("VariantNewtype"), map.remove("tag").unwrap().as_str()); let content = map.remove("content").unwrap(); assert!(map.is_empty()); @@ -277,8 +242,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box> { #[cfg(not(feature = "no_index"))] { - let d = to_dynamic(MyEnum::VariantTuple(123, 456))?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantTuple(123, 456))?.cast::(); assert_eq!(Ok("VariantTuple"), map.remove("tag").unwrap().as_str()); let content = map.remove("content").unwrap().cast::(); assert!(map.is_empty()); @@ -288,8 +252,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box> { } { - let d = to_dynamic(MyEnum::VariantEmptyStruct {})?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantEmptyStruct {})?.cast::(); assert_eq!( Ok("VariantEmptyStruct"), map.remove("tag").unwrap().as_str() @@ -300,8 +263,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box> { } { - let d = to_dynamic(MyEnum::VariantStruct { a: 123 })?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantStruct { a: 123 })?.cast::(); assert_eq!(Ok("VariantStruct"), map.remove("tag").unwrap().as_str()); let mut map_inner = map.remove("content").unwrap().cast::(); assert!(map.is_empty()); @@ -324,21 +286,18 @@ fn test_serde_ser_untagged_enum() -> Result<(), Box> { } { - let d = to_dynamic(MyEnum::VariantEmptyStruct {})?; - let map = d.cast::(); + let map = to_dynamic(MyEnum::VariantEmptyStruct {})?.cast::(); assert!(map.is_empty()); } { - let d = to_dynamic(MyEnum::VariantStruct1 { a: 123 })?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantStruct1 { a: 123 })?.cast::(); assert_eq!(Ok(123), map.remove("a").unwrap().as_int()); assert!(map.is_empty()); } { - let d = to_dynamic(MyEnum::VariantStruct2 { b: 123 })?; - let mut map = d.cast::(); + let mut map = to_dynamic(MyEnum::VariantStruct2 { b: 123 })?.cast::(); assert_eq!(Ok(123), map.remove("b").unwrap().as_int()); assert!(map.is_empty()); }