Skip to content

Commit

Permalink
Round price per period calculations to nearest 2-decimal (RevenueCat#…
Browse files Browse the repository at this point in the history
…4275)

* Round price per period calculations to nearest 2-decimal

* Fix tests

* Update snapshots commit
  • Loading branch information
tonidero authored and MojtabaHs committed Dec 12, 2024
1 parent 07b0828 commit 063df9c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ extension SubscriptionPeriod {
}

private static let roundingBehavior = NSDecimalNumberHandler(
roundingMode: .down,
roundingMode: .plain,
scale: 2,
raiseOnExactness: false,
raiseOnOverflow: false,
Expand Down
40 changes: 20 additions & 20 deletions Tests/RevenueCatUITests/Data/PackageVariablesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ class PackageVariablesTests: TestCase {

func testLocalizedPricePerWeek() {
expect(TestData.weeklyPackage.localizedPricePerWeek()) == "$1.99"
expect(TestData.monthlyPackage.localizedPricePerWeek()) == "$1.60"
expect(TestData.monthlyPackage.localizedPricePerWeek()) == "$1.61"
expect(TestData.threeMonthPackage.localizedPricePerWeek()) == "$0.38"
expect(TestData.sixMonthPackage.localizedPricePerWeek()) == "$0.30"
expect(TestData.annualPackage.localizedPricePerWeek()) == "$1.03"
expect(TestData.sixMonthPackage.localizedPricePerWeek()) == "$0.31"
expect(TestData.annualPackage.localizedPricePerWeek()) == "$1.04"
expect(TestData.lifetimePackage.localizedPricePerWeek()) == "$119.49"
}

func testLocalizedPricePerMonth() {
expect(TestData.weeklyPackage.localizedPricePerMonth()) == "$8.64"
expect(TestData.weeklyPackage.localizedPricePerMonth()) == "$8.65"
expect(TestData.monthlyPackage.localizedPricePerMonth()) == "$6.99"
expect(TestData.threeMonthPackage.localizedPricePerMonth()) == "$1.66"
expect(TestData.sixMonthPackage.localizedPricePerMonth()) == "$1.33"
expect(TestData.annualPackage.localizedPricePerMonth()) == "$4.49"
expect(TestData.annualPackage.localizedPricePerMonth()) == "$4.50"
expect(TestData.lifetimePackage.localizedPricePerMonth()) == "$119.49"
}

Expand Down Expand Up @@ -104,55 +104,55 @@ class PackageVariablesTests: TestCase {
}

func testEnglishLocalizedPriceAndPerMonth() {
expect(TestData.weeklyPackage.localizedPriceAndPerMonth(Self.english)) == "$1.99/wk ($8.64/mo)"
expect(TestData.weeklyPackage.localizedPriceAndPerMonth(Self.english)) == "$1.99/wk ($8.65/mo)"
expect(TestData.monthlyPackage.localizedPriceAndPerMonth(Self.english)) == "$6.99/mo"
expect(TestData.threeMonthPackage.localizedPriceAndPerMonth(Self.english)) == "$4.99/3mo ($1.66/mo)"
expect(TestData.sixMonthPackage.localizedPriceAndPerMonth(Self.english)) == "$7.99/6mo ($1.33/mo)"
expect(TestData.annualPackage.localizedPriceAndPerMonth(Self.english)) == "$53.99/yr ($4.49/mo)"
expect(TestData.annualPackage.localizedPriceAndPerMonth(Self.english)) == "$53.99/yr ($4.50/mo)"
expect(TestData.lifetimePackage.localizedPriceAndPerMonth(Self.english)) == "$119.49"
}

func testSpanishLocalizedPriceAndPerMonth() {
expect(TestData.weeklyPackage.localizedPriceAndPerMonth(Self.spanish)) == "$1.99/sem ($8.64/m.)"
expect(TestData.weeklyPackage.localizedPriceAndPerMonth(Self.spanish)) == "$1.99/sem ($8.65/m.)"
expect(TestData.monthlyPackage.localizedPriceAndPerMonth(Self.spanish)) == "$6.99/m."
expect(TestData.threeMonthPackage.localizedPriceAndPerMonth(Self.spanish)) == "$4.99/3m ($1.66/m.)"
expect(TestData.sixMonthPackage.localizedPriceAndPerMonth(Self.spanish)) == "$7.99/6m ($1.33/m.)"
expect(TestData.annualPackage.localizedPriceAndPerMonth(Self.spanish)) == "$53.99/año ($4.49/m.)"
expect(TestData.annualPackage.localizedPriceAndPerMonth(Self.spanish)) == "$53.99/año ($4.50/m.)"
expect(TestData.lifetimePackage.localizedPriceAndPerMonth(Self.spanish)) == "$119.49"
}

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

expect(TestData.weeklyPackage.with(arabicPrice, Self.arabic).localizedPriceAndPerMonth(Self.arabic))
.to(equalIgnoringRTL("٣.٩٩ درهم/أسبوع (‏8.64 ‏د.إ.‏/شهر)"))
.to(equalIgnoringRTL("٣.٩٩ درهم/أسبوع (‏8.65 ‏د.إ.‏/شهر)"))
expect(TestData.monthlyPackage.with(arabicPrice, Self.arabic).localizedPriceAndPerMonth(Self.arabic))
.to(equalIgnoringRTL("٣.٩٩ درهم/شهر"))
expect(TestData.threeMonthPackage.with(arabicPrice, Self.arabic).localizedPriceAndPerMonth(Self.arabic))
.to(equalIgnoringRTL("٣.٩٩ درهم/3شهر (‏1.66 ‏د.إ.‏/شهر)"))
expect(TestData.sixMonthPackage.with(arabicPrice, Self.arabic).localizedPriceAndPerMonth(Self.arabic))
.to(equalIgnoringRTL("٣.٩٩ درهم/6شهر (‏1.33 ‏د.إ.‏/شهر)"))
expect(TestData.annualPackage.with(arabicPrice, Self.arabic).localizedPriceAndPerMonth(Self.arabic))
.to(equalIgnoringRTL("٣.٩٩ درهم/سنة (‏4.49 ‏د.إ.‏/شهر)"))
.to(equalIgnoringRTL("٣.٩٩ درهم/سنة (‏4.50 ‏د.إ.‏/شهر)"))
expect(TestData.lifetimePackage.with(arabicPrice, Self.arabic).localizedPriceAndPerMonth(Self.arabic))
== arabicPrice
}

func testEnglishLocalizedPriceAndPerMonthFull() {
expect(TestData.weeklyPackage.localizedPriceAndPerMonthFull(Self.english)) == "$1.99/week ($8.64/month)"
expect(TestData.weeklyPackage.localizedPriceAndPerMonthFull(Self.english)) == "$1.99/week ($8.65/month)"
expect(TestData.monthlyPackage.localizedPriceAndPerMonthFull(Self.english)) == "$6.99/month"
expect(TestData.threeMonthPackage.localizedPriceAndPerMonthFull(Self.english)) == "$4.99/3months ($1.66/month)"
expect(TestData.sixMonthPackage.localizedPriceAndPerMonthFull(Self.english)) == "$7.99/6months ($1.33/month)"
expect(TestData.annualPackage.localizedPriceAndPerMonthFull(Self.english)) == "$53.99/year ($4.49/month)"
expect(TestData.annualPackage.localizedPriceAndPerMonthFull(Self.english)) == "$53.99/year ($4.50/month)"
expect(TestData.lifetimePackage.localizedPriceAndPerMonthFull(Self.english)) == "$119.49"
}

func testSpanishLocalizedPriceAndPerMonthFull() {
expect(TestData.weeklyPackage.localizedPriceAndPerMonthFull(Self.spanish)) == "$1.99/semana ($8.64/mes)"
expect(TestData.weeklyPackage.localizedPriceAndPerMonthFull(Self.spanish)) == "$1.99/semana ($8.65/mes)"
expect(TestData.monthlyPackage.localizedPriceAndPerMonthFull(Self.spanish)) == "$6.99/mes"
expect(TestData.threeMonthPackage.localizedPriceAndPerMonthFull(Self.spanish)) == "$4.99/3meses ($1.66/mes)"
expect(TestData.sixMonthPackage.localizedPriceAndPerMonthFull(Self.spanish)) == "$7.99/6meses ($1.33/mes)"
expect(TestData.annualPackage.localizedPriceAndPerMonthFull(Self.spanish)) == "$53.99/año ($4.49/mes)"
expect(TestData.annualPackage.localizedPriceAndPerMonthFull(Self.spanish)) == "$53.99/año ($4.50/mes)"
expect(TestData.lifetimePackage.localizedPriceAndPerMonthFull(Self.spanish)) == "$119.49"
}

Expand Down Expand Up @@ -312,7 +312,7 @@ class PackageVariablesTests: TestCase {
expect(TestData
.annualPackage
.localizedPriceAndPerMonthFull(Self.english,
showZeroDecimalPlacePrices: true)) == "$53.99/year ($4.49/month)"
showZeroDecimalPlacePrices: true)) == "$53.99/year ($4.50/month)"

expect(TestData
.annualPackage60Taiwan
Expand All @@ -322,12 +322,12 @@ class PackageVariablesTests: TestCase {
expect(TestData
.threeMonthPackageThailand
.localizedPriceAndPerMonthFull(Locale.taiwan,
showZeroDecimalPlacePrices: true)) == "฿5/3個月 (฿1.66/個月)"
showZeroDecimalPlacePrices: true)) == "฿5/3個月 (฿1.67/個月)"

expect(TestData
.threeMonthPackageThailand
.localizedPriceAndPerMonthFull(Locale.thailand,
showZeroDecimalPlacePrices: true)) == "฿5/3เดือน (฿1.66/เดือน)"
showZeroDecimalPlacePrices: true)) == "฿5/3เดือน (฿1.67/เดือน)"

// test rounding off
expect(TestData
Expand All @@ -342,7 +342,7 @@ class PackageVariablesTests: TestCase {
expect(TestData
.annualPackage
.localizedPriceAndPerMonthFull(Self.english,
showZeroDecimalPlacePrices: false)) == "$53.99/year ($4.49/month)"
showZeroDecimalPlacePrices: false)) == "$53.99/year ($4.50/month)"

expect(TestData
.annualPackage60Taiwan
Expand All @@ -352,7 +352,7 @@ class PackageVariablesTests: TestCase {
expect(TestData
.threeMonthPackageThailand
.localizedPriceAndPerMonthFull(Locale.thailand,
showZeroDecimalPlacePrices: false)) == "฿5.00/3เดือน (฿1.66/เดือน)"
showZeroDecimalPlacePrices: false)) == "฿5.00/3เดือน (฿1.67/เดือน)"

}

Expand Down
2 changes: 1 addition & 1 deletion Tests/RevenueCatUITests/Data/VariablesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class VariablesTests: TestCase {
offeringIdentifier: ""
)
)
expect(result) == "$53.99/yr ($4.49/mo)"
expect(result) == "$53.99/yr ($4.50/mo)"
}

func testRelativeDiscountWithNoDiscount() {
Expand Down
21 changes: 20 additions & 1 deletion Tests/StoreKitUnitTests/StoreProductTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,30 @@ class StoreProductTests: StoreKitConfigTestCase {
let sk1Fetcher = ProductsFetcherSK1(requestTimeout: Configuration.storeKitRequestTimeoutDefault)

let storeProduct = try await sk1Fetcher.product(withIdentifier: Self.productID)
expect(storeProduct.localizedPricePerWeek) == "$1.14"
expect(storeProduct.localizedPricePerWeek) == "$1.15"
expect(storeProduct.localizedPricePerMonth) == "$4.99"
expect(storeProduct.localizedPricePerYear) == "$59.88"
}

func testPricePerPeriodRounding() async throws {
let locale: Locale = .init(identifier: "en_US")
let product = TestStoreProduct(
localizedTitle: "product",
price: 3.98999999999,
localizedPriceString: "$3.99",
productIdentifier: "identifier",
productType: .autoRenewableSubscription,
localizedDescription: "",
subscriptionPeriod: SubscriptionPeriod(value: 1, unit: .week),
locale: locale
)
let storeProduct = product.toStoreProduct()

expect(storeProduct.localizedPricePerWeek) == "$3.99"
expect(storeProduct.localizedPricePerMonth) == "$17.34"
expect(storeProduct.localizedPricePerYear) == "$208.05"
}

}

@available(iOS 14.0, tvOS 14.0, macOS 11.0, watchOS 7.0, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ class SubscriptionPeriodTests: TestCase {
.init(p(14, .day), 2, 1),
.init(p(1, .week), 10, 10),
.init(p(2, .week), 10, 5),
.init(p(1, .month), 14.99, 3.44),
.init(p(1, .month), 14.99, 3.45),
.init(p(1, .month), 14.9899999999, 3.45),
.init(p(2, .month), 30, 3.45),
.init(p(3, .month), 40, 3.06),
.init(p(3, .month), 40, 3.07),
.init(p(1, .year), 120, 2.3),
.init(p(1, .year), 50, 0.95),
.init(p(1, .year), 29.99, 0.57),
.init(p(1, .year), 50, 0.96),
.init(p(1, .year), 29.99, 0.58),
.init(p(3, .year), 720, 4.6)
]

Expand All @@ -78,13 +79,14 @@ class SubscriptionPeriodTests: TestCase {
.init(p(1, .day), 2, 60),
.init(p(15, .day), 5, 10),
.init(p(1, .week), 10, 43.45),
.init(p(2, .week), 10, 21.72),
.init(p(2, .week), 10, 21.73),
.init(p(1, .month), 14.99, 14.99),
.init(p(1, .month), 14.9899999999, 14.99),
.init(p(2, .month), 30, 15),
.init(p(3, .month), 40, 13.33),
.init(p(1, .year), 120, 10),
.init(p(1, .year), 50, 4.16),
.init(p(1, .year), 29.99, 2.49),
.init(p(1, .year), 50, 4.17),
.init(p(1, .year), 29.99, 2.5),
.init(p(3, .year), 720, 20)
]

Expand All @@ -105,10 +107,11 @@ class SubscriptionPeriodTests: TestCase {
let tests: [Test] = [
.init(p(1, .day), 1, 365),
.init(p(1, .day), 2, 730),
.init(p(15, .day), 5, 121.66),
.init(p(1, .week), 10, 521.42),
.init(p(15, .day), 5, 121.67),
.init(p(1, .week), 10, 521.43),
.init(p(2, .week), 10, 260.71),
.init(p(1, .month), 14.99, 179.88),
.init(p(1, .month), 14.9899999999, 179.88),
.init(p(1, .month), 5, 60),
.init(p(2, .month), 30, 180),
.init(p(3, .month), 40, 160),
Expand Down
2 changes: 1 addition & 1 deletion Tests/purchases-ios-snapshots-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
384fb9b4e8823ad3b8aeea0f1e61a57dc937eeb0
d1dbea99ed0b5b53b1352d4ffce424ae157ea991

0 comments on commit 063df9c

Please sign in to comment.