Files
resgen.swift/Tests/ResgenSwiftTests/Strings/StringFileGeneratorTests/StringsFileGeneratorTests.swift
T
t.schmitt ac5d9cef58
openium/resgen.swift/pipeline/head There was a failure building this commit
openium/resgen.swift/pipeline/pr-master There was a failure building this commit
Migrate tests from XCTest to Swift Testing
- Convert XCTestCase classes to structs with @Test functions
- Replace XCTAssert* with #expect, and force unwraps/XCTFail with #require
- Resolve test bundle via a class token in the CLI smoke test (Swift Testing
  does not register the .xctest bundle in Bundle.allBundles)
- Rename adaptForXCTest() to adaptForTest()
2026-11-23 15:53:21 +01:00

511 lines
18 KiB
Swift

//
// StringsFileGeneratorTests.swift
//
//
// Created by Thibaut Schmitt on 06/09/2022.
//
import Foundation
import Testing
import ToolCore
@testable import ResgenSwift
struct StringsFileGeneratorTests {
// MARK: - Strings File Content
@Test
func generateStringsFileContent() {
// Given
let sectionOne = Section.Mock.getSectionOne()
let sectionTwo = Section.Mock.getSectionTwo()
// When - CPD-OFF
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]
)
// CPD-ON
// Expect
#expect(stringsFileContentFr.adaptForTest() == Self.appleStringsFileExpectationFr.adaptForTest())
#expect(stringsFileContentEn.adaptForTest() == Self.appleStringsFileExpectationEn.adaptForTest())
}
@Test
func generateStringsFileContentWithComment() {
// 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 - CPD-OFF
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]
)
// CPD-ON
// Expect
#expect(stringsFileContentFr.adaptForTest() == Self.appleStringsFileExpectationFr.adaptForTest())
#expect(stringsFileContentEn.adaptForTest() == Self.appleStringsFileExpectationEn.adaptForTest())
}
// MARK: - XcString File Content
@Test
func generateXcStringsRootObject() {
// 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"
)
#expect(rootObject == expect)
}
@Test
func generateXcStringsRootObjectWithEmptyTranslations() {
// 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"
)
#expect(rootObject == expect)
}
@Test
func generateXcStringsRootObjectWithNoTranslations() {
// 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"
)
#expect(rootObject == expect)
}
@Test
func generateXcStringsRootObjectWithComments() {
// 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"
)
#expect(rootObject == expect)
}
// MARK: - Extension Content
@Test
func generatedExtensionContent() {
// 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"],
isStatic: false,
inputFilename: "myInputFilename",
extensionName: "GenStrings",
extensionSuffix: "strings",
visibility: .internal,
assetBundle: .main
)
// Expect
let expect = Self.getExtensionContentExpectation(
isStatic: false
)
if extensionContent != expect {
print(prettyFirstDifferenceBetweenStrings(s1: extensionContent, s2: expect))
}
#expect(extensionContent.adaptForTest() == expect.adaptForTest())
}
@Test
func generatedExtensionContentWithComment() {
// 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"],
isStatic: false,
inputFilename: "myInputFilename",
extensionName: "GenStrings",
extensionSuffix: "strings",
visibility: .public,
assetBundle: .main
)
// Expect
let expect = Self.getExtensionContentExpectation(
isStatic: false,
s1DefOneComment: "This is a comment",
s1DefTwoComment: "This is a comment",
s2DefOneComment: "This is a comment",
s2DefTwoComment: "This is a comment",
visibility: .public
)
if extensionContent != expect {
print(prettyFirstDifferenceBetweenStrings(s1: extensionContent, s2: expect))
}
#expect(extensionContent.adaptForTest() == expect.adaptForTest())
}
// MARK: - Extension Content Static
@Test
func generatedExtensionContentWithIsStatic() {
// 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"],
isStatic: true,
inputFilename: "myInputFilename",
extensionName: "GenStrings",
extensionSuffix: "strings",
visibility: .package,
assetBundle: .module
)
// Expect
let expect = Self.getExtensionContentExpectation(
isStatic: true,
visibility: .package,
assetBundle: .module
)
if extensionContent != expect {
print(prettyFirstDifferenceBetweenStrings(s1: extensionContent, s2: expect))
}
#expect(extensionContent.adaptForTest() == expect.adaptForTest())
}
@Test
func generatedExtensionContentWithIsStaticWithComment() {
// 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"],
isStatic: true,
inputFilename: "myInputFilename",
extensionName: "GenStrings",
extensionSuffix: "strings",
visibility: .internal,
assetBundle: .module
)
// Expect
let expect = Self.getExtensionContentExpectation(
isStatic: true,
s1DefOneComment: "This is a comment",
s1DefTwoComment: "This is a comment",
s2DefOneComment: "This is a comment",
s2DefTwoComment: "This is a comment",
visibility: .internal,
assetBundle: .module
)
if extensionContent != expect {
print(prettyFirstDifferenceBetweenStrings(s1: extensionContent, s2: expect))
}
#expect(extensionContent.adaptForTest() == expect.adaptForTest())
}
}