/usr/share/gocode/src/golang.org/x/text/message/message_test.go is in golang-golang-x-text-dev 0.0~git20170627.0.6353ef0-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | // Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package message
import (
"bytes"
"fmt"
"io"
"testing"
"golang.org/x/text/internal"
"golang.org/x/text/internal/format"
"golang.org/x/text/language"
"golang.org/x/text/message/catalog"
)
type formatFunc func(s fmt.State, v rune)
func (f formatFunc) Format(s fmt.State, v rune) { f(s, v) }
func TestBinding(t *testing.T) {
testCases := []struct {
tag string
value interface{}
want string
}{
{"en", 1, "1"},
{"en", "2", "2"},
{ // Language is passed.
"en",
formatFunc(func(fs fmt.State, v rune) {
s := fs.(format.State)
io.WriteString(s, s.Language().String())
}),
"en",
},
}
for i, tc := range testCases {
p := NewPrinter(language.MustParse(tc.tag))
if got := p.Sprint(tc.value); got != tc.want {
t.Errorf("%d:%s:Sprint(%v) = %q; want %q", i, tc.tag, tc.value, got, tc.want)
}
var buf bytes.Buffer
p.Fprint(&buf, tc.value)
if got := buf.String(); got != tc.want {
t.Errorf("%d:%s:Fprint(%v) = %q; want %q", i, tc.tag, tc.value, got, tc.want)
}
}
}
func TestLocalization(t *testing.T) {
type test struct {
tag string
key Reference
args []interface{}
want string
}
args := func(x ...interface{}) []interface{} { return x }
empty := []interface{}{}
joe := []interface{}{"Joe"}
joeAndMary := []interface{}{"Joe", "Mary"}
testCases := []struct {
desc string
cat []entry
test []test
}{{
desc: "empty",
test: []test{
{"en", "key", empty, "key"},
{"en", "", empty, ""},
{"nl", "", empty, ""},
},
}, {
desc: "hierarchical languages",
cat: []entry{
{"en", "hello %s", "Hello %s!"},
{"en-GB", "hello %s", "Hellø %s!"},
{"en-US", "hello %s", "Howdy %s!"},
{"en", "greetings %s and %s", "Greetings %s and %s!"},
},
test: []test{
{"und", "hello %s", joe, "hello Joe"},
{"nl", "hello %s", joe, "hello Joe"},
{"en", "hello %s", joe, "Hello Joe!"},
{"en-US", "hello %s", joe, "Howdy Joe!"},
{"en-GB", "hello %s", joe, "Hellø Joe!"},
{"en-oxendict", "hello %s", joe, "Hello Joe!"},
{"en-US-oxendict-u-ms-metric", "hello %s", joe, "Howdy Joe!"},
{"und", "greetings %s and %s", joeAndMary, "greetings Joe and Mary"},
{"nl", "greetings %s and %s", joeAndMary, "greetings Joe and Mary"},
{"en", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
{"en-US", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
{"en-GB", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
{"en-oxendict", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
{"en-US-oxendict-u-ms-metric", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
},
}, {
desc: "references",
cat: []entry{
{"en", "hello", "Hello!"},
},
test: []test{
{"en", "hello", empty, "Hello!"},
{"en", Key("hello", "fallback"), empty, "Hello!"},
{"en", Key("xxx", "fallback"), empty, "fallback"},
{"und", Key("hello", "fallback"), empty, "fallback"},
},
}, {
desc: "zero substitution", // work around limitation of fmt
cat: []entry{
{"en", "hello %s", "Hello!"},
{"en", "hi %s and %s", "Hello %[2]s!"},
},
test: []test{
{"en", "hello %s", joe, "Hello!"},
{"en", "hello %s", joeAndMary, "Hello!"},
{"en", "hi %s and %s", joeAndMary, "Hello Mary!"},
// The following tests resolve to the fallback string.
{"und", "hello", joeAndMary, "hello"},
{"und", "hello %%%%", joeAndMary, "hello %%"},
{"und", "hello %#%%4.2% ", joeAndMary, "hello %% "},
{"und", "hello %s", joeAndMary, "hello Joe%!(EXTRA string=Mary)"},
{"und", "hello %+%%s", joeAndMary, "hello %Joe%!(EXTRA string=Mary)"},
{"und", "hello %-42%%s ", joeAndMary, "hello %Joe %!(EXTRA string=Mary)"},
},
}, {
desc: "number formatting", // work around limitation of fmt
cat: []entry{
{"und", "files", "%d files left"},
{"und", "meters", "%.2f meters"},
{"de", "files", "%d Dateien übrig"},
},
test: []test{
{"en", "meters", args(3000.2), "3,000.20 meters"},
{"en-u-nu-gujr", "files", args(123456), "૧૨૩,૪૫૬ files left"},
{"de", "files", args(1234), "1.234 Dateien übrig"},
{"de-CH", "files", args(1234), "1'234 Dateien übrig"},
{"de-CH-u-nu-mong", "files", args(1234), "᠑'᠒᠓᠔ Dateien übrig"},
},
}}
for _, tc := range testCases {
cat, _ := initCat(tc.cat)
for i, pt := range tc.test {
t.Run(fmt.Sprintf("%s:%d", tc.desc, i), func(t *testing.T) {
p := NewPrinter(language.MustParse(pt.tag), Catalog(cat))
if got := p.Sprintf(pt.key, pt.args...); got != pt.want {
t.Errorf("Sprintf(%q, %v) = %s; want %s",
pt.key, pt.args, got, pt.want)
return // Next error will likely be the same.
}
w := &bytes.Buffer{}
p.Fprintf(w, pt.key, pt.args...)
if got := w.String(); got != pt.want {
t.Errorf("Fprintf(%q, %v) = %s; want %s",
pt.key, pt.args, got, pt.want)
}
})
}
}
}
type entry struct{ tag, key, msg string }
func initCat(entries []entry) (*catalog.Catalog, []language.Tag) {
tags := []language.Tag{}
cat := catalog.New()
for _, e := range entries {
tag := language.MustParse(e.tag)
tags = append(tags, tag)
cat.SetString(tag, e.key, e.msg)
}
return cat, internal.UniqueTags(tags)
}
|