tests for sets and maps
This commit is contained in:
parent
ecedb96ae5
commit
28be1971ea
36
src/de.rs
36
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<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(())
|
||||
}
|
14
src/ser.rs
14
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(())
|
||||
}
|
Loading…
Reference in New Issue
Block a user