Skip to content

Commit

Permalink
Added Arabic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto committed Sep 1, 2023
1 parent 4c7f26e commit 0ed2ff7
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 9 deletions.
9 changes: 6 additions & 3 deletions Sources/Misc/PriceFormatterProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,20 @@ final class PriceFormatterProvider: Sendable {

private let cachedPriceFormatterForSK2: Atomic<NumberFormatter?> = nil

func priceFormatterForSK2(withCurrencyCode currencyCode: String) -> NumberFormatter {
func priceFormatterForSK2(
withCurrencyCode currencyCode: String,
locale: Locale = .autoupdatingCurrent
) -> NumberFormatter {
func makePriceFormatterForSK2(with currencyCode: String) -> NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = .autoupdatingCurrent
formatter.locale = locale
formatter.currencyCode = currencyCode
return formatter
}

return self.cachedPriceFormatterForSK2.modify { formatter in
guard let formatter = formatter, formatter.currencyCode == currencyCode else {
guard let formatter = formatter, formatter.currencyCode == currencyCode, formatter.locale == locale else {
let newFormatter = makePriceFormatterForSK2(with: currencyCode)
formatter = newFormatter

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public struct TestStoreProduct {
public var isFamilyShareable: Bool
public var introductoryDiscount: StoreProductDiscount?
public var discounts: [StoreProductDiscount]
public var locale: Locale

public init(
localizedTitle: String,
Expand All @@ -71,7 +72,8 @@ public struct TestStoreProduct {
subscriptionPeriod: SubscriptionPeriod? = nil,
isFamilyShareable: Bool = false,
introductoryDiscount: TestStoreProductDiscount? = nil,
discounts: [TestStoreProductDiscount] = []
discounts: [TestStoreProductDiscount] = [],
locale: Locale = .current
) {
self.localizedTitle = localizedTitle
self.price = price
Expand All @@ -84,6 +86,7 @@ public struct TestStoreProduct {
self.isFamilyShareable = isFamilyShareable
self.introductoryDiscount = introductoryDiscount?.toStoreProductDiscount()
self.discounts = discounts.map { $0.toStoreProductDiscount() }
self.locale = locale
}

// swiftlint:enable missing_docs
Expand All @@ -98,13 +101,14 @@ extension TestStoreProduct: StoreProductType {
internal var productCategory: StoreProduct.ProductCategory { return self.productType.productCategory }

internal var currencyCode: String? {
// Test currency defaults to current locale
return Locale.current.rc_currencyCode
return self.locale.rc_currencyCode
}

internal var priceFormatter: NumberFormatter? {
return self.currencyCode.map {
self.priceFormatterProvider.priceFormatterForSK2(withCurrencyCode: $0)
let code = self.currencyCode

return code.map {
self.priceFormatterProvider.priceFormatterForSK2(withCurrencyCode: $0, locale: self.locale)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func checkTestStoreProductAPI() {
let isFamilyShareable: Bool = testProduct.isFamilyShareable
let introductoryDiscount: StoreProductDiscount? = testProduct.introductoryDiscount
let discounts: [StoreProductDiscount] = testProduct.discounts
let locale: Locale = testProduct.locale

// Setters
testProduct.localizedTitle = localizedTitle
Expand All @@ -37,6 +38,7 @@ func checkTestStoreProductAPI() {
testProduct.isFamilyShareable = isFamilyShareable
testProduct.introductoryDiscount = introductoryDiscount
testProduct.discounts = discounts
testProduct.locale = locale

let _: StoreProduct = testProduct.toStoreProduct()
}
Expand All @@ -62,6 +64,7 @@ private func checkStoreProductCreation(discount: TestStoreProductDiscount) {
subscriptionPeriod: Optional<SubscriptionPeriod>.some(.init(value: 1, unit: .day)),
isFamilyShareable: true,
introductoryDiscount: Optional<TestStoreProductDiscount>.some(discount),
discounts: [discount]
discounts: [discount],
locale: Locale.current
)
}
86 changes: 86 additions & 0 deletions Tests/RevenueCatUITests/Data/PackageVariablesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ class PackageVariablesTests: TestCase {
expect(TestData.lifetimePackage.localizedPricePerPeriod(Self.spanish)) == "$119.49"
}

func testArabicLocalizedPricePerPeriod() {
let arabicPrice = "٣.٩٩ درهم"

expect(TestData.weeklyPackage.with(arabicPrice, Self.arabic).localizedPricePerPeriod(Self.arabic))
== "٣.٩٩ درهم/أسبوع"
expect(TestData.monthlyPackage.with(arabicPrice, Self.arabic).localizedPricePerPeriod(Self.arabic))
== "٣.٩٩ درهم/شهر"
expect(TestData.threeMonthPackage.with(arabicPrice, Self.arabic).localizedPricePerPeriod(Self.arabic))
== "٣.٩٩ درهم/3شهر"
expect(TestData.sixMonthPackage.with(arabicPrice, Self.arabic).localizedPricePerPeriod(Self.arabic))
== "٣.٩٩ درهم/6شهر"
expect(TestData.annualPackage.with(arabicPrice, Self.arabic).localizedPricePerPeriod(Self.arabic))
== "٣.٩٩ درهم/سنة"
expect(TestData.lifetimePackage.with(arabicPrice, Self.arabic).localizedPricePerPeriod(Self.arabic))
== "٣.٩٩ درهم"
}

func testEnglishLocalizedPriceAndPerMonth() {
expect(TestData.weeklyPackage.localizedPriceAndPerMonth(Self.english)) == "$1.99 ($7.96/mo)"
expect(TestData.monthlyPackage.localizedPriceAndPerMonth(Self.english)) == "$6.99/mo"
Expand All @@ -75,6 +92,23 @@ class PackageVariablesTests: TestCase {
expect(TestData.lifetimePackage.localizedPriceAndPerMonth(Self.spanish)) == "$119.49"
}

func testArabicLocalizedPriceAndPerMonth() {
let arabicPrice = "٣.٩٩ درهم"

expect(TestData.weeklyPackage.with(arabicPrice, Self.arabic).localizedPriceAndPerMonth(Self.arabic))
== "٣.٩٩ درهم (‏7.96 ‏د.إ.‏/شهر)"
expect(TestData.monthlyPackage.with(arabicPrice, Self.arabic).localizedPriceAndPerMonth(Self.arabic))
== "٣.٩٩ درهم/شهر"
expect(TestData.threeMonthPackage.with(arabicPrice, Self.arabic).localizedPriceAndPerMonth(Self.arabic))
== "٣.٩٩ درهم (‏1.66 ‏د.إ.‏/شهر)"
expect(TestData.sixMonthPackage.with(arabicPrice, Self.arabic).localizedPriceAndPerMonth(Self.arabic))
== "٣.٩٩ درهم (‏1.33 ‏د.إ.‏/شهر)"
expect(TestData.annualPackage.with(arabicPrice, Self.arabic).localizedPriceAndPerMonth(Self.arabic))
== "٣.٩٩ درهم (‏4.49 ‏د.إ.‏/شهر)"
expect(TestData.lifetimePackage.with(arabicPrice, Self.arabic).localizedPriceAndPerMonth(Self.arabic))
== arabicPrice
}

func testProductName() {
expect(TestData.weeklyPackage.productName) == "Weekly"
expect(TestData.monthlyPackage.productName) == "Monthly"
Expand Down Expand Up @@ -130,5 +164,57 @@ private extension PackageVariablesTests {

static let english: Locale = .init(identifier: "en_US")
static let spanish: Locale = .init(identifier: "es_ES")
static let arabic: Locale = .init(identifier: "ar_AE")

}

// MARK: -

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
private extension Package {

func with(_ newLocalizedPrice: String, _ locale: Locale) -> Package {
return .init(
identifier: self.identifier,
packageType: self.packageType,
storeProduct: self.storeProduct
.toTestProduct()
.with(newLocalizedPrice, locale)
.toStoreProduct(),
offeringIdentifier: self.offeringIdentifier
)
}

}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
private extension TestStoreProduct {

func with(_ newLocalizedPrice: String, _ locale: Locale) -> Self {
var copy = self
copy.localizedPriceString = newLocalizedPrice
copy.locale = locale

return copy
}

}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
private extension StoreProduct {

func toTestProduct() -> TestStoreProduct {
return .init(
localizedTitle: self.localizedTitle,
price: self.price,
localizedPriceString: self.localizedPriceString,
productIdentifier: self.productIdentifier,
productType: self.productType,
localizedDescription: self.localizedDescription,
subscriptionGroupIdentifier: self.subscriptionGroupIdentifier,
subscriptionPeriod: self.subscriptionPeriod,
isFamilyShareable: self.isFamilyShareable
)
}

}
18 changes: 18 additions & 0 deletions Tests/StoreKitUnitTests/StoreProductTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,24 @@ class StoreProductTests: StoreKitConfigTestCase {
expect(storeProduct.isFamilyShareable) == isFamilyShareable
}

func testTestProductWithCustomLocale() {
let locale: Locale = .init(identifier: "es_ES")
let product = TestStoreProduct(
localizedTitle: "product",
price: 3.99,
localizedPriceString: "$3.99",
productIdentifier: "identifier",
productType: .autoRenewableSubscription,
localizedDescription: "",
locale: locale
)
let storeProduct = product.toStoreProduct()

expect(storeProduct.currencyCode) == "EUR"
expect(storeProduct.priceFormatter?.locale) == locale
expect(storeProduct.priceFormatter?.currencyCode) == "EUR"
}

func testWarningLogWhenGettingSK1ProductType() {
let product = StoreProduct(sk1Product: .init())
self.logger.verifyMessageWasNotLogged(Strings.storeKit.sk1_no_known_product_type)
Expand Down

0 comments on commit 0ed2ff7

Please sign in to comment.