|
| 1 | +// |
| 2 | +// ConsolidatableTests.swift |
| 3 | +// Consolidate |
| 4 | +// |
| 5 | +// Created by Chris Hargreaves on 28/09/2019. |
| 6 | +// Copyright © 2019 Software Engineering Limited. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import XCTest |
| 10 | +@testable import Consolidate |
| 11 | + |
| 12 | +final class ConsolidatableTests: XCTestCase { |
| 13 | + |
| 14 | + func test_emptyArray_returnsEmptyArray() { |
| 15 | + |
| 16 | + let consolidatedTaxes: [TaxAmount] = [].consolidated() |
| 17 | + |
| 18 | + XCTAssertEqual(consolidatedTaxes, []) |
| 19 | + } |
| 20 | + |
| 21 | + func test_noConsolidabaleValues_doesNotConsolidate() { |
| 22 | + |
| 23 | + let importTax = TaxAmount(tax: "Import Tax", amount: 3.00) |
| 24 | + let salesTax = TaxAmount(tax: "Sales Tax", amount: 1.75) |
| 25 | + let exportTax = TaxAmount(tax: "Export Tax", amount: 2.30) |
| 26 | + let incomeTax = TaxAmount(tax: "Income Tax", amount: 2.35) |
| 27 | + |
| 28 | + let consolidatedTaxes = [ |
| 29 | + importTax, |
| 30 | + salesTax, |
| 31 | + exportTax, |
| 32 | + incomeTax |
| 33 | + ].consolidated() |
| 34 | + |
| 35 | + XCTAssertEqual(consolidatedTaxes, [ |
| 36 | + TaxAmount(tax: "Import Tax", amount: 3.00), |
| 37 | + TaxAmount(tax: "Sales Tax", amount: 1.75), |
| 38 | + TaxAmount(tax: "Export Tax", amount: 2.30), |
| 39 | + TaxAmount(tax: "Income Tax", amount: 2.35) |
| 40 | + ]) |
| 41 | + } |
| 42 | + |
| 43 | + func test_multipleConsolidabaleValues_consolidates() { |
| 44 | + |
| 45 | + let importTaxA = TaxAmount(tax: "Import Tax", amount: 3.00) |
| 46 | + let salesTaxA = TaxAmount(tax: "Sales Tax", amount: 1.75) |
| 47 | + let importTaxB = TaxAmount(tax: "Import Tax", amount: 2.30) |
| 48 | + let salesTaxB = TaxAmount(tax: "Sales Tax", amount: 2.35) |
| 49 | + |
| 50 | + let consolidatedTaxes = [ |
| 51 | + importTaxA, |
| 52 | + salesTaxA, |
| 53 | + importTaxB, |
| 54 | + salesTaxB |
| 55 | + ].consolidated() |
| 56 | + |
| 57 | + XCTAssertEqual(consolidatedTaxes, [ |
| 58 | + TaxAmount(tax: "Import Tax", amount: 5.30), |
| 59 | + TaxAmount(tax: "Sales Tax", amount: 4.10) |
| 60 | + ]) |
| 61 | + } |
| 62 | + |
| 63 | + static var allTests = [ |
| 64 | + ("test_noConsolidabaleValues_doesNotConsolidate", test_noConsolidabaleValues_doesNotConsolidate), |
| 65 | + ("test_multipleConsolidabaleValues_consolidates", test_multipleConsolidabaleValues_consolidates), |
| 66 | + ] |
| 67 | +} |
| 68 | + |
| 69 | +// MARK: Test Fixtures |
| 70 | + |
| 71 | +struct TaxAmount: Equatable, Consolidatable { |
| 72 | + |
| 73 | + let tax: String |
| 74 | + let amount: Decimal |
| 75 | + |
| 76 | + var consolidationGroup: AnyHashable { |
| 77 | + return tax |
| 78 | + } |
| 79 | + |
| 80 | + func consolidate(with other: Self) -> Self { |
| 81 | + return .init(tax: tax, amount: amount + other.amount) |
| 82 | + } |
| 83 | +} |
0 commit comments