use weft_derive::WeftRenderable; use regex::*; #[derive(WeftRenderable)] #[template(path = "tests/trivial.html")] struct TrivialMarkup; #[test] fn should_derive_trivial_from_markup() { let s = weft::render_to_string(TrivialMarkup).expect("render_to_string"); println!("{}", s); let expected = "
hi
", selector = "#hi" )] struct Canary; let s = weft::render_to_string(Canary).expect("render_to_string"); println!("{}", s); let unexpected = "Hello
"; assert!( s.contains(expected), "String {:?} contains {:?}", s, expected ) } #[derive(WeftRenderable)] #[template(path = "tests/conditional.html")] struct Conditional { enabled: bool, } #[test] fn should_strip_when_conditional_false() { let view = Conditional { enabled: false }; let s = weft::render_to_string(view).expect("render_to_string"); println!("{}", s); let unexpected = "I am enabled"; assert!( !s.contains(unexpected), "String {:?} should not contain {:?}", s, unexpected ) } #[test] fn should_include_when_conditional_true() { let view = Conditional { enabled: true }; let s = weft::render_to_string(view).expect("render_to_string"); println!("{}", s); let expected = "I am enabled"; assert!( s.contains(expected), "String {:?} should contain {:?}", s, expected ) } #[derive(WeftRenderable)] #[template(path = "tests/for-in.html")] struct ForIn<'a> { items: Vec<&'a str>, } #[test] fn should_support_iteration() { let view = ForIn { items: vec!["one", "two", "three"], }; let s = weft::render_to_string(view).expect("render_to_string"); println!("{}", s); let expected = ["one
", "two
", "three
"]; assert!( expected.iter().all(|it| s.contains(it)), "String {:?} should contain all of {:?}", s, expected ) } #[derive(WeftRenderable)] #[template(path = "tests/content.html")] struct WithPolyContenthello
"; assert!( s.contains(expected), "String {:?} should contain {:?}", s, expected ) } #[test] fn should_support_bare_attributes() { // We ostensibly don't differentiate between a bare attribute and // and empty value. #[derive(WeftRenderable)] #[template(source = "")] struct BareAttr; let s = weft::render_to_string(BareAttr).expect("render_to_string"); println!("{}", s); let expected = " some-thing="; assert!( s.contains(expected), "String {:?} should contain {:?}", s, expected ) } #[test] fn should_support_inline_expr_in_cdata() { #[derive(WeftRenderable)] #[template(source = "Hello {{ self.0 }}!
")] struct Greeting(String); let s = weft::render_to_string(Greeting("world".into())).expect("render_to_string"); println!("{}", s); let expected = "Hello world!
"; assert!( s.contains(expected), "String {:?} should contain {:?}", s, expected ) } #[test] fn should_support_inline_expr_in_attrs() { #[derive(WeftRenderable)] #[template(source = "Longer thing")] struct Abbr(String); let s = weft::render_to_string(Abbr("Long".into())).expect("render_to_string"); println!("{}", s); let expected = ""; assert!( s.contains(expected), "String {:?} should contain {:?}", s, expected ) } #[test] fn should_correctly_parse_inline_with_entities() { #[derive(WeftRenderable)] #[template(source = "Hello {{ self.0 }}!
")] struct Greeting(String); let s = weft::render_to_string(Greeting("world".into())).expect("render_to_string"); println!("{}", s); let expected = "Hello world!
"; assert!( s.contains(expected), "String {:?} should contain {:?}", s, expected ) } #[cfg(never)] #[test] #[ignore] fn should_allow_disabling_inline_exprs() { #[derive(WeftRenderable)] #[template(source = "Hello {{ self.0 }}!
")] struct Greeting2(String); let s = weft::render_to_string(Greeting2("world".into())).expect("render_to_string"); println!("{}", s); let expected = "Hello {{ self.0 }}!
"; assert!( s.contains(expected), "String {:?} should contain {:?}", s, expected ) } #[test] fn should_import_displayable() { #[derive(WeftRenderable)] #[template(source = "{{ self.0.display() }}
")] struct Displayer(u64); let s = weft::render_to_string(Displayer(42)).expect("render_to_string"); println!("{}", s); let expected = ">42<"; assert!( s.contains(expected), "String {:?} should contain {:?}", s, expected ) } #[test] fn should_correctly_escape_content_in_text() { let view = WithContent { child: "