225 lines
4.7 KiB
Go
225 lines
4.7 KiB
Go
|
|
package lang
|
||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestParseLanguageCode(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
input string
|
||
|
|
expected string
|
||
|
|
}{
|
||
|
|
// 中文变体
|
||
|
|
{"cn", "zh-CN"},
|
||
|
|
{"zh", "zh-CN"},
|
||
|
|
{"zh-CN", "zh-CN"},
|
||
|
|
{"zh-TW", "zh-TW"},
|
||
|
|
{"zh-HK", "zh-HK"},
|
||
|
|
{"chinese", "zh-CN"},
|
||
|
|
{"简体中文", "zh-CN"},
|
||
|
|
|
||
|
|
// 英语变体
|
||
|
|
{"en", "en-US"},
|
||
|
|
{"en-US", "en-US"},
|
||
|
|
{"en-GB", "en-GB"},
|
||
|
|
{"us", "en-US"},
|
||
|
|
{"uk", "en-GB"},
|
||
|
|
{"english", "en-US"},
|
||
|
|
|
||
|
|
// 其他语言
|
||
|
|
{"jp", "ja"},
|
||
|
|
{"ja", "ja"},
|
||
|
|
{"japanese", "ja"},
|
||
|
|
{"kr", "ko"},
|
||
|
|
{"ko", "ko"},
|
||
|
|
{"korean", "ko"},
|
||
|
|
{"es", "es-ES"},
|
||
|
|
{"spanish", "es-ES"},
|
||
|
|
{"fr", "fr-FR"},
|
||
|
|
{"french", "fr-FR"},
|
||
|
|
{"de", "de-DE"},
|
||
|
|
{"german", "de-DE"},
|
||
|
|
|
||
|
|
// 空值
|
||
|
|
{"", ""},
|
||
|
|
|
||
|
|
// 未知语言(应返回原始输入)
|
||
|
|
{"unknown", "unknown"},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.input, func(t *testing.T) {
|
||
|
|
result := ParseLanguageCode(tt.input)
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("ParseLanguageCode(%q) = %q, 期望 %q", tt.input, result, tt.expected)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestGetLanguageName(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
code string
|
||
|
|
expected string
|
||
|
|
}{
|
||
|
|
{"zh-CN", "中文(简体)"},
|
||
|
|
{"zh-TW", "中文(繁体)"},
|
||
|
|
{"en-US", "English (US)"},
|
||
|
|
{"en-GB", "English (UK)"},
|
||
|
|
{"ja", "日本語"},
|
||
|
|
{"ko", "한국어"},
|
||
|
|
{"es-ES", "Español"},
|
||
|
|
{"fr-FR", "Français"},
|
||
|
|
{"de-DE", "Deutsch"},
|
||
|
|
{"unknown", "unknown"}, // 未知代码返回原值
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.code, func(t *testing.T) {
|
||
|
|
result := GetLanguageName(tt.code)
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("GetLanguageName(%q) = %q, 期望 %q", tt.code, result, tt.expected)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestSupportedLanguages(t *testing.T) {
|
||
|
|
languages := SupportedLanguages()
|
||
|
|
if len(languages) == 0 {
|
||
|
|
t.Error("SupportedLanguages() 不应返回空列表")
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查一些关键语言是否在列表中
|
||
|
|
expectedLanguages := []string{"zh-CN", "en-US", "ja", "ko"}
|
||
|
|
for _, expected := range expectedLanguages {
|
||
|
|
found := false
|
||
|
|
for _, lang := range languages {
|
||
|
|
if lang == expected {
|
||
|
|
found = true
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if !found {
|
||
|
|
t.Errorf("Expected language %q not found in supported languages", expected)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestGetLanguageSuggestions(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
input string
|
||
|
|
limit int
|
||
|
|
minCount int
|
||
|
|
}{
|
||
|
|
{"zh", 5, 1},
|
||
|
|
{"en", 5, 1},
|
||
|
|
{"chinese", 5, 1},
|
||
|
|
{"", 5, 0},
|
||
|
|
{"unknown", 5, 0},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.input, func(t *testing.T) {
|
||
|
|
suggestions := GetLanguageSuggestions(tt.input, tt.limit)
|
||
|
|
if len(suggestions) < tt.minCount {
|
||
|
|
t.Errorf("GetLanguageSuggestions(%q, %d) 返回 %d 个建议,至少需要 %d 个",
|
||
|
|
tt.input, tt.limit, len(suggestions), tt.minCount)
|
||
|
|
}
|
||
|
|
if len(suggestions) > tt.limit {
|
||
|
|
t.Errorf("GetLanguageSuggestions(%q, %d) 返回 %d 个建议,超过限制 %d 个",
|
||
|
|
tt.input, tt.limit, len(suggestions), tt.limit)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestIsLanguageSupported(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
code string
|
||
|
|
expected bool
|
||
|
|
}{
|
||
|
|
{"zh-CN", true},
|
||
|
|
{"en-US", true},
|
||
|
|
{"ja", true},
|
||
|
|
{"unknown", false},
|
||
|
|
{"", false},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.code, func(t *testing.T) {
|
||
|
|
result := IsLanguageSupported(tt.code)
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("IsLanguageSupported(%q) = %v, 期望 %v", tt.code, result, tt.expected)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestGetCommonLanguages(t *testing.T) {
|
||
|
|
languages := GetCommonLanguages()
|
||
|
|
if len(languages) == 0 {
|
||
|
|
t.Error("GetCommonLanguages() 不应返回空列表")
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查一些关键语言是否在列表中
|
||
|
|
expectedLanguages := []string{"zh-CN", "en-US", "ja"}
|
||
|
|
for _, expected := range expectedLanguages {
|
||
|
|
found := false
|
||
|
|
for _, lang := range languages {
|
||
|
|
if lang == expected {
|
||
|
|
found = true
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if !found {
|
||
|
|
t.Errorf("Expected language %q not found in common languages", expected)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestGetLanguageDirection(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
code string
|
||
|
|
expected string
|
||
|
|
}{
|
||
|
|
{"zh-CN", "ltr"},
|
||
|
|
{"en-US", "ltr"},
|
||
|
|
{"ja", "ltr"},
|
||
|
|
{"ar-SA", "rtl"},
|
||
|
|
{"he-IL", "rtl"},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.code, func(t *testing.T) {
|
||
|
|
result := GetLanguageDirection(tt.code)
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("GetLanguageDirection(%q) = %q, 期望 %q", tt.code, result, tt.expected)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNormalizeLanguageTag(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
input string
|
||
|
|
expected string
|
||
|
|
}{
|
||
|
|
{"zh", "zh-CN"},
|
||
|
|
{"en", "en-US"},
|
||
|
|
{"ja", "ja-JP"},
|
||
|
|
{"zh-CN", "zh-CN"},
|
||
|
|
{"zh-tw", "zh-TW"},
|
||
|
|
{"EN-us", "en-US"},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.input, func(t *testing.T) {
|
||
|
|
result := normalizeLanguageTag(tt.input)
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("normalizeLanguageTag(%q) = %q, 期望 %q", tt.input, result, tt.expected)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|