Last active
May 15, 2020 18:07
-
-
Save leoniralves/86dea8b9211629ffa3effb6ed2aa1d02 to your computer and use it in GitHub Desktop.
Trecho para exemplificar o uso incorreto de métodos em protocolos
This file contains 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 ExchangeRate { | |
var value: Double { get } | |
} | |
extension ExchangeRate { | |
func businessTaxCalculated() -> Double { | |
return value*(1.5/100) | |
} | |
} | |
class CurrencyConverter { | |
private var exchangeRate: ExchangeRate | |
init(exchangeRate: ExchangeRate) { | |
self.exchangeRate = exchangeRate | |
} | |
func converter(value: Double) -> Double { | |
return (value * exchangeRate.value) - exchangeRate.businessTaxCalculated() | |
} | |
} | |
/// ... | |
struct Dollar: ExchangeRate { | |
var value: Double | |
} | |
let dollar = Dollar(value: 6) | |
let currencyConverter = CurrencyConverter(exchangeRate: dollar) | |
let valueConverted = currencyConverter.converter(value: 1) | |
print(valueConverted) // 5.91 | |
// Testes | |
import XCTest | |
struct DollarMock: ExchangeRate { | |
var value: Double | |
} | |
class CurrencyConverterTest: XCTestCase { | |
func testConverter_ShouldSuccess() { | |
let dollarMock = DollarMock(value: 6) | |
let currencyConverter = CurrencyConverter(exchangeRate: dollarMock) | |
let amountConverter = currencyConverter.converter(value: 1) | |
XCTAssertEqual(amountConverter, 5.91) | |
} | |
} | |
CurrencyConverterTest.defaultTestSuite.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment