Skip to content

Commit 4b0ee0a

Browse files
authored
Limit the use of concurrent scope in unit tests (#1423)
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 9fce2a5 commit 4b0ee0a

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
@@ -2165,36 +2165,35 @@ extension FormatStylePatternMatchingTests {
21652165
#if FOUNDATION_FRAMEWORK
21662166
extension NumberFormatStyleTests {
21672167
@Test func formattedLeadingDotSyntax() async {
2168-
await usingCurrentInternationalizationPreferences {
2169-
let integer = 12345
2170-
#expect(integer.formatted(.number) == integer.formatted(IntegerFormatStyle.number))
2171-
#expect(integer.formatted(.percent) == integer.formatted(IntegerFormatStyle.Percent.percent))
2172-
#expect(integer.formatted(.currency(code: "usd")) == integer.formatted(IntegerFormatStyle.Currency.currency(code: "usd")))
2173-
2174-
let double = 1.2345
2175-
#expect(double.formatted(.number) == double.formatted(FloatingPointFormatStyle.number))
2176-
#expect(double.formatted(.percent) == double.formatted(FloatingPointFormatStyle.Percent.percent))
2177-
#expect(double.formatted(.currency(code: "usd")) == double.formatted(FloatingPointFormatStyle.Currency.currency(code: "usd")))
2178-
2179-
2180-
func parseableFunc<Style: ParseableFormatStyle>(_ value: Style.FormatInput, style: Style) -> Style { style }
2181-
2182-
#expect(parseableFunc(UInt8(), style: .number) == parseableFunc(UInt8(), style: IntegerFormatStyle.number))
2183-
#expect(parseableFunc(Int16(), style: .percent) == parseableFunc(Int16(), style: IntegerFormatStyle.Percent.percent))
2184-
#expect(parseableFunc(Int(), style: .currency(code: "usd")) == parseableFunc(Int(), style: IntegerFormatStyle.Currency.currency(code: "usd")))
2185-
2186-
#expect(parseableFunc(Float(), style: .number) == parseableFunc(Float(), style: FloatingPointFormatStyle.number))
2187-
#expect(parseableFunc(Double(), style: .percent) == parseableFunc(Double(), style: FloatingPointFormatStyle.Percent.percent))
2188-
#expect(parseableFunc(CGFloat(), style: .currency(code: "usd")) == parseableFunc(CGFloat(), style: FloatingPointFormatStyle.Currency.currency(code: "usd")))
2189-
2190-
#expect(parseableFunc(Decimal(), style: .number) == parseableFunc(Decimal(), style: Decimal.FormatStyle.number))
2191-
#expect(parseableFunc(Decimal(), style: .percent) == parseableFunc(Decimal(), style: Decimal.FormatStyle.Percent.percent))
2192-
#expect(parseableFunc(Decimal(), style: .currency(code: "usd")) == parseableFunc(Decimal(), style: Decimal.FormatStyle.Currency.currency(code: "usd")))
2193-
2194-
struct GenericWrapper<V> {}
2195-
func parseableWrapperFunc<Style: ParseableFormatStyle>(_ value: GenericWrapper<Style.FormatInput>, style: Style) -> Style { style }
2196-
#expect(parseableWrapperFunc(GenericWrapper<Double>(), style: .number) == parseableWrapperFunc(GenericWrapper<Double>(), style: FloatingPointFormatStyle.number))
2197-
}
2168+
let locale = Locale(identifier: "ja_JP")
2169+
let integer = 12345
2170+
#expect(integer.formatted(.number.locale(locale)) == integer.formatted(IntegerFormatStyle.number.locale(locale)))
2171+
#expect(integer.formatted(.percent.locale(locale)) == integer.formatted(IntegerFormatStyle.Percent.percent.locale(locale)))
2172+
#expect(integer.formatted(.currency(code: "usd").locale(locale)) == integer.formatted(IntegerFormatStyle.Currency.currency(code: "usd").locale(locale)))
2173+
2174+
let double = 1.2345
2175+
#expect(double.formatted(.number.locale(locale)) == double.formatted(FloatingPointFormatStyle.number.locale(locale)))
2176+
#expect(double.formatted(.percent.locale(locale)) == double.formatted(FloatingPointFormatStyle.Percent.percent.locale(locale)))
2177+
#expect(double.formatted(.currency(code: "usd").locale(locale)) == double.formatted(FloatingPointFormatStyle.Currency.currency(code: "usd").locale(locale)))
2178+
2179+
2180+
func parseableFunc<Style: ParseableFormatStyle>(_ value: Style.FormatInput, style: Style) -> Style { style }
2181+
2182+
#expect(parseableFunc(UInt8(), style: .number.locale(locale)) == parseableFunc(UInt8(), style: IntegerFormatStyle.number.locale(locale)))
2183+
#expect(parseableFunc(Int16(), style: .percent.locale(locale)) == parseableFunc(Int16(), style: IntegerFormatStyle.Percent.percent.locale(locale)))
2184+
#expect(parseableFunc(Int(), style: .currency(code: "usd").locale(locale)) == parseableFunc(Int(), style: IntegerFormatStyle.Currency.currency(code: "usd").locale(locale)))
2185+
2186+
#expect(parseableFunc(Float(), style: .number.locale(locale)) == parseableFunc(Float(), style: FloatingPointFormatStyle.number.locale(locale)))
2187+
#expect(parseableFunc(Double(), style: .percent.locale(locale)) == parseableFunc(Double(), style: FloatingPointFormatStyle.Percent.percent.locale(locale)))
2188+
#expect(parseableFunc(CGFloat(), style: .currency(code: "usd").locale(locale)) == parseableFunc(CGFloat(), style: FloatingPointFormatStyle.Currency.currency(code: "usd").locale(locale)))
2189+
2190+
#expect(parseableFunc(Decimal(), style: .number.locale(locale)) == parseableFunc(Decimal(), style: Decimal.FormatStyle.number.locale(locale)))
2191+
#expect(parseableFunc(Decimal(), style: .percent.locale(locale)) == parseableFunc(Decimal(), style: Decimal.FormatStyle.Percent.percent.locale(locale)))
2192+
#expect(parseableFunc(Decimal(), style: .currency(code: "usd").locale(locale)) == parseableFunc(Decimal(), style: Decimal.FormatStyle.Currency.currency(code: "usd").locale(locale)))
2193+
2194+
struct GenericWrapper<V> {}
2195+
func parseableWrapperFunc<Style: ParseableFormatStyle>(_ value: GenericWrapper<Style.FormatInput>, style: Style) -> Style { style }
2196+
#expect(parseableWrapperFunc(GenericWrapper<Double>(), style: .number.locale(locale)) == parseableWrapperFunc(GenericWrapper<Double>(), style: FloatingPointFormatStyle.number.locale(locale)))
21982197
}
21992198
}
22002199
#endif

0 commit comments

Comments
 (0)