Remove a lots of code duplication in StringsFileGeneratorTests
Some checks reported warnings
gitea-openium/resgen.swift/pipeline/head This commit is unstable

This commit is contained in:
Thibaut Schmitt 2025-04-30 15:24:39 +02:00
parent d66775730e
commit c3445042b7
6 changed files with 855 additions and 1348 deletions

View File

@ -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";
"""
}

View File

@ -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<GenStrings, String> {
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)")
}
}
"""
}
}

View File

@ -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
}
}
}

View File

@ -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
}
}
}

View File

@ -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())
}
}

File diff suppressed because it is too large Load Diff