| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package main
- import (
- "os"
- "path/filepath"
- "testing"
- )
- func writeLanguageFile(t *testing.T, content string) string {
- t.Helper()
- path := filepath.Join(t.TempDir(), "xx-XX.yaml")
- if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
- t.Fatal(err)
- }
- return path
- }
- func TestLoadLanguageFileRejectsEmptyFile(t *testing.T) {
- if _, err := loadLanguageFile(writeLanguageFile(t, "")); err == nil {
- t.Error("expected an error for an empty language file")
- }
- }
- func TestLoadLanguageFileRejectsWrongSchemaVersion(t *testing.T) {
- if _, err := loadLanguageFile(writeLanguageFile(t, "schemaVersion: 2\ntranslations:\n welcome: Hi\n")); err == nil {
- t.Error("expected an error for an unsupported schemaVersion")
- }
- }
- func TestLoadLanguageFileRejectsNonIntegerSchemaVersion(t *testing.T) {
- for _, schemaVersion := range []string{"1.5", "1.0", "\"1\"", "0x1", "[1]"} {
- content := "schemaVersion: " + schemaVersion + "\ntranslations:\n welcome: Hi\n"
- if _, err := loadLanguageFile(writeLanguageFile(t, content)); err == nil {
- t.Errorf("expected an error for schemaVersion %s", schemaVersion)
- }
- }
- }
- func TestLoadLanguageFileRejectsMissingSchemaVersion(t *testing.T) {
- if _, err := loadLanguageFile(writeLanguageFile(t, "translations:\n welcome: Hi\n")); err == nil {
- t.Error("expected an error for a missing schemaVersion")
- }
- }
- func TestLoadLanguageFileRejectsInvalidYaml(t *testing.T) {
- if _, err := loadLanguageFile(writeLanguageFile(t, "schemaVersion: 1\ntranslations: [\n")); err == nil {
- t.Error("expected an error for invalid YAML")
- }
- }
- func TestLoadLanguageFileAcceptsValidFile(t *testing.T) {
- translations, err := loadLanguageFile(writeLanguageFile(t, "schemaVersion: 1\ntranslations:\n welcome: Hi\n"))
- if err != nil {
- t.Fatal(err)
- }
- if translations["welcome"] != "Hi" {
- t.Errorf("unexpected translations: %v", translations)
- }
- }
- func TestValidateTranslationsAllowsMissingKeys(t *testing.T) {
- output := &CombinedTranslationsOutput{Messages: map[string]map[string]string{
- "en": {"welcome": "Hi", "docs": "Docs"},
- "xx-XX": {"welcome": "Salut"},
- }}
- if err := validateTranslations(output); err != nil {
- t.Errorf("missing keys should only warn, got %v", err)
- }
- }
- func TestValidateTranslationsRejectsExtraKeys(t *testing.T) {
- output := &CombinedTranslationsOutput{Messages: map[string]map[string]string{
- "en": {"welcome": "Hi"},
- "xx-XX": {"welcome": "Salut", "welcom": "Typo"},
- }}
- if err := validateTranslations(output); err == nil {
- t.Error("expected an error for keys not in 'en'")
- }
- }
- func TestValidateTranslationsRequiresEnglish(t *testing.T) {
- output := &CombinedTranslationsOutput{Messages: map[string]map[string]string{
- "xx-XX": {"welcome": "Salut"},
- }}
- if err := validateTranslations(output); err == nil {
- t.Error("expected an error when 'en' is missing")
- }
- }
|