Start of porting over the walk tests.

This commit is contained in:
Tom Alexander 2020-05-05 20:22:25 -04:00
parent 26fe996b0d
commit 5760566be0
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -304,7 +304,7 @@ fn walk_path_from_single_level<'a>(
output = new_val?; output = new_val?;
} }
Ok(WalkResult::FullyWalked(context)) Ok(WalkResult::FullyWalked(output))
} }
fn new_walk_path<'a>( fn new_walk_path<'a>(
@ -321,7 +321,7 @@ fn new_walk_path<'a>(
return Err(RenderError::NotFound { return Err(RenderError::NotFound {
path: path, path: path,
breadcrumbs: breadcrumbs.clone(), breadcrumbs: breadcrumbs.clone(),
}) });
} }
WalkResult::FullyWalked(new_context) => return Ok(new_context), WalkResult::FullyWalked(new_context) => return Ok(new_context),
} }
@ -332,8 +332,6 @@ fn new_walk_path<'a>(
}) })
} }
// TODO: rename walk_path_from_single_level
/// Attempts to walk a path from only 1 level in the context breadcrumbs
fn walk_path<'a>( fn walk_path<'a>(
context: &'a dyn ContextElement, context: &'a dyn ContextElement,
path: &Vec<&str>, path: &Vec<&str>,
@ -355,83 +353,112 @@ mod tests {
use crate::renderer::context_element::Renderable; use crate::renderer::context_element::Renderable;
use crate::renderer::context_element::Walkable; use crate::renderer::context_element::Walkable;
impl ContextElement for u32 {}
impl ContextElement for &str {}
impl<I: ContextElement> ContextElement for HashMap<&str, I> {}
impl Renderable for u32 {
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
// TODO: handle the filters
Ok(self.to_string())
}
}
impl Renderable for &str {
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
// TODO: handle the filters
Ok(self.to_string())
}
}
impl<I: ContextElement> Renderable for HashMap<&str, I> {
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
// TODO: handle the filters
Err(RenderError::CantRender { elem: self })
}
}
impl<I: ContextElement> Walkable for HashMap<&str, I> {
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
let child = self.get(segment).ok_or(RenderError::WontWalk {
segment: segment.to_string(),
elem: self,
})?;
Ok(child)
}
}
impl Walkable for &str {
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
Err(RenderError::CantWalk {
segment: segment.to_string(),
elem: self,
})
}
}
impl Walkable for u32 {
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
Err(RenderError::CantWalk {
segment: segment.to_string(),
elem: self,
})
}
}
impl Loopable for &str {
fn get_loop_elements(&self) -> Result<Vec<&dyn ContextElement>, RenderError> {
if self.is_empty() {
Ok(Vec::new())
} else {
Ok(vec![self])
}
}
}
impl Loopable for u32 {
fn get_loop_elements(&self) -> Result<Vec<&dyn ContextElement>, RenderError> {
Ok(vec![self])
}
}
impl<I: ContextElement> Loopable for HashMap<&str, I> {
fn get_loop_elements(&self) -> Result<Vec<&dyn ContextElement>, RenderError> {
Ok(vec![self])
}
}
#[test]
fn test_new_walk_path() {
let context: HashMap<&str, &str> =
[("cat", "kitty"), ("dog", "doggy"), ("tiger", "murderkitty")]
.iter()
.cloned()
.collect();
let number_context: HashMap<&str, u32> = [("cat", 1), ("dog", 2), ("tiger", 3)]
.iter()
.cloned()
.collect();
let deep_context: HashMap<&str, HashMap<&str, &str>> = [
("cat", [("food", "meat")].iter().cloned().collect()),
("dog", [("food", "meat")].iter().cloned().collect()),
("tiger", [("food", "people")].iter().cloned().collect()),
]
.iter()
.cloned()
.collect();
assert_eq!(
new_walk_path(&vec![&context as &dyn ContextElement], &vec!["cat"])
.unwrap()
.render(&Vec::new())
.unwrap(),
"kitty".to_owned()
);
}
#[test] #[test]
fn test_walk_path() { fn test_walk_path() {
impl ContextElement for u32 {}
impl ContextElement for &str {}
impl<I: ContextElement> ContextElement for HashMap<&str, I> {}
impl Renderable for u32 {
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
// TODO: handle the filters
Ok(self.to_string())
}
}
impl Renderable for &str {
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
// TODO: handle the filters
Ok(self.to_string())
}
}
impl<I: ContextElement> Renderable for HashMap<&str, I> {
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
// TODO: handle the filters
Err(RenderError::CantRender { elem: self })
}
}
impl<I: ContextElement> Walkable for HashMap<&str, I> {
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
let child = self.get(segment).ok_or(RenderError::WontWalk {
segment: segment.to_string(),
elem: self,
})?;
Ok(child)
}
}
impl Walkable for &str {
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
Err(RenderError::CantWalk {
segment: segment.to_string(),
elem: self,
})
}
}
impl Walkable for u32 {
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
Err(RenderError::CantWalk {
segment: segment.to_string(),
elem: self,
})
}
}
impl Loopable for &str {
fn get_loop_elements(&self) -> Result<Vec<&dyn ContextElement>, RenderError> {
if self.is_empty() {
Ok(Vec::new())
} else {
Ok(vec![self])
}
}
}
impl Loopable for u32 {
fn get_loop_elements(&self) -> Result<Vec<&dyn ContextElement>, RenderError> {
Ok(vec![self])
}
}
impl<I: ContextElement> Loopable for HashMap<&str, I> {
fn get_loop_elements(&self) -> Result<Vec<&dyn ContextElement>, RenderError> {
Ok(vec![self])
}
}
let context: HashMap<&str, &str> = let context: HashMap<&str, &str> =
[("cat", "kitty"), ("dog", "doggy"), ("tiger", "murderkitty")] [("cat", "kitty"), ("dog", "doggy"), ("tiger", "murderkitty")]
.iter() .iter()