Skip to content

Commit 5ca1107

Browse files
committed
Fix test failures
1 parent e8db5f9 commit 5ca1107

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

Tests/FoundationInternationalizationTests/CalendarTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,21 +202,21 @@ private struct CalendarTests {
202202
#expect(anyHashables[1] == anyHashables[2])
203203
}
204204

205-
func decodeHelper(_ l: Calendar) -> Calendar {
205+
func decodeHelper(_ l: Calendar) throws -> Calendar {
206206
let je = JSONEncoder()
207-
let data = try! je.encode(l)
207+
let data = try je.encode(l)
208208
let jd = JSONDecoder()
209-
return try! jd.decode(Calendar.self, from: data)
209+
return try jd.decode(Calendar.self, from: data)
210210
}
211211

212-
@Test func serializationOfCurrent() async {
213-
await usingCurrentInternationalizationPreferences {
212+
@Test func serializationOfCurrent() async throws {
213+
try await usingCurrentInternationalizationPreferences {
214214
let current = Calendar.current
215-
let decodedCurrent = decodeHelper(current)
215+
let decodedCurrent = try decodeHelper(current)
216216
#expect(decodedCurrent == current)
217217

218218
let autoupdatingCurrent = Calendar.autoupdatingCurrent
219-
let decodedAutoupdatingCurrent = decodeHelper(autoupdatingCurrent)
219+
let decodedAutoupdatingCurrent = try decodeHelper(autoupdatingCurrent)
220220
#expect(decodedAutoupdatingCurrent == autoupdatingCurrent)
221221

222222
#expect(decodedCurrent != decodedAutoupdatingCurrent)
@@ -227,7 +227,7 @@ private struct CalendarTests {
227227
// Calendar, unlike TimeZone and Locale, has some mutable properties
228228
var modified = Calendar.autoupdatingCurrent
229229
modified.firstWeekday = 6
230-
let decodedModified = decodeHelper(modified)
230+
let decodedModified = try decodeHelper(modified)
231231
#expect(decodedModified != autoupdatingCurrent)
232232
#expect(modified == decodedModified)
233233
}

Tests/FoundationInternationalizationTests/CurrentInternationalizationPreferencesActor.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,25 @@ private actor CurrentInternationalizationPreferencesActor: GlobalActor {
2525

2626
private init() {}
2727

28+
private static func resetCaches() {
29+
LocaleCache.cache.reset()
30+
CalendarCache.cache.reset()
31+
_ = TimeZoneCache.cache.reset()
32+
_ = TimeZone.resetSystemTimeZone()
33+
TimeZoneCache.cache.setDefault(nil)
34+
}
35+
2836
@CurrentInternationalizationPreferencesActor
2937
static func usingCurrentInternationalizationPreferences(
3038
body: () throws -> Void // Must be synchronous to prevent suspension points within body which could introduce a change in the preferences
3139
) rethrows {
40+
// Reset caches before the test to clean up any lingering, eagerly realized values
41+
resetCaches()
42+
3243
try body()
3344

3445
// Reset everything after the test runs to ensure custom values don't persist
35-
LocaleCache.cache.reset()
36-
CalendarCache.cache.reset()
37-
_ = TimeZoneCache.cache.reset()
38-
_ = TimeZone.resetSystemTimeZone()
46+
resetCaches()
3947
}
4048
}
4149

Tests/FoundationInternationalizationTests/Formatting/DateFormatStyleTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ private struct DateFormatStyleTests {
100100
}
101101

102102
@Test func codable() throws {
103-
let style = Date.FormatStyle(date: .long, time: .complete, locale: Locale(identifier: "en_US"), calendar: Calendar(identifier: .gregorian), timeZone: .gmt, capitalizationContext: .unknown)
103+
var calendar = Calendar(identifier: .gregorian)
104+
calendar.timeZone = .gmt
105+
let style = Date.FormatStyle(date: .long, time: .complete, locale: Locale(identifier: "en_US"), calendar: calendar, timeZone: .gmt, capitalizationContext: .unknown)
104106
.era()
105107
.year()
106108
.quarter()

0 commit comments

Comments
 (0)