fixed tests

This commit is contained in:
Edward Shen 2020-07-04 23:43:06 -04:00
parent dab4d52f4d
commit 9df8ea3558
Signed by: edward
GPG key ID: 19182661E818369F
3 changed files with 27 additions and 18 deletions

View file

@ -286,7 +286,10 @@ mod route {
let path = std::path::Path::new(path); let path = std::path::Path::new(path);
assert!(path.is_relative()); assert!(path.is_relative());
let path = path.to_str().unwrap(); let path = path.to_str().unwrap();
assert_eq!(from_str::<Route>(path).unwrap(), Route::Path(path.into())); assert_eq!(
from_str::<Route>(path).unwrap(),
Route::from_str(path).unwrap()
);
} }
#[test] #[test]
@ -294,14 +297,17 @@ mod route {
let tmpfile = NamedTempFile::new().unwrap(); let tmpfile = NamedTempFile::new().unwrap();
let path = format!("{}", tmpfile.path().display()); let path = format!("{}", tmpfile.path().display());
assert!(tmpfile.path().is_absolute()); assert!(tmpfile.path().is_absolute());
assert_eq!(from_str::<Route>(&path).unwrap(), Route::Path(path)); assert_eq!(
from_str::<Route>(&path).unwrap(),
Route::from_str(&path).unwrap()
);
} }
#[test] #[test]
fn deserialize_http_path() { fn deserialize_http_path() {
assert_eq!( assert_eq!(
from_str::<Route>("http://google.com").unwrap(), from_str::<Route>("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() { fn deserialize_https_path() {
assert_eq!( assert_eq!(
from_str::<Route>("https://google.com").unwrap(), from_str::<Route>("https://google.com").unwrap(),
Route::External("https://google.com".into()) Route::from_str("https://google.com").unwrap()
); );
} }
#[test] #[test]
fn serialize() { fn serialize() {
assert_eq!( assert_eq!(
&to_string(&Route::External("hello world".into())).unwrap(), &to_string(&Route::from_str("hello world").unwrap()).unwrap(),
"---\nhello world" "---\nroute_type: External\npath: hello world\nhidden: false\ndescription: ~"
);
assert_eq!(
&to_string(&Route::Path("hello world".into())).unwrap(),
"---\nhello world"
); );
} }
} }

View file

@ -248,14 +248,15 @@ mod init_logger {
mod cache_routes { mod cache_routes {
use super::*; use super::*;
use std::iter::FromIterator; use std::iter::FromIterator;
use std::str::FromStr;
fn generate_external_routes( fn generate_external_routes(
routes: &[(&str, &str)], routes: &[(&str, &str)],
) -> HashMap<String, Route> { ) -> HashMap<String, Route> {
HashMap::from_iter( HashMap::from_iter(
routes routes
.iter() .into_iter()
.map(|(k, v)| ((*k).into(), Route::External((*v).into()))), .map(|kv| (kv.0.into(), Route::from_str(kv.1).unwrap())),
) )
} }
@ -270,12 +271,14 @@ mod cache_routes {
name: String::from("x"), name: String::from("x"),
description: Some(String::from("y")), description: Some(String::from("y")),
routes: generate_external_routes(&[("a", "b"), ("c", "d")]), routes: generate_external_routes(&[("a", "b"), ("c", "d")]),
hidden: false,
}; };
let group2 = RouteGroup { let group2 = RouteGroup {
name: String::from("5"), name: String::from("5"),
description: Some(String::from("6")), description: Some(String::from("6")),
routes: generate_external_routes(&[("1", "2"), ("3", "4")]), routes: generate_external_routes(&[("1", "2"), ("3", "4")]),
hidden: false,
}; };
assert_eq!( assert_eq!(
@ -295,12 +298,14 @@ mod cache_routes {
name: String::from("x"), name: String::from("x"),
description: Some(String::from("y")), description: Some(String::from("y")),
routes: generate_external_routes(&[("a", "b"), ("c", "d")]), routes: generate_external_routes(&[("a", "b"), ("c", "d")]),
hidden: false,
}; };
let group2 = RouteGroup { let group2 = RouteGroup {
name: String::from("5"), name: String::from("5"),
description: Some(String::from("6")), description: Some(String::from("6")),
routes: generate_external_routes(&[("a", "1"), ("c", "2")]), routes: generate_external_routes(&[("a", "1"), ("c", "2")]),
hidden: false,
}; };
assert_eq!( assert_eq!(
@ -312,6 +317,7 @@ mod cache_routes {
name: String::from("5"), name: String::from("5"),
description: Some(String::from("6")), description: Some(String::from("6")),
routes: generate_external_routes(&[("a", "1"), ("b", "2")]), routes: generate_external_routes(&[("a", "1"), ("b", "2")]),
hidden: false,
}; };
assert_eq!( assert_eq!(

View file

@ -205,6 +205,7 @@ fn resolve_path(path: PathBuf, args: &str) -> Result<Vec<u8>, BunBunError> {
#[cfg(test)] #[cfg(test)]
mod resolve_hop { mod resolve_hop {
use super::*; use super::*;
use std::str::FromStr;
fn generate_route_result<'a>( fn generate_route_result<'a>(
keyword: &'a Route, keyword: &'a Route,
@ -238,12 +239,12 @@ mod resolve_hop {
let mut map: HashMap<String, Route> = HashMap::new(); let mut map: HashMap<String, Route> = HashMap::new();
map.insert( map.insert(
"google".into(), "google".into(),
Route::External("https://example.com".into()), Route::from_str("https://example.com").unwrap(),
); );
assert_eq!( assert_eq!(
resolve_hop("hello world", &map, &Some(String::from("google"))), resolve_hop("hello world", &map, &Some(String::from("google"))),
generate_route_result( generate_route_result(
&Route::External("https://example.com".into()), &Route::from_str("https://example.com").unwrap(),
"hello world" "hello world"
), ),
); );
@ -254,12 +255,12 @@ mod resolve_hop {
let mut map: HashMap<String, Route> = HashMap::new(); let mut map: HashMap<String, Route> = HashMap::new();
map.insert( map.insert(
"google".into(), "google".into(),
Route::External("https://example.com".into()), Route::from_str("https://example.com").unwrap(),
); );
assert_eq!( assert_eq!(
resolve_hop("google hello world", &map, &Some(String::from("a"))), resolve_hop("google hello world", &map, &Some(String::from("a"))),
generate_route_result( generate_route_result(
&Route::External("https://example.com".into()), &Route::from_str("https://example.com").unwrap(),
"hello world" "hello world"
), ),
); );
@ -270,12 +271,12 @@ mod resolve_hop {
let mut map: HashMap<String, Route> = HashMap::new(); let mut map: HashMap<String, Route> = HashMap::new();
map.insert( map.insert(
"google".into(), "google".into(),
Route::External("https://example.com".into()), Route::from_str("https://example.com").unwrap(),
); );
assert_eq!( assert_eq!(
resolve_hop("google hello world", &map, &None), resolve_hop("google hello world", &map, &None),
generate_route_result( generate_route_result(
&Route::External("https://example.com".into()), &Route::from_str("https://example.com").unwrap(),
"hello world" "hello world"
), ),
); );