Update serialization tests

This commit is contained in:
Jonas Platte 2020-05-14 23:12:12 +02:00
parent 0c775de766
commit 9092f7d6d8
No known key found for this signature in database
GPG Key ID: 7D261D771D915378

View File

@ -84,86 +84,65 @@ fn serialize_unit_type() {
assert_eq!(urlencoded::to_string(()), Ok("".to_owned()));
}
#[derive(Serialize)]
struct Wrapper<T> {
item: T,
}
#[derive(Serialize)]
struct NewStruct {
list: Vec<String>,
}
#[derive(Serialize)]
struct Struct {
list: Vec<Option<String>>,
}
#[derive(Serialize)]
struct ListStruct {
list: Vec<NewType<usize>>,
}
#[test]
fn serialize_newstruct() {
let s = NewStruct {
list: vec!["hello".into(), "world".into()],
};
fn serialize_list_of_str() {
let s = &[("list", vec!["hello", "world"])];
assert_eq!(
"list=hello&list=world".to_string(),
urlencoded::to_string(s).unwrap()
urlencoded::to_string(s),
Ok("list=hello&list=world".to_owned())
);
}
#[test]
fn serialize_vec_bool() {
let params = Wrapper {
item: vec![true, false, false],
fn serialize_multiple_lists() {
#[derive(Serialize)]
struct Lists {
xs: Vec<bool>,
ys: Vec<u32>,
}
let params = Lists {
xs: vec![true, false],
ys: vec![3, 2, 1],
};
assert_eq!(
urlencoded::to_string(params).unwrap(),
"item=true&item=false&item=false".to_owned()
urlencoded::to_string(params),
Ok("xs=true&xs=false&ys=3&ys=2&ys=1".to_owned())
);
}
#[test]
fn serialize_vec_num() {
let params = Wrapper {
item: vec![0, 1, 2],
};
let params = [("item", vec![0, 1, 2])];
assert_eq!(
urlencoded::to_string(params).unwrap(),
"item=0&item=1&item=2".to_owned()
urlencoded::to_string(params),
Ok("item=0&item=1&item=2".to_owned())
);
}
#[test]
fn serialize_vec_str() {
let params = Wrapper {
item: vec!["hello", "world", "hello"],
};
let params = &[("item", vec!["hello", "world", "hello"])];
assert_eq!(
urlencoded::to_string(params).unwrap(),
"item=hello&item=world&item=hello".to_owned()
urlencoded::to_string(params),
Ok("item=hello&item=world&item=hello".to_owned())
);
}
#[test]
fn serialize_struct_opt() {
let s = Struct {
list: vec![Some("hello".into()), Some("world".into())],
};
let s = &[("list", vec![Some("hello"), Some("world")])];
assert_eq!(
"list=hello&list=world".to_string(),
urlencoded::to_string(s).unwrap()
urlencoded::to_string(s),
Ok("list=hello&list=world".to_string())
);
}
#[test]
fn serialize_struct_newtype() {
let s = ListStruct {
list: vec![NewType(0), NewType(1)],
};
let s = &[("list", vec![NewType(0), NewType(1)])];
assert_eq!(
"list=0&list=1".to_string(),
urlencoded::to_string(s).unwrap()
@ -172,9 +151,7 @@ fn serialize_struct_newtype() {
#[test]
fn serialize_struct_unit_enum() {
let params = Wrapper {
item: vec![X::A, X::B, X::C],
};
let params = &[("item", vec![X::A, X::B, X::C])];
assert_eq!(
urlencoded::to_string(params),
Ok("item=A&item=B&item=C".to_owned())