use ada_idna::{punycode_to_utf32, utf8_to_utf32, utf32_to_punycode, utf32_to_utf8};
#[test]
fn test_utf8_punycode_roundtrip() {
let test_cases = vec![
("a", "a-"),
("A", "A-"),
("London", "London-"),
("ä", "4ca"),
("ü", "tda"),
("ñ", "ida"),
];
for (utf8_input, expected_punycode) in test_cases {
let utf32_chars = utf8_to_utf32(utf8_input.as_bytes());
assert!(
!utf32_chars.is_empty(),
"Failed to convert UTF-8 to UTF-32: {}",
utf8_input
);
let punycode_result = utf32_to_punycode(&utf32_chars);
if let Some(actual_punycode) = punycode_result {
assert_eq!(
actual_punycode, expected_punycode,
"Punycode mismatch for input: {}",
utf8_input
);
let roundtrip_utf32 = punycode_to_utf32(&actual_punycode);
if let Some(roundtrip_chars) = roundtrip_utf32 {
let utf8_buffer = utf32_to_utf8(&roundtrip_chars);
let roundtrip_utf8 = String::from_utf8(utf8_buffer).unwrap();
assert_eq!(
roundtrip_utf8, utf8_input,
"Roundtrip failed for input: {}",
utf8_input
);
}
}
}
}
#[test]
fn test_punycode_edge_cases() {
let empty_result = utf32_to_punycode(&[]);
assert!(empty_result.is_some());
let ascii_result = utf32_to_punycode(&[65, 66, 67]); assert!(ascii_result.is_some());
let invalid_punycode = punycode_to_utf32("xn--invalid");
assert!(invalid_punycode.is_none());
}
#[test]
fn test_specific_unicode_conversions() {
let test_cases = vec![
(vec![0x00E4], "4ca"), (vec![0x00FC], "tda"), (vec![0x00F1], "ida"), (vec![0x1F4A9], "ls8ca"), (vec![0x2603], "n3ha"), (vec![0x03B1, 0x03B2, 0x03B3], "mxacd"), ];
for (utf32_input, expected_punycode) in test_cases {
let result = utf32_to_punycode(&utf32_input);
if let Some(punycode) = result {
assert_eq!(punycode, expected_punycode);
let roundtrip = punycode_to_utf32(&punycode);
assert!(roundtrip.is_some());
assert_eq!(roundtrip.unwrap(), utf32_input);
}
}
}