Skip to content

Commit

Permalink
Added mocks to primitive types like String, Int..
Browse files Browse the repository at this point in the history
  • Loading branch information
skychiarottoa committed Nov 17, 2020
1 parent 02b17ae commit 455d21c
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions Sources/SkyTestFoundation/Mocks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import Foundation

// Primitive Mock
enum RealDataDictionary {
case int
case uint
case url
case uuid
case firstname
case surname
case city
case street
case month
case year
case country
case province
case email
case mobilePhone
case landlinePhone
}

extension String {
static func mock(_ type: RealDataDictionary = .uuid) -> String {
switch type {
case .city: return ["Torino", "Milano", "Ivrea"].randomElement()!
case .street: return ["Via Roma", "Via Giuseppe di Vittorio", "Via Jervis", "Piazza Bodoni", "Corso Trieste"].randomElement()!
case .url: return "http://www.\(String.mock()).com"
case .uuid: return UUID().uuidString
case .int: return "\(Int.random(in: -50...50))"
case .uint: return "\(Int.random(in: 0...100))"
case .month: return "\(Int.random(in: 1...12))"
case .year: return "\(Int.random(in: 2000...2050))"
case .firstname: return ["Don Lurio", "Gino", "Augusto", "Elisa", "Jole", "Amanda"].randomElement()!
case .surname: return ["Rossi", "Esposito", "Ferrari", "Russo", "Bianco", "Fontana"].randomElement()!
case .country: return ["IT", "GB", "FR", "USA", "DK", "SE"].randomElement()!
case .province: return ["To", "Mi", "Na", "Fi", "Bo"].randomElement()!
case .email: return ["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"].randomElement()!
case .mobilePhone: return ["3391212149", "340303328832", "3391349898", "3239039023"].randomElement()!
case .landlinePhone: return ["0554641900", "0230302400", "0957181801", "0516400100", "0115066201"].randomElement()!
}
}

static func mock(from: [String]) -> String {
return from.randomElement()!
}
}

extension Int {
static func mock() -> Int {
return Int.random(in: 0...100)
}
}

extension Bool {
static func mock() -> Bool {
return Bool.random()
}
}

extension URL {
static func mock(string: String = String.mock(.url)) -> URL {
return URL(string: string)!
}
}

extension Date {
static func mock(_ date: Date = Date()) -> Date {
let calendar = Calendar(identifier: .gregorian)
return calendar.date(from: calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date))!
}

static func mock(year: Int, month: Int, day: Int) -> Date {
let calendar = Calendar(identifier: .gregorian)
let dateComponents = DateComponents(calendar: calendar, year: year, month: month, day: day)
return calendar.date(from: dateComponents)!
}

static func mockRandom() -> Date {
return Calendar(identifier: .gregorian).date(byAdding: .day, value: Int.random(in: 1...1000), to: Date.mock())!
}
}

0 comments on commit 455d21c

Please sign in to comment.