/
file_test.go
127 lines (105 loc) · 2.64 KB
/
file_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
package i18n
import (
"os"
"path"
"testing"
"path/filepath"
assert "github.com/pilu/miniassert"
)
func withTestData(files map[string]string, callback func(string)) {
folderName := "test_locales"
err := os.Mkdir(folderName, 0755)
if err != nil {
panic(err)
}
defer func() {
os.RemoveAll(folderName)
}()
for fileName, content := range files {
filePath := path.Join(folderName, fileName)
file, err := os.Create(filePath)
if err != nil {
panic(err)
}
file.WriteString(content)
}
callback(folderName)
}
func TestLoadFile(t *testing.T) {
defer resetLocales()
files := map[string]string{
"en.conf": `
greeting: hello
[en_GB]
greeting: hello GB
[en_US]
greeting: hello US
[en_GB]
greeting2: hello 2 GB
`,
}
withTestData(files, func(folderPath string) {
err := loadFile(filepath.Join(folderPath, "en.conf"))
assert.Nil(t, err)
assert.Equal(t, 3, len(locales))
tests := [][]string {
[]string{"en", "greeting", "hello"},
[]string{"en_GB", "greeting", "hello GB"},
[]string{"en_GB", "greeting2", "hello 2 GB"},
[]string{"en_US", "greeting", "hello US"},
}
for _, items := range tests {
code := items[0]
key := items[1]
value := items[2]
locale := GetLocale(code)
assert.NotNil(t, locale)
assert.Type(t, "*i18n.Locale", locale)
assert.Equal(t, code, locale.Code)
tr := locale.Translations[key]
assert.Equal(t, value, tr.Value())
}
})
}
func TestLoad(t *testing.T) {
defer resetLocales()
files := map[string]string{
"en.conf": `
greeting: hello
`,
"it.conf": `
greeting: ciao
`,
"other_en.conf": `
[en] // add more translations for locale "en"
greeting2: hello2
`,
"other_it.conf": `
[it] // add more translations for locale "it"
greeting2: ciao2
`,
"es.conf": `// blank file`,
}
withTestData(files, func(folderPath string) {
err := Load(filepath.Join(folderPath, "*.conf"))
assert.Nil(t, err)
assert.Equal(t, 2, len(locales))
tests := [][]string {
[]string{"en", "greeting", "hello"},
[]string{"en", "greeting2", "hello2"},
[]string{"it", "greeting", "ciao"},
[]string{"it", "greeting2", "ciao2"},
}
for _, items := range tests {
code := items[0]
key := items[1]
value := items[2]
locale := GetLocale(code)
assert.NotNil(t, locale)
assert.Type(t, "*i18n.Locale", locale)
assert.Equal(t, code, locale.Code)
tr := locale.Translations[key]
assert.Equal(t, value, tr.Value())
}
})
}