Skip to content

Commit 2dbfc50

Browse files
committed
Fix tests
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 3841573 commit 2dbfc50

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

modules/charset/escape_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,18 @@ then resh (ר), and finally heh (ה) (which should appear leftmost).`,
133133
},
134134
}
135135

136+
type nullLocale struct{}
137+
138+
func (nullLocale) Language() string { return "" }
139+
func (nullLocale) Tr(key string, _ ...interface{}) string { return key }
140+
func (nullLocale) TrN(cnt interface{}, key1, keyN string, args ...interface{}) string { return "" }
141+
142+
var _ (translation.Locale) = nullLocale{}
143+
136144
func TestEscapeControlString(t *testing.T) {
137145
for _, tt := range escapeControlTests {
138146
t.Run(tt.name, func(t *testing.T) {
139-
locale := translation.NewLocale("en_US")
140-
status, result := EscapeControlString(tt.text, locale)
147+
status, result := EscapeControlString(tt.text, nullLocale{})
141148
if !reflect.DeepEqual(*status, tt.status) {
142149
t.Errorf("EscapeControlString() status = %v, wanted= %v", status, tt.status)
143150
}
@@ -173,7 +180,7 @@ func TestEscapeControlReader(t *testing.T) {
173180
t.Run(tt.name, func(t *testing.T) {
174181
input := strings.NewReader(tt.text)
175182
output := &strings.Builder{}
176-
status, err := EscapeControlReader(input, output, translation.NewLocale("en_US"))
183+
status, err := EscapeControlReader(input, output, nullLocale{})
177184
result := output.String()
178185
if err != nil {
179186
t.Errorf("EscapeControlReader(): err = %v", err)
@@ -195,5 +202,5 @@ func TestEscapeControlReader_panic(t *testing.T) {
195202
for i := 0; i < 6826; i++ {
196203
bs = append(bs, []byte("—")...)
197204
}
198-
_, _ = EscapeControlString(string(bs), translation.NewLocale("en_US"))
205+
_, _ = EscapeControlString(string(bs), nullLocale{})
199206
}

modules/translation/i18n/localestore.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,27 +98,26 @@ func (store *localeStore) SetDefaultLang(lang string) {
9898
func (store *localeStore) Tr(lang, trKey string, trArgs ...interface{}) string {
9999
l, _ := store.Locale(lang)
100100

101-
if l != nil {
102-
return l.Tr(trKey, trArgs...)
103-
}
104-
return trKey
101+
return l.Tr(trKey, trArgs...)
105102
}
106103

107104
// Has returns whether the given language has a translation for the provided key
108105
func (store *localeStore) Has(lang, trKey string) bool {
109106
l, _ := store.Locale(lang)
110107

111-
if l != nil {
112-
return false
113-
}
114108
return l.Has(trKey)
115109
}
116110

117111
// Locale returns the locale for the lang or the default language
118-
func (store *localeStore) Locale(lang string) (l Locale, found bool) {
119-
l, found = store.localeMap[lang]
112+
func (store *localeStore) Locale(lang string) (Locale, bool) {
113+
l, found := store.localeMap[lang]
120114
if !found {
121-
l = store.localeMap[store.defaultLang]
115+
var ok bool
116+
l, ok = store.localeMap[store.defaultLang]
117+
if !ok {
118+
// no default - return an empty locale
119+
l = &locale{store: store, idxToMsgMap: make(map[int]string)}
120+
}
122121
}
123122
return l, found
124123
}

0 commit comments

Comments
 (0)