diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-06-17 01:05:24 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-06-17 01:05:24 +0800 |
| commit | 1957ba706f62a82a9f60df1f28e23de5fa8bb319 (patch) | |
| tree | fae07ade0aba30a1d0ad66344e4d1f61116f2074 /shakehand-test/src | |
| parent | c9685671512b05ec9b4caba2b7079382120cce1c (diff) | |
Implement fallback chain resolution for translation keys
Diffstat (limited to 'shakehand-test/src')
| -rw-r--r-- | shakehand-test/src/lib.rs | 80 |
1 files changed, 79 insertions, 1 deletions
diff --git a/shakehand-test/src/lib.rs b/shakehand-test/src/lib.rs index c290533..8c60441 100644 --- a/shakehand-test/src/lib.rs +++ b/shakehand-test/src/lib.rs @@ -1,9 +1,18 @@ #[cfg(test)] mod test { use crate::locale::{Global, Languages, set_lang}; + use std::sync::{Mutex, MutexGuard}; + + /// Global lock to serialize tests that mutate `__SHAKE_HAND_LANG`. + /// Without this, parallel test execution causes races on the shared global atomic state. + static LANG_LOCK: Mutex<()> = Mutex::new(()); + fn lock_lang() -> MutexGuard<'static, ()> { + LANG_LOCK.lock().unwrap() + } #[test] fn test_english() { + let _lock = lock_lang(); set_lang(Languages::en); assert_eq!(Global::world(), "world"); assert_eq!(Global::greeting("Alice"), "Hello, Alice!"); @@ -13,6 +22,7 @@ mod test { #[test] fn test_chinese() { + let _lock = lock_lang(); set_lang(Languages::zh_CN); assert_eq!(Global::world(), "世界"); assert_eq!(Global::greeting("小明"), "你好,小明!"); @@ -22,6 +32,7 @@ mod test { #[test] fn test_japanese() { + let _lock = lock_lang(); set_lang(Languages::ja); assert_eq!(Global::world(), "世界"); assert_eq!(Global::greeting("田中"), "こんにちは、田中!"); @@ -31,6 +42,7 @@ mod test { #[test] fn test_korean() { + let _lock = lock_lang(); set_lang(Languages::ko); assert_eq!(Global::world(), "세계"); assert_eq!(Global::greeting("철수"), "안녕하세요, 철수님!"); @@ -40,6 +52,7 @@ mod test { #[test] fn test_french() { + let _lock = lock_lang(); set_lang(Languages::fr); assert_eq!(Global::world(), "monde"); assert_eq!(Global::greeting("Marie"), "Bonjour, Marie !"); @@ -49,6 +62,7 @@ mod test { #[test] fn test_german() { + let _lock = lock_lang(); set_lang(Languages::de); assert_eq!(Global::world(), "Welt"); assert_eq!(Global::greeting("Hans"), "Hallo, Hans!"); @@ -58,6 +72,7 @@ mod test { #[test] fn test_spanish() { + let _lock = lock_lang(); set_lang(Languages::es); assert_eq!(Global::world(), "mundo"); assert_eq!(Global::greeting("Carlos"), "¡Hola, Carlos!"); @@ -67,6 +82,7 @@ mod test { #[test] fn test_russian() { + let _lock = lock_lang(); set_lang(Languages::ru); assert_eq!(Global::world(), "мир"); assert_eq!(Global::greeting("Анна"), "Привет, Анна!"); @@ -76,6 +92,7 @@ mod test { #[test] fn test_arabic() { + let _lock = lock_lang(); set_lang(Languages::ar); assert_eq!(Global::world(), "عالم"); assert_eq!(Global::greeting("أحمد"), "مرحبًا، أحمد!"); @@ -85,6 +102,7 @@ mod test { #[test] fn test_portuguese() { + let _lock = lock_lang(); set_lang(Languages::pt); assert_eq!(Global::world(), "mundo"); assert_eq!(Global::greeting("João"), "Olá, João!"); @@ -93,7 +111,67 @@ mod test { } #[test] - fn test_parameterless_translation_as_param() { + fn test_fallback_zh_hk_to_zh_cn() { + let _lock = lock_lang(); + set_lang(Languages::zh_HK); + assert_eq!(Global::world(), "世界"); + assert_eq!(Global::greeting("小明"), "你好,小明!"); + assert_eq!(Global::farewell("小红"), "再见,小红!"); + assert_eq!(Global::thanks(), "谢谢!"); + } + + #[test] + fn test_fallback_zh_tw_to_zh_cn() { + let _lock = lock_lang(); + set_lang(Languages::zh_TW); + assert_eq!(Global::world(), "世界"); + assert_eq!(Global::greeting("小明"), "你好,小明!"); + } + + #[test] + fn test_fallback_vi_to_zh_cn() { + let _lock = lock_lang(); + set_lang(Languages::vi); + assert_eq!(Global::world(), "世界"); + assert_eq!(Global::greeting("小明"), "你好,小明!"); + } + + #[test] + fn test_fallback_ms_to_id_to_en() { + let _lock = lock_lang(); + set_lang(Languages::ms); + assert_eq!(Global::world(), "world"); + assert_eq!(Global::greeting("Alice"), "Hello, Alice!"); + } + + #[test] + fn test_fallback_it_to_fr() { + let _lock = lock_lang(); + set_lang(Languages::it); + assert_eq!(Global::world(), "monde"); + assert_eq!(Global::greeting("Marie"), "Bonjour, Marie !"); + } + + #[test] + fn test_fallback_pt_to_es() { + let _lock = lock_lang(); + // pt has direct translations, so fallback should not be triggered + set_lang(Languages::pt); + assert_eq!(Global::world(), "mundo"); + assert_eq!(Global::greeting("João"), "Olá, João!"); + } + + #[test] + fn test_fallback_tr_to_ar() { + let _lock = lock_lang(); + set_lang(Languages::tr); + assert_eq!(Global::world(), "عالم"); + assert_eq!(Global::greeting("أحمد"), "مرحبًا، أحمد!"); + } + + #[test] + fn test_fallback_parameterless_translation_as_param() { + let _lock = lock_lang(); set_lang(Languages::en); let greeting = Global::greeting(Global::world()); assert_eq!(greeting, "Hello, world!"); |
