sal/text/tests/string_normalization_tests.rs
Mahmoud-Emad 8012a66250
Some checks are pending
Rhai Tests / Run Rhai Tests (push) Waiting to run
feat: Add Rhai scripting support
- Add new `sal-rhai` crate for Rhai scripting integration
- Integrate Rhai with existing SAL modules
- Improve error handling for Rhai scripts and SAL functions
- Add comprehensive unit and integration tests for `sal-rhai`
2025-06-23 16:23:51 +03:00

223 lines
6.5 KiB
Rust

//! Unit tests for string normalization functionality
//!
//! These tests validate the name_fix and path_fix functions including:
//! - Filename sanitization for safe filesystem usage
//! - Path normalization preserving directory structure
//! - Special character handling and replacement
//! - Unicode character removal and ASCII conversion
use sal_text::{name_fix, path_fix};
#[test]
fn test_name_fix_basic() {
assert_eq!(name_fix("Hello World"), "hello_world");
assert_eq!(name_fix("File-Name.txt"), "file_name.txt");
}
#[test]
fn test_name_fix_special_characters() {
assert_eq!(name_fix("Test!@#$%^&*()"), "test_");
assert_eq!(name_fix("Space, Tab\t, Comma,"), "space_tab_comma_");
assert_eq!(name_fix("Quotes\"'"), "quotes_");
assert_eq!(name_fix("Brackets[]<>"), "brackets_");
assert_eq!(name_fix("Operators=+-"), "operators_");
}
#[test]
fn test_name_fix_unicode_removal() {
assert_eq!(name_fix("Café"), "caf");
assert_eq!(name_fix("Résumé"), "rsum");
assert_eq!(name_fix("Über"), "ber");
assert_eq!(name_fix("Naïve"), "nave");
assert_eq!(name_fix("Piñata"), "piata");
}
#[test]
fn test_name_fix_case_conversion() {
assert_eq!(name_fix("UPPERCASE"), "uppercase");
assert_eq!(name_fix("MixedCase"), "mixedcase");
assert_eq!(name_fix("camelCase"), "camelcase");
assert_eq!(name_fix("PascalCase"), "pascalcase");
}
#[test]
fn test_name_fix_consecutive_underscores() {
assert_eq!(name_fix("Multiple Spaces"), "multiple_spaces");
assert_eq!(name_fix("Special!!!Characters"), "special_characters");
assert_eq!(name_fix("Mixed-_-Separators"), "mixed___separators");
}
#[test]
fn test_name_fix_file_extensions() {
assert_eq!(name_fix("Document.PDF"), "document.pdf");
assert_eq!(name_fix("Image.JPEG"), "image.jpeg");
assert_eq!(name_fix("Archive.tar.gz"), "archive.tar.gz");
assert_eq!(name_fix("Config.json"), "config.json");
}
#[test]
fn test_name_fix_empty_and_edge_cases() {
assert_eq!(name_fix(""), "");
assert_eq!(name_fix(" "), "_");
assert_eq!(name_fix("!!!"), "_");
assert_eq!(name_fix("___"), "___");
}
#[test]
fn test_name_fix_real_world_examples() {
assert_eq!(
name_fix("User's Report [Draft 1].md"),
"user_s_report_draft_1_.md"
);
assert_eq!(
name_fix("Meeting Notes (2023-12-01).txt"),
"meeting_notes_2023_12_01_.txt"
);
assert_eq!(
name_fix("Photo #123 - Vacation!.jpg"),
"photo_123_vacation_.jpg"
);
assert_eq!(
name_fix("Project Plan v2.0 FINAL.docx"),
"project_plan_v2.0_final.docx"
);
}
#[test]
fn test_path_fix_directory_paths() {
assert_eq!(path_fix("/path/to/directory/"), "/path/to/directory/");
assert_eq!(path_fix("./relative/path/"), "./relative/path/");
assert_eq!(path_fix("../parent/path/"), "../parent/path/");
}
#[test]
fn test_path_fix_single_filename() {
assert_eq!(path_fix("filename.txt"), "filename.txt");
assert_eq!(path_fix("UPPER-file.md"), "upper_file.md");
assert_eq!(path_fix("Special!File.pdf"), "special_file.pdf");
}
#[test]
fn test_path_fix_absolute_paths() {
assert_eq!(path_fix("/path/to/File Name.txt"), "/path/to/file_name.txt");
assert_eq!(
path_fix("/absolute/path/to/DOCUMENT-123.pdf"),
"/absolute/path/to/document_123.pdf"
);
assert_eq!(path_fix("/home/user/Résumé.doc"), "/home/user/rsum.doc");
}
#[test]
fn test_path_fix_relative_paths() {
assert_eq!(
path_fix("./relative/path/to/Document.PDF"),
"./relative/path/to/document.pdf"
);
assert_eq!(
path_fix("../parent/Special File.txt"),
"../parent/special_file.txt"
);
assert_eq!(
path_fix("subfolder/User's File.md"),
"subfolder/user_s_file.md"
);
}
#[test]
fn test_path_fix_special_characters_in_filename() {
assert_eq!(
path_fix("/path/with/[special]<chars>.txt"),
"/path/with/_special_chars_.txt"
);
assert_eq!(path_fix("./folder/File!@#.pdf"), "./folder/file_.pdf");
assert_eq!(
path_fix("/data/Report (Final).docx"),
"/data/report_final_.docx"
);
}
#[test]
fn test_path_fix_preserves_path_structure() {
assert_eq!(
path_fix("/very/long/path/to/some/Deep File.txt"),
"/very/long/path/to/some/deep_file.txt"
);
assert_eq!(
path_fix("./a/b/c/d/e/Final Document.pdf"),
"./a/b/c/d/e/final_document.pdf"
);
}
#[test]
fn test_path_fix_windows_style_paths() {
// Note: These tests assume Unix-style path handling
// In a real implementation, you might want to handle Windows paths differently
assert_eq!(
path_fix("C:\\Users\\Name\\Document.txt"),
"c:\\users\\name\\document.txt"
);
}
#[test]
fn test_path_fix_edge_cases() {
assert_eq!(path_fix(""), "");
assert_eq!(path_fix("/"), "/");
assert_eq!(path_fix("./"), "./");
assert_eq!(path_fix("../"), "../");
}
#[test]
fn test_path_fix_unicode_in_filename() {
assert_eq!(path_fix("/path/to/Café.txt"), "/path/to/caf.txt");
assert_eq!(
path_fix("./folder/Naïve Document.pdf"),
"./folder/nave_document.pdf"
);
assert_eq!(
path_fix("/home/user/Piñata Party.jpg"),
"/home/user/piata_party.jpg"
);
}
#[test]
fn test_path_fix_complex_real_world_examples() {
assert_eq!(
path_fix("/Users/john/Documents/Project Files/Final Report (v2.1) [APPROVED].docx"),
"/Users/john/Documents/Project Files/final_report_v2.1_approved_.docx"
);
assert_eq!(
path_fix("./assets/images/Photo #123 - Vacation! (2023).jpg"),
"./assets/images/photo_123_vacation_2023_.jpg"
);
assert_eq!(
path_fix("/var/log/Application Logs/Error Log [2023-12-01].txt"),
"/var/log/Application Logs/error_log_2023_12_01_.txt"
);
}
#[test]
fn test_name_fix_and_path_fix_consistency() {
let filename = "User's Report [Draft].txt";
let path = "/path/to/User's Report [Draft].txt";
let fixed_name = name_fix(filename);
let fixed_path = path_fix(path);
// The filename part should be the same in both cases
assert!(fixed_path.ends_with(&fixed_name));
assert_eq!(fixed_name, "user_s_report_draft_.txt");
assert_eq!(fixed_path, "/path/to/user_s_report_draft_.txt");
}
#[test]
fn test_normalization_preserves_dots_in_extensions() {
assert_eq!(name_fix("file.tar.gz"), "file.tar.gz");
assert_eq!(name_fix("backup.2023.12.01.sql"), "backup.2023.12.01.sql");
assert_eq!(
path_fix("/path/to/archive.tar.bz2"),
"/path/to/archive.tar.bz2"
);
}