62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package i18n
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestPinnedCLDRSamples(t *testing.T) {
|
|
var data struct {
|
|
Supplemental struct {
|
|
Rules map[string]map[string]string `json:"plurals-type-cardinal"`
|
|
} `json:"supplemental"`
|
|
}
|
|
if err := json.Unmarshal(pluralData, &data); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
count := 0
|
|
for locale, entries := range data.Supplemental.Rules {
|
|
for name, raw := range entries {
|
|
want := strings.TrimPrefix(name, "pluralRule-count-")
|
|
for _, section := range strings.Split(raw, "@")[1:] {
|
|
_, samples, ok := strings.Cut(section, " ")
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, sample := range strings.Split(samples, ",") {
|
|
for _, number := range strings.Split(strings.TrimSpace(sample), "~") {
|
|
if number == "" || strings.ContainsAny(number, "…ce") {
|
|
continue
|
|
}
|
|
values, fractional := operands(number)
|
|
got := "other"
|
|
for _, cat := range []string{"zero", "one", "two", "few", "many"} {
|
|
if rule, ok := cardinalRules[locale].rules[cat]; ok && rule.matches(values, fractional) {
|
|
got = cat
|
|
break
|
|
}
|
|
}
|
|
count++
|
|
if got != want {
|
|
t.Errorf("%s %s: got %s want %s", locale, number, got, want)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if count < 5000 {
|
|
t.Fatalf("too few CLDR samples: %d", count)
|
|
}
|
|
t.Logf("validated %d upstream cardinal samples", count)
|
|
}
|
|
|
|
func TestRuleParserRejectsUnsupportedSyntax(t *testing.T) {
|
|
for _, text := range []string{"n = 1; run()", "n % 0 = 1", "n = 5..1", "unknown = 1"} {
|
|
if _, err := parsePluralRule(text); err == nil {
|
|
t.Fatal(text)
|
|
}
|
|
}
|
|
}
|