Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Swift problem
//: 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
You can’t perform that action at this time.