X-Git-Url: https://git.r.bdr.sh/rbdr/blog/blobdiff_plain/b17907faf8d9693cef94a6048d802bd4ced9102f..9c537e473ac117eadad86b68d21f3aaf8d8937c4:/src/generator/txt.rs diff --git a/src/generator/txt.rs b/src/generator/txt.rs index 0a53655..ef48c03 100644 --- a/src/generator/txt.rs +++ b/src/generator/txt.rs @@ -20,3 +20,153 @@ pub fn generate( } Ok(()) } + +#[cfg(test)] +mod tests { + use std::collections::HashMap; + use std::fs::create_dir_all; + + use super::*; + + use crate::template::Value; + + use test_utilities::*; + + #[test] + fn test_generates_txt_with_default_layout() { + let test_dir = setup_test_dir(); + let static_dir = test_dir.join("static"); + let template_dir = test_dir.join("templates"); + let output_dir = test_dir.join("output"); + + create_dir_all(&static_dir).expect("Could not create static directory"); + create_dir_all(&template_dir).expect("Could not create template directory"); + create_dir_all(&output_dir).expect("Could not create output directory"); + + let context = HashMap::from([ + ("has_posts".to_string(), Value::Bool(true)), + ( + "posts".to_string(), + Value::Collection(vec![ + HashMap::from([( + "raw".to_string(), + Value::String("Contextualization".to_string()), + )]), + HashMap::from([( + "raw".to_string(), + Value::String("Contexternalization".to_string()), + )]), + ]), + ), + ]); + + generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed"); + + assert_file_contains(&output_dir.join("index.txt"), "Contextualization"); + assert_file_contains(&output_dir.join("index.txt"), "Contexternalization"); + } + + #[test] + fn test_uses_custom_layout_if_available() { + let test_dir = setup_test_dir(); + let static_dir = test_dir.join("static"); + let template_dir = test_dir.join("templates"); + let output_dir = test_dir.join("output"); + + create_dir_all(&static_dir).expect("Could not create static directory"); + create_dir_all(&template_dir).expect("Could not create template directory"); + create_dir_all(&output_dir).expect("Could not create output directory"); + create_test_file( + &template_dir.join("index.txt"), + "\ +{{~ posts:post }} + {{= post.raw }} +{{~}} +", + ); + let context = HashMap::from([( + "posts".to_string(), + Value::Collection(vec![ + HashMap::from([( + "raw".to_string(), + Value::String("Recontextualization".to_string()), + )]), + HashMap::from([( + "raw".to_string(), + Value::String("Recontexternalization".to_string()), + )]), + ]), + )]); + + generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed"); + + assert_file_contents( + &output_dir.join("index.txt"), + "\ +Recontextualization + + Recontexternalization +", + ); + } + + #[test] + fn test_fails_if_txt_is_malformed() { + let test_dir = setup_test_dir(); + let static_dir = test_dir.join("static"); + let template_dir = test_dir.join("templates"); + let output_dir = test_dir.join("output"); + + create_dir_all(&static_dir).expect("Could not create static directory"); + create_dir_all(&template_dir).expect("Could not create template directory"); + create_dir_all(&output_dir).expect("Could not create output directory"); + create_test_file( + &template_dir.join("index.txt"), + "\ +{{~ posts:post }} + {{ post.raw }} +{{~}} +", + ); + let context = HashMap::new(); + + let result = generate(&static_dir, &template_dir, &output_dir, &context); + + assert!(result.is_err()); + } + + #[test] + fn test_fails_if_output_is_not_writable() { + let test_dir = setup_test_dir(); + let static_dir = test_dir.join("static"); + let template_dir = test_dir.join("templates"); + let output_dir = test_dir.join("output"); + + create_dir_all(&template_dir).expect("Could not create template directory"); + create_test_file( + &template_dir.join("index.txt"), + "\ +{{~ posts:post }} + {{= post.raw }} +{{~}} +", + ); + let context = HashMap::from([( + "posts".to_string(), + Value::Collection(vec![ + HashMap::from([( + "raw".to_string(), + Value::String("Recontextualization".to_string()), + )]), + HashMap::from([( + "raw".to_string(), + Value::String("Recontexternalization".to_string()), + )]), + ]), + )]); + + let result = generate(&static_dir, &template_dir, &output_dir, &context); + + assert!(result.is_err()); + } +}