use bart_derive::BartDisplay; #[test] fn it_supports_conditional_scope_with_boolean() { #[derive(BartDisplay)] #[template_string = "{{#a?}}yes{{/a}}"] struct Test { a: bool, } assert_eq!("yes", Test { a: true }.to_string()); assert_eq!("", Test { a: false }.to_string()); } #[test] fn it_supports_negative_conditional_scope_with_boolean() { #[derive(BartDisplay)] #[template_string = "{{^a?}}no{{/a}}"] struct Test { a: bool, } assert_eq!("", Test { a: true }.to_string()); assert_eq!("no", Test { a: false }.to_string()); } #[test] fn it_supports_conditional_scope_with_non_bool() { struct TestBool<'a> { name: &'a str, } impl<'a> bart::Conditional for TestBool<'a> { fn val(&self) -> bool { self.name.len() > 2 } } #[derive(BartDisplay)] #[template_string = "{{cond.name}}: {{#cond?}}Hello {{.name}}{{/cond}}"] struct Test<'a> { cond: TestBool<'a>, } assert_eq!( "Joe: Hello Joe", Test { cond: TestBool { name: "Joe" } } .to_string() ); assert_eq!( "No: ", Test { cond: TestBool { name: "No" } } .to_string() ); } #[test] fn it_supports_conditional_scope_with_vec() { #[derive(BartDisplay)] #[template_string = "{{#a?}}yes{{/a}}"] struct Test { a: Vec, } assert_eq!("yes", Test { a: vec![1] }.to_string()); assert_eq!("", Test { a: vec![] }.to_string()); } #[test] fn it_supports_conditional_scope_with_borrowed_vec() { #[derive(BartDisplay)] #[template_string = "{{#a?}}yes{{/a}}"] struct Test<'a> { a: &'a Vec, } assert_eq!("yes", Test { a: &vec![1, 2, 3] }.to_string()); assert_eq!("", Test { a: &vec![] }.to_string()); } #[test] fn it_supports_conditional_scope_with_slice() { #[derive(BartDisplay)] #[template_string = "{{#a?}}yes{{/a}}"] struct Test<'a> { a: &'a [i32], } assert_eq!("yes", Test { a: &[1] }.to_string()); assert_eq!("", Test { a: &[] }.to_string()); } #[test] fn it_supports_conditional_scope_with_function() { #[derive(BartDisplay)] #[template_string = "{{#gt()?}}yes{{/gt()}}"] struct Test { a: i32, } impl Test { pub fn gt(&self) -> bool { self.a > 10 } } assert_eq!("yes", Test { a: 20 }.to_string()); assert_eq!("", Test { a: 5 }.to_string()); }