Created
May 30, 2020 01:46
-
-
Save go-cristian/49d3334536768a7a437e80c5339bf5e6 to your computer and use it in GitHub Desktop.
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
//: A UIKit based Playground for presenting user interface | |
import UIKit | |
import PlaygroundSupport | |
class MyViewController : UIViewController { | |
override func loadView() { | |
let view = UIView() | |
view.backgroundColor = .white | |
let label = UILabel() | |
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20) | |
let response = UseCase(service: RealService()).exec().value() | |
label.text = response | |
label.textColor = .black | |
view.addSubview(label) | |
self.view = view | |
} | |
} | |
class RealService: Service { | |
func request() throws -> Int { | |
return Int.random(in: 1...100) | |
} | |
} | |
protocol Service { | |
func request() throws -> Int | |
} | |
enum ServiceError: Error { | |
case requestError | |
} | |
enum State: Equatable { | |
case success(String) | |
case fail | |
func value() -> String { | |
switch self { | |
case .success(let value): | |
return value | |
default: | |
return "" | |
} | |
} | |
} | |
func ==(lhs: State, rhs: State) -> Bool { | |
switch (lhs, rhs) { | |
case (.success(let a), .success(let b)): | |
return a == b | |
case (.fail, .fail): | |
return true | |
default: | |
return false | |
} | |
} | |
class UseCase { | |
let service: Service | |
init(service: Service) { | |
self.service = service | |
} | |
func exec() -> State { | |
do { | |
let response: Int = try service.request() | |
return .success("\(response)") | |
} catch { | |
return .fail | |
} | |
} | |
} | |
class Sum { | |
func with(_ a: Int,_ b: Int) -> Int { | |
return a + b | |
} | |
} | |
allTests() | |
func allTests() { | |
testSum_10_20() | |
testSum_10_40() | |
testMainViewControllerSuccess() | |
testMainViewControllerFail() | |
} | |
class FakeService: Service { | |
enum Response { | |
case success | |
case error | |
} | |
let response: Response | |
init (response: Response) { | |
self.response = response | |
} | |
func request() throws -> Int { | |
if self.response == .success { | |
return 30 | |
} | |
throw ServiceError.requestError | |
} | |
} | |
func testMainViewControllerSuccess() { | |
let service = FakeService(response: .success) | |
let useCase = UseCase(service: service) | |
let current = useCase.exec() | |
let expected = State.success("30") | |
assert(expected, current) | |
} | |
func testMainViewControllerFail() { | |
let service = FakeService(response: .error) | |
let useCase = UseCase(service: service) | |
let current = useCase.exec() | |
let expected = State.fail | |
assert(expected, current) | |
} | |
func testSum_10_20() { | |
let sum = Sum() | |
let current = sum.with(10, 20) | |
let expected = 30 | |
assert(expected, current) | |
} | |
func testSum_10_40() { | |
let sum = Sum() | |
let current = sum.with(10, 40) | |
let expected = 50 | |
assert(expected, current) | |
} | |
func assert<T: Equatable>(_ expected: T, _ current: T) { | |
if expected == current { print("test Passed") } | |
else { print("Test Failed:\n expected -> \(String(describing: expected))\n current -> \(String(describing: current))") } | |
} | |
// Present the view controller in the Live View window | |
PlaygroundPage.current.liveView = MyViewController() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment