From 06b3150252b0949cb3bf43e2127310f3894110fa Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Wed, 25 Jan 2023 10:06:23 -0800 Subject: [PATCH] add unit test to make sure Localizable.strings files are syntactically correct --- .../OmnivoreKit/Sources/Views/LocalText.swift | 4 +-- .../Tests/ViewsTests/LocalTextTests.swift | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 apple/OmnivoreKit/Tests/ViewsTests/LocalTextTests.swift diff --git a/apple/OmnivoreKit/Sources/Views/LocalText.swift b/apple/OmnivoreKit/Sources/Views/LocalText.swift index 811426c7b..148be5252 100644 --- a/apple/OmnivoreKit/Sources/Views/LocalText.swift +++ b/apple/OmnivoreKit/Sources/Views/LocalText.swift @@ -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 diff --git a/apple/OmnivoreKit/Tests/ViewsTests/LocalTextTests.swift b/apple/OmnivoreKit/Tests/ViewsTests/LocalTextTests.swift new file mode 100644 index 000000000..dcd626dab --- /dev/null +++ b/apple/OmnivoreKit/Tests/ViewsTests/LocalTextTests.swift @@ -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) + ] +}