add unit test to make sure Localizable.strings files are syntactically correct

This commit is contained in:
Satindar Dhillon
2023-01-25 10:06:23 -08:00
parent 8c9d8816c7
commit 06b3150252
2 changed files with 32 additions and 3 deletions

View File

@ -2,9 +2,7 @@ import Foundation
public enum LocalText {
public static func localText(key: String, comment: String? = nil) -> String {
let string = NSLocalizedString(key, bundle: .module, comment: comment ?? "no comment provided by developer")
print(string)
return string
NSLocalizedString(key, bundle: .module, comment: comment ?? "no comment provided by developer")
}
// Share extension

View File

@ -0,0 +1,31 @@
@testable import Views
import XCTest
final class LocalTextTests: XCTestCase {
func testThatLocalTextFindsStrings() {
// Make sure that the same key is not returned when looking up a localized string by key
// Testing the first and last entry in teh strings file is adequate for finding syntax errors.
// If any entry is not proper than the key will be returned and the test will fail.
// English (test default)
XCTAssertNotEqual(LocalText.saveArticleSavedState, "saveArticleSavedState")
XCTAssertNotEqual(LocalText.errorNetwork, "errorNetwork")
// Simple Chinese
XCTAssertNotEqual(simpleChineseText(key: "saveArticleSavedState"), "saveArticleSavedState")
XCTAssertNotEqual(simpleChineseText(key: "errorNetwork"), "errorNetwork")
}
private func simpleChineseText(key: String) -> String {
guard
let bundlePath = Bundle.module.path(forResource: "zh-Hans", ofType: "lproj"),
let bundle = Bundle(path: bundlePath)
else { return key }
return NSLocalizedString(key, bundle: bundle, comment: "")
}
static var allTests = [
("testThatLocalTextFindsStrings", testThatLocalTextFindsStrings)
]
}