2020
2121import Testing
2222
23+ import struct Foundation. Date
24+ import struct Foundation. Decimal
2325import class Foundation. JSONEncoder
2426import class Foundation. NSLocale
2527
@@ -39,4 +41,40 @@ struct StringLocalizationValuePolyfillTests {
3941 data: try encoder. encode ( " \( nsLocale) " as String . LocalizationValuePolyfill ) , encoding: . utf8
4042 ) == expected)
4143 }
44+
45+ @Test func testStringInterpolationWithFormatStyle( ) throws {
46+ let encoder = JSONEncoder ( )
47+ encoder. outputFormatting = . sortedKeys
48+ let nsLocale = NSLocale ( localeIdentifier: " en_US " )
49+ let expected = """
50+ { " arguments " :[{ " formattedDecimal " :{ " _0 " :0.6, " _1 " :{ " format " :{ " percent " :{ " _0 " :{ " locale " :{ " current " :2, " identifier " : " en_JP " }, " collection " :{ " scale " :100}}}}}}},{ " formattedDate " :{ " _0 " :759527795.92532802, " _1 " :{ " format " :{ " iso8601 " :{ " _0 " :{ " _formatFields " :[], " timeZone " :{ " identifier " : " GMT " }, " dateSeparator " : " - " , " includingFractionalSeconds " :false, " timeSeparator " : " : " , " timeZoneSeparator " : " " , " dateTimeSeparator " : " 'T' " }}}}}}], " key " : " decimal: %@, date: %@ " }
51+ """
52+
53+ #expect(
54+ String (
55+ data: try encoder. encode (
56+ " decimal: \( Decimal ( 0.6 ) , format: . percent) , date: \( Date ( timeIntervalSince1970: 0 ) , format: . iso8601) ) "
57+ as String . LocalizationValuePolyfill ) , encoding: . utf8
58+ ) == expected)
59+ }
60+
61+ @Test func testStringInterpolationWithFormatSpecifiable( ) throws {
62+ let encoder = JSONEncoder ( )
63+ encoder. outputFormatting = . sortedKeys
64+ #if canImport(Darwin)
65+ let expected = """
66+ { " arguments " :[{ " string " :{ " _0 " : " text " }},{ " uint64 " :{ " _0 " :18446744073709551615}},{ " int64 " :{ " _0 " :-9223372036854775808}},{ " double " :{ " _0 " :42.195}},{ " uint32 " :{ " _0 " :4294967295}},{ " int32 " :{ " _0 " :-2147483648}},{ " float " :{ " _0 " :3.1400001049041748}}], " key " : " %%@, string: %@, 64bit unsigned integer: %llu, 64bit integer: %lld, double: %lf, 32bit unsigned integer: %u, 32bit integer: %d, float: %f " }
67+ """
68+ #else
69+ let expected = """
70+ { " arguments " :[{ " string " :{ " _0 " : " text " }},{ " uint64 " :{ " _0 " :18446744073709551615}},{ " int64 " :{ " _0 " :-9223372036854775808}},{ " double " :{ " _0 " :42.195}},{ " uint32 " :{ " _0 " :4294967295}},{ " int32 " :{ " _0 " :-2147483648}},{ " float " :{ " _0 " :3.14}}], " key " : " %%@, string: %@, 64bit unsigned integer: %llu, 64bit integer: %lld, double: %lf, 32bit unsigned integer: %u, 32bit integer: %d, float: %f " }
71+ """
72+ #endif
73+
74+ #expect(
75+ String (
76+ data: try encoder. encode (
77+ " %@, string: \( " text " ) , 64bit unsigned integer: \( UInt64 . max) , 64bit integer: \( Int64 . min) , double: \( Double ( 42.195 ) ) , 32bit unsigned integer: \( UInt32 . max) , 32bit integer: \( Int32 . min) , float: \( Float ( 3.14 ) ) "
78+ as String . LocalizationValuePolyfill ) , encoding: . utf8) == expected)
79+ }
4280}
0 commit comments