-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintegration_embedded_modes.rs
More file actions
331 lines (276 loc) · 9.25 KB
/
integration_embedded_modes.rs
File metadata and controls
331 lines (276 loc) · 9.25 KB
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
327
328
329
330
331
use anyhow::Result;
use std::fs;
use std::path::Path;
use tempfile::TempDir;
/// Helper to create a test project with translations
fn create_test_project(temp_dir: &Path, mode: &str) -> Result<()> {
// Create translations directory first
let translations_dir = temp_dir.join("translations");
fs::create_dir_all(&translations_dir)?;
// Create English translations
let en_content = r#"{
"ui": {
"button": "Buy",
"message": "Hello, {name}!"
},
"game": {
"score": "Score: {score:int}"
}
}"#;
fs::write(translations_dir.join("en.json"), en_content)?;
// Create Indonesian translations
let id_content = r#"{
"ui": {
"button": "Beli",
"message": "Halo, {name}!"
},
"game": {
"score": "Skor: {score:int}"
}
}"#;
fs::write(translations_dir.join("id.json"), id_content)?;
// Create config with absolute paths
let translations_path = translations_dir.to_str().unwrap();
let output_path = temp_dir.join("output").to_str().unwrap().to_string();
let config_content = format!(
r#"base_locale: en
supported_locales:
- en
- id
input_directory: {}
output_directory: {}
localization:
mode: {}
"#,
translations_path, output_path, mode
);
fs::write(temp_dir.join("slang-roblox.yaml"), config_content)?;
Ok(())
}
#[test]
fn test_embedded_mode_end_to_end() -> Result<()> {
let temp_dir = TempDir::new()?;
create_test_project(temp_dir.path(), "embedded")?;
// Run build
let config_path = temp_dir.path().join("slang-roblox.yaml");
roblox_slang::cli::build::build(&config_path)?;
// Check output files exist
let output_file = temp_dir.path().join("output/Translations.lua");
assert!(output_file.exists(), "Translations.lua should be generated");
let types_file = temp_dir.path().join("output/types/Translations.d.luau");
assert!(types_file.exists(), "Type definitions should be generated");
let csv_file = temp_dir.path().join("output/roblox_upload.csv");
assert!(csv_file.exists(), "CSV file should be generated");
// Check generated code contains embedded data
let code = fs::read_to_string(&output_file)?;
assert!(
code.contains("EMBEDDED_TRANSLATIONS"),
"Should contain embedded data"
);
assert!(
code.contains(r#"["en"] = {"#),
"Should contain English locale"
);
assert!(
code.contains(r#"["id"] = {"#),
"Should contain Indonesian locale"
);
assert!(
code.contains(r#"["ui.button"] = "Buy""#),
"Should contain translation"
);
// Should NOT contain LocalizationService
assert!(
!code.contains("LocalizationService"),
"Should not contain LocalizationService in embedded mode"
);
assert!(
!code.contains("FormatByKey"),
"Should not contain FormatByKey in embedded mode"
);
Ok(())
}
#[test]
fn test_cloud_mode_end_to_end() -> Result<()> {
let temp_dir = TempDir::new()?;
create_test_project(temp_dir.path(), "cloud")?;
// Run build
let config_path = temp_dir.path().join("slang-roblox.yaml");
roblox_slang::cli::build::build(&config_path)?;
// Check output files exist
let output_file = temp_dir.path().join("output/Translations.lua");
assert!(output_file.exists());
// Check generated code uses LocalizationService
let code = fs::read_to_string(&output_file)?;
assert!(
!code.contains("EMBEDDED_TRANSLATIONS"),
"Should NOT contain embedded data in cloud mode"
);
assert!(
code.contains("LocalizationService"),
"Should contain LocalizationService"
);
assert!(code.contains("FormatByKey"), "Should contain FormatByKey");
Ok(())
}
#[test]
fn test_hybrid_mode_end_to_end() -> Result<()> {
let temp_dir = TempDir::new()?;
create_test_project(temp_dir.path(), "hybrid")?;
// Run build
let config_path = temp_dir.path().join("slang-roblox.yaml");
roblox_slang::cli::build::build(&config_path)?;
// Check output files exist
let output_file = temp_dir.path().join("output/Translations.lua");
assert!(output_file.exists());
// Check generated code uses BOTH embedded and cloud
let code = fs::read_to_string(&output_file)?;
assert!(
code.contains("EMBEDDED_TRANSLATIONS"),
"Should contain embedded data"
);
assert!(
code.contains("LocalizationService"),
"Should contain LocalizationService"
);
assert!(code.contains("FormatByKey"), "Should contain FormatByKey");
assert!(
code.contains("pcall"),
"Should use pcall for cloud fallback"
);
Ok(())
}
#[test]
fn test_default_mode_is_embedded() -> Result<()> {
let temp_dir = TempDir::new()?;
// Create translations directory first
let translations_dir = temp_dir.path().join("translations");
fs::create_dir_all(&translations_dir)?;
// Create translations
let en_content = r#"{"test": {"key": "value"}}"#;
fs::write(translations_dir.join("en.json"), en_content)?;
// Create config WITHOUT localization section (using absolute paths)
let translations_path = translations_dir.to_str().unwrap();
let output_path = temp_dir.path().join("output").to_str().unwrap().to_string();
let config_content = format!(
r#"base_locale: en
supported_locales:
- en
input_directory: {}
output_directory: {}
"#,
translations_path, output_path
);
fs::write(temp_dir.path().join("slang-roblox.yaml"), config_content)?;
// Run build
let config_path = temp_dir.path().join("slang-roblox.yaml");
roblox_slang::cli::build::build(&config_path)?;
// Check generated code defaults to embedded mode
let output_file = temp_dir.path().join("output/Translations.lua");
let code = fs::read_to_string(&output_file)?;
assert!(
code.contains("EMBEDDED_TRANSLATIONS"),
"Should default to embedded mode"
);
assert!(
!code.contains("LocalizationService"),
"Should not contain LocalizationService by default"
);
Ok(())
}
#[test]
fn test_multiple_locales_embedded() -> Result<()> {
let temp_dir = TempDir::new()?;
// Create translations directory first
let translations_dir = temp_dir.path().join("translations");
fs::create_dir_all(&translations_dir)?;
// Create translations
fs::write(translations_dir.join("en.json"), r#"{"test": "English"}"#)?;
fs::write(
translations_dir.join("id.json"),
r#"{"test": "Indonesian"}"#,
)?;
fs::write(translations_dir.join("es.json"), r#"{"test": "Spanish"}"#)?;
// Create config with 3 locales (using absolute paths)
let translations_path = translations_dir.to_str().unwrap();
let output_path = temp_dir.path().join("output").to_str().unwrap().to_string();
let config_content = format!(
r#"base_locale: en
supported_locales:
- en
- id
- es
input_directory: {}
output_directory: {}
localization:
mode: embedded
"#,
translations_path, output_path
);
fs::write(temp_dir.path().join("slang-roblox.yaml"), config_content)?;
// Run build
let config_path = temp_dir.path().join("slang-roblox.yaml");
roblox_slang::cli::build::build(&config_path)?;
// Check all locales are embedded
let output_file = temp_dir.path().join("output/Translations.lua");
let code = fs::read_to_string(&output_file)?;
assert!(code.contains(r#"["en"] = {"#));
assert!(code.contains(r#"["id"] = {"#));
assert!(code.contains(r#"["es"] = {"#));
assert!(code.contains(r#""English""#));
assert!(code.contains(r#""Indonesian""#));
assert!(code.contains(r#""Spanish""#));
Ok(())
}
#[test]
fn test_special_characters_embedded() -> Result<()> {
let temp_dir = TempDir::new()?;
// Create translations directory first
let translations_dir = temp_dir.path().join("translations");
fs::create_dir_all(&translations_dir)?;
// Add translations with special characters
let special_content = r#"{
"special": {
"quotes": "He said \"Hello\"",
"newline": "Line1\nLine2",
"tab": "Col1\tCol2",
"backslash": "Path\\to\\file"
}
}"#;
fs::write(translations_dir.join("en.json"), special_content)?;
// Create config (using absolute paths)
let translations_path = translations_dir.to_str().unwrap();
let output_path = temp_dir.path().join("output").to_str().unwrap().to_string();
let config_content = format!(
r#"base_locale: en
supported_locales:
- en
input_directory: {}
output_directory: {}
localization:
mode: embedded
"#,
translations_path, output_path
);
fs::write(temp_dir.path().join("slang-roblox.yaml"), config_content)?;
// Run build
let config_path = temp_dir.path().join("slang-roblox.yaml");
roblox_slang::cli::build::build(&config_path)?;
// Check special characters are properly escaped
let output_file = temp_dir.path().join("output/Translations.lua");
let code = fs::read_to_string(&output_file)?;
assert!(
code.contains(r#"He said \"Hello\""#),
"Quotes should be escaped"
);
assert!(
code.contains(r#"Line1\nLine2"#),
"Newlines should be escaped"
);
assert!(code.contains(r#"Col1\tCol2"#), "Tabs should be escaped");
assert!(
code.contains(r#"Path\\to\\file"#),
"Backslashes should be escaped"
);
Ok(())
}