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::{
|
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(())
|
||||||
|
}
|
14
src/ser.rs
14
src/ser.rs
@ -1,3 +1,4 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
use std::string::FromUtf8Error;
|
use std::string::FromUtf8Error;
|
||||||
|
|
||||||
use serde::{ser, Serialize};
|
use serde::{ser, Serialize};
|
||||||
@ -402,3 +403,16 @@ fn test_enum() -> std::result::Result<(), FromUtf8Error> {
|
|||||||
// 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(())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user