Skip to content

Commit 3456a0e

Browse files
committed
add/fix tests
1 parent 88a68b1 commit 3456a0e

File tree

5 files changed

+116
-34
lines changed

5 files changed

+116
-34
lines changed

Sources/Tolgee/CDN/FetchCdnService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class FetchCdnService: Sendable {
3131
for file in files {
3232
let urlSession = self.urlSession
3333
group.addTask {
34-
let url = cdnURL.appending(component: file.path)
34+
let url = cdnURL.appending(path: file.path)
3535
var request = URLRequest(url: url)
3636
if let etag = file.etag {
3737
request.setValue(etag, forHTTPHeaderField: "If-None-Match")

Sources/Tolgee/Tolgee.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -382,21 +382,6 @@ public final class Tolgee {
382382
}
383383
}
384384

385-
func loadTranslations(from jsonString: String, table: String = "") throws {
386-
387-
guard let data = jsonString.data(using: .utf8) else {
388-
throw TolgeeError.invalidJSONString
389-
}
390-
391-
let translations = try JSONParser.loadTranslations(from: data)
392-
self.translations[table] = translations
393-
}
394-
395-
func loadTranslations(from jsonData: Data, table: String = "") throws {
396-
let translations = try JSONParser.loadTranslations(from: jsonData)
397-
self.translations[table] = translations
398-
}
399-
400385
/// Translates a given key to a localized string with optional format arguments.
401386
///
402387
/// This method first attempts to find the translation in the loaded Tolgee translations,

Tests/TolgeeTests/Networking/FetchCdnServiceTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct FetchCdnServiceTests {
3737

3838
// Set up mock responses
3939
let baseURL = cdnURL.appending(component: "cs.json")
40-
let namespaceURL = cdnURL.appending(component: "Localizable2/cs.json")
40+
let namespaceURL = cdnURL.appending(path: "Localizable2/cs.json")
4141
try await mockSession.setMockJSONResponse(
4242
for: baseURL, json: ["Hello, world!": "Ahoj, světe!"])
4343
try await mockSession.setMockJSONResponse(

Tests/TolgeeTests/TolgeeTestsCs.swift

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ struct TolgeeTestsCs {
4444

4545
let czechLocale = Locale(identifier: "cs_CZ")
4646

47-
@Test func testLoadCzechTranslationsFromJSON() throws {
47+
@Test func testLoadCzechTranslationsFromJSON() async throws {
4848

4949
let context = TestContext()
5050
let tolgee = context.tolgee
5151
tolgee.initialize(cdn: URL(string: "https://cdn.example.com")!, language: "cs")
5252

53-
try tolgee.loadTranslations(from: testTranslationsJSON)
53+
await context.urlSession.setMockResponse(
54+
for: URL(string: "https://cdn.example.com/cs.json")!,
55+
result: .success(Data(testTranslationsJSON.utf8)))
56+
await tolgee.remoteFetch()
5457

5558
if #available(macOS 15.4, *) {
5659

@@ -63,11 +66,15 @@ struct TolgeeTestsCs {
6366
}
6467
}
6568

66-
@Test func testCzechSimplePlaceholderReplacement() throws {
69+
@Test func testCzechSimplePlaceholderReplacement() async throws {
6770
let context = TestContext()
6871
let tolgee = context.tolgee
6972
tolgee.initialize(cdn: URL(string: "https://cdn.example.com")!, language: "cs")
70-
try tolgee.loadTranslations(from: testTranslationsJSON)
73+
74+
await context.urlSession.setMockResponse(
75+
for: URL(string: "https://cdn.example.com/cs.json")!,
76+
result: .success(Data(testTranslationsJSON.utf8)))
77+
await tolgee.remoteFetch()
7178

7279
if #available(macOS 15.4, *) {
7380

@@ -83,11 +90,15 @@ struct TolgeeTestsCs {
8390
}
8491
}
8592

86-
@Test func testCzechPluralFormsWithHashReplacement() throws {
93+
@Test func testCzechPluralFormsWithHashReplacement() async throws {
8794
let context = TestContext()
8895
let tolgee = context.tolgee
8996
tolgee.initialize(cdn: URL(string: "https://cdn.example.com")!, language: "cs")
90-
try tolgee.loadTranslations(from: testTranslationsJSON)
97+
98+
await context.urlSession.setMockResponse(
99+
for: URL(string: "https://cdn.example.com/cs.json")!,
100+
result: .success(Data(testTranslationsJSON.utf8)))
101+
await tolgee.remoteFetch()
91102

92103
if #available(macOS 15.4, *) {
93104

@@ -112,11 +123,15 @@ struct TolgeeTestsCs {
112123
}
113124
}
114125

115-
@Test func testCzechMissingTranslationFallback() throws {
126+
@Test func testCzechMissingTranslationFallback() async throws {
116127
let context = TestContext()
117128
let tolgee = context.tolgee
118129
tolgee.initialize(cdn: URL(string: "https://cdn.example.com")!, language: "cs")
119-
try tolgee.loadTranslations(from: testTranslationsJSON)
130+
131+
await context.urlSession.setMockResponse(
132+
for: URL(string: "https://cdn.example.com/cs.json")!,
133+
result: .success(Data(testTranslationsJSON.utf8)))
134+
await tolgee.remoteFetch()
120135

121136
// Test with a key that doesn't exist (should fallback to NSLocalizedString)
122137
let missingKey = tolgee.translate("nonexistent.key")

Tests/TolgeeTests/TolgeeTestsEn.swift

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,29 @@ struct TolgeeTestsEn {
1919
}
2020
},
2121
"My name is %@": "My name is %@",
22-
"Redraw": "Redraw"
22+
"Redraw": "Redraw",
23+
"name_and_num_apples": "My name is %@ and I have %lld apples"
24+
}
25+
"""
26+
27+
let testTranslationsJSONCustomNamespace = """
28+
{
29+
"hello_world": "Hello, world from custom namespace!"
2330
}
2431
"""
2532

2633
let englishLocale = Locale(identifier: "en_US")
2734

28-
@Test func testLoadEnglishTranslationsFromJSON() throws {
35+
@Test func testLoadEnglishTranslationsFromJSON() async throws {
2936

3037
let context = TestContext()
3138
let tolgee = context.tolgee
3239
tolgee.initialize(cdn: URL(string: "https://cdn.example.com")!, language: "en")
3340

34-
try tolgee.loadTranslations(from: testTranslationsJSON)
41+
await context.urlSession.setMockResponse(
42+
for: URL(string: "https://cdn.example.com/en.json")!,
43+
result: .success(Data(testTranslationsJSON.utf8)))
44+
await tolgee.remoteFetch()
3545

3646
if #available(macOS 15.4, *) {
3747

@@ -44,11 +54,15 @@ struct TolgeeTestsEn {
4454
}
4555
}
4656

47-
@Test func testEnglishSimplePlaceholderReplacement() throws {
57+
@Test func testEnglishSimplePlaceholderReplacement() async throws {
4858
let context = TestContext()
4959
let tolgee = context.tolgee
5060
tolgee.initialize(cdn: URL(string: "https://cdn.example.com")!, language: "en")
51-
try tolgee.loadTranslations(from: testTranslationsJSON)
61+
62+
await context.urlSession.setMockResponse(
63+
for: URL(string: "https://cdn.example.com/en.json")!,
64+
result: .success(Data(testTranslationsJSON.utf8)))
65+
await tolgee.remoteFetch()
5266

5367
if #available(macOS 15.4, *) {
5468

@@ -64,11 +78,15 @@ struct TolgeeTestsEn {
6478
}
6579
}
6680

67-
@Test func testEnglishPluralFormsWithHashReplacement() throws {
81+
@Test func testEnglishPluralFormsWithHashReplacement() async throws {
6882
let context = TestContext()
6983
let tolgee = context.tolgee
7084
tolgee.initialize(cdn: URL(string: "https://cdn.example.com")!, language: "en")
71-
try tolgee.loadTranslations(from: testTranslationsJSON)
85+
86+
await context.urlSession.setMockResponse(
87+
for: URL(string: "https://cdn.example.com/en.json")!,
88+
result: .success(Data(testTranslationsJSON.utf8)))
89+
await tolgee.remoteFetch()
7290

7391
if #available(macOS 15.4, *) {
7492

@@ -93,14 +111,78 @@ struct TolgeeTestsEn {
93111
}
94112
}
95113

96-
@Test func testEnglishMissingTranslationFallback() throws {
114+
@Test func testEnglishMissingTranslationFallback() async throws {
97115
let context = TestContext()
98116
let tolgee = context.tolgee
99117
tolgee.initialize(cdn: URL(string: "https://cdn.example.com")!, language: "en")
100-
try tolgee.loadTranslations(from: testTranslationsJSON)
118+
119+
await context.urlSession.setMockResponse(
120+
for: URL(string: "https://cdn.example.com/en.json")!,
121+
result: .success(Data(testTranslationsJSON.utf8)))
122+
await tolgee.remoteFetch()
101123

102124
// Test with a key that doesn't exist (should fallback to NSLocalizedString)
103125
let missingKey = tolgee.translate("nonexistent.key")
104126
#expect(missingKey == "nonexistent.key")
105127
}
128+
129+
@Test func testEnglishNameAndNumberPlaceholderReplacement() async throws {
130+
let context = TestContext()
131+
let tolgee = context.tolgee
132+
tolgee.initialize(cdn: URL(string: "https://cdn.example.com")!, language: "en")
133+
134+
await context.urlSession.setMockResponse(
135+
for: URL(string: "https://cdn.example.com/en.json")!,
136+
result: .success(Data(testTranslationsJSON.utf8)))
137+
await tolgee.remoteFetch()
138+
139+
if #available(macOS 15.4, *) {
140+
141+
// Test English name and number replacement
142+
let nameAndNumber = tolgee.translate("name_and_num_apples", "John", 5, locale: englishLocale)
143+
#expect(nameAndNumber == "My name is John and I have 5 apples")
144+
145+
let anotherNameAndNumber = tolgee.translate(
146+
"name_and_num_apples", "Alice", 1, locale: englishLocale)
147+
#expect(anotherNameAndNumber == "My name is Alice and I have 1 apples")
148+
149+
let zeroApples = tolgee.translate("name_and_num_apples", "Bob", 0, locale: englishLocale)
150+
#expect(zeroApples == "My name is Bob and I have 0 apples")
151+
152+
} else {
153+
#expect(Bool(false)) // Skip this test on older versions
154+
}
155+
}
156+
157+
@Test func testLoadEnglishTranslationsFromCustomNamespace() async throws {
158+
let context = TestContext()
159+
let tolgee = context.tolgee
160+
tolgee.initialize(
161+
cdn: URL(string: "https://cdn.example.com")!, language: "en", namespaces: ["namespace"],
162+
enableDebugLogs: true)
163+
164+
await context.urlSession.setMockResponse(
165+
for: URL(string: "https://cdn.example.com/en.json")!,
166+
result: .success(Data(testTranslationsJSON.utf8)))
167+
168+
await context.urlSession.setMockResponse(
169+
for: URL(string: "https://cdn.example.com/namespace/en.json")!,
170+
result: .success(Data(testTranslationsJSONCustomNamespace.utf8)))
171+
172+
await tolgee.remoteFetch()
173+
174+
if #available(macOS 15.4, *) {
175+
176+
// Test basic English translation from custom namespace
177+
let greeting = tolgee.translate("hello_world", table: "namespace", locale: englishLocale)
178+
#expect(greeting == "Hello, world from custom namespace!")
179+
180+
// Test that keys from the main namespace are not available
181+
let missingKey = tolgee.translate("Hello, world!", table: "namespace", locale: englishLocale)
182+
#expect(missingKey == "Hello, world!") // Should fallback to the key itself
183+
184+
} else {
185+
#expect(Bool(false)) // Skip this test on older versions
186+
}
187+
}
106188
}

0 commit comments

Comments
 (0)