-
Notifications
You must be signed in to change notification settings - Fork 8
/
locale_shared_test.go
100 lines (87 loc) · 2.02 KB
/
locale_shared_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
package locale
import (
"errors"
"os"
"reflect"
"testing"
)
func TestDetectViaEnvLanguage(t *testing.T) {
tests := []struct {
name string
envValue string
want []string
wantErr error
}{
{"Valid single value", "en_US", []string{"en_US"}, nil},
{"Multiple values", "en_US:zh_CN", []string{"en_US", "zh_CN"}, nil},
{"Empty value", "", nil, ErrNotDetected},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setupEnv()
defer setupEnv()
err := os.Setenv("LANGUAGE", tt.envValue)
if err != nil {
t.Fatal(err)
}
got, err := detectViaEnvLanguage()
t.Logf("langs: %v", got)
if !errors.Is(err, tt.wantErr) {
t.Errorf("detectViaEnvLanguage() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("detectViaEnvLanguage() = %v, want %v", got, tt.want)
}
})
}
}
func TestDetectViaEnvLc(t *testing.T) {
tests := []struct {
name string
setEnv bool
envKey string
envVal string
want []string
wantErr error
}{
{"LC_ALL set", true, "LC_ALL", "en_US.UTF-8", []string{"en_US"}, nil},
{"No LC env set", false, "", "", nil, ErrNotDetected},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setupEnv()
defer setupEnv()
if tt.setEnv {
err := os.Setenv(tt.envKey, tt.envVal)
if err != nil {
t.Fatal(err)
}
}
got, err := detectViaEnvLc()
t.Logf("langs: %v", got)
if !errors.Is(err, tt.wantErr) {
t.Errorf("detectViaEnvLc() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("detectViaEnvLc() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseEnvLc(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{"en_US.UTF-8", "en_US.UTF-8", "en_US"},
{"C.UTF-8", "C.UTF-8", "en_US"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseEnvLc(tt.input); got != tt.want {
t.Errorf("parseEnvLc() = %v, want %v", got, tt.want)
}
})
}
}