tests for sets and maps

This commit is contained in:
Sergey Chernov 2023-10-16 12:34:49 +01:00
parent ecedb96ae5
commit 28be1971ea
2 changed files with 49 additions and 1 deletions

View File

@ -1,3 +1,4 @@
use std::collections::{HashMap, HashSet};
use serde::de::{ use serde::de::{
self, DeserializeSeed, MapAccess, SeqAccess, self, DeserializeSeed, MapAccess, SeqAccess,
Visitor, Visitor,
@ -236,7 +237,7 @@ fn test_ints() -> Result<()> {
} }
#[test] #[test]
fn test_struct_de() -> Result<()> { fn test_struct() -> Result<()> {
#[derive(Serialize, Deserialize, PartialEq, Debug)] #[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Test { struct Test {
int: u32, int: u32,
@ -251,8 +252,41 @@ fn test_struct_de() -> Result<()> {
println!("::{}", to_dump(&packed)); println!("::{}", to_dump(&packed));
let unpacked: Test = from_bytes(packed)?; let unpacked: Test = from_bytes(packed)?;
println!("::{:?}", unpacked); println!("::{:?}", unpacked);
assert_eq!(&expected, &unpacked);
Ok(()) Ok(())
// let j = r#"{"int":1,"seq":["a","b"]}"#; // let j = r#"{"int":1,"seq":["a","b"]}"#;
// assert_eq!(expected, from_str(j).unwrap()); // 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<String,i32> = 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<String> = from_bytes(packed)?;
println!("{:?}", restored);
assert_eq!(src, restored);
Ok(())
} }

View File

@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
use serde::{ser, Serialize}; use serde::{ser, Serialize};
@ -401,4 +402,17 @@ fn test_enum() -> std::result::Result<(), FromUtf8Error> {
// let expected = r#"{"Struct":{"a":1}}"#; // let expected = r#"{"Struct":{"a":1}}"#;
// assert_eq!(to_string(&s).unwrap(), expected); // assert_eq!(to_string(&s).unwrap(), expected);
Ok(()) 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(())
} }