use crate::core::Node;
use crate::elements::div;
pub trait Layout {
fn render(&self, content: Node) -> Node;
fn render_many(&self, contents: Vec<Node>) -> Node {
self.render(div().children_from(contents).into())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::Render;
use crate::elements::{body, h1, main_tag, p};
struct Minimal;
impl Layout for Minimal {
fn render(&self, content: Node) -> Node {
body()
.child(h1().text("Site"))
.child(main_tag().child(content))
.into()
}
}
#[test]
fn a_layout_wraps_a_single_node() {
let page = Minimal.render(p().text("body").into());
assert_eq!(
page.render(),
"<body><h1>Site</h1><main><p>body</p></main></body>"
);
}
#[test]
fn render_many_groups_the_contents_in_a_div() {
let page = Minimal.render_many(vec![p().text("a").into(), p().text("b").into()]);
assert_eq!(
page.render(),
"<body><h1>Site</h1><main><div><p>a</p><p>b</p></div></main></body>"
);
}
#[test]
fn render_many_with_no_contents_still_produces_the_wrapper() {
let page = Minimal.render_many(vec![]);
assert!(page.render().contains("<div></div>"));
}
}