use std::collections::HashMap; use platelet::{render_with_custom_filesystem, renderer::Filesystem}; use serde_json::{json, Map}; struct MockMultiFile { data: HashMap, } impl Filesystem<()> for MockMultiFile { fn read(&self, path: &String) -> Result { Ok(self.data.get(path).unwrap().clone()) } fn move_to(&self, _current: &String, path: &String) -> Result { Ok(path.to_owned()) } } #[test] fn pl_src() { let vars = Map::new().into(); let result = render_with_custom_filesystem( &vars, &"index.html".into(), &MockMultiFile { data: HashMap::from([ ( "index.html".into(), "
".to_owned(), ), ("embed.html".into(), "

hello world

".to_owned()), ]), }, ); assert_eq!(result.unwrap(), "

hello world

"); } #[test] fn pl_src_with_slot() { let vars = Map::new().into(); let result = render_with_custom_filesystem( &vars, &"index.html".into(), &MockMultiFile { data: HashMap::from([ ( "index.html".into(), "
innercontent
".to_owned(), ), ("embed.html".into(), "
".to_owned()), ]), }, ); assert_eq!( result.unwrap(), "
innercontent
" ); } #[test] fn pl_src_with_named_slots() { let vars = Map::new().into(); let result = render_with_custom_filesystem( &vars, &"index.html".into(), &MockMultiFile { data: HashMap::from([ ( "index.html".into(), "\ \ \ " .to_owned(), ), ( "embed.html".into(), "\ " .to_owned(), ), ]), }, ); assert_eq!( result.unwrap(), "Left hand sideRight hand side" ); } #[test] fn pl_src_with_cotext() { let vars = Map::new().into(); let result = render_with_custom_filesystem( &vars, &"index.html".into(), &MockMultiFile { data: HashMap::from([ ( "index.html".into(), r#""#.to_owned(), ), ("embed.html".into(), "{{message}}".to_owned()), ]), }, ); assert_eq!(result.unwrap(), "hello world"); } #[test] fn pl_for_and_pl_src() { let vars = Map::new().into(); let result = render_with_custom_filesystem( &vars, &"index.html".into(), &MockMultiFile { data: HashMap::from([ ( "index.html".into(), "".to_owned(), ), ("x.html".into(), "

{{x}}

".to_owned()), ]), }, ); assert_eq!(result.unwrap(), "

1

2

3

"); } #[test] fn example() { let vars = json!({ "title": "Angus' Blog", "blogposts": [ { "img_url": "http://angusjf.com/x.png", "link": "http://angusjf.com/", "summary": "

hello world

", "title": "SOMETHING COOL", "date": "01/11/2025" }, { "img_url": "http://angusjf.com/x.png", "link": "http://angusjf.com/", "summary": "

hello world

", "title": "SOMETHING ELSE", "date": "01/11/2025" } ] }); let result = render_with_custom_filesystem( &vars, &"index.html".into(), &MockMultiFile { data: HashMap::from([ ( "index.html".into(), "\ \ \ {{ title }}\ \ \ \ \ \ " .to_owned(), ), ( "blogpost.html".into(), "\ \ " .to_owned(), ), ]), }, ); assert_eq!( result.unwrap(), "\ \ \ Angus' Blog\ \ \ \ \ \ \ " ); } #[test] fn pl_for_lots_of_children() { let vars = Map::new().into(); let result = render_with_custom_filesystem( &vars, &"index.html".into(), &MockMultiFile { data: HashMap::from([ ( "index.html".into(), r#"
"#.to_owned(), ), ("embed.html".into(), "
{{i}}
({{s}})".to_owned()), ]), }, ); assert_eq!( result.unwrap(), "
    0
    (my)\ \
    1
    (cousin)\
    2
    (carl)\
    3
    (duckworth)\
    4
    (said)
" ); }