diff options
author | Sam Scott <sam.scott89@gmail.com> | 2017-04-25 11:11:39 +0100 |
---|---|---|
committer | Sam Scott <sam.scott89@gmail.com> | 2017-04-25 11:14:43 +0100 |
commit | 7f4764dd6b63c0c0cf0d27268dfa812c0f2334fc (patch) | |
tree | 7196fad8b398c5e235bf149739285c9102d02759 /tests/test_deserialize.rs | |
parent | 53b0c87687fc6dd2f8bca5c4277a41e3e281025d (diff) |
Implement deserializing primitives properly.
This finishes the work to upgrade to serde 1.0.
Thanks to @kardeiz for starting the work in issue #3.
This also clairifies how enums work with serde_qs: only
with adjacently tagged enums for the time being.
Diffstat (limited to 'tests/test_deserialize.rs')
-rw-r--r-- | tests/test_deserialize.rs | 60 |
1 files changed, 56 insertions, 4 deletions
diff --git a/tests/test_deserialize.rs b/tests/test_deserialize.rs index 3632c7d..cd22458 100644 --- a/tests/test_deserialize.rs +++ b/tests/test_deserialize.rs @@ -24,7 +24,7 @@ struct QueryParams { macro_rules! map_test { ($string:expr, $($mapvars:tt)*) => { let expected_map = hash_to_map!(New $($mapvars)*); - let testmap: HashMap<_, _> = qs::from_str($string).unwrap(); + let testmap: HashMap<String, _> = qs::from_str($string).unwrap(); assert_eq!(expected_map, testmap); } } @@ -37,13 +37,13 @@ macro_rules! hash_to_map { //{} // This parses a single map entry, with a value explicitly an expression. ($map:expr, $k:tt[e $v:expr] $($rest:tt)*) => {{ - $map.insert($k.to_owned(), $v.to_owned()); + $map.insert($k.to_string(), $v.to_owned()); hash_to_map!($map, $($rest)*); }}; // This parses a single map entry, plus the rest of the values. ($map:expr, $k:tt[$v:tt] $($rest:tt)*) => {{ - $map.insert($k.to_owned(), $v.to_owned()); + $map.insert($k.to_string(), $v.to_owned()); hash_to_map!($map, $($rest)*); }}; @@ -52,7 +52,7 @@ macro_rules! hash_to_map { ($map:expr, $k:tt[$($inner:tt)*] $($rest:tt)*) => {{ let mut inner_map = HashMap::new(); hash_to_map!(inner_map, $($inner)*); - $map.insert($k.to_owned(), inner_map); + $map.insert($k.to_string(), inner_map); hash_to_map!($map, $($rest)*); }}; @@ -253,3 +253,55 @@ fn optional_struct() { let rec_params: Query = qs::from_str(params).unwrap(); assert_eq!(rec_params, query); } + +#[test] +fn deserialize_enum_untagged() { + #[derive(Deserialize, Debug, PartialEq)] + #[serde(untagged)] + enum E { + B(bool), + S(String), + } + + #[derive(Deserialize, Debug, PartialEq)] + struct Query { + e: E, + } + + let params = "e=true"; + let rec_params: Query = qs::from_str(params).unwrap(); + assert_eq!(rec_params, Query { e: E::S("true".to_string()) }); +} + +#[test] +fn deserialize_enum_adjacently() { + #[derive(Deserialize, Debug, PartialEq)] + #[serde(tag = "type", content = "val")] + enum E { + B(bool), + S(String), + } + + #[derive(Deserialize, Debug, PartialEq)] + #[serde(tag = "type", content = "val")] + enum V { + V1 { x: u8, y: u16 }, + V2(String), + } + + #[derive(Deserialize, Debug, PartialEq)] + struct Query { + e: E, + v: Option<V> + } + + let params = "e[type]=B&e[val]=true&v[type]=V1&v[val][x]=12&v[val][y]=300"; + let rec_params: Query = qs::from_str(params).unwrap(); + assert_eq!(rec_params, + Query { e: E::B(true), v: Some(V::V1 { x: 12, y: 300 }) } + ); + + let params = "e[type]=S&e[val]=other"; + let rec_params: Query = qs::from_str(params).unwrap(); + assert_eq!(rec_params, Query { e: E::S("other".to_string()), v: None }); +} |