This change should have no user impact.
We commonly pair a currency, like USD, and an amount, like 1.23. This
adds the `FiatMoney` struct. It's a simple struct with two fields.
After adding it, I tried to use it everywhere. (It's possible I missed a
spot.)
I think this is a useful change on its own, but it'll be nice for an
upcoming change, too.
See also: [Android's equivalent class][0].
[0]: cb65347bb3/core-util/src/main/java/org/signal/core/util/money/FiatMoney.java (L1)
62 lines
1.9 KiB
Swift
62 lines
1.9 KiB
Swift
//
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
//
|
|
|
|
import XCTest
|
|
@testable import Signal
|
|
|
|
class BadgeGiftingChooseBadgeViewControllerStateTest: XCTestCase {
|
|
typealias State = BadgeGiftingChooseBadgeViewController.State
|
|
|
|
private func getGiftBadge() -> ProfileBadge {
|
|
try! ProfileBadge(jsonDictionary: [
|
|
"id": "GIFT",
|
|
"category": "donor",
|
|
"name": "A Gift",
|
|
"description": "A gift badge!",
|
|
"sprites6": ["ldpi.png", "mdpi.png", "hdpi.png", "xhdpi.png", "xxhdpi.png", "xxxhdpi.png"]
|
|
])
|
|
}
|
|
|
|
func testCanContinue() throws {
|
|
let stuck: [State] = [.initializing, .loading, .loadFailed]
|
|
stuck.forEach { XCTAssertFalse($0.canContinue) }
|
|
|
|
let notStuck: State = .loaded(
|
|
selectedCurrencyCode: "EUR",
|
|
badge: getGiftBadge(),
|
|
pricesByCurrencyCode: [
|
|
"EUR": FiatMoney(currencyCode: "EUR", value: 123),
|
|
"USD": FiatMoney(currencyCode: "USD", value: 456)
|
|
]
|
|
)
|
|
XCTAssertTrue(notStuck.canContinue)
|
|
}
|
|
|
|
func testSelectCurrencyCode() throws {
|
|
let badge = getGiftBadge()
|
|
|
|
let before = State.loaded(
|
|
selectedCurrencyCode: "USD",
|
|
badge: badge,
|
|
pricesByCurrencyCode: [
|
|
"EUR": FiatMoney(currencyCode: "EUR", value: 123),
|
|
"USD": FiatMoney(currencyCode: "USD", value: 456)
|
|
]
|
|
)
|
|
let after = before.selectCurrencyCode("EUR")
|
|
|
|
func assertCurrencyCode(_ state: State, expected: Currency.Code) throws {
|
|
switch state {
|
|
case let .loaded(actual, _, _):
|
|
XCTAssertEqual(actual, expected)
|
|
default:
|
|
XCTFail("Invalid state")
|
|
}
|
|
}
|
|
try assertCurrencyCode(before, expected: "USD")
|
|
try assertCurrencyCode(after, expected: "EUR")
|
|
}
|
|
}
|