diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-01-07 20:19:02 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-01-07 20:19:02 +0100 |
| commit | 795d79afdbe5bfe5fd80902f08afdb6b9fa4db03 (patch) | |
| tree | 3fdd9b1d5e987f97e8f577348a88db2a866deea4 /src/generator/html.rs | |
| parent | 40a5de55c35d6481fd538f4c5205e47c201e3289 (diff) | |
Add tests for generators
Diffstat (limited to 'src/generator/html.rs')
| -rw-r--r-- | src/generator/html.rs | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/src/generator/html.rs b/src/generator/html.rs index 70cc320..7895a83 100644 --- a/src/generator/html.rs +++ b/src/generator/html.rs @@ -20,3 +20,159 @@ 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_html_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([ + ("index".to_string(), Value::Unsigned(2828)), + ( + "html".to_string(), + Value::String("<p>Contextualization</p>".to_string()), + ), + ]), + HashMap::from([ + ("index".to_string(), Value::Unsigned(2828)), + ( + "html".to_string(), + Value::String("<p>Contexternalization</p>".to_string()), + ), + ]), + ]), + ), + ]); + + generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed"); + + assert_file_contains(&output_dir.join("index.html"), "<p>Contextualization</p>"); + assert_file_contains(&output_dir.join("index.html"), "<p>Contexternalization</p>"); + } + + #[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.html"), + "\ +{{~ posts:post }} + {{= post.html }} +{{~}} +", + ); + let context = HashMap::from([( + "posts".to_string(), + Value::Collection(vec![ + HashMap::from([( + "html".to_string(), + Value::String("<p>Recontextualization</p>".to_string()), + )]), + HashMap::from([( + "html".to_string(), + Value::String("<p>Recontexternalization</p>".to_string()), + )]), + ]), + )]); + + generate(&static_dir, &template_dir, &output_dir, &context).expect("Generate failed"); + + assert_file_contents( + &output_dir.join("index.html"), + "\ +<p>Recontextualization</p> + + <p>Recontexternalization</p> +", + ); + } + + #[test] + fn test_fails_if_html_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.html"), + "\ +{{~ posts:post }} + {{ post.html }} +{{~}} +", + ); + 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.html"), + "\ +{{~ posts:post }} + {{= post.html }} +{{~}} +", + ); + let context = HashMap::from([( + "posts".to_string(), + Value::Collection(vec![ + HashMap::from([( + "html".to_string(), + Value::String("<p>Recontextualization</p>".to_string()), + )]), + HashMap::from([( + "html".to_string(), + Value::String("<p>Recontexternalization</p>".to_string()), + )]), + ]), + )]); + + let result = generate(&static_dir, &template_dir, &output_dir, &context); + + assert!(result.is_err()); + } +} |