better tests organization

This commit is contained in:
Sergey Chernov 2023-10-17 00:38:07 +01:00
parent ffe7da2995
commit 53e3c568a8

113
src/de.rs
View File

@ -1,15 +1,11 @@
use std::collections::{HashMap, HashSet};
use serde::de::{ use serde::de::{
self, DeserializeSeed, MapAccess, SeqAccess, self, DeserializeSeed, MapAccess, SeqAccess,
Visitor, Visitor,
}; };
use serde::{Serialize};
use serde::Deserialize; use serde::Deserialize;
use crate::bipack_source::{BipackSource, VecSource}; use crate::bipack_source::{BipackSource, VecSource};
use crate::error::{Error, Result}; use crate::error::{Error, Result};
use crate::ser::to_bytes;
use crate::tools::to_dump;
pub struct Deserializer { pub struct Deserializer {
// This string starts with the input data and characters are truncated off // This string starts with the input data and characters are truncated off
@ -236,57 +232,66 @@ fn test_ints() -> Result<()> {
Ok(()) Ok(())
} }
#[test] mod tests {
fn test_struct() -> Result<()> { use std::collections::{HashMap, HashSet};
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Test { use serde::{Deserialize, Serialize};
int: u32,
seq: Vec<String>, use crate::de::from_bytes;
use crate::error::Result;
use crate::ser::to_bytes;
use crate::tools::to_dump;
#[test]
fn test_struct() -> Result<()> {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Test {
int: u32,
seq: Vec<String>,
}
let expected = Test {
int: 1,
seq: vec!["a".to_owned(), "b".to_owned()],
};
let packed = to_bytes(&expected)?;
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());
} }
let expected = Test {
int: 1,
seq: vec!["a".to_owned(), "b".to_owned()],
};
let packed = to_bytes(&expected)?; #[test]
println!("::{}", to_dump(&packed)); fn test_map() -> Result<()> {
let unpacked: Test = from_bytes(packed)?; let mut src = HashMap::new();
println!("::{:?}", unpacked); src.insert("foo".to_string(), 1);
assert_eq!(&expected, &unpacked); src.insert("foo".to_string(), 42);
Ok(()) src.insert("bar".to_string(), 1);
src.insert("baz".to_string(), 17);
let packed = to_bytes(&src)?;
println!("{}", to_dump(&packed));
// let j = r#"{"int":1,"seq":["a","b"]}"#; let restored: HashMap<String, i32> = from_bytes(packed)?;
// assert_eq!(expected, from_str(j).unwrap()); println!("{:?}", restored);
} assert_eq!(src, restored);
#[test] Ok(())
fn test_map() -> Result<()> { }
let mut src = HashMap::new(); #[test]
src.insert("foo".to_string(), 1); fn test_set() -> Result<()> {
src.insert("foo".to_string(), 42); let src = HashSet::from(["foo", "bar", "buz"].map(|i| i.to_string()));
src.insert("bar".to_string(), 1); let packed = to_bytes(&src)?;
src.insert("baz".to_string(), 17); println!("{}", to_dump(&packed));
let packed = to_bytes(&src)?;
println!("{}", to_dump(&packed)); let restored: HashSet<String> = from_bytes(packed)?;
println!("{:?}", restored);
let restored: HashMap<String,i32> = from_bytes(packed)?; assert_eq!(src, restored);
println!("{:?}", restored);
assert_eq!(src, restored); Ok(())
}
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(())
} }