forked from FlineDev/BartyCrouch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringsFileUpdaterTests.swift
More file actions
511 lines (420 loc) · 26.2 KB
/
StringsFileUpdaterTests.swift
File metadata and controls
511 lines (420 loc) · 26.2 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//
// Created by Cihat Gündüz on 11.02.16.
// Copyright © 2016 Flinesoft. All rights reserved.
//
// swiftlint:disable file_length
// swiftlint:disable function_body_length
// swiftlint:disable type_body_length
@testable import BartyCrouchKit
import XCTest
class StringsFileUpdaterTests: XCTestCase {
// MARK: - Stored Instance Properties
static let stringsFilesDirPath = "\(BASE_DIR)/Tests/Assets/Strings Files"
let oldStringsFilePath = "\(stringsFilesDirPath)/OldExample.strings"
let longOldStringsFilePath = "\(stringsFilesDirPath)/LongOldExample.strings"
let newStringsFilePath = "\(stringsFilesDirPath)/NewExample.strings"
let longNewStringsFilePath = "\(stringsFilesDirPath)/LongNewExample.strings"
let testStringsFilePath = "\(stringsFilesDirPath)/TestExample.strings"
func testStringsFilePath(_ iteration: Int) -> String {
return "\(StringsFileUpdaterTests.stringsFilesDirPath)/TestExample\(iteration).strings"
}
let testExamplesRange = 0...1
// MARK: - Test Callbacks
override func setUp() {
super.setUp()
// ensure temporary files are cleaned up before testing
do {
try FileManager.default.removeItem(atPath: self.testStringsFilePath)
} catch { print("No TestExample.strings to clean up") }
do {
for index in self.testExamplesRange {
try FileManager.default.removeItem(atPath: self.testStringsFilePath(index))
}
} catch { print("No TestExample{i}.strings to clean up") }
}
override func tearDown() {
super.tearDown()
// cleanup temporary files after testing
do {
try FileManager.default.removeItem(atPath: self.testStringsFilePath)
} catch { print("No TestExample.strings to clean up") }
do {
for index in self.testExamplesRange {
try FileManager.default.removeItem(atPath: self.testStringsFilePath(index))
}
} catch { print("No TestExample{i}.strings to clean up") }
}
// MARK: - Unit Tests
func testFindTranslationsInLines() {
let stringsFileUpdater = StringsFileUpdater(path: oldStringsFilePath)!
let expectedTranslations = [
("35F-cl-mdI.normalTitle", "Example Button 1", " Class = \"UIButton\"; normalTitle = \"Example Button 1\"; ObjectID = \"35F-cl-mdI\"; "),
("COa-YO-eGf.normalTitle", "Already Translated", "! Class = \"UIButton\"; normalTitle = \"Example Button 2\"; ObjectID = \"COa-YO-eGf\"; "),
("cHL-Zc-L39.normalTitle", "Example Button 3", " Class = \"UIButton\"; normalTitle = \"Example Button 3\"; ObjectID = \"cHL-Zc-L39\"; "),
("test.key", "This is a test key", " Completely custom comment structure in one line "),
("test.key.ignored", "This is a test key to be ignored #bc-ignore!", " Completely custom comment structure in one line to be ignored "),
("test.key.ignoreEmptyStrings", " ", " This key should be ignored when ignoreEmptyKeys is set "),
("abc-12-345.normalTitle", "😀", " Class = \"UIButton\"; normalTitle = \"😀\"; ObjectID = \"abc-12-345\"; "),
("em1-3S-vgp.text", "Refrakční vzdálenost v metrech",
" Class = \"UILabel\"; text = \"Refraktionsentfernung in Meter\"; ObjectID = \"em1-3S-vgp\"; ")
]
let results = stringsFileUpdater.findTranslations(inString: stringsFileUpdater.oldContentString)
XCTAssertEqual(results.count, expectedTranslations.count)
var index = 0
expectedTranslations.forEach { key, value, comment in
XCTAssertGreaterThan(results.count, index)
XCTAssertEqual(results[index].0, key)
XCTAssertEqual(results[index].1, value)
XCTAssertEqual(results[index].2, comment)
index += 1
}
}
func testStringFromTranslations() {
let translations: [StringsFileUpdater.TranslationEntry] = [
("key1", "value1", "comment1", 1),
("key2", "value2", nil, 2),
("key3", "value3", "comment3", 3)
]
let expectedString = "\n/*comment1*/\n\"key1\" = \"value1\";\n\n\"key2\" = \"value2\";\n\n/*comment3*/\n\"key3\" = \"value3\";\n"
let stringsFileUpdater = StringsFileUpdater(path: oldStringsFilePath)!
let resultingString = stringsFileUpdater.stringFromTranslations(translations: translations)
XCTAssertEqual(resultingString, expectedString)
}
func testExampleStringsFileWithEmptyNewValues() {
do {
try FileManager.default.copyItem(atPath: oldStringsFilePath, toPath: testStringsFilePath)
let stringsFileUpdater = StringsFileUpdater(path: testStringsFilePath)!
let expectedLinesBeforeIncrementalUpdate = [
"/* Class = \"UIButton\"; normalTitle = \"Example Button 1\"; ObjectID = \"35F-cl-mdI\"; */",
"\"35F-cl-mdI.normalTitle\" = \"Example Button 1\";", "",
"/*! Class = \"UIButton\"; normalTitle = \"Example Button 2\"; ObjectID = \"COa-YO-eGf\"; */",
"\"COa-YO-eGf.normalTitle\" = \"Already Translated\";", "",
"/* Class = \"UIButton\"; normalTitle = \"Example Button 3\"; ObjectID = \"cHL-Zc-L39\"; */",
"\"cHL-Zc-L39.normalTitle\" = \"Example Button 3\";", "",
"/* Completely custom comment structure in one line */",
"\"test.key\" = \"This is a test key\";", "",
"/* Completely custom comment structure in one line to be ignored */",
"\"test.key.ignored\" = \"This is a test key to be ignored #bc-ignore!\";", "",
"/* This key should be ignored when ignoreEmptyKeys is set */",
"\"test.key.ignoreEmptyStrings\" = \" \";", "",
"/* Class = \"UIButton\"; normalTitle = \"😀\"; ObjectID = \"abc-12-345\"; */",
"\"abc-12-345.normalTitle\" = \"😀\";", "",
"/* Class = \"UILabel\"; text = \"Refraktionsentfernung in Meter\"; ObjectID = \"em1-3S-vgp\"; */",
"\"em1-3S-vgp.text\" = \"Refrakční vzdálenost v metrech\";", "", ""
]
var oldLinesInFile = stringsFileUpdater.oldContentString.components(separatedBy: .newlines)
XCTAssertEqual(oldLinesInFile.count, expectedLinesBeforeIncrementalUpdate.count)
for (index, expectedLine) in expectedLinesBeforeIncrementalUpdate.enumerated() {
XCTAssertEqual(oldLinesInFile[index], expectedLine)
}
stringsFileUpdater.incrementallyUpdateKeys(withStringsFileAtPath: newStringsFilePath, addNewValuesAsEmpty: true, updateCommentWithBase: false, ignoreEmptyStrings: true)
let expectedLinesAfterIncrementalUpdate = [
"", "/* Class = \"UIButton\"; normalTitle = \"Example Button 1\"; ObjectID = \"35F-cl-mdI\"; */",
"\"35F-cl-mdI.normalTitle\" = \"New Example Button 1\";", "",
"/*! Class = \"UIButton\"; normalTitle = \"Example Button 2\"; ObjectID = \"COa-YO-eGf\"; */",
"\"COa-YO-eGf.normalTitle\" = \"Already Translated\";", "",
"/* Completely custom comment structure in one line */",
"\"test.key\" = \"This is a test key\";", "",
"/* Class = \"UIButton\"; normalTitle = \"New Example Button 4\"; ObjectID = \"xyz-12-345\"; */",
"\"xyz-12-345.normalTitle\" = \"\";", "",
"/* test comment 1", " test comment 2 */",
"\"test.multiline_comment\" = \"\";", "",
"/* (test comment with brackets) */",
"\"test.brackets_comment\" = \"\";", "",
"/* test with % character 1 */", "\"more than 23% or less than 45%\" = \"\";", "",
"/* test with % character 2 */", "\"%A%B%C\" = \"\";", "",
"/* test with placeholders */", "\"%d%@%.2f\" = \"\";", ""
]
oldLinesInFile = stringsFileUpdater.oldContentString.components(separatedBy: .newlines)
XCTAssertEqual(oldLinesInFile.count, expectedLinesAfterIncrementalUpdate.count)
for (index, expectedLine) in expectedLinesAfterIncrementalUpdate.enumerated() {
XCTAssertEqual(oldLinesInFile[index], expectedLine)
}
} catch {
XCTFail((error as NSError).description)
}
}
func testExampleStringsFileWithSpecialNewlineSurroundings() {
do {
try FileManager.default.copyItem(atPath: oldStringsFilePath, toPath: testStringsFilePath)
let stringsFileUpdater = StringsFileUpdater(path: testStringsFilePath)!
let expectedLinesBeforeIncrementalUpdate = [
"/* Class = \"UIButton\"; normalTitle = \"Example Button 1\"; ObjectID = \"35F-cl-mdI\"; */",
"\"35F-cl-mdI.normalTitle\" = \"Example Button 1\";", "",
"/*! Class = \"UIButton\"; normalTitle = \"Example Button 2\"; ObjectID = \"COa-YO-eGf\"; */",
"\"COa-YO-eGf.normalTitle\" = \"Already Translated\";", "",
"/* Class = \"UIButton\"; normalTitle = \"Example Button 3\"; ObjectID = \"cHL-Zc-L39\"; */",
"\"cHL-Zc-L39.normalTitle\" = \"Example Button 3\";", "",
"/* Completely custom comment structure in one line */",
"\"test.key\" = \"This is a test key\";", "",
"/* Completely custom comment structure in one line to be ignored */",
"\"test.key.ignored\" = \"This is a test key to be ignored #bc-ignore!\";", "",
"/* This key should be ignored when ignoreEmptyKeys is set */",
"\"test.key.ignoreEmptyStrings\" = \" \";", "",
"/* Class = \"UIButton\"; normalTitle = \"😀\"; ObjectID = \"abc-12-345\"; */",
"\"abc-12-345.normalTitle\" = \"😀\";", "",
"/* Class = \"UILabel\"; text = \"Refraktionsentfernung in Meter\"; ObjectID = \"em1-3S-vgp\"; */",
"\"em1-3S-vgp.text\" = \"Refrakční vzdálenost v metrech\";", "", ""
]
var oldLinesInFile = stringsFileUpdater.oldContentString.components(separatedBy: .newlines)
XCTAssertEqual(oldLinesInFile.count, expectedLinesBeforeIncrementalUpdate.count)
for (index, expectedLine) in expectedLinesBeforeIncrementalUpdate.enumerated() {
XCTAssertEqual(oldLinesInFile[index], expectedLine)
}
stringsFileUpdater.incrementallyUpdateKeys(
withStringsFileAtPath: newStringsFilePath,
addNewValuesAsEmpty: true,
updateCommentWithBase: false,
keepWhitespaceSurroundings: true,
ignoreEmptyStrings: true
)
let expectedLinesAfterIncrementalUpdate = [
"/* Class = \"UIButton\"; normalTitle = \"Example Button 1\"; ObjectID = \"35F-cl-mdI\"; */",
"\"35F-cl-mdI.normalTitle\" = \"New Example Button 1\";", "",
"/*! Class = \"UIButton\"; normalTitle = \"Example Button 2\"; ObjectID = \"COa-YO-eGf\"; */",
"\"COa-YO-eGf.normalTitle\" = \"Already Translated\";", "",
"/* Completely custom comment structure in one line */",
"\"test.key\" = \"This is a test key\";", "",
"/* Class = \"UIButton\"; normalTitle = \"New Example Button 4\"; ObjectID = \"xyz-12-345\"; */",
"\"xyz-12-345.normalTitle\" = \"\";", "",
"/* test comment 1", " test comment 2 */",
"\"test.multiline_comment\" = \"\";", "",
"/* (test comment with brackets) */",
"\"test.brackets_comment\" = \"\";", "",
"/* test with % character 1 */", "\"more than 23% or less than 45%\" = \"\";", "",
"/* test with % character 2 */", "\"%A%B%C\" = \"\";", "",
"/* test with placeholders */", "\"%d%@%.2f\" = \"\";", "", ""
]
oldLinesInFile = stringsFileUpdater.oldContentString.components(separatedBy: .newlines)
XCTAssertEqual(oldLinesInFile.count, expectedLinesAfterIncrementalUpdate.count)
for (index, expectedLine) in expectedLinesAfterIncrementalUpdate.enumerated() {
XCTAssertEqual(oldLinesInFile[index], expectedLine)
}
} catch {
XCTFail((error as NSError).description)
}
}
func testExampleStringsFileWithPrefilledNewValues() {
do {
try FileManager.default.copyItem(atPath: oldStringsFilePath, toPath: testStringsFilePath)
let stringsFileUpdater = StringsFileUpdater(path: testStringsFilePath)!
let expectedLinesBeforeIncrementalUpdate = [
"/* Class = \"UIButton\"; normalTitle = \"Example Button 1\"; ObjectID = \"35F-cl-mdI\"; */",
"\"35F-cl-mdI.normalTitle\" = \"Example Button 1\";", "",
"/*! Class = \"UIButton\"; normalTitle = \"Example Button 2\"; ObjectID = \"COa-YO-eGf\"; */",
"\"COa-YO-eGf.normalTitle\" = \"Already Translated\";", "",
"/* Class = \"UIButton\"; normalTitle = \"Example Button 3\"; ObjectID = \"cHL-Zc-L39\"; */",
"\"cHL-Zc-L39.normalTitle\" = \"Example Button 3\";", "",
"/* Completely custom comment structure in one line */",
"\"test.key\" = \"This is a test key\";", "",
"/* Completely custom comment structure in one line to be ignored */",
"\"test.key.ignored\" = \"This is a test key to be ignored #bc-ignore!\";", "",
"/* This key should be ignored when ignoreEmptyKeys is set */",
"\"test.key.ignoreEmptyStrings\" = \" \";", "",
"/* Class = \"UIButton\"; normalTitle = \"😀\"; ObjectID = \"abc-12-345\"; */",
"\"abc-12-345.normalTitle\" = \"😀\";", "",
"/* Class = \"UILabel\"; text = \"Refraktionsentfernung in Meter\"; ObjectID = \"em1-3S-vgp\"; */",
"\"em1-3S-vgp.text\" = \"Refrakční vzdálenost v metrech\";", "", ""
]
var oldLinesInFile = stringsFileUpdater.oldContentString.components(separatedBy: .newlines)
XCTAssertEqual(oldLinesInFile.count, expectedLinesBeforeIncrementalUpdate.count)
for (index, expectedLine) in expectedLinesBeforeIncrementalUpdate.enumerated() {
XCTAssertEqual(oldLinesInFile[index], expectedLine)
}
stringsFileUpdater.incrementallyUpdateKeys(withStringsFileAtPath: newStringsFilePath, addNewValuesAsEmpty: false, ignoreEmptyStrings: true)
let expectedLinesAfterIncrementalUpdate = [
"", "/* Class = \"UIButton\"; normalTitle = \"New Example Button 1\"; ObjectID = \"35F-cl-mdI\"; */",
"\"35F-cl-mdI.normalTitle\" = \"New Example Button 1\";", "",
"/*! Class = \"UIButton\"; normalTitle = \"Example Button 2\"; ObjectID = \"COa-YO-eGf\"; */",
"\"COa-YO-eGf.normalTitle\" = \"Already Translated\";", "",
"/* Completely custom comment structure in one line */",
"\"test.key\" = \"This is a test key\";", "",
"/* Class = \"UIButton\"; normalTitle = \"New Example Button 4\"; ObjectID = \"xyz-12-345\"; */",
"\"xyz-12-345.normalTitle\" = \"New Example Button 4\";", "",
"/* test comment 1", " test comment 2 */",
"\"test.multiline_comment\" = \"test.multiline_comment.value\";", "",
"/* (test comment with brackets) */",
"\"test.brackets_comment\" = \"test.brackets_comment\";", "",
"/* test with % character 1 */", "\"more than 23% or less than 45%\" = \"more than 23% or less than 45%\";", "",
"/* test with % character 2 */", "\"%A%B%C\" = \"%A%B%C\";", "",
"/* test with placeholders */", "\"%d%@%.2f\" = \"%1$d%2$@%3$.2f\";", ""
]
oldLinesInFile = stringsFileUpdater.oldContentString.components(separatedBy: .newlines)
XCTAssertEqual(oldLinesInFile.count, expectedLinesAfterIncrementalUpdate.count)
for (index, expectedLine) in expectedLinesAfterIncrementalUpdate.enumerated() {
XCTAssertEqual(oldLinesInFile[index], expectedLine)
}
} catch {
XCTFail((error as NSError).description)
}
}
func testExtractLocale() {
let updater = StringsFileUpdater(path: newStringsFilePath)!
let expectedPairs: [String: (String, String?)?] = [
"bli/bla/blubb/de.lproj/Main.strings": ("de", nil),
"bli/bla/blubb/en-GB.lproj/Main.strings": ("en", "GB"),
"bli/bla/blubb/pt-BR.lproj/Main.strings": ("pt", "BR"),
"bli/bla/blubb/zh-Hans.lproj/Main.strings": ("zh", "Hans"),
"bli/bla/blubb/No-Locale/de-DE/Main.strings": nil
]
expectedPairs.forEach { path, expectedResult in
if expectedResult == nil {
XCTAssertNil(updater.extractLocale(fromPath: path))
} else {
let result = updater.extractLocale(fromPath: path)
XCTAssertEqual(result?.0, expectedResult?.0)
XCTAssertEqual(result?.1, expectedResult?.1)
}
}
}
func testTranslateEmptyValues() { // swiftlint:disable:this function_body_length
// Note: This test only runs with correct Microsoft Translator API credentials provided
let id: String? = nil // specify this to run this test
let secret: String? = nil // specify this to run this test
if let id = id, let secret = secret {
let sourceStringsFilePath = "\(BASE_DIR)/Tests/Assets/Strings Files/en.lproj/Localizable.strings"
let expectedTranslatedCarsValues: [String: String] = [
"de": "Autos",
"ja": "車",
"zh-Hans": "汽车"
]
for locale in ["de", "ja", "zh-Hans"] {
let localizableStringsFilePath = "\(BASE_DIR)/Tests/Assets/Strings Files/\(locale).lproj/Localizable.strings"
// create temporary file for testing
do {
if FileManager.default.fileExists(atPath: localizableStringsFilePath + ".tmp") {
try FileManager.default.removeItem(atPath: localizableStringsFilePath + ".tmp")
}
try FileManager.default.copyItem(atPath: localizableStringsFilePath, toPath: localizableStringsFilePath + ".tmp")
} catch {
XCTAssertTrue(false)
return
}
let stringsFileUpdater = StringsFileUpdater(path: localizableStringsFilePath + ".tmp")!
var translations = stringsFileUpdater.findTranslations(inString: stringsFileUpdater.oldContentString)
// test before state (update if failing)
XCTAssertEqual(translations[0].key, "Test key")
XCTAssertEqual(translations[0].value, "Test value (\(locale))")
XCTAssertEqual(translations[0].comment, " A string already localized in all languages. ")
XCTAssertEqual(translations[1].key, "menu.cars")
XCTAssertEqual(translations[1].value.utf16.count, 0)
XCTAssertEqual(translations[1].value, "")
XCTAssertEqual(translations[1].comment, " A string where value only available in English. ")
XCTAssertEqual(translations[2].key, "TEST.KEY.UNESCAPED_DOUBLE_QUOTES")
XCTAssertEqual(translations[2].value.utf16.count, 0)
XCTAssertEqual(translations[2].value, "")
XCTAssertEqual(translations[2].comment, nil)
// run tested method
let changedValuesCount = stringsFileUpdater.translateEmptyValues(
usingValuesFromStringsFile: sourceStringsFilePath, clientId: id, clientSecret: secret
)
translations = stringsFileUpdater.findTranslations(inString: stringsFileUpdater.oldContentString)
XCTAssertEqual(changedValuesCount, 3)
// test after state (update if failing)
XCTAssertEqual(translations.count, 4)
XCTAssertEqual(translations[0].key, "Test key")
XCTAssertEqual(translations[0].value, "Test value (\(locale))")
XCTAssertEqual(translations[0].comment, " A string already localized in all languages. ")
XCTAssertEqual(translations[1].key, "menu.cars")
XCTAssertGreaterThan(translations[1].value.utf16.count, 0)
XCTAssertEqual(translations[1].value, expectedTranslatedCarsValues[locale])
XCTAssertEqual(translations[1].comment, " A string where value only available in English. ")
// cleanup temporary file after testing
do {
try FileManager.default.removeItem(atPath: localizableStringsFilePath + ".tmp")
} catch {
XCTFail("File couldn't be removed at path: \(localizableStringsFilePath).tmp")
}
}
let expectedTranslatedBicyclesValues: [String: String] = [
"de": "Fahrräder",
"ja": "自転車",
"zh-Hans": "自行车"
]
let expectedTranslatedSheSaidStopValues: [String: String] = [
"de": "Sie sagte: \\\"Stop!\\\"", // BartyCrouch is expected to escape double quotes
"ja": "彼女は言った: '停止'!",
"zh-Hans": "她说: '停止' !"
]
// test with create keys options
for locale in ["de", "ja", "zh-Hans"] {
let localizableStringsFilePath = "\(BASE_DIR)/Tests/Assets/Strings Files/\(locale).lproj/Localizable.strings"
// create temporary file for testing
do {
if FileManager.default.fileExists(atPath: localizableStringsFilePath + ".tmp") {
try FileManager.default.removeItem(atPath: localizableStringsFilePath + ".tmp")
}
try FileManager.default.copyItem(atPath: localizableStringsFilePath, toPath: localizableStringsFilePath + ".tmp")
} catch {
XCTAssertTrue(false)
return
}
let stringsFileUpdater = StringsFileUpdater(path: localizableStringsFilePath + ".tmp")!
var translations = stringsFileUpdater.findTranslations(inString: stringsFileUpdater.oldContentString)
// test before state (update if failing)
XCTAssertEqual(translations.count, 3)
XCTAssertEqual(translations[0].key, "Test key")
XCTAssertEqual(translations[0].value, "Test value (\(locale))")
XCTAssertEqual(translations[0].comment, " A string already localized in all languages. ")
XCTAssertEqual(translations[1].key, "menu.cars")
XCTAssertEqual(translations[1].value.utf16.count, 0)
XCTAssertEqual(translations[1].value, "")
XCTAssertEqual(translations[1].comment, " A string where value only available in English. ")
// run tested method
let changedValuesCount = stringsFileUpdater.translateEmptyValues(
usingValuesFromStringsFile: sourceStringsFilePath, clientId: id, clientSecret: secret
)
translations = stringsFileUpdater.findTranslations(inString: stringsFileUpdater.oldContentString)
XCTAssertEqual(changedValuesCount, 3)
// test after state (update if failing)
XCTAssertEqual(translations.count, 4)
XCTAssertEqual(translations[0].key, "Test key")
XCTAssertEqual(translations[0].value, "Test value (\(locale))")
XCTAssertEqual(translations[0].comment, " A string already localized in all languages. ")
XCTAssertEqual(translations[1].key, "menu.cars")
XCTAssertGreaterThan(translations[1].value.utf16.count, 0)
XCTAssertEqual(translations[1].value, expectedTranslatedCarsValues[locale])
XCTAssertEqual(translations[1].comment, " A string where value only available in English. ")
XCTAssertEqual(translations[2].key, "menu.bicycles")
XCTAssertGreaterThan(translations[2].value.utf16.count, 0)
XCTAssertEqual(translations[2].value, expectedTranslatedBicyclesValues[locale])
XCTAssertEqual(translations[2].comment, " A string where key only available in English. ")
XCTAssertEqual(translations[3].key, "TEST.KEY.UNESCAPED_DOUBLE_QUOTES")
XCTAssertGreaterThan(translations[2].value.utf16.count, 0)
XCTAssertEqual(translations[3].value, expectedTranslatedSheSaidStopValues[locale])
XCTAssertEqual(translations[3].comment, nil)
// cleanup temporary file after testing
do {
try FileManager.default.removeItem(atPath: localizableStringsFilePath + ".tmp")
} catch {
XCTFail((error as NSError).description)
}
}
}
}
// MARK: - Performance Tests
func testInitPerformance() {
measure {
100.times {
_ = StringsFileUpdater(path: self.longOldStringsFilePath)!
}
}
}
func testIncrementallyUpdateKeysPerformance() {
do {
for index in self.testExamplesRange {
try FileManager.default.copyItem(atPath: longOldStringsFilePath, toPath: self.testStringsFilePath(index))
}
measure {
for index in self.testExamplesRange {
let stringsFileUpdater = StringsFileUpdater(path: self.testStringsFilePath(index))!
stringsFileUpdater.incrementallyUpdateKeys(withStringsFileAtPath: self.longNewStringsFilePath, addNewValuesAsEmpty: false)
}
}
} catch {
XCTFail((error as NSError).description)
}
}
// Note that the method translateEmptyValues is not tested as this would consume unnecessary API requests.
// Also it is not the scope of this library to make sure the third party Translation API is fast.
}