55 lines
1.9 KiB
Go
55 lines
1.9 KiB
Go
package errori18n_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
|
"git.yuntuo.hk/k2safe/go-omni/i18n"
|
|
"git.yuntuo.hk/k2safe/go-omni/i18n/errori18n"
|
|
)
|
|
|
|
func TestCatalogAdapterIsExplicitAndDetached(t *testing.T) {
|
|
c := apierrors.Catalog{Definitions: map[string]apierrors.Definition{"A": {Status: 409, Code: 7, Message: "{value}", Params: []apierrors.Parameter{{Name: "value", Type: "int64"}}}, "LITERAL": {Code: 8, Message: "${HOME}"}}}
|
|
bound := errori18n.Bind(c, "errors")
|
|
if c.Definitions["A"].Translation != nil {
|
|
t.Fatal("mutated source")
|
|
}
|
|
source, err := errori18n.Source(bound, "en")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := i18n.New(i18n.Config{SourceLocale: "en", DefaultLocale: "en", Locales: []string{"en"}}, source...)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l := errori18n.New(b, nil)
|
|
if err := l.Validate(bound); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := apierrors.MustRegistry(bound)
|
|
bound.Definitions["A"].Translation.Namespace = "changed"
|
|
metadata, _ := r.Definition("A")
|
|
metadata.Translation.Namespace = "changed"
|
|
metadata.Params[0].Type = "bool"
|
|
private := errors.New("private")
|
|
failure := r.Resolve(apierrors.ByCode("A").WithParams(map[string]any{"value": int64(9223372036854775807)}).WithCause(private))
|
|
message, ok := l.Localize(context.Background(), r, failure)
|
|
if !ok || message != "9223372036854775807" || failure.Status != 409 || !errors.Is(failure, private) {
|
|
t.Fatal(message, ok, failure)
|
|
}
|
|
if _, ok := l.Localize(context.Background(), apierrors.MustRegistry(c), failure); ok {
|
|
t.Fatal("implicit translation")
|
|
}
|
|
missing := errori18n.New(nil, nil)
|
|
if _, ok := missing.Localize(context.Background(), r, failure); ok {
|
|
t.Fatal("missing bundle translated")
|
|
}
|
|
args := failure.Parameters()
|
|
args["value"] = int64(0)
|
|
if failure.Parameters()["value"] != int64(9223372036854775807) {
|
|
t.Fatal("mutable parameters")
|
|
}
|
|
}
|