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

Add convenient fact getter and setter to Contact

parent f538f821
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!71Fact improvements & helpers
...@@ -44,6 +44,18 @@ extension Contact: Equatable { ...@@ -44,6 +44,18 @@ extension Contact: Equatable {
} }
} }
extension Contact {
public func getFact(_ type: FactType) throws -> Fact? {
try getFacts().get(type)
}
public mutating func setFact(_ type: FactType, _ value: String?) throws {
var facts = try getFacts()
facts.set(type, value)
try setFacts(facts)
}
}
extension Contact { extension Contact {
public static func live( public static func live(
_ data: Data, _ data: Data,
......
import CustomDump
import XCTest
@testable import XXClient
final class ContactTests: XCTestCase {
func testGetFact() throws {
var contact = Contact.unimplemented("contact-data".data(using: .utf8)!)
contact.getFactsFromContact.run = { _ in
[
Fact(fact: "username", type: 0),
Fact(fact: "email", type: 1),
Fact(fact: "phone", type: 2),
Fact(fact: "other", type: 3),
]
}
XCTAssertNoDifference(
[
try contact.getFact(.username),
try contact.getFact(.email),
try contact.getFact(.phone),
try contact.getFact(.other(3)),
try contact.getFact(.other(4)),
],
[
Fact(fact: "username", type: 0),
Fact(fact: "email", type: 1),
Fact(fact: "phone", type: 2),
Fact(fact: "other", type: 3),
nil
]
)
}
func testSetFact() throws {
var contact = Contact.unimplemented("contact-data".data(using: .utf8)!)
var facts: [Fact] = [
Fact(fact: "username", type: 0),
Fact(fact: "email", type: 1),
Fact(fact: "phone", type: 2),
Fact(fact: "other-3", type: 3),
]
contact.getFactsFromContact.run = { _ in facts }
contact.setFactsOnContact.run = { data, newFacts in
facts = newFacts
return data
}
try contact.setFact(.username, "new-username")
try contact.setFact(.other(4), "new-other-4")
try contact.setFact(.other(3), "new-other-3")
try contact.setFact(.email, nil)
XCTAssertNoDifference(facts, [
Fact(fact: "new-username", type: 0),
Fact(fact: "phone", type: 2),
Fact(fact: "new-other-3", type: 3),
Fact(fact: "new-other-4", type: 4),
])
}
}
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