-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslation_test.go
143 lines (123 loc) · 4.33 KB
/
translation_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
133
134
135
136
137
138
139
140
141
142
143
package i18n
import (
"testing"
)
const (
Validity = "test_data/validity/"
Count = "test_data/count/"
)
func TestLanguage(t *testing.T) {
fn := func(code string, expected bool) func(t *testing.T) {
return func(t *testing.T) {
lang := Language(code)
if lang.Valid() != expected {
t.Fail()
}
}
}
t.Run("empty", fn("", false))
t.Run("too long", fn("een", false))
t.Run("too short", fn("k", false))
t.Run("non alpha", fn("43", false))
t.Run("part alpha", fn("d3", false))
t.Run("valid", fn("de", true))
}
func TestKey(t *testing.T) {
fn := func(keys []string, expected string) func(t *testing.T) {
return func(t *testing.T) {
var rootKey Key
for _, key := range keys {
rootKey = rootKey.Append(key)
}
if rootKey.String() != expected {
t.Fail()
}
}
}
t.Run("empty", fn([]string{}, ""))
t.Run("empty level", fn([]string{""}, ""))
t.Run("one level", fn([]string{"a"}, "a"))
t.Run("two levels", fn([]string{"a", "b"}, "a.b"))
t.Run("three levels", fn([]string{"a", "b", "c"}, "a.b.c"))
t.Run("empty levels", fn([]string{"a", "b", "", "d"}, "a.b.d"))
t.Run("invalid level", fn([]string{"a", ".", "b", "c"}, "a.b.c"))
t.Run("leading / trailing", fn([]string{"a", ".b.", "c"}, "a.b.c"))
t.Run("non-alpha", fn([]string{"a", "/", "#", "?"}, "a./.#.?"))
}
func TestLoad(t *testing.T) {
fn := func(directory string, expected bool) func(t *testing.T) {
return func(t *testing.T) {
_, err := NewTranslations(directory, "en").Load()
got := (err == nil)
if got != expected {
t.Fatalf("expected %v, got %v: %v", expected, got, err)
}
}
}
t.Run("no file", fn(Validity+"noooooo", false))
t.Run("invalid file format", fn(Validity+"invalid_file_format", false))
t.Run("invalid json", fn(Validity+"invalid_json", false))
t.Run("invalid key #1", fn(Validity+"invalid_key_1", false))
t.Run("invalid key #2", fn(Validity+"invalid_key_2", false))
t.Run("duplicate key", fn(Validity+"duplicate_key", false))
t.Run("invalid lang", fn(Validity+"invalid_lang", false))
t.Run("invalid intermediate #1", fn(Validity+"invalid_intermediate_1", false))
t.Run("invalid intermediate #2", fn(Validity+"invalid_intermediate_2", false))
t.Run("invalid intermediate #3", fn(Validity+"invalid_intermediate_3", false))
t.Run("invalid translation type #1", fn(Validity+"invalid_translation_type_1", false))
t.Run("invalid translation type #2", fn(Validity+"invalid_translation_type_2", false))
t.Run("empty", fn(Validity+"empty", false))
t.Run("empty translation", fn(Validity+"empty_translation", false))
t.Run("valid", fn(Validity+"valid", true))
}
func TestNumberTranslations(t *testing.T) {
fn := func(directory string, expected int) func(t *testing.T) {
return func(t *testing.T) {
translations, err := NewTranslations(directory, "en").Load()
if err != nil {
t.Fatal(err)
}
store := translations.translations[translations.defaultLanguage]
if len(store) != expected {
t.Fatalf("expected %v translations, got %v", expected, len(store))
}
}
}
t.Run("first", fn(Count+"first", 9))
t.Run("second", fn(Count+"second", 5))
}
func TestNumberIntermediates(t *testing.T) {
fn := func(directory string, key string, expected int) func(t *testing.T) {
return func(t *testing.T) {
translations, err := NewTranslations(directory, "en").Load()
if err != nil {
t.Fatal(err)
}
store := translations.translations[translations.defaultLanguage]
if _, ok := store[Key(key)]; !ok {
t.Fatalf("could not find key %q", key)
}
translation := store[Key(key)]
if len(translation.Intermediates) != expected {
t.Fatalf("expected %v translations for %q, got %v", expected, key, len(translation.Intermediates))
}
}
}
t.Run("first #1", fn(Count+"first", "whoami", 1))
t.Run("first #2", fn(Count+"first", "expired", 2))
t.Run("first #3", fn(Count+"first", "tyson.defeated", 1))
t.Run("second #1", fn(Count+"second", "hi", 3))
t.Run("second #2", fn(Count+"second", "whoami", 0))
t.Run("second #3", fn(Count+"second", "expired", 0))
t.Run("second #4", fn(Count+"second", "tyson.defeated", 0))
}
func TestAvailableLanguages(t *testing.T) {
translations, err := NewTranslations(Validity+"valid", "en").Load()
if err != nil {
t.Fatal(err)
}
expected := 1
if l := len(translations.AvailableLanguages()); l != expected {
t.Fatalf("expected %d and not %d available languages", expected, l)
}
}