2026-04-27 04:07:28 +03:00
|
|
|
package core
|
|
|
|
|
|
2026-04-30 23:32:39 +03:00
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
2026-04-27 04:07:28 +03:00
|
|
|
|
|
|
|
|
func TestParseLocale(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
in string
|
|
|
|
|
wantLang string
|
|
|
|
|
wantCC string
|
|
|
|
|
}{
|
|
|
|
|
{"empty", "", "", ""},
|
|
|
|
|
{"whitespace only", " ", "", ""},
|
|
|
|
|
{"language only", "EN", "en", ""},
|
|
|
|
|
{"language with region dash", "en-US", "en", "US"},
|
|
|
|
|
{"language with region underscore", "de_AT", "de", "AT"},
|
|
|
|
|
{"mixed casing", "Pt-bR", "pt", "BR"},
|
|
|
|
|
{"trailing whitespace", " fr-CA ", "fr", "CA"},
|
|
|
|
|
{"empty region after dash", "ru-", "ru", ""},
|
|
|
|
|
{"language only after split", "-US", "", ""},
|
|
|
|
|
{"extra subtags ignored", "en-US-x-private", "en", "US"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
got := ParseLocale(tt.in)
|
|
|
|
|
if got.Language != tt.wantLang || got.Country != tt.wantCC {
|
|
|
|
|
t.Fatalf("ParseLocale(%q) = {%q, %q}, want {%q, %q}",
|
|
|
|
|
tt.in, got.Language, got.Country, tt.wantLang, tt.wantCC)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-27 04:28:53 +03:00
|
|
|
|
2026-05-21 20:45:10 +03:00
|
|
|
func TestCountryFromRegion(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
in string
|
|
|
|
|
want string
|
|
|
|
|
}{
|
|
|
|
|
{name: "empty", in: "", want: ""},
|
|
|
|
|
{name: "country lowercase", in: "ru", want: "RU"},
|
|
|
|
|
{name: "country uppercase", in: "US", want: "US"},
|
|
|
|
|
{name: "locale dash", in: "en-GB", want: "GB"},
|
|
|
|
|
{name: "locale underscore", in: "de_AT", want: "AT"},
|
|
|
|
|
{name: "numeric engine region is not country", in: "213", want: ""},
|
|
|
|
|
{name: "unknown shape", in: "moscow", want: ""},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
if got := CountryFromRegion(tt.in); got != tt.want {
|
|
|
|
|
t.Fatalf("CountryFromRegion(%q) = %q, want %q", tt.in, got, tt.want)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 04:28:53 +03:00
|
|
|
func TestBuildAcceptLanguageHeader(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
in string
|
|
|
|
|
want string
|
|
|
|
|
}{
|
|
|
|
|
{name: "empty", in: "", want: ""},
|
|
|
|
|
{name: "language only with default country", in: "de", want: "de-DE,de;q=0.9"},
|
|
|
|
|
{name: "language only with mapped country", in: "pt", want: "pt-BR,pt;q=0.9"},
|
|
|
|
|
{name: "explicit region", in: "en-GB", want: "en-GB,en;q=0.9"},
|
|
|
|
|
{name: "unknown language emits bare tag", in: "sw", want: "sw"},
|
|
|
|
|
{name: "invalid locale", in: "-US", want: ""},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
got := BuildAcceptLanguageHeader(tt.in)
|
|
|
|
|
if got != tt.want {
|
|
|
|
|
t.Fatalf("BuildAcceptLanguageHeader(%q) = %q, want %q", tt.in, got, tt.want)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-30 23:32:39 +03:00
|
|
|
|
|
|
|
|
func TestTimezoneForLocale(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
in string
|
|
|
|
|
want string
|
|
|
|
|
}{
|
|
|
|
|
{name: "de-DE explicit country", in: "de-DE", want: "Europe/Berlin"},
|
|
|
|
|
{name: "pt-BR explicit country", in: "pt-BR", want: "America/Sao_Paulo"},
|
|
|
|
|
{name: "bare de via default country", in: "de", want: "Europe/Berlin"},
|
|
|
|
|
{name: "unknown locale", in: "xx-YY", want: ""},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
got := TimezoneForLocale(ParseLocale(tt.in))
|
|
|
|
|
if got != tt.want {
|
|
|
|
|
t.Fatalf("TimezoneForLocale(%q) = %q, want %q", tt.in, got, tt.want)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|