forked from klauspost/lctime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlctime_test.go
126 lines (109 loc) · 2.26 KB
/
lctime_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
package lctime
import (
"os"
"path/filepath"
"sort"
"strings"
"testing"
)
const (
gotWant = "got '%v', want '%v'"
gotWantKey = "%s: got '%v', want '%v'"
gotWantIdx = "%d: got '%v', want '%v'"
)
func TestSetLocale(t *testing.T) {
tests := []struct {
input string
want error
}{
{"ab_CD", ErrNoLocale},
{"fake", ErrNoLocale},
{"POSIX", nil},
{"en_US", nil},
{"en_US.UTF-8", nil},
{"uz_UZ@cyrillic", nil},
{"es_MX", nil},
{"", ErrNoLocale},
{"nan_TW@latin", nil},
{"eo", nil},
{"sr_RS.UTF-8@latin", nil},
}
for i, test := range tests {
if got := SetLocale(test.input); got != test.want {
t.Errorf(gotWantIdx, i, got, test.want)
}
}
}
func TestGetLocale(t *testing.T) {
tests := []struct {
input string
want string
}{
{"fake", ""},
{"POSIX", "POSIX"},
{"uz_UZ@cyrillic", "uz_UZ@cyrillic"},
{"ab_CD", ""},
{"", ""},
{"nan_TW@latin", "nan_TW@latin"},
{"en_US", "en_US"},
{"es_MX", "es_MX"},
{"eo", "eo"},
{"sr_RS.UTF-8@latin", "sr_RS@latin"},
}
for i, test := range tests {
SetLocale(test.input)
if got := GetLocale(); got != test.want {
t.Errorf(gotWantIdx, i, got, test.want)
}
}
}
func TestGetLocales(t *testing.T) {
want := make([]string, 0, 8)
err := filepath.Walk("internal/locales", func(p string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() || fi.Name()[0] == '.' {
return nil
}
fiName := fi.Name()
if filepath.Ext(fiName) != ".json" {
return nil
}
want = append(want, strings.TrimSuffix(fiName, filepath.Ext(fiName)))
return nil
})
if err != nil {
t.Fatal("Locale walk:", err)
}
got := GetLocales()
sort.Strings(got)
sort.Strings(want)
if len(got) != len(want) {
t.Error("Unexpected locale length")
t.Fatalf(gotWant, len(got), len(want))
}
for i := range want {
if got[i] != want[i] {
t.Errorf(gotWantIdx, i, got[i], want[i])
}
}
}
func TestRemoveCodeset(t *testing.T) {
tests := []struct {
input string
want string
}{
{"", ""},
{"es", "es"},
{"es_MX", "es_MX"},
{"es_MX.UTF-8", "es_MX"},
{"es_MX.UTF-8@foo", "es_MX@foo"},
{"uz_UZ@cyrillic", "uz_UZ@cyrillic"},
}
for i, test := range tests {
if got := removeCodeset(test.input); got != test.want {
t.Errorf(gotWantIdx, i, got, test.want)
}
}
}