From 9df8ea3558ec6dcb8b5bb97dd1a0d6eaf40c7c58 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Sat, 4 Jul 2020 23:43:06 -0400 Subject: [PATCH] fixed tests --- src/config.rs | 22 ++++++++++++---------- src/main.rs | 10 ++++++++-- src/routes.rs | 13 +++++++------ 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/config.rs b/src/config.rs index 609e95b..f490a8b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -286,7 +286,10 @@ mod route { let path = std::path::Path::new(path); assert!(path.is_relative()); let path = path.to_str().unwrap(); - assert_eq!(from_str::(path).unwrap(), Route::Path(path.into())); + assert_eq!( + from_str::(path).unwrap(), + Route::from_str(path).unwrap() + ); } #[test] @@ -294,14 +297,17 @@ mod route { let tmpfile = NamedTempFile::new().unwrap(); let path = format!("{}", tmpfile.path().display()); assert!(tmpfile.path().is_absolute()); - assert_eq!(from_str::(&path).unwrap(), Route::Path(path)); + assert_eq!( + from_str::(&path).unwrap(), + Route::from_str(&path).unwrap() + ); } #[test] fn deserialize_http_path() { assert_eq!( from_str::("http://google.com").unwrap(), - Route::External("http://google.com".into()) + Route::from_str("http://google.com").unwrap() ); } @@ -309,19 +315,15 @@ mod route { fn deserialize_https_path() { assert_eq!( from_str::("https://google.com").unwrap(), - Route::External("https://google.com".into()) + Route::from_str("https://google.com").unwrap() ); } #[test] fn serialize() { assert_eq!( - &to_string(&Route::External("hello world".into())).unwrap(), - "---\nhello world" - ); - assert_eq!( - &to_string(&Route::Path("hello world".into())).unwrap(), - "---\nhello world" + &to_string(&Route::from_str("hello world").unwrap()).unwrap(), + "---\nroute_type: External\npath: hello world\nhidden: false\ndescription: ~" ); } } diff --git a/src/main.rs b/src/main.rs index 5ef8c23..bb9c330 100644 --- a/src/main.rs +++ b/src/main.rs @@ -248,14 +248,15 @@ mod init_logger { mod cache_routes { use super::*; use std::iter::FromIterator; + use std::str::FromStr; fn generate_external_routes( routes: &[(&str, &str)], ) -> HashMap { HashMap::from_iter( routes - .iter() - .map(|(k, v)| ((*k).into(), Route::External((*v).into()))), + .into_iter() + .map(|kv| (kv.0.into(), Route::from_str(kv.1).unwrap())), ) } @@ -270,12 +271,14 @@ mod cache_routes { name: String::from("x"), description: Some(String::from("y")), routes: generate_external_routes(&[("a", "b"), ("c", "d")]), + hidden: false, }; let group2 = RouteGroup { name: String::from("5"), description: Some(String::from("6")), routes: generate_external_routes(&[("1", "2"), ("3", "4")]), + hidden: false, }; assert_eq!( @@ -295,12 +298,14 @@ mod cache_routes { name: String::from("x"), description: Some(String::from("y")), routes: generate_external_routes(&[("a", "b"), ("c", "d")]), + hidden: false, }; let group2 = RouteGroup { name: String::from("5"), description: Some(String::from("6")), routes: generate_external_routes(&[("a", "1"), ("c", "2")]), + hidden: false, }; assert_eq!( @@ -312,6 +317,7 @@ mod cache_routes { name: String::from("5"), description: Some(String::from("6")), routes: generate_external_routes(&[("a", "1"), ("b", "2")]), + hidden: false, }; assert_eq!( diff --git a/src/routes.rs b/src/routes.rs index f053627..362126d 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -205,6 +205,7 @@ fn resolve_path(path: PathBuf, args: &str) -> Result, BunBunError> { #[cfg(test)] mod resolve_hop { use super::*; + use std::str::FromStr; fn generate_route_result<'a>( keyword: &'a Route, @@ -238,12 +239,12 @@ mod resolve_hop { let mut map: HashMap = HashMap::new(); map.insert( "google".into(), - Route::External("https://example.com".into()), + Route::from_str("https://example.com").unwrap(), ); assert_eq!( resolve_hop("hello world", &map, &Some(String::from("google"))), generate_route_result( - &Route::External("https://example.com".into()), + &Route::from_str("https://example.com").unwrap(), "hello world" ), ); @@ -254,12 +255,12 @@ mod resolve_hop { let mut map: HashMap = HashMap::new(); map.insert( "google".into(), - Route::External("https://example.com".into()), + Route::from_str("https://example.com").unwrap(), ); assert_eq!( resolve_hop("google hello world", &map, &Some(String::from("a"))), generate_route_result( - &Route::External("https://example.com".into()), + &Route::from_str("https://example.com").unwrap(), "hello world" ), ); @@ -270,12 +271,12 @@ mod resolve_hop { let mut map: HashMap = HashMap::new(); map.insert( "google".into(), - Route::External("https://example.com".into()), + Route::from_str("https://example.com").unwrap(), ); assert_eq!( resolve_hop("google hello world", &map, &None), generate_route_result( - &Route::External("https://example.com".into()), + &Route::from_str("https://example.com").unwrap(), "hello world" ), );