#[cfg(test)]
mod core_edge_case_tests {
use sdforge::core::{
error::ErrorContext,
str::{
format_empty_error, format_env_key, format_invalid_error, format_not_found,
format_range_error, format_validation_error, sanitize_for_identifier,
truncate_with_ellipsis,
},
};
#[test]
fn test_format_env_key_with_empty_strings() {
let result = format_env_key("", "");
assert_eq!(result, "_");
let result = format_env_key("", "KEY");
assert_eq!(result, "_KEY");
let result = format_env_key("PREFIX", "");
assert_eq!(result, "PREFIX_");
}
#[test]
fn test_format_env_key_with_long_strings() {
let long_prefix = "A".repeat(1000);
let long_key = "B".repeat(1000);
let result = format_env_key(&long_prefix, &long_key);
assert_eq!(result.len(), 2001); assert!(result.starts_with("AAA"));
assert!(result.ends_with("BBB"));
}
#[test]
fn test_format_env_key_with_unicode() {
let result = format_env_key("前缀", "键");
assert_eq!(result, "前缀_键");
let result = format_env_key("🚀", "TEST");
assert_eq!(result, "🚀_TEST");
}
#[test]
fn test_format_env_key_with_special_chars() {
let result = format_env_key("PRE-FIX", "KEY@123");
assert_eq!(result, "PRE-FIX_KEY@123");
}
#[test]
fn test_format_not_found_edge_cases() {
let result = format_not_found("");
assert_eq!(result, "Resource not found: ");
let long_name = "User".repeat(100);
let result = format_not_found(&long_name);
assert!(result.contains(&long_name));
let result = format_not_found("用户");
assert_eq!(result, "Resource not found: 用户");
}
#[test]
fn test_format_validation_error_edge_cases() {
let result = format_validation_error("", "");
assert_eq!(result, "Validation failed for : ");
let long_field = "field_name".repeat(50);
let result = format_validation_error(&long_field, "required");
assert!(result.contains(&long_field));
let result = format_validation_error("字段", "必须填写");
assert!(result.contains("字段"));
assert!(result.contains("必须填写"));
}
#[test]
fn test_format_empty_error_edge_cases() {
let result = format_empty_error("");
assert_eq!(result, " cannot be empty");
let long_field = "password".repeat(20);
let result = format_empty_error(&long_field);
assert!(result.contains(&long_field));
let result = format_empty_error("密码");
assert_eq!(result, "密码 cannot be empty");
}
#[test]
fn test_format_invalid_error_edge_cases() {
let result = format_invalid_error("", "");
assert_eq!(result, " has invalid format: ");
let field = "email_address".repeat(10);
let format = "RFC 5322 compliant email address with extended validation";
let result = format_invalid_error(&field, format);
assert!(result.contains(&field));
assert!(result.contains(format));
}
#[test]
fn test_format_range_error_edge_cases() {
let result = format_range_error("age", 0, 0);
assert_eq!(result, "age must be between 0 and 0");
let result = format_range_error("score", 0, u64::MAX);
assert!(result.contains(&u64::MAX.to_string()));
let result = format_range_error("exact", 42, 42);
assert_eq!(result, "exact must be between 42 and 42");
}
#[test]
fn test_sanitize_for_identifier_comprehensive() {
assert_eq!(sanitize_for_identifier("valid_name"), "valid_name");
assert_eq!(sanitize_for_identifier("_underscore_"), "_underscore_");
assert_eq!(sanitize_for_identifier("with123numbers"), "with123numbers");
assert_eq!(sanitize_for_identifier("hello world"), "hello_world");
assert_eq!(sanitize_for_identifier("test@case"), "test_case");
assert_eq!(sanitize_for_identifier("dash-name"), "dash_name");
assert_eq!(sanitize_for_identifier("a!@#$%b"), "a_____b");
assert_eq!(sanitize_for_identifier("中文"), "中文");
assert_eq!(sanitize_for_identifier("français"), "français");
assert_eq!(sanitize_for_identifier(""), "");
assert_eq!(sanitize_for_identifier("!@#$%"), "_____");
}
#[test]
fn test_truncate_with_ellipsis_boundaries() {
assert_eq!(truncate_with_ellipsis("hello", 5), "hello");
assert_eq!(truncate_with_ellipsis("hello!", 5), "he...");
assert_eq!(truncate_with_ellipsis("hello world", 5), "he...");
assert_eq!(truncate_with_ellipsis("", 5), "");
assert_eq!(truncate_with_ellipsis("hello", 0), "");
assert_eq!(truncate_with_ellipsis("hello", 1), ".");
assert_eq!(truncate_with_ellipsis("hello", 2), "..");
assert_eq!(truncate_with_ellipsis("hello", 3), "...");
assert_eq!(truncate_with_ellipsis("hello", 4), "h...");
}
#[test]
fn test_truncate_with_ellipsis_unicode() {
let result = truncate_with_ellipsis("你好世界", 5);
assert!(result.len() >= 5);
assert!(result.ends_with("..."));
let result = truncate_with_ellipsis("🚀🌟💫⭐", 5);
assert!(result.len() >= 5);
assert!(result.ends_with("..."));
let result = truncate_with_ellipsis("hello 世界", 8);
assert!(result.len() >= 8);
}
#[test]
fn test_truncate_with_ellipsis_very_long() {
let long_string = "a".repeat(10000);
let result = truncate_with_ellipsis(&long_string, 100);
assert_eq!(result.len(), 100);
assert!(result.ends_with("..."));
assert!(result.starts_with("aaa"));
}
#[test]
fn test_error_context_extensive_extra_data() {
let mut ctx = ErrorContext::new();
for i in 0..100 {
ctx = ctx.with_extra(format!("key_{}", i), format!("value_{}", i));
}
assert_eq!(ctx.extra.len(), 100);
assert_eq!(ctx.extra.get("key_50"), Some(&"value_50".to_string()));
}
#[test]
fn test_error_context_duplicate_keys() {
let ctx = ErrorContext::new()
.with_extra("key".to_string(), "value1".to_string())
.with_extra("key".to_string(), "value2".to_string());
assert_eq!(ctx.extra.get("key"), Some(&"value2".to_string()));
}
#[test]
fn test_error_context_empty_values() {
let ctx = ErrorContext::new()
.with_extra("".to_string(), "".to_string())
.with_extra("key".to_string(), "".to_string());
assert_eq!(ctx.extra.get(""), Some(&"".to_string()));
assert_eq!(ctx.extra.get("key"), Some(&"".to_string()));
}
#[test]
fn test_real_world_error_composition() {
let field = "user_email_address";
let constraint = "must be valid RFC 5322 email";
let error_msg = format_validation_error(field, constraint);
assert!(error_msg.contains(field));
assert!(error_msg.contains(constraint));
let ctx = ErrorContext::new()
.with_extra("field".to_string(), field.to_string())
.with_extra("constraint".to_string(), constraint.to_string())
.with_extra("user_id".to_string(), "user_123".to_string());
assert_eq!(ctx.extra.len(), 3);
}
#[test]
fn test_identifier_sanitization_pipeline() {
let test_cases = vec![
("Hello World!", "Hello_World_"),
("user-name@example.com", "user_name_example_com"),
("Product #123", "Product__123"),
("Café résumé", "Café_résumé"),
("日本語テスト", "日本語テスト"),
];
for (input, expected) in test_cases {
let sanitized = sanitize_for_identifier(input);
assert_eq!(sanitized, expected, "Failed for input: {}", input);
}
}
#[test]
fn test_truncation_in_error_context() {
let very_long_value = "x".repeat(1000);
let truncated = truncate_with_ellipsis(&very_long_value, 50);
assert_eq!(truncated.len(), 50);
assert!(truncated.ends_with("..."));
let ctx = ErrorContext::new().with_extra("long_value".to_string(), truncated.clone());
assert_eq!(ctx.extra.get("long_value"), Some(&truncated));
}
}