Skip to content

Commit a6078f4

Browse files
Adds examples presented in README as unit tests
1 parent 5f01cb5 commit a6078f4

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

RoundedDecimalTests/RoundedDecimalTests.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ class RoundedDecimalTests: XCTestCase {
7676
XCTAssertNotEqual(decimalA, decimalB)
7777
}
7878

79+
func test_equatable_variousDecimals_returnsTrue() {
80+
81+
let decimalA: RoundedDecimal<Places.two> = "1.25"
82+
83+
let decimalB: RoundedDecimal<Places.two> = "1.254"
84+
85+
let decimalC: RoundedDecimal<Places.two> = "1.2545"
86+
87+
let decimalD: RoundedDecimal<Places.two> = "1.250000"
88+
89+
XCTAssertEqual(decimalA, decimalB)
90+
91+
XCTAssertEqual(decimalA, decimalC)
92+
93+
XCTAssertEqual(decimalA, decimalD)
94+
}
95+
7996
// MARK: Comparable
8097

8198
func test_comparable_lessThan_lhsLessThanRhs_returnsTrue() {
@@ -334,4 +351,32 @@ class RoundedDecimalTests: XCTestCase {
334351

335352
XCTAssertEqual(roundedDecimal.debugDescription, "1.97648")
336353
}
354+
355+
// MARK: Examples
356+
357+
func test_dealingWithMultiplePrecisions_decreasingPrecision() {
358+
359+
let listedUSDPrice: RoundedDecimal<Places.two> = "2.59"
360+
361+
let exchangeRate: RoundedDecimal<Places.five> = "1.12345"
362+
363+
let shortExchangeRate: RoundedDecimal<Places.two> = exchangeRate.withInferredPrecision()
364+
365+
let localPrice = listedUSDPrice * shortExchangeRate
366+
367+
XCTAssertEqual(localPrice, "2.90")
368+
}
369+
370+
func test_dealingWithMultiplePrecisions_increasingPrecision() {
371+
372+
let listedUSDPrice: RoundedDecimal<Places.two> = "2.59"
373+
374+
let exchangeRate: RoundedDecimal<Places.five> = "1.12345"
375+
376+
let longListedUSDPrice: RoundedDecimal<Places.five> = listedUSDPrice.withInferredPrecision()
377+
378+
let localPrice = longListedUSDPrice * exchangeRate
379+
380+
XCTAssertEqual(localPrice, "2.90974")
381+
}
337382
}

0 commit comments

Comments
 (0)