@@ -25,7 +25,7 @@ class PriceFormatterProviderTests: StoreKitConfigTestCase {
2525 override func setUp( ) {
2626 super. setUp ( )
2727
28- self . priceFormatterProvider = PriceFormatterProvider ( priceFormattingRuleSet : nil )
28+ self . priceFormatterProvider = PriceFormatterProvider ( )
2929 }
3030
3131 func testReturnsCachedPriceFormatterForSK1( ) {
@@ -45,6 +45,15 @@ class PriceFormatterProviderTests: StoreKitConfigTestCase {
4545
4646 expect ( firstPriceFormatter) === secondPriceFormatter
4747 }
48+
49+ func testReturnsCachedPriceFormatterForWebProducts( ) throws {
50+ let currencyCode = " USD "
51+ let firstPriceFormatter = self . priceFormatterProvider. priceFormatterForWebProducts ( withCurrencyCode: currencyCode)
52+
53+ let secondPriceFormatter = self . priceFormatterProvider. priceFormatterForWebProducts ( withCurrencyCode: currencyCode)
54+
55+ expect ( firstPriceFormatter) === secondPriceFormatter
56+ }
4857
4958 func testSk1PriceFormatterUsesCurrentStorefront( ) async throws {
5059 self . testSession. locale = Locale ( identifier: " es_ES " )
@@ -71,6 +80,59 @@ class PriceFormatterProviderTests: StoreKitConfigTestCase {
7180 priceFormatter = try XCTUnwrap ( storeProduct. priceFormatter)
7281 expect ( priceFormatter. currencyCode) == " USD "
7382 }
83+
84+ func testSk1PriceFormatterCurrencySymbolOverriding( ) async throws {
85+ priceFormatterProvider = . init(
86+ priceFormattingRuleSet: . init( currencySymbolOverrides: [
87+ " EUR " : . init(
88+ zero: " zero " ,
89+ one: " one " ,
90+ two: " two " ,
91+ few: " few " ,
92+ many: " many " ,
93+ other: " other "
94+ )
95+ ] )
96+ )
97+
98+ let priceFormatter = priceFormatterProvider. priceFormatterForSK1 (
99+ with: . init( identifier: " nl_NL " )
100+ )
101+
102+ expect ( priceFormatter. currencyCode) == " EUR "
103+ expect ( priceFormatter. currencySymbol) == " € "
104+ XCTAssert ( type ( of: priceFormatter) == CurrencySymbolOverridingPriceFormatter . self)
105+
106+ XCTAssertEqual ( priceFormatter. string ( from: NSNumber ( integerLiteral: 0 ) ) , " zero 0,00 " )
107+ XCTAssertEqual ( priceFormatter. string ( from: NSNumber ( integerLiteral: 1 ) ) , " one 1,00 " )
108+ XCTAssertEqual ( priceFormatter. string ( from: NSNumber ( integerLiteral: 2 ) ) , " two 2,00 " )
109+ XCTAssertEqual ( priceFormatter. string ( from: NSNumber ( integerLiteral: 3 ) ) , " other 3,00 " )
110+ }
111+
112+ func testSk1PriceFormatterCurrencySymbolOverridingUsesCachedPriceFormatter( ) async throws {
113+ priceFormatterProvider = . init(
114+ priceFormattingRuleSet: . init( currencySymbolOverrides: [
115+ " EUR " : . init(
116+ zero: " zero " ,
117+ one: " one " ,
118+ two: " two " ,
119+ few: " few " ,
120+ many: " many " ,
121+ other: " other "
122+ )
123+ ] )
124+ )
125+
126+ let firstPriceFormatter = priceFormatterProvider. priceFormatterForSK1 (
127+ with: . init( identifier: " nl_NL " )
128+ )
129+
130+ let secondPriceFormatter = priceFormatterProvider. priceFormatterForSK1 (
131+ with: . init( identifier: " nl_NL " )
132+ )
133+
134+ expect ( firstPriceFormatter) == secondPriceFormatter
135+ }
74136
75137 @available ( iOS 15 . 0 , tvOS 15 . 0 , macOS 12 . 0 , watchOS 8 . 0 , * )
76138 func testSk2PriceFormatterUsesCurrentStorefront( ) async throws {
@@ -97,31 +159,24 @@ class PriceFormatterProviderTests: StoreKitConfigTestCase {
97159
98160 @available ( iOS 15 . 0 , tvOS 15 . 0 , macOS 12 . 0 , watchOS 8 . 0 , * )
99161 func testSk2PriceFormatterCurrencySymbolOverriding( ) async throws {
100- try AvailabilityChecks . iOS16APIAvailableOrSkipTest ( )
101-
102- self . testSession. locale = Locale ( identifier: " nl_NL " )
103- try await self . changeStorefront ( " NLD " )
104-
105- let sk2Fetcher = ProductsFetcherSK2 (
106- priceFormattingRuleSetProvider: . init(
107- priceFormattingRuleSet: {
108- . init( currencySymbolOverrides: [
109- " EUR " : . init(
110- zero: " zero " ,
111- one: " one " ,
112- two: " two " ,
113- few: " few " ,
114- many: " many " ,
115- other: " other "
116- )
117- ] )
118- }
119- )
120- )
121-
122- let storeProduct = try await sk2Fetcher. product ( withIdentifier: Self . productID)
123-
124- let priceFormatter = try XCTUnwrap ( storeProduct. priceFormatter)
162+ priceFormatterProvider = . init(
163+ priceFormattingRuleSet: . init( currencySymbolOverrides: [
164+ " EUR " : . init(
165+ zero: " zero " ,
166+ one: " one " ,
167+ two: " two " ,
168+ few: " few " ,
169+ many: " many " ,
170+ other: " other "
171+ )
172+ ] )
173+ )
174+
175+ let priceFormatter = priceFormatterProvider. priceFormatterForSK2 (
176+ withCurrencyCode: " EUR " ,
177+ locale: . init( identifier: " nl_NL " )
178+ )
179+
125180 expect ( priceFormatter. currencyCode) == " EUR "
126181 expect ( priceFormatter. currencySymbol) == " € "
127182 XCTAssert ( type ( of: priceFormatter) == CurrencySymbolOverridingPriceFormatter . self)
@@ -134,31 +189,24 @@ class PriceFormatterProviderTests: StoreKitConfigTestCase {
134189
135190 @available ( iOS 15 . 0 , tvOS 15 . 0 , macOS 12 . 0 , watchOS 8 . 0 , * )
136191 func testSk2PriceFormatterCurrencySymbolOverridingRomania( ) async throws {
137- try AvailabilityChecks . iOS16APIAvailableOrSkipTest ( )
138-
139- self . testSession. locale = Locale ( identifier: " ro_RO " )
140- try await self . changeStorefront ( " ROU " )
141-
142- let sk2Fetcher = ProductsFetcherSK2 (
143- priceFormattingRuleSetProvider: . init(
144- priceFormattingRuleSet: {
145- . init( currencySymbolOverrides: [
146- " RON " : . init(
147- zero: " lei " ,
148- one: " leu " ,
149- two: " lei " ,
150- few: " lei " ,
151- many: " lei " ,
152- other: " lei "
153- )
154- ] )
155- }
156- )
157- )
158-
159- let storeProduct = try await sk2Fetcher. product ( withIdentifier: Self . productID)
160-
161- let priceFormatter = try XCTUnwrap ( storeProduct. priceFormatter)
192+ priceFormatterProvider = . init(
193+ priceFormattingRuleSet: . init( currencySymbolOverrides: [
194+ " RON " : . init(
195+ zero: " lei " ,
196+ one: " leu " ,
197+ two: " lei " ,
198+ few: " lei " ,
199+ many: " lei " ,
200+ other: " lei "
201+ )
202+ ] )
203+ )
204+
205+ let priceFormatter = priceFormatterProvider. priceFormatterForSK2 (
206+ withCurrencyCode: " RON " ,
207+ locale: . init( identifier: " ro_RO " )
208+ )
209+
162210 expect ( priceFormatter. currencyCode) == " RON "
163211 expect ( priceFormatter. currencySymbol) == " RON "
164212 XCTAssert ( type ( of: priceFormatter) == CurrencySymbolOverridingPriceFormatter . self)
@@ -167,6 +215,90 @@ class PriceFormatterProviderTests: StoreKitConfigTestCase {
167215 XCTAssertEqual ( priceFormatter. string ( from: NSNumber ( integerLiteral: 1 ) ) , " 1,00 leu " )
168216 XCTAssertEqual ( priceFormatter. string ( from: NSNumber ( integerLiteral: 2 ) ) , " 2,00 lei " )
169217 }
218+
219+ @available ( iOS 15 . 0 , tvOS 15 . 0 , macOS 12 . 0 , watchOS 8 . 0 , * )
220+ func testSk2PriceFormatterCurrencySymbolOverridingUsesCachedPriceFormatter( ) async throws {
221+ priceFormatterProvider = . init(
222+ priceFormattingRuleSet: . init( currencySymbolOverrides: [
223+ " EUR " : . init(
224+ zero: " zero " ,
225+ one: " one " ,
226+ two: " two " ,
227+ few: " few " ,
228+ many: " many " ,
229+ other: " other "
230+ )
231+ ] )
232+ )
233+
234+ let firstPriceFormatter = priceFormatterProvider. priceFormatterForSK2 (
235+ withCurrencyCode: " EUR " ,
236+ locale: . init( identifier: " nl_NL " )
237+ )
238+
239+ let secondPriceFormatter = priceFormatterProvider. priceFormatterForSK2 (
240+ withCurrencyCode: " EUR " ,
241+ locale: . init( identifier: " nl_NL " )
242+ )
243+
244+ expect ( firstPriceFormatter) == secondPriceFormatter
245+ }
246+
247+ func testWebProductsPriceFormatterCurrencySymbolOverriding( ) async throws {
248+ priceFormatterProvider = . init(
249+ priceFormattingRuleSet: . init( currencySymbolOverrides: [
250+ " EUR " : . init(
251+ zero: " zero " ,
252+ one: " one " ,
253+ two: " two " ,
254+ few: " few " ,
255+ many: " many " ,
256+ other: " other "
257+ )
258+ ] )
259+ )
260+
261+ let priceFormatter = priceFormatterProvider. priceFormatterForWebProducts (
262+ withCurrencyCode: " EUR " ,
263+ locale: Locale ( identifier: " nl_NL " )
264+ )
265+
266+ expect ( priceFormatter. currencyCode) == " EUR "
267+ expect ( priceFormatter. currencySymbol) == " € "
268+ XCTAssert ( type ( of: priceFormatter) == CurrencySymbolOverridingPriceFormatter . self)
269+
270+ XCTAssertEqual ( priceFormatter. string ( from: NSNumber ( integerLiteral: 0 ) ) , " zero 0,00 " )
271+ XCTAssertEqual ( priceFormatter. string ( from: NSNumber ( integerLiteral: 1 ) ) , " one 1,00 " )
272+ XCTAssertEqual ( priceFormatter. string ( from: NSNumber ( integerLiteral: 2 ) ) , " two 2,00 " )
273+ XCTAssertEqual ( priceFormatter. string ( from: NSNumber ( integerLiteral: 3 ) ) , " other 3,00 " )
274+ }
275+
276+ func testWebProductsFormatterCurrencySymbolOverridingUsesCachedPriceFormatter( ) async throws {
277+ priceFormatterProvider = . init(
278+ priceFormattingRuleSet: . init( currencySymbolOverrides: [
279+ " EUR " : . init(
280+ zero: " zero " ,
281+ one: " one " ,
282+ two: " two " ,
283+ few: " few " ,
284+ many: " many " ,
285+ other: " other "
286+ )
287+ ] )
288+ )
289+
290+ let firstPriceFormatter = priceFormatterProvider. priceFormatterForWebProducts (
291+ withCurrencyCode: " EUR " ,
292+ locale: Locale ( identifier: " nl_NL " )
293+ )
294+
295+ let secondPriceFormatter = priceFormatterProvider. priceFormatterForWebProducts (
296+ withCurrencyCode: " EUR " ,
297+ locale: Locale ( identifier: " nl_NL " )
298+ )
299+
300+ expect ( firstPriceFormatter) == secondPriceFormatter
301+ }
170302}
171303
172304extension PriceFormattingRuleSetProvider {
0 commit comments