From 6a735daf416d0f0c575b547468d4d7026553dc06 Mon Sep 17 00:00:00 2001 From: Marco Betschart Date: Sat, 17 Mar 2018 13:12:03 +0100 Subject: [PATCH] Decimal Arithmetics Added support for arithmetic operations using Decimals --- Sources/MoneyProtocol.swift | 35 +++++++++++++++++++++++++++++++++ Tests/AdditionTests.swift | 16 +++++++++++++++ Tests/DivisionTests.swift | 16 +++++++++++++++ Tests/MultiplicationTests.swift | 16 +++++++++++++++ 4 files changed, 83 insertions(+) diff --git a/Sources/MoneyProtocol.swift b/Sources/MoneyProtocol.swift index 08e8f10..0d59d29 100644 --- a/Sources/MoneyProtocol.swift +++ b/Sources/MoneyProtocol.swift @@ -151,6 +151,41 @@ extension MoneyProtocol where FloatLiteralType == Decimal.FloatLiteralType { } } +extension MoneyProtocol { + + public static func +(lhs: Self, rhs: Decimal) -> Self { + return Self(decimal: lhs.decimal + rhs) + } + + public static func +(lhs: Decimal, rhs: Self) -> Self { + return Self(decimal: lhs + rhs.decimal) + } + + public static func -(lhs: Self, rhs: Decimal) -> Self { + return Self(decimal: lhs.decimal - rhs) + } + + public static func -(lhs: Decimal, rhs: Self) -> Self { + return Self(decimal: lhs - rhs.decimal) + } + + public static func *(lhs: Self, rhs: Decimal) -> Self { + return Self(decimal: lhs.decimal * rhs) + } + + public static func *(lhs: Decimal, rhs: Self) -> Self { + return Self(decimal: lhs * rhs.decimal) + } + + public static func /(lhs: Self, rhs: Decimal) -> Self { + return Self(decimal: lhs.decimal / rhs) + } + + public static func /(lhs: Decimal, rhs: Self) -> Self { + return Self(decimal: lhs / rhs.decimal) + } +} + public extension MoneyProtocol { func distance(to other: Self) -> Self { diff --git a/Tests/AdditionTests.swift b/Tests/AdditionTests.swift index c469693..47e182d 100644 --- a/Tests/AdditionTests.swift +++ b/Tests/AdditionTests.swift @@ -31,6 +31,14 @@ extension MoneyTestCase { let result = 3.5 + money + 2.5 XCTAssertEqual(result, 16) } + + func test__money_addition_by_decimals() { + money = 10 + let a: Decimal = 3.5 + let b: Decimal = 2.5 + let result = a + money + b + XCTAssertEqual(result, 16) + } } extension MoneyTestCase { @@ -52,4 +60,12 @@ extension MoneyTestCase { let result = 3.5 + gbp + 2.5 XCTAssertEqual(result, 16) } + + func test__iso_addition_by_decimals() { + gbp = 10 + let a: Decimal = 3.5 + let b: Decimal = 2.5 + let result = a + gbp + b + XCTAssertEqual(result, 16) + } } diff --git a/Tests/DivisionTests.swift b/Tests/DivisionTests.swift index de08824..1ecb8d8 100644 --- a/Tests/DivisionTests.swift +++ b/Tests/DivisionTests.swift @@ -31,6 +31,14 @@ extension MoneyTestCase { let result = 3.5 / money / 2.5 XCTAssertEqual(result.floatValue, 0.14, accuracy: 0.001) } + + func test__money_division_by_decimals() { + money = 10 + let a: Decimal = 3.5 + let b: Decimal = 2.5 + let result = a / money / b + XCTAssertEqual(result.floatValue, 0.14, accuracy: 0.001) + } } extension MoneyTestCase { @@ -52,6 +60,14 @@ extension MoneyTestCase { let result = 3.5 / gbp / 2.5 XCTAssertEqual(result.floatValue, 0.14, accuracy: 0.001) } + + func test__iso_division_by_decimals() { + gbp = 10 + let a: Decimal = 3.5 + let b: Decimal = 2.5 + let result = a / gbp / b + XCTAssertEqual(result.floatValue, 0.14, accuracy: 0.001) + } } diff --git a/Tests/MultiplicationTests.swift b/Tests/MultiplicationTests.swift index 11b0d4d..b582ec6 100644 --- a/Tests/MultiplicationTests.swift +++ b/Tests/MultiplicationTests.swift @@ -31,6 +31,14 @@ extension MoneyTestCase { let result = 3.5 * money * 2.5 XCTAssertEqual(result, 87.5) } + + func test__money_multiplation_by_decimals() { + money = 10 + let a: Decimal = 3.5 + let b: Decimal = 2.5 + let result = a * money * b + XCTAssertEqual(result, 87.5) + } } extension MoneyTestCase { @@ -52,4 +60,12 @@ extension MoneyTestCase { let result = 3.5 * gbp * 2.5 XCTAssertEqual(result, 87.5) } + + func test__iso_multiplation_by_decimals() { + gbp = 10 + let a: Decimal = 3.5 + let b: Decimal = 2.5 + let result = a * gbp * b + XCTAssertEqual(result, 87.5) + } }