github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/text/message/message_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 "bytes" 9 "fmt" 10 "io" 11 "testing" 12 13 "golang.org/x/text/internal/format" 14 "golang.org/x/text/language" 15 ) 16 17 type formatFunc func(s fmt.State, v rune) 18 19 func (f formatFunc) Format(s fmt.State, v rune) { f(s, v) } 20 21 func TestBinding(t *testing.T) { 22 testCases := []struct { 23 tag string 24 value interface{} 25 want string 26 }{ 27 {"en", 1, "1"}, 28 {"en", "2", "2"}, 29 { // Language is passed. 30 "en", 31 formatFunc(func(fs fmt.State, v rune) { 32 s := fs.(format.State) 33 io.WriteString(s, s.Language().String()) 34 }), 35 "en", 36 }, 37 } 38 for i, tc := range testCases { 39 p := NewPrinter(language.MustParse(tc.tag)) 40 if got := p.Sprint(tc.value); got != tc.want { 41 t.Errorf("%d:%s:Sprint(%v) = %q; want %q", i, tc.tag, tc.value, got, tc.want) 42 } 43 var buf bytes.Buffer 44 p.Fprint(&buf, tc.value) 45 if got := buf.String(); got != tc.want { 46 t.Errorf("%d:%s:Fprint(%v) = %q; want %q", i, tc.tag, tc.value, got, tc.want) 47 } 48 } 49 } 50 51 func TestFormatSelection(t *testing.T) { 52 type test struct { 53 tag string 54 key Reference 55 args []interface{} 56 want string 57 } 58 empty := []interface{}{} 59 joe := []interface{}{"Joe"} 60 joeAndMary := []interface{}{"Joe", "Mary"} 61 62 testCases := []struct { 63 desc string 64 cat []entry 65 test []test 66 }{{ 67 desc: "empty", 68 test: []test{ 69 {"en", "key", empty, "key"}, 70 {"en", "", empty, ""}, 71 {"nl", "", empty, ""}, 72 }, 73 }, { 74 desc: "hierarchical languages", 75 cat: []entry{ 76 {"en", "hello %s", "Hello %s!"}, 77 {"en-GB", "hello %s", "Hellø %s!"}, 78 {"en-US", "hello %s", "Howdy %s!"}, 79 {"en", "greetings %s and %s", "Greetings %s and %s!"}, 80 }, 81 test: []test{ 82 {"und", "hello %s", joe, "hello Joe"}, 83 {"nl", "hello %s", joe, "hello Joe"}, 84 {"en", "hello %s", joe, "Hello Joe!"}, 85 {"en-US", "hello %s", joe, "Howdy Joe!"}, 86 {"en-GB", "hello %s", joe, "Hellø Joe!"}, 87 {"en-oxendict", "hello %s", joe, "Hello Joe!"}, 88 {"en-US-oxendict-u-ms-metric", "hello %s", joe, "Howdy Joe!"}, 89 90 {"und", "greetings %s and %s", joeAndMary, "greetings Joe and Mary"}, 91 {"nl", "greetings %s and %s", joeAndMary, "greetings Joe and Mary"}, 92 {"en", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"}, 93 {"en-US", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"}, 94 {"en-GB", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"}, 95 {"en-oxendict", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"}, 96 {"en-US-oxendict-u-ms-metric", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"}, 97 }, 98 }, { 99 desc: "references", 100 cat: []entry{ 101 {"en", "hello", "Hello!"}, 102 }, 103 test: []test{ 104 {"en", "hello", empty, "Hello!"}, 105 {"en", Key("hello", "fallback"), empty, "Hello!"}, 106 {"en", Key("xxx", "fallback"), empty, "fallback"}, 107 {"und", Key("hello", "fallback"), empty, "fallback"}, 108 }, 109 }, { 110 desc: "zero substitution", // work around limitation of fmt 111 cat: []entry{ 112 {"en", "hello %s", "Hello!"}, 113 {"en", "hi %s and %s", "Hello %[2]s!"}, 114 }, 115 test: []test{ 116 {"en", "hello %s", joe, "Hello!"}, 117 {"en", "hello %s", joeAndMary, "Hello!"}, 118 {"en", "hi %s and %s", joeAndMary, "Hello Mary!"}, 119 // The following tests resolve to the fallback string. 120 {"und", "hello", joeAndMary, "hello"}, 121 {"und", "hello %%%%", joeAndMary, "hello %%"}, 122 {"und", "hello %#%%4.2% ", joeAndMary, "hello %% "}, 123 {"und", "hello %s", joeAndMary, "hello Joe%!(EXTRA string=Mary)"}, 124 {"und", "hello %+%%s", joeAndMary, "hello %Joe%!(EXTRA string=Mary)"}, 125 {"und", "hello %-42%%s ", joeAndMary, "hello %Joe %!(EXTRA string=Mary)"}, 126 }, 127 }} 128 129 for _, tc := range testCases { 130 cat, _ := initCat(tc.cat) 131 132 for i, pt := range tc.test { 133 p := cat.Printer(language.MustParse(pt.tag)) 134 135 if got := p.Sprintf(pt.key, pt.args...); got != pt.want { 136 t.Errorf("%s:%d:Sprintf(%s, %v) = %s; want %s", 137 tc.desc, i, pt.key, pt.args, got, pt.want) 138 continue // Next error will likely be the same. 139 } 140 141 w := &bytes.Buffer{} 142 p.Fprintf(w, pt.key, pt.args...) 143 if got := w.String(); got != pt.want { 144 t.Errorf("%s:%d:Fprintf(%s, %v) = %s; want %s", 145 tc.desc, i, pt.key, pt.args, got, pt.want) 146 } 147 } 148 } 149 }