diff --git a/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/Expectation/StringsFileGenerator+OldStringsFile.swift b/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/Expectation/StringsFileGenerator+OldStringsFile.swift new file mode 100644 index 0000000..524c2e6 --- /dev/null +++ b/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/Expectation/StringsFileGenerator+OldStringsFile.swift @@ -0,0 +1,51 @@ +// +// StringsFileGenerator+OldStringsFile.swift +// ResgenSwift +// +// Created by Thibaut Schmitt on 30/04/2025. +// + +import ToolCore + +extension StringsFileGeneratorTests { + + static let appleStringsFileExpectationFr = """ + /** + * Apple Strings File + * Generated by ResgenSwift \(ResgenSwiftVersion) + * Language: fr + */ + + /********** section_one **********/ + + "s1_def_one" = "Section Un - Definition Un"; + + "s1_def_two" = "Section Un - Definition Deux"; + + /********** section_two **********/ + + "s2_def_one" = "Section Deux - Definition Un"; + + "s2_def_two" = "Section Deux - Definition Deux"; + """ + + static let appleStringsFileExpectationEn = """ + /** + * Apple Strings File + * Generated by ResgenSwift \(ResgenSwiftVersion) + * Language: en + */ + + /********** section_one **********/ + + "s1_def_one" = "Section One - Definition One"; + + "s1_def_two" = "Section One - Definition Two"; + + /********** section_two **********/ + + "s2_def_one" = "Section Two - Definition One"; + + "s2_def_two" = "Section Deux - Definition Deux"; + """ +} diff --git a/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/Expectation/StringsFileGenerator+R2ExtensionsExpectation.swift b/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/Expectation/StringsFileGenerator+R2ExtensionsExpectation.swift new file mode 100644 index 0000000..54b5bcf --- /dev/null +++ b/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/Expectation/StringsFileGenerator+R2ExtensionsExpectation.swift @@ -0,0 +1,91 @@ +// +// StringsFileGenerator+R2ExtensionsExpectation.swift +// ResgenSwift +// +// Created by Thibaut Schmitt on 30/04/2025. +// + +@testable import ResgenSwift +import ToolCore + +extension StringsFileGeneratorTests { + + static func getExtensionContentExpectation( + staticVar: Bool, + s1DefOneFr: String = "Section Un - Definition Un", + s1DefOneComment: String = "", + s1DefTwoFr: String = "Section Un - Definition Deux", + s1DefTwoComment: String = "", + s2DefOneFr: String = "Section Deux - Definition Un", + s2DefOneComment: String = "", + s2DefTwoFr: String = "Section Deux - Definition Deux", + s2DefTwoComment: String = "", + ) -> String { + """ + // Generated by ResgenSwift.Strings.Stringium \(ResgenSwiftVersion) + + import UIKit + + fileprivate let kStringsFileName = "myInputFilename" + + extension GenStrings { + + enum KeyStrings: String { + case s1_def_one = "s1_def_one" + case s1_def_two = "s1_def_two" + case s2_def_one = "s2_def_one" + case s2_def_two = "s2_def_two" + + var keyPath: KeyPath { + switch self { + case .s1_def_one: return \\GenStrings.s1_def_one + case .s1_def_two: return \\GenStrings.s1_def_two + case .s2_def_one: return \\GenStrings.s2_def_one + case .s2_def_two: return \\GenStrings.s2_def_two + } + } + } + + // MARK: - section_one + + /// Translation in fr : + /// \(s1DefOneFr) + /// + /// Comment : + /// \(s1DefOneComment.isEmpty ? "No comment" : s1DefOneComment) + \(staticVar ? "static " : "")var s1_def_one: String { + NSLocalizedString("s1_def_one", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Un - Definition Un", comment: "\(s1DefOneComment)") + } + + /// Translation in fr : + /// \(s1DefTwoFr) + /// + /// Comment : + /// \(s1DefTwoComment.isEmpty ? "No comment" : s1DefTwoComment) + \(staticVar ? "static " : "")var s1_def_two: String { + NSLocalizedString("s1_def_two", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Un - Definition Deux", comment: "\(s1DefTwoComment)") + } + + // MARK: - section_two + + /// Translation in fr : + /// \(s2DefOneFr) + /// + /// Comment : + /// \(s2DefOneComment.isEmpty ? "No comment" : s2DefOneComment) + \(staticVar ? "static " : "")var s2_def_one: String { + NSLocalizedString("s2_def_one", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Deux - Definition Un", comment: "\(s2DefOneComment)") + } + + /// Translation in fr : + /// \(s2DefTwoFr) + /// + /// Comment : + /// \(s2DefTwoComment.isEmpty ? "No comment" : s2DefTwoComment) + \(staticVar ? "static " : "")var s2_def_two: String { + NSLocalizedString("s2_def_two", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Deux - Definition Deux", comment: "\(s2DefTwoComment)") + } + } + """ + } +} diff --git a/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/Expectation/StringsFileGenerator+XCStringsExpectation.swift b/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/Expectation/StringsFileGenerator+XCStringsExpectation.swift new file mode 100644 index 0000000..ede3a59 --- /dev/null +++ b/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/Expectation/StringsFileGenerator+XCStringsExpectation.swift @@ -0,0 +1,129 @@ +// +// StringsFileGenerator+XCStringsExpectation.swift +// ResgenSwift +// +// Created by Thibaut Schmitt on 30/04/2025. +// + +@testable import ResgenSwift + +extension XCStringDefinition { + + enum Mock { + + static func getDefinitionSectionOne( + defOneFr: String = "Section Un - Definition Un", + defOneEn: String = "Section One - Definition One", + defOneComment: String? = nil, + defTwoFr: String = "Section Un - Definition Deux", + defTwoEn: String = "Section One - Definition Two", + defTwoComment: String? = nil + ) -> [XCStringDefinition] { + [ + XCStringDefinition( + title: "s1_def_one", + content: XCStringDefinitionContent( + comment: defOneComment, + extractionState: "manual", + localizations: XCStringLocalizationContainer( + localizations: getXCStringLocalization( + defFr: defOneFr, + defEn: defOneEn + ) + ) + ) + ), + XCStringDefinition( + title: "s1_def_two", + content: XCStringDefinitionContent( + comment: defTwoComment, + extractionState: "manual", + localizations: XCStringLocalizationContainer( + localizations: getXCStringLocalization( + defFr: defTwoFr, + defEn: defTwoEn + ) + ) + ) + ) + ] + } + + static func getDefinitionSectionTwo( + defOneFr: String = "Section Deux - Definition Un", + defOneEn: String = "Section Two - Definition One", + defOneComment: String? = nil, + defTwoFr: String = "Section Deux - Definition Deux", + defTwoEn: String? = nil, + defTwoComment: String? = nil + ) -> [XCStringDefinition] { + [ + XCStringDefinition( + title: "s2_def_one", + content: XCStringDefinitionContent( + comment: defOneComment, + extractionState: "manual", + localizations: XCStringLocalizationContainer( + localizations: getXCStringLocalization( + defFr: defOneFr, + defEn: defOneEn + ) + ) + ) + ), + XCStringDefinition( + title: "s2_def_two", + content: XCStringDefinitionContent( + comment: defTwoComment, + extractionState: "manual", + localizations: XCStringLocalizationContainer( + localizations: getXCStringLocalization( + defFr: defTwoFr, + defEn: defTwoEn ?? "" + ) + ) + ) + ) + ] + } + + // MARK: - Private methods + + private static func getXCStringLocalization( + defFr: String, + defEn: String + ) -> [XCStringLocalization] { + var localizations = [XCStringLocalization]() + + if defFr.isEmpty == false { + localizations.append( + XCStringLocalization( + lang: "fr", + content: XCStringLocalizationLangContent( + stringUnit: DefaultStringUnit( + state: "translated", + value: defFr + ) + ) + ) + ) + } + + if defEn.isEmpty == false { + localizations.append( + XCStringLocalization( + lang: "en", + content: XCStringLocalizationLangContent( + stringUnit: DefaultStringUnit( + state: "translated", + value: defEn + ) + ) + ) + ) + } + + return localizations + } + } +} diff --git a/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/Mock/Section+Mock.swift b/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/Mock/Section+Mock.swift new file mode 100644 index 0000000..417b3d3 --- /dev/null +++ b/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/Mock/Section+Mock.swift @@ -0,0 +1,95 @@ +// +// Section+Mock.swift +// ResgenSwift +// +// Created by Thibaut Schmitt on 30/04/2025. +// + +@testable import ResgenSwift + +extension Section { + + enum Mock { + + static func getSectionOne( + defOneFr: String = "Section Un - Definition Un", + defOneEn: String = "Section One - Definition One", + defOneComment: String? = nil, + defOneTag: [String] = ["ios","iosonly"], + defTwoFr: String = "Section Un - Definition Deux", + defTwoEn: String = "Section One - Definition Two", + defTwoComment: String? = nil, + defTwoTag: [String] = ["ios","iosonly"] + ) -> Section { + let section = Section(name: "section_one") + section.definitions = [ + Self.getDefinition( + name: "s1_def_one", + translations: ["fr": defOneFr, + "en": defOneEn], + tags: defOneTag, + comment: defOneComment + ), + Self.getDefinition( + name: "s1_def_two", + translations: ["fr": defTwoFr, + "en": defTwoEn], + tags: defTwoTag, + comment: defTwoComment + ) + ] + return section + } + + static func getSectionTwo( + defOneFr: String = "Section Deux - Definition Un", + defOneEn: String = "Section Two - Definition One", + defOneComment: String? = nil, + defOneTag: [String] = ["ios","iosonly"], + defTwoFr: String = "Section Deux - Definition Deux", + defTwoEn: String? = nil, + defTwoComment: String? = nil, + defTwoTag: [String] = ["notranslation"] + ) -> Section { + let section = Section(name: "section_two") + let defTwoTranslations: [String: String] = { + var translations = ["fr": "Section Deux - Definition Deux"] + if let defTwoEn { + translations["en"] = defTwoEn + } + return translations + }() + section.definitions = [ + Self.getDefinition( + name: "s2_def_one", + translations: ["fr": defOneFr, + "en": defOneEn], + tags: defOneTag, + comment: defOneComment + ), + Self.getDefinition( + name: "s2_def_two", + translations: defTwoTranslations, + tags: defTwoTag, + comment: defTwoComment + ) + ] + return section + } + + // MARK: - Private methods + + private static func getDefinition( + name: String, + translations: [String: String], + tags: [String], + comment: String? = nil + ) -> Definition { + let definition = Definition(name: name) + definition.tags = tags + definition.translations = translations + definition.comment = comment + return definition + } + } +} diff --git a/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/StringsFileGeneratorTests.swift b/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/StringsFileGeneratorTests.swift new file mode 100644 index 0000000..d9777cb --- /dev/null +++ b/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/StringsFileGeneratorTests.swift @@ -0,0 +1,489 @@ +// +// StringsFileGeneratorTests.swift +// +// +// Created by Thibaut Schmitt on 06/09/2022. +// + +import Foundation +import XCTest +import ToolCore + +@testable import ResgenSwift + +final class StringsFileGeneratorTests: XCTestCase { + + // MARK: - Strings File Content + + func testGenerateStringsFileContent() { + // Given + let sectionOne = Section.Mock.getSectionOne() + let sectionTwo = Section.Mock.getSectionTwo() + + // When + let stringsFileContentFr = StringsFileGenerator.generateStringsFileContent( + lang: "fr", + defaultLang: "fr", + tags: ["ios", "iosonly", "notranslation"], + sections: [sectionOne, sectionTwo] + ) + let stringsFileContentEn = StringsFileGenerator.generateStringsFileContent( + lang: "en", + defaultLang: "fr", + tags: ["ios", "iosonly", "notranslation"], + sections: [sectionOne, sectionTwo] + ) + + // Expect + XCTAssertEqual( + stringsFileContentFr.adaptForXCTest(), + Self.appleStringsFileExpectationFr.adaptForXCTest() + ) + XCTAssertEqual( + stringsFileContentEn.adaptForXCTest(), + Self.appleStringsFileExpectationEn.adaptForXCTest() + ) + } + + func testGenerateStringsFileContentWithComment() { + // Given + let sectionOne = Section.Mock.getSectionOne( + defOneComment: "This is a comment", + defTwoComment: "This is a comment" + ) + let sectionTwo = Section.Mock.getSectionTwo( + defOneComment: "This is a comment", + defTwoComment: "This is a comment" + ) + + // When + let stringsFileContentFr = StringsFileGenerator.generateStringsFileContent( + lang: "fr", + defaultLang: "fr", + tags: ["ios", "iosonly", "notranslation"], + sections: [sectionOne, sectionTwo] + ) + let stringsFileContentEn = StringsFileGenerator.generateStringsFileContent( + lang: "en", + defaultLang: "fr", + tags: ["ios", "iosonly", "notranslation"], + sections: [sectionOne, sectionTwo] + ) + + // Expect + XCTAssertEqual( + stringsFileContentFr.adaptForXCTest(), + Self.appleStringsFileExpectationFr.adaptForXCTest() + ) + XCTAssertEqual( + stringsFileContentEn.adaptForXCTest(), + Self.appleStringsFileExpectationEn.adaptForXCTest() + ) + } + + // MARK: - XcString File Content + + func testGenerateXcStringsRootObject() { + // Given + let sectionOne = Section.Mock.getSectionOne() + let sectionTwo = Section.Mock.getSectionTwo( + defTwoEn: "Section Two - Definition Two" + ) + + // When + let rootObject = StringsFileGenerator.generateRootObject( + langs: ["fr", "en"], + defaultLang: "en", + tags: ["ios", "iosonly", "notranslation"], + sections: [sectionOne, sectionTwo] + ) + + // Expect + let expect = + Root( + sourceLanguage: "en", + strings: XCStringDefinitionContainer( + strings: [ + XCStringDefinition( + title: "s1_def_one", + content: XCStringDefinitionContent( + extractionState: "manual", + localizations: XCStringLocalizationContainer( + localizations: [ + XCStringLocalization( + lang: "en", + content: XCStringLocalizationLangContent( + stringUnit: DefaultStringUnit( + state: "translated", + value: "Section One - Definition One" + ) + ) + ), + XCStringLocalization( + lang: "fr", + content: XCStringLocalizationLangContent( + stringUnit: DefaultStringUnit( + state: "translated", + value: "Section Un - Definition Un" + ) + ) + ) + ] + ) + ) + ), + XCStringDefinition( + title: "s1_def_two", + content: XCStringDefinitionContent( + extractionState: "manual", + localizations: XCStringLocalizationContainer( + localizations: [ + XCStringLocalization( + lang: "en", + content: XCStringLocalizationLangContent( + stringUnit: DefaultStringUnit( + state: "translated", + value: "Section One - Definition Two" + ) + ) + ), + XCStringLocalization( + lang: "fr", + content: XCStringLocalizationLangContent( + stringUnit: DefaultStringUnit( + state: "translated", + value: "Section Un - Definition Deux" + ) + ) + ) + ] + ) + ) + ), + XCStringDefinition( + title: "s2_def_one", + content: XCStringDefinitionContent( + extractionState: "manual", + localizations: XCStringLocalizationContainer( + localizations: [ + XCStringLocalization( + lang: "en", + content: XCStringLocalizationLangContent( + stringUnit: DefaultStringUnit( + state: "translated", + value: "Section Two - Definition One" + ) + ) + ), + XCStringLocalization( + lang: "fr", + content: XCStringLocalizationLangContent( + stringUnit: DefaultStringUnit( + state: "translated", + value: "Section Deux - Definition Un" + ) + ) + ) + ] + ) + ) + ), + XCStringDefinition( + title: "s2_def_two", + content: XCStringDefinitionContent( + extractionState: "manual", + localizations: XCStringLocalizationContainer( + localizations: [ + XCStringLocalization( + lang: "en", + content: XCStringLocalizationLangContent( + stringUnit: DefaultStringUnit( + state: "translated", + value: "Section Two - Definition Two" + ) + ) + ), + XCStringLocalization( + lang: "fr", + content: XCStringLocalizationLangContent( + stringUnit: DefaultStringUnit( + state: "translated", + value: "Section Two - Definition Two" + ) + ) + ) + ] + ) + ) + ) + ] + ), + version: "1.0" + ) + + XCTAssertEqual(rootObject, expect) + } + + func testGenerateXcStringsRootObjectWithEmptyTranslations() { + // Given + let sectionOne = Section.Mock.getSectionOne( + defOneFr: "", + defTwoFr: "" + ) + let sectionTwo = Section.Mock.getSectionTwo( + defOneFr: "", + defTwoFr: "", + defTwoEn: "Section Two - Definition Two" + ) + // By default Section2.s2_def_two has a fr value => remove it + sectionTwo.definitions + .first { def in + def.name == "s2_def_two" + }? + .translations.removeValue(forKey: "fr") + + // When + let rootObject = StringsFileGenerator.generateRootObject( + langs: ["fr", "en"], + defaultLang: "en", + tags: ["ios", "iosonly", "notranslation"], + sections: [sectionOne, sectionTwo] + ) + + // Expect + let expect = + Root( + sourceLanguage: "en", + strings: XCStringDefinitionContainer( + strings: [ + XCStringDefinition.Mock.getDefinitionSectionOne( + defOneFr: "", + defTwoFr: "", + ), + XCStringDefinition.Mock.getDefinitionSectionTwo( + defOneFr: "", + defTwoFr: "Section Two - Definition Two", + defTwoEn: "Section Two - Definition Two", + ) + ] + .flatMap { $0 } + ), + version: "1.0" + ) + + XCTAssertEqual(rootObject, expect) + } + + func testGenerateXcStringsRootObjectWithNoTranslations() { + // Given + let sectionOne = Section.Mock.getSectionOne( + defOneFr: "", + defOneEn: "", + defTwoFr: "", + defTwoEn : "" + ) + let sectionTwo = Section.Mock.getSectionTwo( + defOneFr: "", + defOneEn: "", + defTwoFr: "", + defTwoEn : "" + ) + + // When + let rootObject = StringsFileGenerator.generateRootObject( + langs: ["fr", "en"], + defaultLang: "en", + tags: ["ios", "iosonly", "notranslation"], + sections: [sectionOne, sectionTwo] + ) + + // Expect + let expect = + Root( + sourceLanguage: "en", + strings: XCStringDefinitionContainer( + strings: [ + XCStringDefinition.Mock.getDefinitionSectionOne( + defOneFr: "", + defOneEn: "", + defTwoFr: "", + defTwoEn : "" + ), + XCStringDefinition.Mock.getDefinitionSectionTwo( + defOneFr: "", + defOneEn: "", + defTwoFr: "", + defTwoEn : "" + ) + ] + .flatMap { $0 } + ), + version: "1.0" + ) + + XCTAssertEqual(rootObject, expect) + } + + func testGenerateXcStringsRootObjectWithComments() { + // Given + let sectionOne = Section.Mock.getSectionOne( + defOneComment: "Comment 1", + defTwoComment: "Comment 2" + ) + let sectionTwo = Section.Mock.getSectionTwo() + + // When + let rootObject = StringsFileGenerator.generateRootObject( + langs: ["fr", "en"], + defaultLang: "fr", + tags: ["ios", "iosonly", "notranslation"], + sections: [sectionOne, sectionTwo] + ) + + // Expect + let expect = + Root( + sourceLanguage: "fr", + strings: XCStringDefinitionContainer( + strings: [ + XCStringDefinition.Mock.getDefinitionSectionOne( + defOneComment: "Comment 1", + defTwoComment: "Comment 2" + ), + XCStringDefinition.Mock.getDefinitionSectionTwo( + // SourceLanguage is frn en definition should the same as fr + defTwoEn: "Section Deux - Definition Deux" + ) + ] + .flatMap { $0 } + ), + version: "1.0" + ) + + XCTAssertEqual(rootObject, expect) + } + + // MARK: - Extension Content + + func testGeneratedExtensionContent() { + // Given + let sectionOne = Section.Mock.getSectionOne() + let sectionTwo = Section.Mock.getSectionTwo() + + // When + let extensionContent = StringsFileGenerator.getExtensionContent(sections: [sectionOne, sectionTwo], + defaultLang: "fr", + tags: ["ios", "iosonly", "notranslation"], + staticVar: false, + inputFilename: "myInputFilename", + extensionName: "GenStrings", + extensionSuffix: "strings") + + // Expect + let expect = Self.getExtensionContentExpectation( + staticVar: false + ) + + if extensionContent != expect { + print(prettyFirstDifferenceBetweenStrings(s1: extensionContent, s2: expect)) + } + XCTAssertEqual(extensionContent.adaptForXCTest(), expect.adaptForXCTest()) + } + + func testGeneratedExtensionContentWithComment() { + // Given + let sectionOne = Section.Mock.getSectionOne( + defOneComment: "This is a comment", + defTwoComment: "This is a comment" + ) + let sectionTwo = Section.Mock.getSectionTwo( + defOneComment: "This is a comment", + defTwoComment: "This is a comment" + ) + + // When + let extensionContent = StringsFileGenerator.getExtensionContent(sections: [sectionOne, sectionTwo], + defaultLang: "fr", + tags: ["ios", "iosonly", "notranslation"], + staticVar: false, + inputFilename: "myInputFilename", + extensionName: "GenStrings", + extensionSuffix: "strings") + + // Expect + let expect = Self.getExtensionContentExpectation( + staticVar: false, + s1DefOneComment: "This is a comment", + s1DefTwoComment: "This is a comment", + s2DefOneComment: "This is a comment", + s2DefTwoComment: "This is a comment", + ) + + if extensionContent != expect { + print(prettyFirstDifferenceBetweenStrings(s1: extensionContent, s2: expect)) + } + XCTAssertEqual(extensionContent.adaptForXCTest(), expect.adaptForXCTest()) + } + + // MARK: - Extension Content Static + func testGeneratedExtensionContentWithStaticVar() { + // Given + let sectionOne = Section.Mock.getSectionOne() + let sectionTwo = Section.Mock.getSectionTwo() + + // When + let extensionContent = StringsFileGenerator.getExtensionContent(sections: [sectionOne, sectionTwo], + defaultLang: "fr", + tags: ["ios", "iosonly", "notranslation"], + staticVar: true, + inputFilename: "myInputFilename", + extensionName: "GenStrings", + extensionSuffix: "strings") + + // Expect + let expect = Self.getExtensionContentExpectation( + staticVar: true + ) + + if extensionContent != expect { + print(prettyFirstDifferenceBetweenStrings(s1: extensionContent, s2: expect)) + } + XCTAssertEqual(extensionContent.adaptForXCTest(), expect.adaptForXCTest()) + } + + func testGeneratedExtensionContentWithStaticVarWithComment() { + // Given + let sectionOne = Section.Mock.getSectionOne( + defOneComment: "This is a comment", + defTwoComment: "This is a comment" + ) + let sectionTwo = Section.Mock.getSectionTwo( + defOneComment: "This is a comment", + defTwoComment: "This is a comment" + ) + + // When + let extensionContent = StringsFileGenerator.getExtensionContent(sections: [sectionOne, sectionTwo], + defaultLang: "fr", + tags: ["ios", "iosonly", "notranslation"], + staticVar: true, + inputFilename: "myInputFilename", + extensionName: "GenStrings", + extensionSuffix: "strings") + + // Expect + let expect = Self.getExtensionContentExpectation( + staticVar: true, + s1DefOneComment: "This is a comment", + s1DefTwoComment: "This is a comment", + s2DefOneComment: "This is a comment", + s2DefTwoComment: "This is a comment", + ) + + if extensionContent != expect { + print(prettyFirstDifferenceBetweenStrings(s1: extensionContent, s2: expect)) + } + XCTAssertEqual(extensionContent.adaptForXCTest(), expect.adaptForXCTest()) + } +} + diff --git a/Tests/ResgenSwiftTests/Strings/StringsFileGeneratorTests.swift b/Tests/ResgenSwiftTests/Strings/StringsFileGeneratorTests.swift deleted file mode 100644 index dec6627..0000000 --- a/Tests/ResgenSwiftTests/Strings/StringsFileGeneratorTests.swift +++ /dev/null @@ -1,1348 +0,0 @@ -// -// StringsFileGeneratorTests.swift -// -// -// Created by Thibaut Schmitt on 06/09/2022. -// - -import Foundation -import XCTest -import ToolCore - -@testable import ResgenSwift - -final class StringsFileGeneratorTests: XCTestCase { - - private func getDefinition(name: String, translations: [String: String], tags: [String], comment: String? = nil) -> Definition { - let definition = Definition(name: name) - definition.tags = tags - definition.translations = translations - definition.comment = comment - return definition - } - - // MARK: - Strings File Content - - func testGenerateStringsFileContent() { - // Given - let sectionOne = Section(name: "section_one") - sectionOne.definitions = [ - getDefinition(name: "s1_def_one", - translations: ["fr": "Section Un - Definition Un", - "en": "Section One - Definition One"], - tags: ["ios","iosonly"]), - getDefinition(name: "s1_def_two", - translations: ["fr": "Section Un - Definition Deux", - "en": "Section One - Definition Two"], - tags: ["ios","iosonly"]) - ] - - let sectionTwo = Section(name: "section_two") - sectionTwo.definitions = [ - getDefinition(name: "s2_def_one", - translations: ["fr": "Section Deux - Definition Un", - "en": "Section Two - Definition One"], - tags: ["ios","iosonly"]), - getDefinition(name: "s2_def_two", - translations: ["fr": "Section Deux - Definition Deux"], - tags: ["notranslation"]) - ] - - // When - let stringsFileContentFr = StringsFileGenerator.generateStringsFileContent(lang: "fr", - defaultLang: "fr", - tags: ["ios", "iosonly", "notranslation"], - sections: [sectionOne, sectionTwo]) - let stringsFileContentEn = StringsFileGenerator.generateStringsFileContent(lang: "en", - defaultLang: "fr", - tags: ["ios", "iosonly", "notranslation"], - sections: [sectionOne, sectionTwo]) - - // Expect - let expectFr = """ - /** - * Apple Strings File - * Generated by ResgenSwift \(ResgenSwiftVersion) - * Language: fr - */ - - /********** section_one **********/ - - "s1_def_one" = "Section Un - Definition Un"; - - "s1_def_two" = "Section Un - Definition Deux"; - - /********** section_two **********/ - - "s2_def_one" = "Section Deux - Definition Un"; - - "s2_def_two" = "Section Deux - Definition Deux"; - """ - - let expectEn = """ - /** - * Apple Strings File - * Generated by ResgenSwift \(ResgenSwiftVersion) - * Language: en - */ - - /********** section_one **********/ - - "s1_def_one" = "Section One - Definition One"; - - "s1_def_two" = "Section One - Definition Two"; - - /********** section_two **********/ - - "s2_def_one" = "Section Two - Definition One"; - - "s2_def_two" = "Section Deux - Definition Deux"; - """ - - XCTAssertEqual(stringsFileContentFr.adaptForXCTest(), expectFr.adaptForXCTest()) - XCTAssertEqual(stringsFileContentEn.adaptForXCTest(), expectEn.adaptForXCTest()) - } - - func testGenerateStringsFileContentWithComment() { - // Given - let sectionOne = Section(name: "section_one") - sectionOne.definitions = [ - getDefinition(name: "s1_def_one", - translations: ["fr": "Section Un - Definition Un", - "en": "Section One - Definition One"], - tags: ["ios","iosonly"], - comment: "This is a comment"), - getDefinition(name: "s1_def_two", - translations: ["fr": "Section Un - Definition Deux", - "en": "Section One - Definition Two"], - tags: ["ios","iosonly"], - comment: "This is a comment") - ] - - let sectionTwo = Section(name: "section_two") - sectionTwo.definitions = [ - getDefinition(name: "s2_def_one", - translations: ["fr": "Section Deux - Definition Un", - "en": "Section Two - Definition One"], - tags: ["ios","iosonly"], - comment: "This is a comment"), - getDefinition(name: "s2_def_two", - translations: ["fr": "Section Deux - Definition Deux"], - tags: ["notranslation"], - comment: "This is a comment") - ] - - // When - let stringsFileContentFr = StringsFileGenerator.generateStringsFileContent(lang: "fr", - defaultLang: "fr", - tags: ["ios", "iosonly", "notranslation"], - sections: [sectionOne, sectionTwo]) - let stringsFileContentEn = StringsFileGenerator.generateStringsFileContent(lang: "en", - defaultLang: "fr", - tags: ["ios", "iosonly", "notranslation"], - sections: [sectionOne, sectionTwo]) - - // Expect - let expectFr = """ - /** - * Apple Strings File - * Generated by ResgenSwift \(ResgenSwiftVersion) - * Language: fr - */ - - /********** section_one **********/ - - "s1_def_one" = "Section Un - Definition Un"; - - "s1_def_two" = "Section Un - Definition Deux"; - - /********** section_two **********/ - - "s2_def_one" = "Section Deux - Definition Un"; - - "s2_def_two" = "Section Deux - Definition Deux"; - """ - - let expectEn = """ - /** - * Apple Strings File - * Generated by ResgenSwift \(ResgenSwiftVersion) - * Language: en - */ - - /********** section_one **********/ - - "s1_def_one" = "Section One - Definition One"; - - "s1_def_two" = "Section One - Definition Two"; - - /********** section_two **********/ - - "s2_def_one" = "Section Two - Definition One"; - - "s2_def_two" = "Section Deux - Definition Deux"; - """ - - XCTAssertEqual(stringsFileContentFr.adaptForXCTest(), expectFr.adaptForXCTest()) - XCTAssertEqual(stringsFileContentEn.adaptForXCTest(), expectEn.adaptForXCTest()) - } - - // MARK: - XcString File Content - - func testGenerateXcStringsRootObject() { - // Given - - // [[section_one]] - // [s1_def_one] - // fr = Section Un - Definition Un - // en = Section One - Definition One - // tags = ios,iosonly - // comments = - // [s1_def_two] - // fr = Section Un - Definition Deux - // en = Section One - Definition Two - // tags = ios,iosonly - // comments = - // - // [[section_two] - // [s2_def_one] - // fr = Section Deux - Definition Un - // en = Section Two - Definition One - // tags = ios,iosonly - // comments = - // [s2_def_two] - // fr = Section Deux - Definition deux - // en = Section Two - Definition Two - // tags = notranslation - // comments = - - - let sectionOne = Section(name: "section_one") - sectionOne.definitions = [ - getDefinition(name: "s1_def_one", - translations: ["fr": "Section Un - Definition Un", - "en": "Section One - Definition One"], - tags: ["ios","iosonly"]), - getDefinition(name: "s1_def_two", - translations: ["fr": "Section Un - Definition Deux", - "en": "Section One - Definition Two"], - tags: ["ios","iosonly"]) - ] - - let sectionTwo = Section(name: "section_two") - sectionTwo.definitions = [ - getDefinition(name: "s2_def_one", - translations: ["fr": "Section Deux - Definition Un", - "en": "Section Two - Definition One"], - tags: ["ios","iosonly"]), - getDefinition(name: "s2_def_two", - translations: ["fr": "Section Deux - Definition Deux", - "en": "Section Two - Definition Two"], - tags: ["notranslation"]) - ] - - // When - let rootObject = StringsFileGenerator.generateRootObject( - langs: ["fr", "en"], - defaultLang: "en", - tags: ["ios", "iosonly", "notranslation"], - sections: [sectionOne, sectionTwo] - ) - - // Expect - let expect = - Root( - sourceLanguage: "en", - strings: XCStringDefinitionContainer( - strings: [ - XCStringDefinition( - title: "s1_def_one", - content: XCStringDefinitionContent( - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [ - XCStringLocalization( - lang: "en", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section One - Definition One" - ) - ) - ), - XCStringLocalization( - lang: "fr", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Un - Definition Un" - ) - ) - ) - ] - ) - ) - ), - XCStringDefinition( - title: "s1_def_two", - content: XCStringDefinitionContent( - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [ - XCStringLocalization( - lang: "en", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section One - Definition Two" - ) - ) - ), - XCStringLocalization( - lang: "fr", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Un - Definition Deux" - ) - ) - ) - ] - ) - ) - ), - XCStringDefinition( - title: "s2_def_one", - content: XCStringDefinitionContent( - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [ - XCStringLocalization( - lang: "en", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Two - Definition One" - ) - ) - ), - XCStringLocalization( - lang: "fr", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Deux - Definition Un" - ) - ) - ) - ] - ) - ) - ), - XCStringDefinition( - title: "s2_def_two", - content: XCStringDefinitionContent( - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [ - XCStringLocalization( - lang: "en", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Two - Definition Two" - ) - ) - ), - XCStringLocalization( - lang: "fr", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Two - Definition Two" - ) - ) - ) - ] - ) - ) - ) - ] - ), - version: "1.0" - ) - - XCTAssertEqual(rootObject, expect) - } - - func testGenerateXcStringsRootObjectWithEmptyTranslations() { - // Given - let sectionOne = Section(name: "section_one") - sectionOne.definitions = [ - getDefinition(name: "s1_def_one", - translations: ["fr": "", - "en": "Section One - Definition One"], - tags: ["ios","iosonly"]), - getDefinition(name: "s1_def_two", - translations: ["fr": "", - "en": "Section One - Definition Two"], - tags: ["ios","iosonly"]) - ] - - let sectionTwo = Section(name: "section_two") - sectionTwo.definitions = [ - getDefinition(name: "s2_def_one", - translations: ["fr": "", - "en": "Section Two - Definition One"], - tags: ["ios","iosonly"]), - getDefinition(name: "s2_def_two", - translations: ["en": "Section Two - Definition Two"], - tags: ["notranslation"]) - ] - - // When - let rootObject = StringsFileGenerator.generateRootObject( - langs: ["fr", "en"], - defaultLang: "en", - tags: ["ios", "iosonly", "notranslation"], - sections: [sectionOne, sectionTwo] - ) - - // [[section_one]] - // [s1_def_one] - // fr = - // en = Section One - Definition One - // tags = ios,iosonly - // comments = - // [s1_def_two] - // fr = - // en = Section One - Definition Two - // tags = ios,iosonly - // comments = - // - // [[section_two] - // [s2_def_one] - // fr = - // en = Section Two - Definition One - // tags = ios,iosonly - // comments = - // [s2_def_two] - // en = Section Two - Definition Two - // tags = notranslation - // comments = - - // Expect - let expect = - Root( - sourceLanguage: "en", - strings: XCStringDefinitionContainer( - strings: [ - XCStringDefinition( - title: "s1_def_one", - content: XCStringDefinitionContent( - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [ - XCStringLocalization( - lang: "en", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section One - Definition One" - ) - ) - ) - ] - ) - ) - ), - XCStringDefinition( - title: "s1_def_two", - content: XCStringDefinitionContent( - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [ - XCStringLocalization( - lang: "en", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section One - Definition Two" - ) - ) - ) - ] - ) - ) - ), - XCStringDefinition( - title: "s2_def_one", - content: XCStringDefinitionContent( - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [ - XCStringLocalization( - lang: "en", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Two - Definition One" - ) - ) - ) - ] - ) - ) - ), - XCStringDefinition( - title: "s2_def_two", - content: XCStringDefinitionContent( - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [ - XCStringLocalization( - lang: "en", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Two - Definition Two" - ) - ) - ), - XCStringLocalization( - lang: "fr", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Two - Definition Two" - ) - ) - ) - ] - ) - ) - ) - ] - ), - version: "1.0" - ) - - XCTAssertEqual(rootObject, expect) - } - - func testGenerateXcStringsRootObjectWithNoTranslations() { - // Given - let sectionOne = Section(name: "section_one") - sectionOne.definitions = [ - getDefinition(name: "s1_def_one", - translations: ["fr": "", - "en": ""], - tags: ["ios","iosonly"]), - getDefinition(name: "s1_def_two", - translations: ["fr": "", - "en": ""], - tags: ["ios","iosonly"]) - ] - - let sectionTwo = Section(name: "section_two") - sectionTwo.definitions = [ - getDefinition(name: "s2_def_one", - translations: ["fr": "", - "en": ""], - tags: ["ios","iosonly"]), - getDefinition(name: "s2_def_two", - translations: ["fr": "", - "en": ""], - tags: ["notranslation"]) - ] - - // When - let rootObject = StringsFileGenerator.generateRootObject( - langs: ["fr", "en"], - defaultLang: "en", - tags: ["ios", "iosonly", "notranslation"], - sections: [sectionOne, sectionTwo] - ) - - // [[section_one]] - // [s1_def_one] - // fr = - // en = - // tags = ios,iosonly - // comments = - // [s1_def_two] - // fr = - // en = - // tags = ios,iosonly - // comments = - // - // [[section_two] - // [s2_def_one] - // fr = - // en = - // tags = ios,iosonly - // comments = - // [s2_def_two] - // fr = - // en = - // tags = ios,iosonly - // comments = - - // Expect - let expect = - Root( - sourceLanguage: "en", - strings: XCStringDefinitionContainer( - strings: [ - XCStringDefinition( - title: "s1_def_one", - content: XCStringDefinitionContent( - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [] - ) - ) - ), - XCStringDefinition( - title: "s1_def_two", - content: XCStringDefinitionContent( - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [] - ) - ) - ), - XCStringDefinition( - title: "s2_def_one", - content: XCStringDefinitionContent( - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [] - ) - ) - ), - XCStringDefinition( - title: "s2_def_two", - content: XCStringDefinitionContent( - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [] - ) - ) - ) - ] - ), - version: "1.0" - ) - - XCTAssertEqual(rootObject, expect) - } - - func testGenerateXcStringsRootObjectWithComments() { - // Given - - // [[section_one]] - // [s1_def_one] - // fr = Section Un - Definition Un - // en = Section One - Definition One - // tags = ios,iosonly - // comments = Comment 1 - // [s1_def_two] - // fr = Section Un - Definition Deux - // en = Section One - Definition Two - // tags = ios,iosonly - // comments = Comment 2 - // - // [[section_two] - // [s2_def_one] - // fr = Section Deux - Definition Un - // en = Section Two - Definition One - // tags = ios,iosonly - // comments = - // [s2_def_two] - // fr = Section Deux - Definition deux - // en = Section Two - Definition Two - // tags = notranslation - // comments = - - - let sectionOne = Section(name: "section_one") - sectionOne.definitions = [ - getDefinition(name: "s1_def_one", - translations: ["fr": "Section Un - Definition Un", - "en": "Section One - Definition One"], - tags: ["ios","iosonly"], - comment: "Comment 1"), - getDefinition(name: "s1_def_two", - translations: ["fr": "Section Un - Definition Deux", - "en": "Section One - Definition Two"], - tags: ["ios","iosonly"], - comment: "Comment 2") - ] - - let sectionTwo = Section(name: "section_two") - sectionTwo.definitions = [ - getDefinition(name: "s2_def_one", - translations: ["fr": "Section Deux - Definition Un", - "en": "Section Two - Definition One"], - tags: ["ios","iosonly"]), - getDefinition(name: "s2_def_two", - translations: ["fr": "Section Deux - Definition Deux"], - tags: ["notranslation"]) - ] - - // When - let rootObject = StringsFileGenerator.generateRootObject( - langs: ["fr", "en"], - defaultLang: "fr", - tags: ["ios", "iosonly", "notranslation"], - sections: [sectionOne, sectionTwo] - ) - - // Expect - let expect = - Root( - sourceLanguage: "fr", - strings: XCStringDefinitionContainer( - strings: [ - XCStringDefinition( - title: "s1_def_one", - content: XCStringDefinitionContent( - comment: "Comment 1", - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [ - XCStringLocalization( - lang: "en", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section One - Definition One" - ) - ) - ), - XCStringLocalization( - lang: "fr", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Un - Definition Un" - ) - ) - ) - ] - ) - ) - ), - XCStringDefinition( - title: "s1_def_two", - content: XCStringDefinitionContent( - comment: "Comment 2", - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [ - XCStringLocalization( - lang: "en", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section One - Definition Two" - ) - ) - ), - XCStringLocalization( - lang: "fr", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Un - Definition Deux" - ) - ) - ) - ] - ) - ) - ), - XCStringDefinition( - title: "s2_def_one", - content: XCStringDefinitionContent( - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [ - XCStringLocalization( - lang: "en", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Two - Definition One" - ) - ) - ), - XCStringLocalization( - lang: "fr", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Deux - Definition Un" - ) - ) - ) - ] - ) - ) - ), - XCStringDefinition( - title: "s2_def_two", - content: XCStringDefinitionContent( - extractionState: "manual", - localizations: XCStringLocalizationContainer( - localizations: [ - XCStringLocalization( - lang: "en", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Deux - Definition Deux" - ) - ) - ), - XCStringLocalization( - lang: "fr", - content: XCStringLocalizationLangContent( - stringUnit: DefaultStringUnit( - state: "translated", - value: "Section Deux - Definition Deux" - ) - ) - ) - ] - ) - ) - ) - ] - ), - version: "1.0" - ) - - // """ - // { - // "sourceLanguage" : "en", - // "strings" : { - // "s1_def_one" : { - // "extractionState" : "manual", - // "localizations" : { - // "en" : { - // "stringUnit" : { - // "state" : "translated", - // "value" : "Section One - Definition One" - // } - // }, - // "fr" : { - // "stringUnit" : { - // "state" : "translated", - // "value" : "Section Un - Definition Un" - // } - // } - // } - // }, - // "s1_def_two" : { - // "extractionState" : "manual", - // "localizations" : { - // "en" : { - // "stringUnit" : { - // "state" : "translated", - // "value" : "Section One - Definition Two" - // } - // }, - // "fr" : { - // "stringUnit" : { - // "state" : "translated", - // "value" : "Section Un - Definition Deux" - // } - // } - // } - // }, - // "s2_def_one" : { - // "extractionState" : "manual", - // "localizations" : { - // "en" : { - // "stringUnit" : { - // "state" : "translated", - // "value" : "Section Two - Definition One" - // } - // }, - // "fr" : { - // "stringUnit" : { - // "state" : "translated", - // "value" : "Section Deux - Definition Une" - // } - // } - // } - // }, - // "s2_def_two" : { - // "extractionState" : "manual", - // "localizations" : { - // "en" : { - // "stringUnit" : { - // "state" : "translated", - // "value" : "Section Two - Definition Two" - // } - // }, - // "fr" : { - // "stringUnit" : { - // "state" : "translated", - // "value" : "Section Deux - Definition Deux" - // } - // } - // } - // } - // }, - // "version" : "1.0" - // } - // """ - XCTAssertEqual(rootObject, expect) - } - - // MARK: - Extension Content - func testGeneratedExtensionContent() { - // Given - let sectionOne = Section(name: "section_one") - sectionOne.definitions = [ - getDefinition(name: "s1_def_one", - translations: ["fr": "Section Un - Definition Un", - "en": "Section One - Definition One"], - tags: ["ios","iosonly"]), - getDefinition(name: "s1_def_two", - translations: ["fr": "Section Un - Definition Deux", - "en": "Section One - Definition Two"], - tags: ["ios","iosonly"]) - ] - - let sectionTwo = Section(name: "section_two") - sectionTwo.definitions = [ - getDefinition(name: "s2_def_one", - translations: ["fr": "Section Deux - Definition Un", - "en": "Section Two - Definition One"], - tags: ["ios","iosonly"]), - getDefinition(name: "s2_def_two", - translations: ["fr": "Section Deux - Definition Deux"], - tags: ["notranslation"]) - ] - - // When - let extensionContent = StringsFileGenerator.getExtensionContent(sections: [sectionOne, sectionTwo], - defaultLang: "fr", - tags: ["ios", "iosonly", "notranslation"], - staticVar: false, - inputFilename: "myInputFilename", - extensionName: "GenStrings", - extensionSuffix: "strings") - - // Expect - let expect = """ - // Generated by ResgenSwift.Strings.Stringium \(ResgenSwiftVersion) - - import UIKit - - fileprivate let kStringsFileName = "myInputFilename" - - extension GenStrings { - - enum KeyStrings: String { - case s1_def_one = "s1_def_one" - case s1_def_two = "s1_def_two" - case s2_def_one = "s2_def_one" - case s2_def_two = "s2_def_two" - - var keyPath: KeyPath { - switch self { - case .s1_def_one: return \\GenStrings.s1_def_one - case .s1_def_two: return \\GenStrings.s1_def_two - case .s2_def_one: return \\GenStrings.s2_def_one - case .s2_def_two: return \\GenStrings.s2_def_two - } - } - } - - // MARK: - section_one - - /// Translation in fr : - /// Section Un - Definition Un - /// - /// Comment : - /// No comment - var s1_def_one: String { - NSLocalizedString("s1_def_one", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Un - Definition Un", comment: "") - } - - /// Translation in fr : - /// Section Un - Definition Deux - /// - /// Comment : - /// No comment - var s1_def_two: String { - NSLocalizedString("s1_def_two", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Un - Definition Deux", comment: "") - } - - // MARK: - section_two - - /// Translation in fr : - /// Section Deux - Definition Un - /// - /// Comment : - /// No comment - var s2_def_one: String { - NSLocalizedString("s2_def_one", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Deux - Definition Un", comment: "") - } - - /// Translation in fr : - /// Section Deux - Definition Deux - /// - /// Comment : - /// No comment - var s2_def_two: String { - NSLocalizedString("s2_def_two", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Deux - Definition Deux", comment: "") - } - } - """ - - if extensionContent != expect { - print(prettyFirstDifferenceBetweenStrings(s1: extensionContent, s2: expect)) - } - XCTAssertEqual(extensionContent.adaptForXCTest(), expect.adaptForXCTest()) - } - - func testGeneratedExtensionContentWithComment() { - // Given - let sectionOne = Section(name: "section_one") - sectionOne.definitions = [ - getDefinition(name: "s1_def_one", - translations: ["fr": "Section Un - Definition Un", - "en": "Section One - Definition One"], - tags: ["ios","iosonly"], - comment: "This is a comment"), - getDefinition(name: "s1_def_two", - translations: ["fr": "Section Un - Definition Deux", - "en": "Section One - Definition Two"], - tags: ["ios","iosonly"], - comment: "This is a comment") - ] - - let sectionTwo = Section(name: "section_two") - sectionTwo.definitions = [ - getDefinition(name: "s2_def_one", - translations: ["fr": "Section Deux - Definition Un", - "en": "Section Two - Definition One"], - tags: ["ios","iosonly"], - comment: "This is a comment"), - getDefinition(name: "s2_def_two", - translations: ["fr": "Section Deux - Definition Deux"], - tags: ["notranslation"], - comment: "This is a comment") - ] - - // When - let extensionContent = StringsFileGenerator.getExtensionContent(sections: [sectionOne, sectionTwo], - defaultLang: "fr", - tags: ["ios", "iosonly", "notranslation"], - staticVar: false, - inputFilename: "myInputFilename", - extensionName: "GenStrings", - extensionSuffix: "strings") - - // Expect - let expect = """ - // Generated by ResgenSwift.Strings.Stringium \(ResgenSwiftVersion) - - import UIKit - - fileprivate let kStringsFileName = "myInputFilename" - - extension GenStrings { - - enum KeyStrings: String { - case s1_def_one = "s1_def_one" - case s1_def_two = "s1_def_two" - case s2_def_one = "s2_def_one" - case s2_def_two = "s2_def_two" - - var keyPath: KeyPath { - switch self { - case .s1_def_one: return \\GenStrings.s1_def_one - case .s1_def_two: return \\GenStrings.s1_def_two - case .s2_def_one: return \\GenStrings.s2_def_one - case .s2_def_two: return \\GenStrings.s2_def_two - } - } - } - - // MARK: - section_one - - /// Translation in fr : - /// Section Un - Definition Un - /// - /// Comment : - /// This is a comment - var s1_def_one: String { - NSLocalizedString("s1_def_one", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Un - Definition Un", comment: "This is a comment") - } - - /// Translation in fr : - /// Section Un - Definition Deux - /// - /// Comment : - /// This is a comment - var s1_def_two: String { - NSLocalizedString("s1_def_two", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Un - Definition Deux", comment: "This is a comment") - } - - // MARK: - section_two - - /// Translation in fr : - /// Section Deux - Definition Un - /// - /// Comment : - /// This is a comment - var s2_def_one: String { - NSLocalizedString("s2_def_one", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Deux - Definition Un", comment: "This is a comment") - } - - /// Translation in fr : - /// Section Deux - Definition Deux - /// - /// Comment : - /// This is a comment - var s2_def_two: String { - NSLocalizedString("s2_def_two", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Deux - Definition Deux", comment: "This is a comment") - } - } - """ - - if extensionContent != expect { - print(prettyFirstDifferenceBetweenStrings(s1: extensionContent, s2: expect)) - } - XCTAssertEqual(extensionContent.adaptForXCTest(), expect.adaptForXCTest()) - } - - // MARK: - Extension Content Static - func testGeneratedExtensionContentWithStaticVar() { - // Given - let sectionOne = Section(name: "section_one") - sectionOne.definitions = [ - getDefinition(name: "s1_def_one", - translations: ["fr": "Section Un - Definition Un", - "en": "Section One - Definition One"], - tags: ["ios","iosonly"]), - getDefinition(name: "s1_def_two", - translations: ["fr": "Section Un - Definition Deux", - "en": "Section One - Definition Two"], - tags: ["ios","iosonly"]) - ] - - let sectionTwo = Section(name: "section_two") - sectionTwo.definitions = [ - getDefinition(name: "s2_def_one", - translations: ["fr": "Section Deux - Definition Un", - "en": "Section Two - Definition One"], - tags: ["ios","iosonly"]), - getDefinition(name: "s2_def_two", - translations: ["fr": "Section Deux - Definition Deux"], - tags: ["notranslation"]) - ] - - // When - let extensionContent = StringsFileGenerator.getExtensionContent(sections: [sectionOne, sectionTwo], - defaultLang: "fr", - tags: ["ios", "iosonly", "notranslation"], - staticVar: true, - inputFilename: "myInputFilename", - extensionName: "GenStrings", - extensionSuffix: "strings") - - // Expect - let expect = """ - // Generated by ResgenSwift.Strings.Stringium \(ResgenSwiftVersion) - - import UIKit - - fileprivate let kStringsFileName = "myInputFilename" - - extension GenStrings { - - enum KeyStrings: String { - case s1_def_one = "s1_def_one" - case s1_def_two = "s1_def_two" - case s2_def_one = "s2_def_one" - case s2_def_two = "s2_def_two" - - var keyPath: KeyPath { - switch self { - case .s1_def_one: return \\GenStrings.s1_def_one - case .s1_def_two: return \\GenStrings.s1_def_two - case .s2_def_one: return \\GenStrings.s2_def_one - case .s2_def_two: return \\GenStrings.s2_def_two - } - } - } - - // MARK: - section_one - - /// Translation in fr : - /// Section Un - Definition Un - /// - /// Comment : - /// No comment - static var s1_def_one: String { - NSLocalizedString("s1_def_one", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Un - Definition Un", comment: "") - } - - /// Translation in fr : - /// Section Un - Definition Deux - /// - /// Comment : - /// No comment - static var s1_def_two: String { - NSLocalizedString("s1_def_two", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Un - Definition Deux", comment: "") - } - - // MARK: - section_two - - /// Translation in fr : - /// Section Deux - Definition Un - /// - /// Comment : - /// No comment - static var s2_def_one: String { - NSLocalizedString("s2_def_one", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Deux - Definition Un", comment: "") - } - - /// Translation in fr : - /// Section Deux - Definition Deux - /// - /// Comment : - /// No comment - static var s2_def_two: String { - NSLocalizedString("s2_def_two", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Deux - Definition Deux", comment: "") - } - } - """ - - if extensionContent != expect { - print(prettyFirstDifferenceBetweenStrings(s1: extensionContent, s2: expect)) - } - XCTAssertEqual(extensionContent.adaptForXCTest(), expect.adaptForXCTest()) - } - - func testGeneratedExtensionContentWithStaticVarWithComment() { - // Given - let sectionOne = Section(name: "section_one") - sectionOne.definitions = [ - getDefinition(name: "s1_def_one", - translations: ["fr": "Section Un - Definition Un", - "en": "Section One - Definition One"], - tags: ["ios","iosonly"], - comment: "This is a comment"), - getDefinition(name: "s1_def_two", - translations: ["fr": "Section Un - Definition Deux", - "en": "Section One - Definition Two"], - tags: ["ios","iosonly"], - comment: "This is a comment") - ] - - let sectionTwo = Section(name: "section_two") - sectionTwo.definitions = [ - getDefinition(name: "s2_def_one", - translations: ["fr": "Section Deux - Definition Un", - "en": "Section Two - Definition One"], - tags: ["ios","iosonly"], - comment: "This is a comment"), - getDefinition(name: "s2_def_two", - translations: ["fr": "Section Deux - Definition Deux"], - tags: ["notranslation"], - comment: "This is a comment") - ] - - // When - let extensionContent = StringsFileGenerator.getExtensionContent(sections: [sectionOne, sectionTwo], - defaultLang: "fr", - tags: ["ios", "iosonly", "notranslation"], - staticVar: true, - inputFilename: "myInputFilename", - extensionName: "GenStrings", - extensionSuffix: "strings") - - // Expect - let expect = """ - // Generated by ResgenSwift.Strings.Stringium \(ResgenSwiftVersion) - - import UIKit - - fileprivate let kStringsFileName = "myInputFilename" - - extension GenStrings { - - enum KeyStrings: String { - case s1_def_one = "s1_def_one" - case s1_def_two = "s1_def_two" - case s2_def_one = "s2_def_one" - case s2_def_two = "s2_def_two" - - var keyPath: KeyPath { - switch self { - case .s1_def_one: return \\GenStrings.s1_def_one - case .s1_def_two: return \\GenStrings.s1_def_two - case .s2_def_one: return \\GenStrings.s2_def_one - case .s2_def_two: return \\GenStrings.s2_def_two - } - } - } - - // MARK: - section_one - - /// Translation in fr : - /// Section Un - Definition Un - /// - /// Comment : - /// This is a comment - static var s1_def_one: String { - NSLocalizedString("s1_def_one", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Un - Definition Un", comment: "This is a comment") - } - - /// Translation in fr : - /// Section Un - Definition Deux - /// - /// Comment : - /// This is a comment - static var s1_def_two: String { - NSLocalizedString("s1_def_two", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Un - Definition Deux", comment: "This is a comment") - } - - // MARK: - section_two - - /// Translation in fr : - /// Section Deux - Definition Un - /// - /// Comment : - /// This is a comment - static var s2_def_one: String { - NSLocalizedString("s2_def_one", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Deux - Definition Un", comment: "This is a comment") - } - - /// Translation in fr : - /// Section Deux - Definition Deux - /// - /// Comment : - /// This is a comment - static var s2_def_two: String { - NSLocalizedString("s2_def_two", tableName: kStringsFileName, bundle: Bundle.main, value: "Section Deux - Definition Deux", comment: "This is a comment") - } - } - """ - - if extensionContent != expect { - print(prettyFirstDifferenceBetweenStrings(s1: extensionContent, s2: expect)) - } - XCTAssertEqual(extensionContent.adaptForXCTest(), expect.adaptForXCTest()) - } -} -