Skip to content

Instantly share code, notes, and snippets.

@leoniralves
Last active May 15, 2020 18:07
Show Gist options
  • Save leoniralves/86dea8b9211629ffa3effb6ed2aa1d02 to your computer and use it in GitHub Desktop.
Save leoniralves/86dea8b9211629ffa3effb6ed2aa1d02 to your computer and use it in GitHub Desktop.
Trecho para exemplificar o uso incorreto de métodos em protocolos
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