From fbe8db01eb05e230b08a76fd48cdaa852417b45b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20K=C3=B6tte?= Date: Mon, 27 Jul 2020 23:18:28 -0700 Subject: [PATCH] Add amount function to MultiCurrencyAmount --- .../MultiCurrencyAmount.swift | 14 +++++++++++++ .../MultiCurrencyAmountTests.swift | 20 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/Sources/SwiftBeanCountModel/MultiCurrencyAmount.swift b/Sources/SwiftBeanCountModel/MultiCurrencyAmount.swift index 94b7ff6..957425e 100644 --- a/Sources/SwiftBeanCountModel/MultiCurrencyAmount.swift +++ b/Sources/SwiftBeanCountModel/MultiCurrencyAmount.swift @@ -53,6 +53,20 @@ public struct MultiCurrencyAmount { return .valid } + /// Returns the amount of one commodity + /// + /// If there is not amount for the given symbol in this MultiCurrencyAmount + /// it returns an amount with zero. + /// + /// - Parameter symbol: symbol of the commodity to get + /// - Returns: Amount + public func amountFor(symbol: CommoditySymbol) -> Amount { + guard let number = amounts[symbol], let digits = decimalDigits[symbol] else { + return Amount(number: 0, commoditySymbol: symbol) + } + return Amount(number: number, commoditySymbol: symbol, decimalDigits: digits) + } + /// Checks is the amount is zero within the allowed tolerance /// /// - Returns: Bool diff --git a/Tests/SwiftBeanCountModelTests/MultiCurrencyAmountTests.swift b/Tests/SwiftBeanCountModelTests/MultiCurrencyAmountTests.swift index 76e0e0d..e815235 100644 --- a/Tests/SwiftBeanCountModelTests/MultiCurrencyAmountTests.swift +++ b/Tests/SwiftBeanCountModelTests/MultiCurrencyAmountTests.swift @@ -21,6 +21,26 @@ class MultiCurrencyAmountTests: XCTestCase { XCTAssertEqual(multiCurrencyAmount.multiCurrencyAmount, multiCurrencyAmount) } + func testAmountFor() { + var multiCurrencyAmount = MultiCurrencyAmount(amounts: [TestUtils.eur: Decimal(10)], decimalDigits: [TestUtils.eur: 3]) + var result = multiCurrencyAmount.amountFor(symbol: TestUtils.eur) + XCTAssertEqual(result.commoditySymbol, TestUtils.eur) + XCTAssertEqual(result.number, Decimal(10)) + XCTAssertEqual(result.decimalDigits, 3) + + multiCurrencyAmount = MultiCurrencyAmount(amounts: [TestUtils.eur: Decimal(10), TestUtils.usd: Decimal(4)], decimalDigits: [TestUtils.eur: 3, TestUtils.usd: 0]) + result = multiCurrencyAmount.amountFor(symbol: TestUtils.eur) + XCTAssertEqual(result.commoditySymbol, TestUtils.eur) + XCTAssertEqual(result.number, Decimal(10)) + XCTAssertEqual(result.decimalDigits, 3) + + multiCurrencyAmount = MultiCurrencyAmount(amounts: [TestUtils.eur: Decimal(10)], decimalDigits: [TestUtils.eur: 3]) + result = multiCurrencyAmount.amountFor(symbol: TestUtils.usd) + XCTAssertEqual(result.commoditySymbol, TestUtils.usd) + XCTAssertEqual(result.number, Decimal(0)) + XCTAssertEqual(result.decimalDigits, 0) + } + func testPlusSameCurrency() { let fifteenEuro = MultiCurrencyAmount(amounts: [TestUtils.eur: Decimal(15)], decimalDigits: [TestUtils.eur: 0]) let tenEuro = MultiCurrencyAmount(amounts: [TestUtils.eur: Decimal(10)], decimalDigits: [TestUtils.eur: 0])