From: Ruben Beltran del Rio Date: Fri, 3 Jan 2025 22:57:40 +0000 (+0100) Subject: Update layout tests X-Git-Url: https://git.r.bdr.sh/rbdr/page/commitdiff_plain/2cbae13cfd94f48dfe9a8c903e05aea49106b778?hp=d9f6d3c1536092c832c399c1dbe5e05f712a006e Update layout tests --- diff --git a/src/file_handler/file_strategies/file.rs b/src/file_handler/file_strategies/file.rs index 7e95adf..603d189 100644 --- a/src/file_handler/file_strategies/file.rs +++ b/src/file_handler/file_strategies/file.rs @@ -56,6 +56,12 @@ mod tests { let strategy = Strategy {}; assert!(strategy.is(&test_dir.join("image.png"))); assert!(strategy.is(&test_dir.join("style.css"))); + } + + #[test] + fn rejects_directories() { + let test_dir = setup_test_dir(); + let strategy = Strategy {}; assert!(!strategy.is(&test_dir)); } @@ -66,9 +72,14 @@ mod tests { } #[test] - fn handles_correct_file_type() { + fn handles_file_type() { let strategy = Strategy {}; assert!(strategy.can_handle(&FileType::File)); + } + + #[test] + fn rejects_non_file_types() { + let strategy = Strategy {}; assert!(!strategy.can_handle(&FileType::Layout)); assert!(!strategy.can_handle(&FileType::Gemini)); assert!(!strategy.can_handle(&FileType::Unknown)); diff --git a/src/file_handler/file_strategies/layout.rs b/src/file_handler/file_strategies/layout.rs index 44b5000..41acde1 100644 --- a/src/file_handler/file_strategies/layout.rs +++ b/src/file_handler/file_strategies/layout.rs @@ -28,115 +28,103 @@ impl FileHandlerStrategy for Strategy { #[cfg(test)] mod tests { - use super::*; - use std::path::PathBuf; - - fn setup() -> Strategy { - Strategy {} - } - - mod is_tests { - use super::*; - - #[test] - fn identifies_layout_file() { - let strategy = setup(); - let path = PathBuf::from("_layout.html"); - assert!(strategy.is(&path)); - } + use std::fs::create_dir_all; - #[test] - fn rejects_non_layout_html() { - let strategy = setup(); - let path = PathBuf::from("regular.html"); - assert!(!strategy.is(&path)); - } + use super::*; - #[test] - fn rejects_layout_with_different_extension() { - let strategy = setup(); - let path = PathBuf::from("_layout.txt"); - assert!(!strategy.is(&path)); - } + use test_utilities::*; - #[test] - fn rejects_layout_with_prefix() { - let strategy = setup(); - let path = PathBuf::from("prefix_layout.html"); - assert!(!strategy.is(&path)); - } + #[test] + fn identifies_layout_file() { + let test_dir = setup_test_dir(); + create_test_file(&test_dir.join("_layout.html"), ""); + let strategy = Strategy {}; + assert!(strategy.is(&test_dir.join("_layout.html"))); + } - #[test] - fn rejects_directory_named_layout() { - let strategy = setup(); - let path = PathBuf::from("tests/fixtures"); + #[test] + fn rejects_non_layout_html() { + let test_dir = setup_test_dir(); + create_test_file(&test_dir.join("regular.html"), ""); + let strategy = Strategy {}; + assert!(!strategy.is(&test_dir.join("regular.html"))); + } - assert!(!strategy.is(&path)); - } + #[test] + fn rejects_layout_with_different_extension() { + let test_dir = setup_test_dir(); + create_test_file(&test_dir.join("_layout.txt"), ""); + let strategy = Strategy {}; + assert!(!strategy.is(&test_dir.join("_layout.txt"))); } - mod identify_tests { - use super::*; + #[test] + fn rejects_layout_with_prefix() { + let test_dir = setup_test_dir(); + create_test_file(&test_dir.join("prefix_layout.txt"), ""); + let strategy = Strategy {}; + assert!(!strategy.is(&test_dir.join("prefix_layout.txt"))); + } - #[test] - fn returns_layout_type() { - let strategy = setup(); - assert!(matches!(strategy.identify(), FileType::Layout)); - } + #[test] + fn rejects_directory_named_layout() { + let test_dir = setup_test_dir(); + let layout_dir = test_dir.join("_layout"); + create_dir_all(&layout_dir) + .expect("Could not create _layout test directory"); + let strategy = Strategy {}; + assert!(!strategy.is(&layout_dir)); } - mod can_handle_tests { - use super::*; + #[test] + fn identifies_layout_type() { + let strategy = Strategy {}; + assert!(matches!(strategy.identify(), FileType::Layout)); + } - #[test] - fn handles_layout_type() { - let strategy = setup(); - assert!(strategy.can_handle(&FileType::Layout)); - } + #[test] + fn handles_layout_type() { + let strategy = Strategy {}; + assert!(strategy.can_handle(&FileType::Layout)); + } - #[test] - fn rejects_non_layout_types() { - let strategy = setup(); - assert!(!strategy.can_handle(&FileType::File)); - assert!(!strategy.can_handle(&FileType::Gemini)); - assert!(!strategy.can_handle(&FileType::Unknown)); - } + #[test] + fn rejects_non_layout_types() { + let strategy = Strategy {}; + assert!(!strategy.can_handle(&FileType::File)); + assert!(!strategy.can_handle(&FileType::Gemini)); + assert!(!strategy.can_handle(&FileType::Unknown)); } - mod handle_methods { - use super::*; - - #[test] - fn handle_html_does_nothing() { - let strategy = setup(); - let file = File { - path: PathBuf::from("test.html"), - file_type: FileType::Layout, - }; - - // Should not panic - strategy.handle_html( - &PathBuf::from("source"), - &PathBuf::from("dest"), - &file, - "layout content" - ); - } + #[test] + fn handle_html_does_nothing() { + let strategy = Strategy {}; + let file = File { + path: PathBuf::from("_layout.html"), + file_type: FileType::Layout, + }; + + strategy.handle_html( + &PathBuf::from("source"), + &PathBuf::from("dest"), + &file, + "layout content" + ); + } - #[test] - fn handle_gemini_does_nothing() { - let strategy = setup(); - let file = File { - path: PathBuf::from("test.gmi"), - file_type: FileType::Layout, - }; - - // Should not panic - strategy.handle_gemini( - &PathBuf::from("source"), - &PathBuf::from("dest"), - &file - ); - } + #[test] + fn handle_gemini_does_nothing() { + let strategy = Strategy {}; + let file = File { + path: PathBuf::from("test.gmi"), + file_type: FileType::Layout, + }; + + // Should not panic + strategy.handle_gemini( + &PathBuf::from("source"), + &PathBuf::from("dest"), + &file + ); } }