github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/text/message/catalog_test.go (about) 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. 4 5 package message 6 7 import ( 8 "reflect" 9 "testing" 10 11 "golang.org/x/text/internal" 12 "golang.org/x/text/language" 13 ) 14 15 type entry struct{ tag, key, msg string } 16 17 var testCases = []struct { 18 desc string 19 cat []entry 20 lookup []entry 21 }{{ 22 desc: "empty catalog", 23 lookup: []entry{ 24 {"en", "key", ""}, 25 {"en", "", ""}, 26 {"nl", "", ""}, 27 }, 28 }, { 29 desc: "one entry", 30 cat: []entry{ 31 {"en", "hello", "Hello!"}, 32 }, 33 lookup: []entry{ 34 {"und", "hello", ""}, 35 {"nl", "hello", ""}, 36 {"en", "hello", "Hello!"}, 37 {"en-US", "hello", "Hello!"}, 38 {"en-GB", "hello", "Hello!"}, 39 {"en-oxendict", "hello", "Hello!"}, 40 {"en-oxendict-u-ms-metric", "hello", "Hello!"}, 41 }, 42 }, { 43 desc: "hierarchical languages", 44 cat: []entry{ 45 {"en", "hello", "Hello!"}, 46 {"en-GB", "hello", "Hellø!"}, 47 {"en-US", "hello", "Howdy!"}, 48 {"en", "greetings", "Greetings!"}, 49 }, 50 lookup: []entry{ 51 {"und", "hello", ""}, 52 {"nl", "hello", ""}, 53 {"en", "hello", "Hello!"}, 54 {"en-US", "hello", "Howdy!"}, 55 {"en-GB", "hello", "Hellø!"}, 56 {"en-oxendict", "hello", "Hello!"}, 57 {"en-US-oxendict-u-ms-metric", "hello", "Howdy!"}, 58 59 {"und", "greetings", ""}, 60 {"nl", "greetings", ""}, 61 {"en", "greetings", "Greetings!"}, 62 {"en-US", "greetings", "Greetings!"}, 63 {"en-GB", "greetings", "Greetings!"}, 64 {"en-oxendict", "greetings", "Greetings!"}, 65 {"en-US-oxendict-u-ms-metric", "greetings", "Greetings!"}, 66 }, 67 }} 68 69 func initCat(entries []entry) (*Catalog, []language.Tag) { 70 tags := []language.Tag{} 71 cat := newCatalog() 72 for _, e := range entries { 73 tag := language.MustParse(e.tag) 74 tags = append(tags, tag) 75 cat.SetString(tag, e.key, e.msg) 76 } 77 return cat, internal.UniqueTags(tags) 78 } 79 80 func TestCatalog(t *testing.T) { 81 for _, tc := range testCases { 82 cat, wantTags := initCat(tc.cat) 83 84 // languages 85 if got := cat.Languages(); !reflect.DeepEqual(got, wantTags) { 86 t.Errorf("%s:Languages: got %v; want %v", tc.desc, got, wantTags) 87 } 88 89 // Lookup 90 for _, e := range tc.lookup { 91 tag := language.MustParse(e.tag) 92 msg, ok := cat.get(tag, e.key) 93 if okWant := e.msg != ""; ok != okWant || msg != e.msg { 94 t.Errorf("%s:Lookup(%s, %s) = %s, %v; want %s, %v", tc.desc, tag, e.key, msg, ok, e.msg, okWant) 95 } 96 } 97 } 98 }