Skip to content

Commit

Permalink
Add amount function to MultiCurrencyAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
Nef10 committed Jul 28, 2020
1 parent a940b7b commit fbe8db0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Sources/SwiftBeanCountModel/MultiCurrencyAmount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions Tests/SwiftBeanCountModelTests/MultiCurrencyAmountTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit fbe8db0

Please sign in to comment.