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.go247
1 files changed, 110 insertions, 137 deletions
diff --git a/vendor/golang.org/x/text/language/match_test.go b/vendor/golang.org/x/text/language/match_test.go
index 4fb56a025..8b60b07a3 100644
--- a/vendor/golang.org/x/text/language/match_test.go
+++ b/vendor/golang.org/x/text/language/match_test.go
@@ -10,6 +10,7 @@ import (
"fmt"
"os"
"path"
+ "path/filepath"
"strings"
"testing"
@@ -19,39 +20,42 @@ import (
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
+func TestCompliance(t *testing.T) {
+ filepath.Walk("testdata", func(file string, info os.FileInfo, err error) error {
+ if info.IsDir() {
+ return nil
}
- 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)
+ r, err := os.Open(file)
+ 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(info.Name()+"/"+name, func(t *testing.T) {
+ supported := makeTagList(p.String(0))
+ desired := makeTagList(p.String(1))
+ gotCombined, index, conf := NewMatcher(supported).Match(desired...)
+
+ gotMatch := supported[index]
+ wantMatch := mk(p.String(2))
+ if gotMatch != wantMatch {
+ t.Fatalf("match: got %q; want %q (%v)", gotMatch, wantMatch, conf)
+ }
+ wantCombined, err := Raw.Parse(p.String(3))
+ if err == nil && gotCombined != wantCombined {
+ t.Errorf("combined: got %q; want %q (%v)", gotCombined, wantCombined, conf)
+ }
+ })
})
+ return nil
})
}
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"
@@ -63,15 +67,6 @@ var skip = map[string]bool{
// 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"
@@ -80,20 +75,8 @@ var skip = map[string]bool{
"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.
+ // The initial number is a threshold. As we don't use scoring, we will not
+ // implement this.
"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"
@@ -106,11 +89,65 @@ var skip = map[string]bool{
func makeTagList(s string) (tags []Tag) {
for _, s := range strings.Split(s, ",") {
- tags = append(tags, Make(strings.TrimSpace(s)))
+ tags = append(tags, mk(strings.TrimSpace(s)))
}
return tags
}
+func TestMatchStrings(t *testing.T) {
+ testCases := []struct {
+ supported string
+ desired string // strings separted by |
+ tag string
+ index int
+ }{{
+ supported: "en",
+ desired: "",
+ tag: "en",
+ index: 0,
+ }, {
+ supported: "en",
+ desired: "nl",
+ tag: "en",
+ index: 0,
+ }, {
+ supported: "en,nl",
+ desired: "nl",
+ tag: "nl",
+ index: 1,
+ }, {
+ supported: "en,nl",
+ desired: "nl|en",
+ tag: "nl",
+ index: 1,
+ }, {
+ supported: "en-GB,nl",
+ desired: "en ; q=0.1,nl",
+ tag: "nl",
+ index: 1,
+ }, {
+ supported: "en-GB,nl",
+ desired: "en;q=0.005 | dk; q=0.1,nl ",
+ tag: "en-GB",
+ index: 0,
+ }, {
+ // do not match faulty tags with und
+ supported: "en,und",
+ desired: "|en",
+ tag: "en",
+ index: 0,
+ }}
+ for _, tc := range testCases {
+ t.Run(path.Join(tc.supported, tc.desired), func(t *testing.T) {
+ m := NewMatcher(makeTagList(tc.supported))
+ tag, index := MatchStrings(m, strings.Split(tc.desired, "|")...)
+ if tag.String() != tc.tag || index != tc.index {
+ t.Errorf("got %v, %d; want %v, %d", tag, index, tc.tag, tc.index)
+ }
+ })
+ }
+}
+
func TestAddLikelySubtags(t *testing.T) {
tests := []struct{ in, out string }{
{"aa", "aa-Latn-ET"},
@@ -132,6 +169,7 @@ func TestAddLikelySubtags(t *testing.T) {
{"und-YT", "fr-Latn-YT"},
{"und-Arab", "ar-Arab-EG"},
{"und-AM", "hy-Armn-AM"},
+ {"und-TW", "zh-Hant-TW"},
{"und-002", "en-Latn-NG"},
{"und-Latn-002", "en-Latn-NG"},
{"en-Latn-002", "en-Latn-NG"},
@@ -266,6 +304,12 @@ func TestRegionGroups(t *testing.T) {
}{
{"zh-TW", "zh-HK", 5},
{"zh-MO", "zh-HK", 4},
+ {"es-ES", "es-AR", 5},
+ {"es-ES", "es", 4},
+ {"es-419", "es-MX", 4},
+ {"es-AR", "es-MX", 4},
+ {"es-ES", "es-MX", 5},
+ {"es-PT", "es-MX", 5},
}
for _, tc := range testCases {
a := MustParse(tc.a)
@@ -277,67 +321,27 @@ func TestRegionGroups(t *testing.T) {
t.Errorf("scripts differ: %q vs %q", aScript, bScript)
continue
}
- d := regionGroupDist(a.region, b.region, aScript.scriptID, a.lang)
+ 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
- d int
- }{
- {"NL", "NL", 0},
- {"NL", "EU", 1},
- {"EU", "NL", 1},
- {"005", "005", 0},
- {"NL", "BE", 2},
- {"CO", "005", 1},
- {"005", "CO", 1},
- {"CO", "419", 2},
- {"419", "CO", 2},
- {"005", "419", 1},
- {"419", "005", 1},
- {"001", "013", 2},
- {"013", "001", 2},
- {"CO", "CW", 4},
- {"CO", "PW", 6},
- {"CO", "BV", 6},
- {"ZZ", "QQ", 2},
- }
- for i, tt := range tests {
- testtext.Run(t, tt.a+"/"+tt.b, func(t *testing.T) {
- ra, _ := getRegionID([]byte(tt.a))
- rb, _ := getRegionID([]byte(tt.b))
- if d := regionDistance(ra, rb); d != tt.d {
- t.Errorf("%d: d(%s, %s) = %v; want %v", i, tt.a, tt.b, d, tt.d)
- }
- })
- }
-}
-
-func TestParentDistance(t *testing.T) {
- tests := []struct {
- parent string
- tag string
- d uint8
- }{
- {"en-001", "en-AU", 1},
- {"pt-PT", "pt-AO", 1},
- {"pt", "pt-AO", 2},
- {"en-AU", "en-GB", 255},
- {"en-NL", "en-AU", 255},
- // Note that pt-BR and en-US are not automatically minimized.
- {"pt-BR", "pt-AO", 255},
- {"en-US", "en-AU", 255},
+func TestIsParadigmLocale(t *testing.T) {
+ testCases := map[string]bool{
+ "en-US": true,
+ "en-GB": true,
+ "en-VI": false,
+ "es-GB": false,
+ "es-ES": true,
+ "es-419": true,
}
- for _, tt := range tests {
- r := Raw.MustParse(tt.parent).region
- tag := Raw.MustParse(tt.tag)
- if d := parentDistance(r, tag); d != tt.d {
- t.Errorf("d(%s, %s) was %d; want %d", r, tag, d, tt.d)
+ for str, want := range testCases {
+ tag := Make(str)
+ got := isParadigmLocale(tag.lang, tag.region)
+ if got != want {
+ t.Errorf("isPL(%q) = %v; want %v", str, got, want)
}
}
}
@@ -355,12 +359,8 @@ func (m *matcher) String() string {
func (h *matchHeader) String() string {
w := &bytes.Buffer{}
- fmt.Fprintf(w, "exact: ")
- for _, h := range h.exact {
- fmt.Fprintf(w, "%v, ", h)
- }
- fmt.Fprint(w, "; max: ")
- for _, h := range h.max {
+ fmt.Fprint(w, "haveTag: ")
+ for _, h := range h.haveTags {
fmt.Fprintf(w, "%v, ", h)
}
return w.String()
@@ -370,35 +370,8 @@ func (t haveTag) String() string {
return fmt.Sprintf("%v:%d:%v:%v-%v|%v", t.tag, t.index, t.conf, t.maxRegion, t.maxScript, t.altScript)
}
-func parseSupported(list string) (out []Tag) {
- for _, s := range strings.Split(list, ",") {
- out = append(out, mk(strings.TrimSpace(s)))
- }
- return out
-}
-
-// The test set for TestBestMatch is defined in data_test.go.
-func TestBestMatch(t *testing.T) {
- for _, tt := range matchTests {
- supported := parseSupported(tt.supported)
- m := newMatcher(supported, nil)
- if *verbose {
- fmt.Printf("%s:\n%v\n", tt.comment, m)
- }
- for _, tm := range tt.test {
- 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)
- }
- })
-
- }
- }
-}
-
func TestBestMatchAlloc(t *testing.T) {
- m := NewMatcher(parseSupported("en sr nl"))
+ m := NewMatcher(makeTagList("en sr nl"))
// Go allocates when creating a list of tags from a single tag!
list := []Tag{English}
avg := testtext.AllocsPerRun(1, func() {