// // 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: - 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()) } }