From 474fed4dfeb4f6d80df0ea5a45798feb695eaa63 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 6 Jan 2019 08:12:51 -0800 Subject: [PATCH] more tests for de --- router/src/de.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++- router/src/path.rs | 6 ++-- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/router/src/de.rs b/router/src/de.rs index 02e6ca16..68562909 100644 --- a/router/src/de.rs +++ b/router/src/de.rs @@ -487,12 +487,27 @@ mod tests { id: String, } - #[derive(Deserialize)] + #[derive(Debug, Deserialize)] + struct Test1(String, u32); + + #[derive(Debug, Deserialize)] struct Test2 { key: String, value: u32, } + #[derive(Debug, Deserialize, PartialEq)] + #[serde(rename_all = "lowercase")] + enum TestEnum { + Val1, + Val2, + } + + #[derive(Debug, Deserialize)] + struct Test3 { + val: TestEnum, + } + #[test] fn test_request_extract() { let mut router = Router::<()>::build(); @@ -518,6 +533,10 @@ mod tests { let mut path = Path::new("/name/32/"); assert!(router.recognize(&mut path).is_some()); + let s: Test1 = de::Deserialize::deserialize(PathDeserializer::new(&path)).unwrap(); + assert_eq!(s.0, "name"); + assert_eq!(s.1, 32); + let s: Test2 = de::Deserialize::deserialize(PathDeserializer::new(&path)).unwrap(); assert_eq!(s.key, "name"); assert_eq!(s.value, 32); @@ -545,6 +564,55 @@ mod tests { assert_eq!(i, 32); } + #[test] + fn test_extract_enum_value() { + let mut router = Router::<()>::build(); + router.path("/{val}/", ()); + let router = router.finish(); + + let mut path = Path::new("/val1/"); + assert!(router.recognize(&mut path).is_some()); + let i: Test3 = de::Deserialize::deserialize(PathDeserializer::new(&path)).unwrap(); + assert_eq!(i.val, TestEnum::Val1); + + let mut path = Path::new("/val3/"); + assert!(router.recognize(&mut path).is_some()); + let i: Result = + de::Deserialize::deserialize(PathDeserializer::new(&path)); + assert!(i.is_err()); + assert!(format!("{:?}", i).contains("unknown variant")); + } + + #[test] + fn test_extract_errors() { + let mut router = Router::<()>::build(); + router.path("/{value}/", ()); + let router = router.finish(); + + let mut path = Path::new("/name/"); + assert!(router.recognize(&mut path).is_some()); + + let s: Result = + de::Deserialize::deserialize(PathDeserializer::new(&path)); + assert!(s.is_err()); + assert!(format!("{:?}", s).contains("wrong number of parameters")); + + let s: Result = + de::Deserialize::deserialize(PathDeserializer::new(&path)); + assert!(s.is_err()); + assert!(format!("{:?}", s).contains("can not parse")); + + let s: Result<(String, String), de::value::Error> = + de::Deserialize::deserialize(PathDeserializer::new(&path)); + assert!(s.is_err()); + assert!(format!("{:?}", s).contains("wrong number of parameters")); + + let s: Result = + de::Deserialize::deserialize(PathDeserializer::new(&path)); + assert!(s.is_err()); + assert!(format!("{:?}", s).contains("can not parse")); + } + // #[test] // fn test_extract_path_decode() { // let mut router = Router::<()>::default(); diff --git a/router/src/path.rs b/router/src/path.rs index ee467af8..bcd4439f 100644 --- a/router/src/path.rs +++ b/router/src/path.rs @@ -58,6 +58,7 @@ impl Path { &mut self.path } + /// Path pub fn path(&self) -> &str { let skip = self.skip as usize; let path = self.path.path(); @@ -68,7 +69,8 @@ impl Path { } } - pub fn set_path(&mut self, path: T) { + /// Reset inner path + pub fn set(&mut self, path: T) { self.skip = 0; self.path = path; self.segments.clear(); @@ -123,7 +125,7 @@ impl Path { } } - /// Get unprocessed part of path + /// Get unprocessed part of the path pub fn unprocessed(&self) -> &str { &self.path.path()[(self.skip as usize)..] }