From 0e627a8ac1578c43febe88f8eff5e949fe569296 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 17 May 2020 22:02:32 +0200 Subject: [PATCH] Update url_deserialize tests --- tests/url_deserialize.rs | 105 ++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 58 deletions(-) diff --git a/tests/url_deserialize.rs b/tests/url_deserialize.rs index 2947035b..ee421f25 100644 --- a/tests/url_deserialize.rs +++ b/tests/url_deserialize.rs @@ -83,19 +83,39 @@ fn deserialize_unit_type() { assert_eq!(urlencoded::from_str(""), Ok(())); } +#[derive(Clone, Copy, Debug, PartialEq, Deserialize)] +struct Params<'a> { + a: usize, + b: &'a str, + c: Option, +} + +#[test] +fn deserialize_struct() { + let de = Params { + a: 10, + b: "Hello", + c: None, + }; + assert_eq!(Ok(de), urlencoded::from_str("a=10&b=Hello")); + assert_eq!(Ok(de), urlencoded::from_str("b=Hello&a=10")); +} + #[derive(Debug, Deserialize, PartialEq)] struct Wrapper { item: T, } #[derive(Debug, PartialEq, Deserialize)] -struct NewStruct { - list: Vec, +struct NewStruct<'a> { + #[serde(borrow)] + list: Vec<&'a str>, } #[derive(Debug, PartialEq, Deserialize)] -struct Struct { - list: Vec>, +struct Struct<'a> { + #[serde(borrow)] + list: Vec>, } #[derive(Debug, PartialEq, Deserialize)] @@ -108,36 +128,13 @@ struct ListStruct { list: Vec>, } -#[derive(Debug, PartialEq, Deserialize)] -struct MapStruct { - a: usize, - b: String, - c: Option, -} - -#[test] -fn deserialize_mapstruct() { - let de = MapStruct { - a: 10, - b: "Hello".into(), - c: None, - }; - assert_eq!( - de, - urlencoded::from_str::("a=10&b=Hello").unwrap() - ); -} - #[test] #[ignore] fn deserialize_newstruct() { let de = NewStruct { - list: vec!["hello".into(), "world".into()], + list: vec!["hello", "world"], }; - assert_eq!( - de, - urlencoded::from_str::("list=hello&list=world").unwrap() - ); + assert_eq!(urlencoded::from_str("list=hello&list=world"), Ok(de)); } #[test] @@ -146,21 +143,17 @@ fn deserialize_numlist() { let de = NumList { list: vec![1, 2, 3, 4], }; - assert_eq!( - de, - urlencoded::from_str::("list=1&list=2&list=3&list=4").unwrap() - ); + assert_eq!(urlencoded::from_str("list=1&list=2&list=3&list=4"), Ok(de)); } #[test] #[ignore] fn deserialize_vec_bool() { assert_eq!( - Wrapper { + urlencoded::from_str("item=true&item=false&item=false"), + Ok(Wrapper { item: vec![true, false, false] - }, - urlencoded::from_str::>("item=true&item=false&item=false") - .unwrap() + }) ); } @@ -168,15 +161,14 @@ fn deserialize_vec_bool() { #[ignore] fn deserialize_vec_string() { assert_eq!( - Wrapper { + urlencoded::from_str("item=hello&item=matrix&item=hello"), + Ok(Wrapper { item: vec![ "hello".to_string(), "matrix".to_string(), "hello".to_string() ], - }, - urlencoded::from_str::>("item=hello&item=matrix&item=hello") - .unwrap() + }) ); } @@ -196,10 +188,10 @@ struct Nested { } #[derive(Debug, Deserialize, PartialEq)] -struct Inner { - c: String, +struct Inner<'a> { + c: &'a str, a: usize, - b: String, + b: &'a str, } #[derive(Debug, Deserialize, PartialEq)] @@ -214,19 +206,18 @@ fn deserialize_nested() { let nested = Nested { item: Inner { - c: "hello".into(), + c: "hello", a: 10, - b: "bye".into(), + b: "bye", }, }; assert_eq!( - nested, - urlencoded::from_str::>( + urlencoded::from_str( &encoder .append_pair("item", r#"{"c":"hello","a":10,"b":"bye"}"#) .finish(), - ) - .unwrap() + ), + Ok(nested) ); } @@ -242,11 +233,10 @@ fn deserialize_nested_list() { }; assert_eq!( - nested, - urlencoded::from_str::>>( + urlencoded::from_str( &encoder.append_pair("item", r#"{"list":[1,2,3]}"#).finish(), - ) - .unwrap() + ), + Ok(nested) ); } @@ -261,12 +251,11 @@ fn deserialize_nested_list_option() { }, }; assert_eq!( - nested, - urlencoded::from_str::>>>( + urlencoded::from_str( &encoder .append_pair("item", r#"{"list":[1,2,null]}"#) .finish(), - ) - .unwrap() + ), + Ok(nested) ); }