-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary_test.go
123 lines (95 loc) · 2.71 KB
/
dictionary_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
package lang
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
var existingLanguages = []string{
"en", "zh", "hi", "es", "fr", "ar", "fa",
}
func TestNewDictionaryManager(t *testing.T) {
t.Run("correct-path", func(t *testing.T) {
dm, err := newDictionaryManager(defaultPath)
assert.Nil(t, err)
assert.NotNil(t, dm)
assert.NotEmpty(t, dm.path)
// since we know the defaultPath exists dm.path should exist.
i, err := os.Stat(dm.path)
assert.Nil(t, err)
assert.True(t, i.IsDir())
})
t.Run("incorrect-path", func(t *testing.T) {
dm, err := newDictionaryManager("path/to/somewhere/else")
assert.Nil(t, err)
assert.NotNil(t, dm)
assert.NotEmpty(t, dm.path)
// since we know the defaultPath exists dm.path should exist.
i, err := os.Stat(dm.path)
assert.NotNil(t, err)
assert.True(t, os.IsNotExist(err))
assert.Nil(t, i)
})
}
func TestDictionaryManager_find(t *testing.T) {
dm, _ := newDictionaryManager(defaultPath)
t.Run("success", func(t *testing.T) {
for _, lang := range existingLanguages {
t.Run(".yml>"+lang, func(t *testing.T) {
key := "user.not_found"
str, ok := dm.find(lang, key)
assert.True(t, ok)
assert.NotEqual(t, key, str)
})
t.Run(".yaml>"+lang, func(t *testing.T) {
key := "validation.password_length_is_insufficient"
str, ok := dm.find(lang, key)
assert.True(t, ok)
assert.NotEqual(t, key, str)
})
t.Run(".json>"+lang, func(t *testing.T) {
key := "messages.insufficient_balance"
str, ok := dm.find(lang, key)
assert.True(t, ok)
assert.NotEqual(t, key, str)
})
}
})
t.Run("failure", func(t *testing.T) {
t.Run("incorrect-key", func(t *testing.T) {
key := "incorrect_key"
str, ok := dm.find(existingLanguages[0], key)
assert.False(t, ok)
assert.Equal(t, key, str)
})
t.Run("unknown-topic", func(t *testing.T) {
key := "unknown_topic.some_key"
str, ok := dm.find(existingLanguages[0], key)
assert.False(t, ok)
assert.Equal(t, key, str)
})
t.Run("unknown-topic-key", func(t *testing.T) {
key := "user.unknown_key"
str, ok := dm.find(existingLanguages[0], key)
assert.False(t, ok)
assert.Equal(t, key, str)
})
t.Run("unknown-locale", func(t *testing.T) {
key := "user.not_found"
str, ok := dm.find("nl", key)
assert.False(t, ok)
assert.Equal(t, key, str)
})
t.Run("malformed-yaml", func(t *testing.T) {
key := "malformed_yaml.some_key"
str, ok := dm.find(existingLanguages[0], key)
assert.False(t, ok)
assert.Equal(t, key, str)
})
t.Run("malformed-json", func(t *testing.T) {
key := "malformed_json.some_key"
str, ok := dm.find(existingLanguages[0], key)
assert.False(t, ok)
assert.Equal(t, key, str)
})
})
}