-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathintegration_tests.rs
326 lines (278 loc) · 9.01 KB
/
integration_tests.rs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
use std::collections::HashMap;
struct TestBackend {
trs: HashMap<String, String>,
}
impl TestBackend {
fn new() -> Self {
let mut trs = HashMap::new();
trs.insert("foo".into(), "pt-fake.foo".to_string());
Self { trs }
}
}
impl rust_i18n::Backend for TestBackend {
fn available_locales(&self) -> Vec<&str> {
vec!["pt", "en"]
}
fn translate(&self, locale: &str, key: &str) -> Option<&str> {
if locale == "pt" {
return self.trs.get(key).map(|v| v.as_str());
}
None
}
}
rust_i18n::i18n!(
"./tests/locales",
fallback = "en",
backend = TestBackend::new()
);
#[cfg(test)]
mod tests {
use rust_i18n::t;
use rust_i18n_support::load_locales;
mod test0 {
rust_i18n::i18n!("./tests/locales");
}
mod test1 {
rust_i18n::i18n!("./tests/locales", fallback = "en");
#[test]
fn test_fallback() {
assert_eq!(
crate::tests::test1::_rust_i18n_translate("en", "missing.default"),
"This is missing key fallbacked to en."
);
}
}
mod test2 {
rust_i18n::i18n!("./tests/locales", fallback = "zh-CN");
#[test]
fn test_fallback() {
assert_eq!(
crate::tests::test2::_rust_i18n_translate("en", "fallback_to_cn"),
"这是一个中文的翻译。"
);
}
}
mod test3 {
rust_i18n::i18n!(fallback = "foo");
}
mod test4 {
rust_i18n::i18n!("./tests/locales", fallback = ["zh", "en"]);
#[test]
fn test_fallback() {
assert_eq!(
crate::tests::test4::_rust_i18n_translate("zh-CN", "messages.zero"),
"你没有消息。"
);
assert_eq!(
crate::tests::test4::_rust_i18n_translate("zh-CN", "messages.one"),
"You have one message."
);
}
}
mod test5 {
rust_i18n::i18n!();
}
#[test]
fn check_test_environment() {
assert_eq!(
std::env::var("RUST_TEST_THREADS").unwrap_or_else(|_| "0".to_string()),
"1",
"The tests assume that they are run in single-threaded environment because \
setting the locale is a global state. If using cargo version prior to 1.56, \
You should set RUST_TEST_THREADS instead of running with --test-threads."
);
}
#[test]
fn test_load() {
assert!(load_locales("./tests/locales", |_| false).contains_key("en"));
}
#[test]
fn test_translate() {
assert_eq!(
crate::_rust_i18n_translate("en", "hello"),
"Bar - Hello, World!"
);
}
#[test]
fn test_available_locales() {
assert_eq!(
rust_i18n::available_locales!(),
&["de", "en", "fr", "ja", "ko", "pt", "ru", "vi", "zh", "zh-CN"]
);
}
#[test]
fn test_t() {
rust_i18n::set_locale("en");
assert_eq!(t!("hello"), "Bar - Hello, World!");
// Vars
assert_eq!(
t!("a.very.nested.message"),
"Hello, %{name}. Your message is: %{msg}"
);
assert_eq!(
t!("a.very.nested.message", name = "Jason"),
"Hello, Jason. Your message is: %{msg}"
);
assert_eq!(
t!("a.very.nested.message", name = "Jason", msg = "Bla bla"),
"Hello, Jason. Your message is: Bla bla"
);
rust_i18n::set_locale("zh-CN");
assert_eq!(t!("messages.hello", name = "world"), "你好,world!");
rust_i18n::set_locale("en");
assert_eq!(t!("messages.hello", name = "world"), "Hello, world!");
}
#[test]
fn test_t_with_tt_val() {
rust_i18n::set_locale("en");
assert_eq!(t!("messages.other", count = 100), "You have 100 messages.");
assert_eq!(
t!("messages.other", count = 1.01),
"You have 1.01 messages."
);
assert_eq!(t!("messages.other", count = 1 + 2), "You have 3 messages.");
// Test end with a comma
assert_eq!(
t!("messages.other", locale = "zh-CN", count = 1 + 2,),
"你收到了 3 条新消息。"
);
let a = 100;
assert_eq!(t!("messages.other", count = a / 2), "You have 50 messages.");
}
#[test]
fn test_t_with_locale_and_args() {
rust_i18n::set_locale("en");
assert_eq!(t!("hello", locale = "zh-CN"), "Bar - 你好世界!");
assert_eq!(t!("hello", locale = "en"), "Bar - Hello, World!");
assert_eq!(t!("messages.hello", name = "Jason"), "Hello, Jason!");
assert_eq!(
t!("messages.hello", locale = "en", name = "Jason"),
"Hello, Jason!"
);
// Invalid locale position, will ignore
assert_eq!(
t!("messages.hello", name = "Jason", locale = "en"),
"Hello, Jason!"
);
assert_eq!(
t!("messages.hello", locale = "zh-CN", name = "Jason"),
"你好,Jason!"
);
}
#[test]
fn test_t_with_hash_args() {
rust_i18n::set_locale("en");
// Hash args
assert_eq!(t!("messages.hello", name => "Jason"), "Hello, Jason!");
assert_eq!(t!("messages.hello", "name" => "Jason"), "Hello, Jason!");
assert_eq!(
t!("messages.hello", locale = "zh-CN", "name" => "Jason"),
"你好,Jason!"
);
}
#[test]
fn test_with_merge_file() {
rust_i18n::set_locale("en");
assert_eq!(t!("user.title"), "User Title");
assert_eq!(t!("messages.user.title"), "Message User Title");
}
#[test]
fn test_support_expr() {
rust_i18n::set_locale("en");
let name = "Jason Lee";
let locale = "en";
let key = "messages.hello";
let dyn_key = format!("messages.{}", "hello");
assert_eq!(t!(&dyn_key, name = name), "Hello, Jason Lee!");
assert_eq!(t!(key, name = name), "Hello, Jason Lee!");
assert_eq!(t!("messages.hello", name = name), "Hello, Jason Lee!");
assert_eq!(
t!("messages.hello", name = &format!("this is {name}")),
"Hello, this is Jason Lee!"
);
assert_eq!(t!("messages.hello", locale = locale), "Hello, %{name}!");
assert_eq!(
t!("messages.hello", locale = locale, name = name),
"Hello, Jason Lee!"
);
}
#[test]
fn test_fallback_missing_locale() {
assert_eq!(
t!("missing.default", locale = "zh-CN"),
"This is missing key fallbacked to en."
);
assert_eq!(
t!("missing.default", locale = "foo"),
"This is missing key fallbacked to en."
);
}
#[test]
fn test_multiple_formats() {
// Test from JSON
assert_eq!(t!("json-key", locale = "en"), "This is from test.json");
assert_eq!(
t!("custom.json-key", locale = "en"),
"This is from nested merged from test.json"
);
// Test from TOML
assert_eq!(t!("toml-key", locale = "en"), "This is a toml key");
assert_eq!(
t!("custom.toml-key", locale = "en"),
"This is a toml key under the custom"
);
assert_eq!(
t!("custom.foo.toml-key", locale = "en"),
"This is a toml key under the custom.foo"
);
}
#[test]
fn test_extend_backend() {
assert_eq!(t!("foo", locale = "pt"), "pt-fake.foo")
}
#[test]
fn test_nested_locale_texts() {
assert_eq!(t!("nested_locale_test", locale = "en"), "Hello test");
assert_eq!(t!("nested_locale_test", locale = "zh-CN"), "你好 test");
assert_eq!(t!("nested_locale_test", locale = "ja"), "こんにちは test");
assert_eq!(t!("nested_locale_test.hello", locale = "en"), "Hello test2");
assert_eq!(
t!("nested_locale_test.hello", locale = "zh-CN"),
"你好 test2"
);
assert_eq!(
t!("nested_locale_test.hello", locale = "ja"),
"こんにちは test2"
);
assert_eq!(
t!("nested_locale_test.hello.world", locale = "en"),
"Hello test3"
);
assert_eq!(
t!("nested_locale_test.hello.world", locale = "zh-CN"),
"你好 test3"
);
assert_eq!(
t!("nested_locale_test.hello.world", locale = "ja"),
"こんにちは test3"
);
}
#[test]
fn test_lookup_fallback() {
assert_eq!(
t!("missing.lookup-fallback", locale = "zh-CN"),
"在 zh-XXX 中缺失的的翻译。"
);
assert_eq!(
t!("missing.default", locale = "zh-CN", fallback = "en"),
"This is missing key fallbacked to en."
);
}
#[test]
fn test_set_locale() {
rust_i18n::set_locale("zh-CN");
for _ in 0..5 {
assert_eq!(t!("hello"), "Bar - 你好世界!");
}
}
}