-
Notifications
You must be signed in to change notification settings - Fork 8
/
locale_shared_test.go
132 lines (108 loc) · 2.79 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
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
package locale
import (
"errors"
"os"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestDetectViaEnvLanguage(t *testing.T) {
Convey("detect via env language", t, func() {
// Make sure env has clear before current test.
setupEnv()
Reset(func() {
// Reset all env after every Convey.
setupEnv()
})
Convey("When LANGUAGE has valid value", func() {
err := os.Setenv("LANGUAGE", "en_US")
if err != nil {
t.Error(err)
}
lang, err := detectViaEnvLanguage()
t.Logf("langs: %v", lang)
Convey("The error should not be nil", func() {
So(err, ShouldBeNil)
})
Convey("The lang should not be equal", func() {
So(lang, ShouldResemble, []string{"en_US"})
})
})
Convey("When LANGUAGE has multiple value", func() {
err := os.Setenv("LANGUAGE", "en_US:zh_CN")
if err != nil {
t.Error(err)
}
lang, err := detectViaEnvLanguage()
t.Logf("langs: %v", lang)
Convey("The error should not be nil", func() {
So(err, ShouldBeNil)
})
Convey("The lang should not be equal", func() {
So(lang, ShouldResemble, []string{"en_US", "zh_CN"})
})
})
Convey("When LANGUAGE is empty", func() {
err := os.Setenv("LANGUAGE", "")
if err != nil {
t.Error(err)
}
lang, err := detectViaEnvLanguage()
t.Logf("langs: %v", lang)
Convey("The error should be ErrNotDetected", func() {
So(errors.Is(err, ErrNotDetected), ShouldBeTrue)
})
Convey("The lang should be empty", func() {
So(lang, ShouldBeEmpty)
})
})
})
}
func TestDetectViaEnvLc(t *testing.T) {
Convey("detect via env language", t, func() {
// Make sure env has clear before current test.
setupEnv()
Reset(func() {
// Reset all env after every Convey.
setupEnv()
})
Convey("When LC_ALL has been set", func() {
err := os.Setenv("LC_ALL", "en_US.UTF-8")
if err != nil {
t.Error(err)
}
lang, err := detectViaEnvLc()
t.Logf("langs: %v", lang)
Convey("The error should not be nil", func() {
So(err, ShouldBeNil)
})
Convey("The lang should not be equal", func() {
So(lang, ShouldResemble, []string{"en_US"})
})
})
Convey("When no LC env has been set", func() {
lang, err := detectViaEnvLc()
Convey("The error should be ErrNotDetected", func() {
So(errors.Is(err, ErrNotDetected), ShouldBeTrue)
})
Convey("The lang should be empty", func() {
So(lang, ShouldBeEmpty)
})
})
})
}
func TestParseEnvLc(t *testing.T) {
Convey("parse env lc", t, func() {
Convey("When input en_US.UTF-8", func() {
x := parseEnvLc("en_US.UTF-8")
Convey("The lang should be en_US", func() {
So(x, ShouldEqual, "en_US")
})
})
Convey("When input C.UTF-8", func() {
x := parseEnvLc("C.UTF-8")
Convey("The lang should be en_US", func() {
So(x, ShouldEqual, "en_US")
})
})
})
}