-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore_test.go
93 lines (82 loc) · 1.67 KB
/
store_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
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package i18n
import (
"io/ioutil"
"log"
"os"
"path"
"reflect"
"testing"
)
var (
tempDir string
)
func TestMain(m *testing.M) {
var err error
tempDir, err = ioutil.TempDir(os.TempDir(), "translations")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tempDir)
enTempDir := path.Join(tempDir, "en")
err = os.Mkdir(enTempDir, 0744)
if err != nil {
log.Fatal(err)
}
file, err := ioutil.TempFile(enTempDir, "test.json")
if err != nil {
log.Fatal(err)
}
_, err = file.Write([]byte(`{"hello": "Hello"}`))
if err != nil {
log.Fatal(err)
}
zhTempDir := path.Join(tempDir, "zh")
err = os.Mkdir(zhTempDir, 0744)
if err != nil {
log.Fatal(err)
}
file, err = ioutil.TempFile(zhTempDir, "test.json")
if err != nil {
log.Fatal(err)
}
_, err = file.Write([]byte(`{"hello": "你好"}`))
if err != nil {
log.Fatal(err)
}
os.Exit(m.Run())
}
func TestFileStore(t *testing.T) {
tests := []struct {
dir string
shouldError bool
translations Translations
}{
{"", true, nil},
{tempDir, false, Translations{
"en": {
"hello": "Hello",
},
"zh": {
"hello": "你好",
},
}},
}
for _, test := range tests {
fs := NewFileStore(test.dir, JSONFileDecoder{})
translations, err := fs.Get()
if test.shouldError {
if err == nil {
t.Error("expected an error, got nil")
}
continue
}
if !reflect.DeepEqual(test.translations, translations) {
t.Errorf("expected translations %+v, got %+v", test.translations, translations)
}
}
}
func TestJSONDecoder(t *testing.T) {
}