feat(domain): Language, a closed set carried to the engines on the context (#61)
sv and auto; an empty form value is sv, anything else is refused. LanguageFrom defaults to sv so a caller that sets nothing behaves as before. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package domain_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"git.d-ma.be/mathias/tapir/internal/domain"
|
||||
)
|
||||
|
||||
func TestParseLanguage_KnownValuesAndEmptyAreAcceptedEverythingElseIsNot(t *testing.T) {
|
||||
tests := []struct {
|
||||
in string
|
||||
want domain.Language
|
||||
wantOK bool
|
||||
}{
|
||||
{"sv", domain.LangSwedish, true},
|
||||
{"auto", domain.LangAuto, true},
|
||||
{"", domain.LangSwedish, true}, // a client that sends no field keeps today's behaviour
|
||||
{"SV", "", false}, // exact match: the value reaches a request field
|
||||
{" sv", "", false},
|
||||
{"sv\n", "", false},
|
||||
{"en", "", false}, // a specific other language is not offered yet
|
||||
{"sv,en", "", false},
|
||||
{"../sv", "", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.in, func(t *testing.T) {
|
||||
got, ok := domain.ParseLanguage(tt.in)
|
||||
assert.Equal(t, tt.wantOK, ok)
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLanguageFrom_IsSwedishWhenTheContextCarriesNoneAndOtherwiseWhatWasSet(t *testing.T) {
|
||||
assert.Equal(t, domain.LangSwedish, domain.LanguageFrom(context.Background()))
|
||||
ctx := domain.WithLanguage(context.Background(), domain.LangAuto)
|
||||
assert.Equal(t, domain.LangAuto, domain.LanguageFrom(ctx))
|
||||
}
|
||||
Reference in New Issue
Block a user