From 3241b40d01a01f985b9c8ba47b51f3a644317713 Mon Sep 17 00:00:00 2001 From: dschlueter Date: Tue, 10 Feb 2026 10:12:40 +0100 Subject: [PATCH] =?UTF-8?q?test:=20Unit-Tests=20f=C3=BCr=20clean=5Ffilenam?= =?UTF-8?q?e()=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 8 Tests für verschiedene Edge-Cases: - Basis-Funktionalität (Spaces, Klammern) - Versteckte Dateien (.gitignore, .my config) - Umlaute (Müller → Mueller) - Extensions (.tar.gz, mehrfache Punkte) - Spezial-Identifier (C++, C#) - Apostrophe (O'Reilly → OReilly) - Leere Dateinamen nach Bereinigung - Dateien die keine Änderung benötigen Alle Tests bestehen ✓ Co-Authored-By: Claude Sonnet 4.5 --- src/sanitizer.rs | 166 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) diff --git a/src/sanitizer.rs b/src/sanitizer.rs index c0badfb..bd7de80 100644 --- a/src/sanitizer.rs +++ b/src/sanitizer.rs @@ -192,3 +192,169 @@ pub fn is_safe_rename(src: &Path, dst: &Path, force: bool) -> bool { true } + +#[cfg(test)] +mod tests { + use super::*; + use std::ffi::OsStr; + + fn make_test_config() -> Config { + let mut replacements = std::collections::HashMap::new(); + replacements.insert("ä".to_string(), "ae".to_string()); + replacements.insert("ö".to_string(), "oe".to_string()); + replacements.insert("ü".to_string(), "ue".to_string()); + replacements.insert("ß".to_string(), "ss".to_string()); + Config { replacements } + } + + #[test] + fn test_clean_filename_basic() { + let config = Config::default(); + + // Spaces should become underscores + assert_eq!( + clean_filename(OsStr::new("test file.txt"), &config, false), + Some("test_file.txt".to_string()) + ); + + // Parentheses should become underscores + assert_eq!( + clean_filename(OsStr::new("file (1).txt"), &config, false), + Some("file_1.txt".to_string()) + ); + + // Multiple underscores should be collapsed + assert_eq!( + clean_filename(OsStr::new("test__file.txt"), &config, false), + Some("test_file.txt".to_string()) + ); + } + + #[test] + fn test_clean_filename_hidden_files() { + let config = Config::default(); + + // Hidden files should keep their leading dot + assert_eq!( + clean_filename(OsStr::new(".gitignore"), &config, false), + None // No change needed + ); + + // Hidden files with spaces + assert_eq!( + clean_filename(OsStr::new(".my config"), &config, false), + Some(".my_config".to_string()) + ); + + // Hidden files with extension + assert_eq!( + clean_filename(OsStr::new(".test file.txt"), &config, false), + Some(".test_file.txt".to_string()) + ); + + // Multiple leading dots + assert_eq!( + clean_filename(OsStr::new("...strange"), &config, false), + Some(".unnamed.strange".to_string()) + ); + } + + #[test] + fn test_clean_filename_umlauts() { + let config = make_test_config(); + + // German umlauts + assert_eq!( + clean_filename(OsStr::new("Müller.pdf"), &config, false), + Some("Mueller.pdf".to_string()) + ); + + assert_eq!( + clean_filename(OsStr::new("schön.txt"), &config, false), + Some("schoen.txt".to_string()) + ); + + assert_eq!( + clean_filename(OsStr::new("Größe.doc"), &config, false), + Some("Groesse.doc".to_string()) + ); + } + + #[test] + fn test_clean_filename_extensions() { + let config = Config::default(); + + // Single extension + assert_eq!( + clean_filename(OsStr::new("test file.txt"), &config, false), + Some("test_file.txt".to_string()) + ); + + // Double extension (currently only keeps last) + assert_eq!( + clean_filename(OsStr::new("archive.tar.gz"), &config, false), + None // No special chars to clean + ); + + // Multiple dots + assert_eq!( + clean_filename(OsStr::new("foo..bar.txt"), &config, false), + Some("foo.bar.txt".to_string()) + ); + } + + #[test] + fn test_clean_filename_special_identifiers() { + let config = Config::default(); + + // C++ should be preserved + assert_eq!( + clean_filename(OsStr::new("test C++.txt"), &config, false), + Some("test_C++.txt".to_string()) + ); + + // C# should be preserved + assert_eq!( + clean_filename(OsStr::new("guide C#.pdf"), &config, false), + Some("guide_C#.pdf".to_string()) + ); + } + + #[test] + fn test_clean_filename_no_change_needed() { + let config = Config::default(); + + // Already clean filenames should return None + assert_eq!( + clean_filename(OsStr::new("clean_file.txt"), &config, false), + None + ); + + assert_eq!( + clean_filename(OsStr::new("another-file.pdf"), &config, false), + None + ); + } + + #[test] + fn test_clean_filename_empty_after_cleaning() { + let config = Config::default(); + + // File with only special chars should become "unnamed" + assert_eq!( + clean_filename(OsStr::new("###.txt"), &config, false), + Some("unnamed.txt".to_string()) + ); + } + + #[test] + fn test_clean_filename_apostrophe() { + let config = Config::default(); + + // Apostrophes should be removed (not replaced with underscore) + assert_eq!( + clean_filename(OsStr::new("O'Reilly.pdf"), &config, false), + Some("OReilly.pdf".to_string()) + ); + } +}