Skip to content

Commit fb1c147

Browse files
committed
Limit the use of concurrent scope in unit tests
Many tests for the dot syntax assume that the left hand side of the comparison is using the same locale as the right side, in which case is `Locale.autoupdatingCurrent`. Previously we put them inside `usingCurrentInternationalizationPreferences` to ensure that the current locale isn't mutated concurrently while the tests are running. That works but we can also set the locale of both the left and right side to a specific locale so they don't depend on the current status. Addresses 155437764
1 parent c5f4563 commit fb1c147

File tree

3 files changed

+52
-47
lines changed

3 files changed

+52
-47
lines changed

Tests/FoundationInternationalizationTests/Formatting/DateFormatStyleTests.swift

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -188,24 +188,27 @@ private struct DateFormatStyleTests {
188188
}
189189

190190
@Test func leadingDotSyntax() async {
191+
let date = Date.now
192+
let locale = Locale(identifier: "es_ES")
191193
await usingCurrentInternationalizationPreferences {
192-
let date = Date.now
193194
#expect(date.formatted(date: .long, time: .complete) == date.formatted(Date.FormatStyle(date: .long, time: .complete)))
194-
#expect(
195-
date.formatted(
196-
.dateTime
197-
.day()
198-
.month()
199-
.year()
200-
) ==
201-
date.formatted(
202-
Date.FormatStyle()
203-
.day()
204-
.month()
205-
.year()
206-
)
207-
)
208195
}
196+
#expect(
197+
date.formatted(
198+
.dateTime
199+
.day()
200+
.month()
201+
.year()
202+
.locale(locale)
203+
) ==
204+
date.formatted(
205+
Date.FormatStyle()
206+
.day()
207+
.month()
208+
.year()
209+
.locale(locale)
210+
)
211+
)
209212
}
210213

211214
@Test func dateFormatStyleIndividualFields() {

Tests/FoundationInternationalizationTests/Formatting/DateIntervalFormatStyleTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,15 @@ private struct DateIntervalFormatStyleTests {
100100
}
101101

102102
@Test func leadingDotSyntax() async {
103+
let locale = Locale(identifier: "en_GB")
104+
let range = (Date(timeIntervalSinceReferenceDate: 0) ..< Date(timeIntervalSinceReferenceDate: 0) + (60 * 60))
103105
await usingCurrentInternationalizationPreferences {
104-
let range = (Date(timeIntervalSinceReferenceDate: 0) ..< Date(timeIntervalSinceReferenceDate: 0) + (60 * 60))
106+
// This test assumes both the left side and the right side of the comparison has the same `Locale.autoupdatingCurrent`, which assumes the state of the host machine remains unchanged.
105107
#expect(range.formatted() == Date.IntervalFormatStyle().format(range))
106108
#expect(range.formatted(date: .numeric, time: .shortened) == Date.IntervalFormatStyle(date: .numeric, time: .shortened).format(range))
107-
#expect(range.formatted(.interval.day().month().year()) == Date.IntervalFormatStyle().day().month().year().format(range))
108109
}
110+
111+
#expect(range.formatted(.interval.day().month().year().locale(locale)) == Date.IntervalFormatStyle().day().month().year().locale(locale).format(range))
109112
}
110113

111114
#if FIXED_ICU_74_DAYPERIOD

Tests/FoundationInternationalizationTests/Formatting/NumberFormatStyleTests.swift

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,36 +2163,35 @@ extension FormatStylePatternMatchingTests {
21632163
#if FOUNDATION_FRAMEWORK
21642164
extension NumberFormatStyleTests {
21652165
@Test func formattedLeadingDotSyntax() async {
2166-
await usingCurrentInternationalizationPreferences {
2167-
let integer = 12345
2168-
#expect(integer.formatted(.number) == integer.formatted(IntegerFormatStyle.number))
2169-
#expect(integer.formatted(.percent) == integer.formatted(IntegerFormatStyle.Percent.percent))
2170-
#expect(integer.formatted(.currency(code: "usd")) == integer.formatted(IntegerFormatStyle.Currency.currency(code: "usd")))
2171-
2172-
let double = 1.2345
2173-
#expect(double.formatted(.number) == double.formatted(FloatingPointFormatStyle.number))
2174-
#expect(double.formatted(.percent) == double.formatted(FloatingPointFormatStyle.Percent.percent))
2175-
#expect(double.formatted(.currency(code: "usd")) == double.formatted(FloatingPointFormatStyle.Currency.currency(code: "usd")))
2176-
2177-
2178-
func parseableFunc<Style: ParseableFormatStyle>(_ value: Style.FormatInput, style: Style) -> Style { style }
2179-
2180-
#expect(parseableFunc(UInt8(), style: .number) == parseableFunc(UInt8(), style: IntegerFormatStyle.number))
2181-
#expect(parseableFunc(Int16(), style: .percent) == parseableFunc(Int16(), style: IntegerFormatStyle.Percent.percent))
2182-
#expect(parseableFunc(Int(), style: .currency(code: "usd")) == parseableFunc(Int(), style: IntegerFormatStyle.Currency.currency(code: "usd")))
2183-
2184-
#expect(parseableFunc(Float(), style: .number) == parseableFunc(Float(), style: FloatingPointFormatStyle.number))
2185-
#expect(parseableFunc(Double(), style: .percent) == parseableFunc(Double(), style: FloatingPointFormatStyle.Percent.percent))
2186-
#expect(parseableFunc(CGFloat(), style: .currency(code: "usd")) == parseableFunc(CGFloat(), style: FloatingPointFormatStyle.Currency.currency(code: "usd")))
2187-
2188-
#expect(parseableFunc(Decimal(), style: .number) == parseableFunc(Decimal(), style: Decimal.FormatStyle.number))
2189-
#expect(parseableFunc(Decimal(), style: .percent) == parseableFunc(Decimal(), style: Decimal.FormatStyle.Percent.percent))
2190-
#expect(parseableFunc(Decimal(), style: .currency(code: "usd")) == parseableFunc(Decimal(), style: Decimal.FormatStyle.Currency.currency(code: "usd")))
2191-
2192-
struct GenericWrapper<V> {}
2193-
func parseableWrapperFunc<Style: ParseableFormatStyle>(_ value: GenericWrapper<Style.FormatInput>, style: Style) -> Style { style }
2194-
#expect(parseableWrapperFunc(GenericWrapper<Double>(), style: .number) == parseableWrapperFunc(GenericWrapper<Double>(), style: FloatingPointFormatStyle.number))
2195-
}
2166+
let locale = Locale(identifier: "ja_JP")
2167+
let integer = 12345
2168+
#expect(integer.formatted(.number.locale(locale)) == integer.formatted(IntegerFormatStyle.number.locale(locale)))
2169+
#expect(integer.formatted(.percent.locale(locale)) == integer.formatted(IntegerFormatStyle.Percent.percent.locale(locale)))
2170+
#expect(integer.formatted(.currency(code: "usd").locale(locale)) == integer.formatted(IntegerFormatStyle.Currency.currency(code: "usd").locale(locale)))
2171+
2172+
let double = 1.2345
2173+
#expect(double.formatted(.number.locale(locale)) == double.formatted(FloatingPointFormatStyle.number.locale(locale)))
2174+
#expect(double.formatted(.percent.locale(locale)) == double.formatted(FloatingPointFormatStyle.Percent.percent.locale(locale)))
2175+
#expect(double.formatted(.currency(code: "usd").locale(locale)) == double.formatted(FloatingPointFormatStyle.Currency.currency(code: "usd").locale(locale)))
2176+
2177+
2178+
func parseableFunc<Style: ParseableFormatStyle>(_ value: Style.FormatInput, style: Style) -> Style { style }
2179+
2180+
#expect(parseableFunc(UInt8(), style: .number.locale(locale)) == parseableFunc(UInt8(), style: IntegerFormatStyle.number.locale(locale)))
2181+
#expect(parseableFunc(Int16(), style: .percent.locale(locale)) == parseableFunc(Int16(), style: IntegerFormatStyle.Percent.percent.locale(locale)))
2182+
#expect(parseableFunc(Int(), style: .currency(code: "usd").locale(locale)) == parseableFunc(Int(), style: IntegerFormatStyle.Currency.currency(code: "usd").locale(locale)))
2183+
2184+
#expect(parseableFunc(Float(), style: .number.locale(locale)) == parseableFunc(Float(), style: FloatingPointFormatStyle.number.locale(locale)))
2185+
#expect(parseableFunc(Double(), style: .percent.locale(locale)) == parseableFunc(Double(), style: FloatingPointFormatStyle.Percent.percent.locale(locale)))
2186+
#expect(parseableFunc(CGFloat(), style: .currency(code: "usd").locale(locale)) == parseableFunc(CGFloat(), style: FloatingPointFormatStyle.Currency.currency(code: "usd").locale(locale)))
2187+
2188+
#expect(parseableFunc(Decimal(), style: .number.locale(locale)) == parseableFunc(Decimal(), style: Decimal.FormatStyle.number.locale(locale)))
2189+
#expect(parseableFunc(Decimal(), style: .percent.locale(locale)) == parseableFunc(Decimal(), style: Decimal.FormatStyle.Percent.percent.locale(locale)))
2190+
#expect(parseableFunc(Decimal(), style: .currency(code: "usd").locale(locale)) == parseableFunc(Decimal(), style: Decimal.FormatStyle.Currency.currency(code: "usd").locale(locale)))
2191+
2192+
struct GenericWrapper<V> {}
2193+
func parseableWrapperFunc<Style: ParseableFormatStyle>(_ value: GenericWrapper<Style.FormatInput>, style: Style) -> Style { style }
2194+
#expect(parseableWrapperFunc(GenericWrapper<Double>(), style: .number.locale(locale)) == parseableWrapperFunc(GenericWrapper<Double>(), style: FloatingPointFormatStyle.number.locale(locale)))
21962195
}
21972196
}
21982197
#endif

0 commit comments

Comments
 (0)