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::{
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<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 serde::{ser, Serialize};
@ -402,3 +403,16 @@ fn test_enum() -> std::result::Result<(), FromUtf8Error> {
// 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(())
}