X-Git-Url: https://git.r.bdr.sh/rbdr/page/blobdiff_plain/4fd89b808cabc8afb0d75b9700be1da96989c4b7..36a5c232b1f5d86bf97e29af987862732556ee07:/src/file_handler/file_strategies/layout.rs diff --git a/src/file_handler/file_strategies/layout.rs b/src/file_handler/file_strategies/layout.rs index bac1059..9a60f12 100644 --- a/src/file_handler/file_strategies/layout.rs +++ b/src/file_handler/file_strategies/layout.rs @@ -1,12 +1,12 @@ pub struct Strategy {} -use std::path::PathBuf; +use std::path::Path; -use crate::file_handler::{File, FileType, FileHandlerStrategy}; +use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy}; impl FileHandlerStrategy for Strategy { - fn is(&self, path: &PathBuf) -> bool { - return !path.is_dir() && path.ends_with("_layout.html"); + fn is(&self, path: &Path) -> bool { + !path.is_dir() && path.ends_with("_layout.html") } fn identify(&self) -> FileType { @@ -14,11 +14,110 @@ impl FileHandlerStrategy for Strategy { } fn can_handle(&self, file_type: &FileType) -> bool { - match file_type { - FileType::Layout => true, - _ => false, - } + matches!(file_type, FileType::Layout) } - fn handle(&self, _file: &File) {} + // We don't implement handling for layout, as we assume there's only one + // and it got handled before. + fn handle_html(&self, _s: &Path, _d: &Path, _f: &File, _l: &str) {} + fn handle_gemini(&self, _s: &Path, _d: &Path, _f: &File) {} +} + +#[cfg(test)] +mod tests { + use std::fs::create_dir_all; + use std::path::PathBuf; + + use super::*; + + use test_utilities::*; + + #[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_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"))); + } + + #[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"))); + } + + #[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 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)); + } + + #[test] + fn identifies_layout_type() { + let strategy = Strategy {}; + assert!(matches!(strategy.identify(), FileType::Layout)); + } + + #[test] + fn handles_layout_type() { + let strategy = Strategy {}; + assert!(strategy.can_handle(&FileType::Layout)); + } + + #[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)); + } + + #[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 = 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); + } }