From 28be1971ea535bda3dbcd9a70462576aac6dac68 Mon Sep 17 00:00:00 2001 From: sergeych Date: Mon, 16 Oct 2023 12:34:49 +0100 Subject: [PATCH] tests for sets and maps --- src/de.rs | 36 +++++++++++++++++++++++++++++++++++- src/ser.rs | 14 ++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/de.rs b/src/de.rs index b9ff00d..a435e16 100644 --- a/src/de.rs +++ b/src/de.rs @@ -1,3 +1,4 @@ +use std::collections::{HashMap, HashSet}; use serde::de::{ self, DeserializeSeed, MapAccess, SeqAccess, Visitor, @@ -236,7 +237,7 @@ fn test_ints() -> Result<()> { } #[test] -fn test_struct_de() -> Result<()> { +fn test_struct() -> Result<()> { #[derive(Serialize, Deserialize, PartialEq, Debug)] struct Test { int: u32, @@ -251,8 +252,41 @@ fn test_struct_de() -> Result<()> { println!("::{}", to_dump(&packed)); let unpacked: Test = from_bytes(packed)?; println!("::{:?}", unpacked); + assert_eq!(&expected, &unpacked); Ok(()) // let j = r#"{"int":1,"seq":["a","b"]}"#; // assert_eq!(expected, from_str(j).unwrap()); +} + +#[test] +fn test_map() -> Result<()> { + + let mut src = HashMap::new(); + src.insert("foo".to_string(), 1); + src.insert("foo".to_string(), 42); + src.insert("bar".to_string(), 1); + src.insert("baz".to_string(), 17); + let packed = to_bytes(&src)?; + println!("{}", to_dump(&packed)); + + let restored: HashMap = from_bytes(packed)?; + println!("{:?}", restored); + assert_eq!(src, restored); + + Ok(()) +} + +#[test] +fn test_set() -> Result<()> { + + let src = HashSet::from(["foo", "bar", "buz"].map(|i| i.to_string())); + let packed = to_bytes(&src)?; + println!("{}", to_dump(&packed)); + + let restored: HashSet = from_bytes(packed)?; + println!("{:?}", restored); + assert_eq!(src, restored); + + Ok(()) } \ No newline at end of file diff --git a/src/ser.rs b/src/ser.rs index c623160..3630c9e 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::string::FromUtf8Error; use serde::{ser, Serialize}; @@ -401,4 +402,17 @@ fn test_enum() -> std::result::Result<(), FromUtf8Error> { // let expected = r#"{"Struct":{"a":1}}"#; // assert_eq!(to_string(&s).unwrap(), expected); Ok(()) +} + +#[test] +fn test_map() -> Result<()> { + + let mut src = HashMap::new(); + src.insert("foo", 1); + src.insert("foo", 42); + src.insert("bar", 1); + src.insert("baz", 17); + let packed = to_bytes(&src)?; + println!("{}", to_dump(&packed)); + Ok(()) } \ No newline at end of file