Skip to content
Snippets Groups Projects
Commit 6a4c64ee authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Add FactType model

parent dd4e995a
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!71Fact improvements & helpers
import Foundation
public enum FactType: Equatable {
case username
case email
case phone
case other(Int)
public static let knownTypes: [FactType] = [.username, .email, .phone]
public init(rawValue: Int) {
if let known = FactType.knownTypes.first(where: { $0.rawValue == rawValue }) {
self = known
} else {
self = .other(rawValue)
}
}
public var rawValue: Int {
switch self {
case .username: return 0
case .email: return 1
case .phone: return 2
case .other(let rawValue): return rawValue
}
}
}
extension FactType: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self.init(rawValue: try container.decode(Int.self))
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(rawValue)
}
}
import CustomDump
import Foundation
import XCTest
@testable import XXClient
final class FactTypeTests: XCTestCase {
func testDecoding() throws {
let decoder = Foundation.JSONDecoder()
XCTAssertNoDifference(
[
try decoder.decode(FactType.self, from: "0".data(using: .utf8)!),
try decoder.decode(FactType.self, from: "1".data(using: .utf8)!),
try decoder.decode(FactType.self, from: "2".data(using: .utf8)!),
try decoder.decode(FactType.self, from: "3".data(using: .utf8)!),
],
[
FactType.username,
FactType.email,
FactType.phone,
FactType.other(3),
]
)
}
func testEncoding() throws {
let encoder = Foundation.JSONEncoder()
XCTAssertNoDifference(
[
try encoder.encode(FactType.username),
try encoder.encode(FactType.email),
try encoder.encode(FactType.phone),
try encoder.encode(FactType.other(3)),
],
[
"0".data(using: .utf8)!,
"1".data(using: .utf8)!,
"2".data(using: .utf8)!,
"3".data(using: .utf8)!,
]
)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment