-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_test.go
133 lines (120 loc) · 3.4 KB
/
set_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
package translator
import (
"fmt"
"github.com/donseba/gotext"
"testing"
)
var (
testSetLanguageTL = "en_TL"
testSetLanguageTN = "en_TN"
)
func TestTranslator_SetTL(t *testing.T) {
type fields struct {
translationsDir string
templateDir string
languages map[string]*gotext.Po
uniqueKeys map[string]uniqueKey
uniqueKeysCtx map[string]map[string]uniqueKey
}
type args struct {
key string
plural string
n int
value string
}
localizerTL := NewLocalizer(testSetLanguageTL)
localizerTN := NewLocalizer(testSetLanguageTN)
tests := []struct {
name string
mode string
loc Localizer
lang string
fields fields
args []args
wantErr bool
}{
{
name: "Test SetTL",
mode: "SetTL",
lang: testSetLanguageTL,
fields: fields{
translationsDir: "test_translations",
templateDir: "test_templates",
uniqueKeys: map[string]uniqueKey{},
uniqueKeysCtx: map[string]map[string]uniqueKey{},
},
loc: localizerTL,
args: []args{
{key: "test_key", value: "test_value"},
{key: "test_key2", value: "test_value2"},
{key: "test_key3", value: "test_value3"},
},
wantErr: false,
},
{
name: "Test SetTN",
mode: "SetTN",
lang: testSetLanguageTN,
fields: fields{
translationsDir: "test_translations",
templateDir: "test_templates",
uniqueKeys: map[string]uniqueKey{},
uniqueKeysCtx: map[string]map[string]uniqueKey{},
},
loc: localizerTN,
args: []args{
{key: "test_key", plural: "test_plural", n: 0, value: "test_value 0"},
{key: "test_key", plural: "test_plural", n: 1, value: "test_value 1"},
{key: "test_key", plural: "test_plural", n: 2, value: "test_value 2"},
{key: "test_key", plural: "test_plural", n: 3, value: "test_value 2"},
{key: "test_key2", plural: "test_plural2", n: 2, value: "test_value2"},
{key: "test_key3", plural: "test_plural3", n: 3, value: "test_value3"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := &Translator{
translationsDir: tt.fields.translationsDir,
templateDir: tt.fields.templateDir,
uniqueKeys: tt.fields.uniqueKeys,
uniqueKeysCtx: tt.fields.uniqueKeysCtx,
}
err := tr.NewLanguage(tt.loc, map[string]string{
"Language-Team": fmt.Sprintf("%s %s", "English", tt.mode),
"Last-Translator:": "Don Seba <donseba[at]someemail.com>",
"Language": tt.lang,
"Content-Type": "text/plain; charset=UTF-8",
"Plural-Forms": "nplurals=2; plural=(n != 1)",
"X-Generator": "Poedit 2.2.4",
"Project-Id-Version": "TEST VERSION",
})
if err != nil {
t.Errorf("error = %v", err)
}
for _, arg := range tt.args {
switch tt.mode {
case "SetTL":
if err := tr.SetTL(tt.loc, arg.key, arg.value); (err != nil) != tt.wantErr {
t.Errorf("Translator.SetTL() error = %v, wantErr %v", err, tt.wantErr)
}
case "SetTN":
if err := tr.SetTLN(tt.loc, arg.key, arg.plural, arg.n, arg.value); (err != nil) != tt.wantErr {
t.Errorf("Translator.SetTL() error = %v, wantErr %v", err, tt.wantErr)
}
}
}
tls := tr.languages[tt.lang].GetTranslations()
for _, tl := range tls {
fmt.Printf("%+v\n", tl)
}
err = tr.Write(tt.loc)
if err != nil {
t.Errorf("error = %v", err)
}
})
}
}
func NewLocalizer(s string) Localizer {
return mockLocalizer{locale: s}
}