* Implement FCrDNS and other DNS features * Redesign DNS cache and methods * Fix DNS cache * Rename regexSafe arg * Alter verifyFCrDNS(addr) behaviour * Remove unused dnsCache field from Server struct * Upd expressions docs * Update docs/docs/CHANGELOG.md Signed-off-by: Xe Iaso <me@xeiaso.net> * refactor(dns): simplify FCrDNS logging * docs: clarify verifyFCrDNS behavior Add a note to the documentation for `verifyFCrDNS` to clarify that it returns true when no PTR records are found for the given IP address. * fix(dns): Improve FCrDNS error handling and tests The `VerifyFCrDNS` function previously ignored errors returned from reverse DNS lookups. This could lead to incorrect passes when a DNS failure (other than a simple 'not found') occurred. This change ensures that any error from a reverse lookup will cause the FCrDNS check to fail. The test suite for FCrDNS has been updated to reflect this change. The mock DNS lookups now simulate both 'not found' errors and other generic DNS errors. The test cases have been updated to ensure that the function behaves correctly in both scenarios, resolving a situation where two test cases were effectively duplicates. * docs: Update FCrDNS documentation and spelling Corrected a typo in the `verifyFCrDNS` function documentation. Additionally, updated the spelling exception list to include new terms and remove redundant entries. * chore: update spelling Signed-off-by: Xe Iaso <me@xeiaso.net> --------- Signed-off-by: Xe Iaso <me@xeiaso.net> Co-authored-by: Xe Iaso <me@xeiaso.net>
139 lines
3.1 KiB
Go
139 lines
3.1 KiB
Go
package localization
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
)
|
|
|
|
func TestLocalizationService(t *testing.T) {
|
|
service := NewLocalizationService()
|
|
|
|
loadingStrMap := map[string]string{
|
|
"de": "Ladevorgang...",
|
|
"en": "Loading...",
|
|
"es": "Cargando...",
|
|
"et": "Laadin...",
|
|
"fil": "Naglo-load...",
|
|
"fr": "Chargement...",
|
|
"ja": "ロード中...",
|
|
"is": "Hleður...",
|
|
"nb": "Laster inn...",
|
|
"nl": "Laden...",
|
|
"nn": "Lastar inn...",
|
|
"pt-BR": "Carregando...",
|
|
"tr": "Yükleniyor...",
|
|
"ru": "Загрузка...",
|
|
"uk": "Завантаження...",
|
|
"vi": "Đang nạp...",
|
|
"zh-CN": "加载中...",
|
|
"zh-TW": "載入中...",
|
|
"sv": "Laddar...",
|
|
}
|
|
|
|
var keys []string
|
|
|
|
for lang := range loadingStrMap {
|
|
keys = append(keys, lang)
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, lang := range keys {
|
|
expected := loadingStrMap[lang]
|
|
t.Run(fmt.Sprintf("%s localization", lang), func(t *testing.T) {
|
|
localizer := service.GetLocalizer(lang)
|
|
result := localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "loading"})
|
|
if result != expected {
|
|
t.Errorf("Expected '%s', got '%s'", expected, result)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Test for requiredKeys localization
|
|
requiredKeys := []string{
|
|
"loading", "why_am_i_seeing", "protected_by", "protected_from", "made_with",
|
|
"mascot_design", "try_again", "go_home", "javascript_required",
|
|
}
|
|
|
|
for _, lang := range keys {
|
|
t.Run(fmt.Sprintf("All required keys exist in %s", lang), func(t *testing.T) {
|
|
loc := service.GetLocalizer(lang)
|
|
for _, key := range requiredKeys {
|
|
result := loc.MustLocalize(&i18n.LocalizeConfig{MessageID: key})
|
|
if result == "" {
|
|
t.Errorf("Key '%s' returned empty string", key)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type manifest struct {
|
|
SupportedLanguages []string `json:"supportedLanguages"`
|
|
}
|
|
|
|
func loadManifest(t *testing.T) manifest {
|
|
t.Helper()
|
|
|
|
fin, err := localeFS.Open("locales/manifest.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer fin.Close()
|
|
|
|
var result manifest
|
|
if err := json.NewDecoder(fin).Decode(&result); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func TestComprehensiveTranslations(t *testing.T) {
|
|
service := NewLocalizationService()
|
|
|
|
var translations = map[string]any{}
|
|
fin, err := localeFS.Open("locales/en.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer fin.Close()
|
|
|
|
if err := json.NewDecoder(fin).Decode(&translations); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var keys []string
|
|
for k := range translations {
|
|
keys = append(keys, k)
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
manifest := loadManifest(t)
|
|
if len(manifest.SupportedLanguages) == 0 {
|
|
t.Fatal("no languages loaded")
|
|
}
|
|
|
|
for _, lang := range loadManifest(t).SupportedLanguages {
|
|
t.Run(lang, func(t *testing.T) {
|
|
loc := service.GetLocalizer(lang)
|
|
sl := SimpleLocalizer{Localizer: loc}
|
|
service_lang := sl.GetLang()
|
|
if service_lang != lang {
|
|
t.Error("Localizer language not same as specified")
|
|
}
|
|
for _, key := range keys {
|
|
t.Run(key, func(t *testing.T) {
|
|
if result := sl.T(key); result == "" {
|
|
t.Error("key not defined")
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|