summaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/text/language/match_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/golang.org/x/text/language/match_test.go')
-rw-r--r--vendor/golang.org/x/text/language/match_test.go149
1 files changed, 136 insertions, 13 deletions
diff --git a/vendor/golang.org/x/text/language/match_test.go b/vendor/golang.org/x/text/language/match_test.go
index 26cc2af2b..4fb56a025 100644
--- a/vendor/golang.org/x/text/language/match_test.go
+++ b/vendor/golang.org/x/text/language/match_test.go
@@ -8,14 +8,109 @@ import (
"bytes"
"flag"
"fmt"
+ "os"
+ "path"
"strings"
"testing"
"golang.org/x/text/internal/testtext"
+ "golang.org/x/text/internal/ucd"
)
var verbose = flag.Bool("verbose", false, "set to true to print the internal tables of matchers")
+func TestCLDRCompliance(t *testing.T) {
+ r, err := os.Open("testdata/localeMatcherTest.txt")
+ if err != nil {
+ t.Fatal(err)
+ }
+ ucd.Parse(r, func(p *ucd.Parser) {
+ name := strings.Replace(path.Join(p.String(0), p.String(1)), " ", "", -1)
+ if skip[name] {
+ return
+ }
+ t.Run(name, func(t *testing.T) {
+ supported := makeTagList(p.String(0))
+ desired := makeTagList(p.String(1))
+ gotCombined, index, _ := NewMatcher(supported).Match(desired...)
+
+ gotMatch := supported[index]
+ wantMatch := Make(p.String(2))
+ if gotMatch != wantMatch {
+ t.Fatalf("match: got %q; want %q", gotMatch, wantMatch)
+ }
+ wantCombined, err := Parse(p.String(3))
+ if err == nil && gotCombined != wantCombined {
+ t.Errorf("combined: got %q; want %q", gotCombined, wantCombined)
+ }
+ })
+ })
+}
+
+var skip = map[string]bool{
+ // TODO: bugs
+ // und-<region> is not expanded to the appropriate language.
+ "en-Hant-TW,und-TW/zh-Hant": true, // match: got "en-Hant-TW"; want "und-TW"
+ "en-Hant-TW,und-TW/zh": true, // match: got "en-Hant-TW"; want "und-TW"
+ // Honor the wildcard match. This may only be useful to select non-exact
+ // stuff.
+ "mul,af/nl": true, // match: got "af"; want "mul"
+
+ // TODO: include other extensions.
+ // combined: got "en-GB-u-ca-buddhist-nu-arab"; want "en-GB-fonipa-t-m0-iso-i0-pinyin-u-ca-buddhist-nu-arab"
+ "und,en-GB-u-sd-gbsct/en-fonipa-u-nu-Arab-ca-buddhist-t-m0-iso-i0-pinyin": true,
+
+ // Inconsistencies with Mark Davis' implementation where it is not clear
+ // which is better.
+
+ // Go prefers exact matches over less exact preferred ones.
+ // Preferring desired ones might be better.
+ "en,de,fr,ja/de-CH,fr": true, // match: got "fr"; want "de"
+ "en-GB,en,de,fr,ja/de-CH,fr": true, // match: got "fr"; want "de"
+ "pt-PT,pt-BR,es,es-419/pt-US,pt-PT": true, // match: got "pt-PT"; want "pt-BR"
+ "pt-PT,pt,es,es-419/pt-US,pt-PT,pt": true, // match: got "pt-PT"; want "pt"
+ "en,sv/en-GB,sv": true, // match: got "sv"; want "en"
+ "en-NZ,en-IT/en-US": true, // match: got "en-IT"; want "en-NZ"
+
+ // Inconsistencies in combined. I think the Go approach is more appropriate.
+ // We could use -u-rg- and -u-va- as alternative.
+ "und,fr/fr-BE-fonipa": true, // combined: got "fr"; want "fr-BE-fonipa"
+ "und,fr-CA/fr-BE-fonipa": true, // combined: got "fr-CA"; want "fr-BE-fonipa"
+ "und,fr-fonupa/fr-BE-fonipa": true, // combined: got "fr-fonupa"; want "fr-BE-fonipa"
+ "und,no/nn-BE-fonipa": true, // combined: got "no"; want "no-BE-fonipa"
+ "50,und,fr-CA-fonupa/fr-BE-fonipa": true, // combined: got "fr-CA-fonupa"; want "fr-BE-fonipa"
+
+ // Spec says prefer primary locales. But what is the benefit? Shouldn't
+ // the developer just not specify the primary locale first in the list?
+ // TODO: consider adding a SortByPreferredLocale function to ensure tags
+ // are ordered such that the preferred locale rule is observed.
+ // TODO: most of these cases are solved by getting rid of the region
+ // distance tie-breaker rule (see comments there).
+ "und,es,es-MA,es-MX,es-419/es-EA": true, // match: got "es-MA"; want "es"
+ "und,es-MA,es,es-419,es-MX/es-EA": true, // match: got "es-MA"; want "es"
+ "und,en,en-GU,en-IN,en-GB/en-ZA": true, // match: got "en-IN"; want "en-GB"
+ "und,en,en-GU,en-IN,en-GB/en-VI": true, // match: got "en-GU"; want "en"
+ "und,en-GU,en,en-GB,en-IN/en-VI": true, // match: got "en-GU"; want "en"
+
+ // Falling back to the default seems more appropriate than falling back
+ // on a language with the same script.
+ "50,und,fr-Cyrl-CA-fonupa/fr-BE-fonipa": true,
+ // match: got "und"; want "fr-Cyrl-CA-fonupa"
+ // combined: got "und"; want "fr-Cyrl-BE-fonipa"
+
+ // Other interesting cases to test:
+ // - Should same language or same script have the preference if there is
+ // usually no understanding of the other script?
+ // - More specific region in desired may replace enclosing supported.
+}
+
+func makeTagList(s string) (tags []Tag) {
+ for _, s := range strings.Split(s, ",") {
+ tags = append(tags, Make(strings.TrimSpace(s)))
+ }
+ return tags
+}
+
func TestAddLikelySubtags(t *testing.T) {
tests := []struct{ in, out string }{
{"aa", "aa-Latn-ET"},
@@ -164,6 +259,31 @@ func TestMinimize(t *testing.T) {
}
}
+func TestRegionGroups(t *testing.T) {
+ testCases := []struct {
+ a, b string
+ distance uint8
+ }{
+ {"zh-TW", "zh-HK", 5},
+ {"zh-MO", "zh-HK", 4},
+ }
+ for _, tc := range testCases {
+ a := MustParse(tc.a)
+ aScript, _ := a.Script()
+ b := MustParse(tc.b)
+ bScript, _ := b.Script()
+
+ if aScript != bScript {
+ t.Errorf("scripts differ: %q vs %q", aScript, bScript)
+ continue
+ }
+ d := regionGroupDist(a.region, b.region, aScript.scriptID, a.lang)
+ if d != tc.distance {
+ t.Errorf("got %q; want %q", d, tc.distance)
+ }
+ }
+}
+
func TestRegionDistance(t *testing.T) {
tests := []struct {
a, b string
@@ -259,17 +379,20 @@ func parseSupported(list string) (out []Tag) {
// The test set for TestBestMatch is defined in data_test.go.
func TestBestMatch(t *testing.T) {
- for i, tt := range matchTests {
+ for _, tt := range matchTests {
supported := parseSupported(tt.supported)
- m := newMatcher(supported)
+ m := newMatcher(supported, nil)
if *verbose {
fmt.Printf("%s:\n%v\n", tt.comment, m)
}
for _, tm := range tt.test {
- tag, _, conf := m.Match(parseSupported(tm.desired)...)
- if tag.String() != tm.match {
- t.Errorf("%d:%s: find %s in %q: have %s; want %s (%v)\n", i, tt.comment, tm.desired, tt.supported, tag, tm.match, conf)
- }
+ t.Run(path.Join(tt.comment, tt.supported, tm.desired), func(t *testing.T) {
+ tag, _, conf := m.Match(parseSupported(tm.desired)...)
+ if tag.String() != tm.match {
+ t.Errorf("find %s in %q: have %s; want %s (%v)", tm.desired, tt.supported, tag, tm.match, conf)
+ }
+ })
+
}
}
}
@@ -352,7 +475,7 @@ var benchWant = [][]Tag{
}
func BenchmarkMatch(b *testing.B) {
- m := newMatcher(benchHave)
+ m := newMatcher(benchHave, nil)
for i := 0; i < b.N; i++ {
for _, want := range benchWant {
m.getBest(want...)
@@ -362,7 +485,7 @@ func BenchmarkMatch(b *testing.B) {
func BenchmarkMatchExact(b *testing.B) {
want := mk("en")
- m := newMatcher(benchHave)
+ m := newMatcher(benchHave, nil)
for i := 0; i < b.N; i++ {
m.getBest(want)
}
@@ -370,7 +493,7 @@ func BenchmarkMatchExact(b *testing.B) {
func BenchmarkMatchAltLanguagePresent(b *testing.B) {
want := mk("hr")
- m := newMatcher(benchHave)
+ m := newMatcher(benchHave, nil)
for i := 0; i < b.N; i++ {
m.getBest(want)
}
@@ -378,7 +501,7 @@ func BenchmarkMatchAltLanguagePresent(b *testing.B) {
func BenchmarkMatchAltLanguageNotPresent(b *testing.B) {
want := mk("nn")
- m := newMatcher(benchHave)
+ m := newMatcher(benchHave, nil)
for i := 0; i < b.N; i++ {
m.getBest(want)
}
@@ -386,7 +509,7 @@ func BenchmarkMatchAltLanguageNotPresent(b *testing.B) {
func BenchmarkMatchAltScriptPresent(b *testing.B) {
want := mk("zh-Hant-CN")
- m := newMatcher(benchHave)
+ m := newMatcher(benchHave, nil)
for i := 0; i < b.N; i++ {
m.getBest(want)
}
@@ -394,7 +517,7 @@ func BenchmarkMatchAltScriptPresent(b *testing.B) {
func BenchmarkMatchAltScriptNotPresent(b *testing.B) {
want := mk("fr-Cyrl")
- m := newMatcher(benchHave)
+ m := newMatcher(benchHave, nil)
for i := 0; i < b.N; i++ {
m.getBest(want)
}
@@ -402,7 +525,7 @@ func BenchmarkMatchAltScriptNotPresent(b *testing.B) {
func BenchmarkMatchLimitedExact(b *testing.B) {
want := []Tag{mk("he-NL"), mk("iw-NL")}
- m := newMatcher(benchHave)
+ m := newMatcher(benchHave, nil)
for i := 0; i < b.N; i++ {
m.getBest(want...)
}