Created
May 15, 2020 18:08
-
-
Save leoniralves/b62d9c1a94e76351b39b902452ba439a to your computer and use it in GitHub Desktop.
Exemplo de uma forma mais correta no uso de protocolos e testes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol ExchangeProtocol { | |
var value: Double { get } | |
} | |
protocol Taxable { | |
var value: Double { get } | |
func calculated(exch: ExchangeProtocol) -> Double | |
} | |
extension Taxable { | |
func calculated(exch: ExchangeProtocol) -> Double { | |
return exch.value*(value/100) | |
} | |
} | |
class CurrencyConverter { | |
private var exchange: ExchangeProtocol | |
private var tax: Taxable | |
init(exchange: ExchangeProtocol, tax: Taxable) { | |
self.exchange = exchange | |
self.tax = tax | |
} | |
func converter(value: Double) -> Double { | |
return (value*exchange.value)-tax.calculated(exch: exchange) | |
} | |
} | |
// Testes | |
import XCTest | |
struct DollarMock: ExchangeProtocol { | |
var value: Double | |
} | |
struct TaxMock: Taxable { | |
var value: Double | |
func calculated(exch: ExchangeProtocol) -> Double { | |
return 0 | |
} | |
} | |
class CurrencyConverterTest: XCTestCase { | |
func testConverter_ShouldSuccess() { | |
let dollarMock = DollarMock(value: 6) | |
let taxMock = TaxMock(value: 1.5) | |
let currencyConverter = CurrencyConverter(exchange: dollarMock, tax: taxMock) | |
let amountConverter = currencyConverter.converter(value: 1) | |
XCTAssertEqual(amountConverter, 6) | |
} | |
} | |
CurrencyConverterTest.defaultTestSuite.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment