use regex::*; use std::fmt; use std::io; use weft::*; #[test] fn should_render_trivial_example() { struct TrivialExample; // This simulates what a template of the form `
Hello
` should compile to. impl WeftRenderable for TrivialExample { fn render_to(&self, target: &mut impl RenderTarget) -> Result<(), io::Error> { target.start_element_attrs("p".into(), &[])?; target.text("Hello")?; target.end_element("p".into())?; Ok(()) } } let s = render_to_string(TrivialExample).expect("render_to_string"); let expected = "Hello
"; assert!( s.contains(expected), "String {:?} contains {:?}", s, expected ); } #[test] fn should_render_attrs() { struct TrivialExample; // This simulates what a template of the form `Hello
` should compile to. impl WeftRenderable for TrivialExample { fn render_to(&self, target: &mut impl RenderTarget) -> Result<(), io::Error> { target.start_element_attrs( "p".into(), &[&AttrPair::new("class".into(), "some-classes".into())], )?; target.text("Hello")?; target.end_element("p".into())?; Ok(()) } } let s = render_to_string(TrivialExample).expect("render_to_string"); let matcher = Regex::new("\\s+class=[\"']some-classes[\"']").expect("Regex::new"); assert!( matcher.find(&s).is_some(), "String {:?} matches {:?}", s, matcher ) } #[test] fn render_supports_builtins() { struct TrivialExample; // This simulates what a template of the form `Hello
` should compile to. impl WeftRenderable for TrivialExample { fn render_to(&self, target: &mut impl RenderTarget) -> Result<(), io::Error> { "Hello world!".render_to(target)?; Ok(()) } } let s = render_to_string(TrivialExample).expect("render_to_string"); let expected = "Hello world!"; assert!( s.contains(expected), "String {:?} contains {:?}", s, expected ); } #[test] fn display_supports_renderable() { struct DisplayableHello
` should compile to. implHello
` should compile to. impl