-
Notifications
You must be signed in to change notification settings - Fork 2
/
locale_unix_test.go
163 lines (121 loc) · 4.01 KB
/
locale_unix_test.go
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
//go:build !windows && !darwin && !js && !android
// +build !windows,!darwin,!js,!android
package locale
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMultipleLocales(t *testing.T) {
t.Setenv("LANGUAGE", "en_US:fr:ja")
t.Setenv("LC_ALL", "")
t.Setenv("LC_MESSAGES", "")
t.Setenv("LANG", "en_US.UTF-8")
locales, err := GetLocales()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, []string{"en-US", "fr", "ja"}, locales)
locale, err := GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "en-US", locale)
lang, err := GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "en", lang)
region, err := GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "US", region)
// If "C" is set, we should ignore LANGUAGE
t.Setenv("LC_ALL", "C")
var nilStringSlice []string
locales, err = GetLocales()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, nilStringSlice, locales)
locale, err = GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "", locale)
lang, err = GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "", lang)
region, err = GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "", region)
}
func TestSingleLocale(t *testing.T) {
t.Setenv("LANGUAGE", "ja_JP")
t.Setenv("LC_ALL", "en_US")
t.Setenv("LC_MESSAGES", "ko_KR")
t.Setenv("LANG", "fr_FR")
locales, err := GetLocales()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, []string{"ja-JP"}, locales)
locale, err := GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "ja-JP", locale)
lang, err := GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "ja", lang)
region, err := GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "JP", region)
t.Setenv("LANGUAGE", "")
locale, err = GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "en-US", locale)
lang, err = GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "en", lang)
region, err = GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "US", region)
t.Setenv("LC_ALL", "")
locale, err = GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "ko-KR", locale)
lang, err = GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "ko", lang)
region, err = GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "KR", region)
t.Setenv("LC_MESSAGES", "")
locale, err = GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "fr-FR", locale)
lang, err = GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "fr", lang)
region, err = GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "FR", region)
}
func TestLocaleNoRegion(t *testing.T) {
t.Setenv("LANG", "fr")
locales, err := GetLocales()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, []string{"fr"}, locales)
locale, err := GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "fr", locale)
lang, err := GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "fr", lang)
region, err := GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "", region)
}
func TestNoLocale(t *testing.T) {
var nilStringSlice []string
for _, env := range [...]string{"LANGUAGE", "LC_ALL", "LC_MESSAGES", "LANG"} {
t.Setenv(env, "")
}
locales, err := GetLocales()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, nilStringSlice, locales)
locale, err := GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "", locale)
lang, err := GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "", lang)
region, err := GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "", region)
}