Skip to content
This repository has been archived by the owner on Jul 2, 2018. It is now read-only.

Decimal Arithmetics #92

Open
wants to merge 1 commit into
base: feature/MNY-79_updates_for_xcode_9
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Sources/MoneyProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions Tests/AdditionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
16 changes: 16 additions & 0 deletions Tests/DivisionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}


16 changes: 16 additions & 0 deletions Tests/MultiplicationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}