1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 "golang.org/x/text/language"
14 func TestInheritanceMatcher(t *testing.T) {
15 for i, tt := range []struct {
19 conf language.Confidence
21 {"und,en,en-US", "en-US", "en-US", language.Exact}, // most specific match
22 {"zh-Hant,zh", "zh-TW", "zh-Hant", language.High}, // zh-TW implies Hant.
23 {"und,zh", "zh-TW", "und", language.High}, // zh-TW does not match zh.
24 {"zh", "zh-TW", "und", language.No}, // zh-TW does not match zh.
25 {"iw,en,nl", "he", "he", language.Exact}, // matches after canonicalization
26 {"he,en,nl", "iw", "he", language.Exact}, // matches after canonicalization
27 // Prefer first match over more specific match for various reasons:
28 // a) consistency of user interface is more important than an exact match,
29 // b) _if_ und is specified, it should be considered a correct and useful match,
30 // Note that a call to this Match will almost always be with a single tag.
31 {"und,en,en-US", "he,en-US", "und", language.High},
33 have := parseTags(tt.haveTags)
34 m := NewInheritanceMatcher(have)
35 tag, index, conf := m.Match(parseTags(tt.wantTags)...)
36 want := language.Raw.Make(tt.match)
38 t.Errorf("%d:tag: got %q; want %q", i, tag, want)
40 if conf != language.No {
41 if got, _ := language.All.Canonicalize(have[index]); got != want {
42 t.Errorf("%d:index: got %q; want %q ", i, got, want)
46 t.Errorf("%d:conf: got %v; want %v", i, conf, tt.conf)
51 func parseTags(list string) (out []language.Tag) {
52 for _, s := range strings.Split(list, ",") {
53 out = append(out, language.Raw.Make(strings.TrimSpace(s)))