go-html/context_test.go
Virgil afa0337bbd
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run
feat(html): add context locale setter
Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-03 16:48:59 +00:00

72 lines
1.7 KiB
Go

// SPDX-Licence-Identifier: EUPL-1.2
package html
import "testing"
type localeTranslator struct {
language string
}
func (t *localeTranslator) T(key string, args ...any) string {
if key == "prompt.yes" && t.language == "fr" {
return "o"
}
if key == "prompt.yes" && t.language == "en" {
return "y"
}
return key
}
func (t *localeTranslator) SetLanguage(language string) error {
t.language = language
return nil
}
func TestContext_SetService_AppliesLocale(t *testing.T) {
svc := &localeTranslator{}
ctx := NewContext("fr-FR")
if got := ctx.SetService(svc); got != ctx {
t.Fatal("SetService should return the same context for chaining")
}
if svc.language != "fr" {
t.Fatalf("SetService should apply locale to translator, got %q", svc.language)
}
if got := Text("prompt.yes").Render(ctx); got != "o" {
t.Fatalf("SetService locale translation = %q, want %q", got, "o")
}
}
func TestContext_SetLocale_AppliesLocale(t *testing.T) {
svc := &localeTranslator{}
ctx := NewContextWithService(svc)
if got := ctx.SetLocale("en-GB"); got != ctx {
t.Fatal("SetLocale should return the same context for chaining")
}
if svc.language != "en" {
t.Fatalf("SetLocale should apply locale to translator, got %q", svc.language)
}
if got := Text("prompt.yes").Render(ctx); got != "y" {
t.Fatalf("SetLocale translation = %q, want %q", got, "y")
}
}
func TestContext_SetService_NilContext(t *testing.T) {
var ctx *Context
if got := ctx.SetService(nil); got != nil {
t.Fatal("SetService on nil context should return nil")
}
}
func TestContext_SetLocale_NilContext(t *testing.T) {
var ctx *Context
if got := ctx.SetLocale("en-GB"); got != nil {
t.Fatal("SetLocale on nil context should return nil")
}
}