1
2
3
4
5 package internal
6
7 import (
8 "strings"
9 "testing"
10
11 "golang.org/x/text/language"
12 )
13
14 func TestInheritanceMatcher(t *testing.T) {
15 for i, tt := range []struct {
16 haveTags string
17 wantTags string
18 match string
19 conf language.Confidence
20 }{
21 {"und,en,en-US", "en-US", "en-US", language.Exact},
22 {"zh-Hant,zh", "zh-TW", "zh-Hant", language.High},
23 {"und,zh", "zh-TW", "und", language.High},
24 {"zh", "zh-TW", "und", language.No},
25 {"iw,en,nl", "he", "he", language.Exact},
26 {"he,en,nl", "iw", "he", language.Exact},
27
28
29
30
31 {"und,en,en-US", "he,en-US", "und", language.High},
32 } {
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)
37 if tag != want {
38 t.Errorf("%d:tag: got %q; want %q", i, tag, want)
39 }
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)
43 }
44 }
45 if conf != tt.conf {
46 t.Errorf("%d:conf: got %v; want %v", i, conf, tt.conf)
47 }
48 }
49 }
50
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)))
54 }
55 return out
56 }
57
View as plain text