Last active
January 29, 2020 10:27
-
-
Save filipemp/68ae897b2886f3f7cdba710e027369a6 to your computer and use it in GitHub Desktop.
Swift problem
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
//: Playground - noun: a place where people can play | |
import Foundation | |
// MARK: Protocols | |
protocol UserRequestProtocol { | |
var name: String { get set } | |
var age: Int { get set } | |
var password: String { get set } | |
var likesBacon: Bool { get set } | |
} | |
protocol UserViewModelProtocol { | |
var name: String? { get set } | |
var age: Int? { get set } | |
var password: String? { get set } | |
var likesBacon: Bool? { get set } | |
} | |
protocol MapperProtocol { | |
func mapUserViewModel(userViewModel: UserViewModelProtocol) throws -> UserRequestProtocol? | |
} | |
enum MapperError: ErrorType { | |
case InvalidParameter(String) | |
} | |
// MARK: Structs | |
struct UserRequest: UserRequestProtocol { | |
var name: String | |
var age: Int | |
var password: String | |
var likesBacon: Bool | |
} | |
struct UserViewModel: UserViewModelProtocol { | |
var name: String? | |
var age: Int? | |
var password: String? | |
var likesBacon: Bool? | |
} | |
// MARK: Mapper | |
class UserViewModelUserRequestMapper: MapperProtocol { | |
func mapUserViewModel(userViewModel: UserViewModelProtocol) throws -> UserRequestProtocol? { | |
// FILL THIS WITH BEAUTIFUL CODE | |
return UserRequest(name: "Nome", age: 20, password: "senha", likesBacon: true) | |
} | |
} | |
// MARK: Setup | |
let mapper = UserViewModelUserRequestMapper() | |
let nullViewModel = UserViewModel(name: nil, age: nil, password: nil, likesBacon: nil) | |
let knowMyName = UserViewModel(name: "Chris Lattner", age: nil, password: nil, likesBacon: nil) | |
let dontKnowIfILikeBacon = UserViewModel(name: "Steve Wozniak", age: 66, password: "b>#F{mt>A-%6FZ^w", likesBacon: nil) | |
let jobs = UserViewModel(name: "Steve Jobs", age: 999, password: "lisa2", likesBacon: true) | |
let invalidPassword = UserViewModel(name: "John Doe", age: 32, password: "", likesBacon: false) | |
let passwordTooLongUser = UserViewModel(name: "Andre Sales", age: 24, password: "jackdaniels", likesBacon: true) | |
var nullResult, knowMyNameResult, dontKnowIfILikeBaconResult, jobsResult, invalidPasswordResult, passwordTooLongUserResult: UserRequestProtocol? | |
do { | |
nullResult = try mapper.mapUserViewModel(nullViewModel) | |
} catch let error { | |
print("\(error)") | |
} | |
do { | |
knowMyNameResult = try mapper.mapUserViewModel(knowMyName) | |
} catch let error { | |
print("\(error)") | |
} | |
do { | |
dontKnowIfILikeBaconResult = try mapper.mapUserViewModel(dontKnowIfILikeBacon) | |
} catch let error { | |
print("\(error)") | |
} | |
do { | |
jobsResult = try mapper.mapUserViewModel(jobs) | |
} catch let error { | |
print("\(error)") | |
} | |
do { | |
invalidPasswordResult = try mapper.mapUserViewModel(invalidPassword) | |
} catch let error { | |
print("\(error)") | |
} | |
do { | |
passwordTooLongUserResult = try mapper.mapUserViewModel(passwordTooLongUser) | |
} catch let error { | |
print("\(error)") | |
} | |
// MARK: Testing | |
func check() -> Bool { | |
return (nullResult == nil) && (knowMyNameResult == nil) && (dontKnowIfILikeBaconResult == nil) && (jobsResult != nil) && (invalidPasswordResult == nil) && (passwordTooLongUserResult == nil) | |
} | |
if check() { | |
print("YOU DID IT, CONGRATZ CHAMP") | |
} else { | |
print("LET'S TRY THIS AGAIN, YOU CAN DO IT, BRO!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment