-
Notifications
You must be signed in to change notification settings - Fork 2
/
ValueOf.swift
47 lines (39 loc) · 1.18 KB
/
ValueOf.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import AtomicModels
import Foundation
import XCTest
public final class ValueOf<T> {
public let value: AtomicValue<T?>
public var expectation: XCTestExpectation
public init(
_ value: T? = nil,
expectation: XCTestExpectation = XCTestExpectation(description: "Set value of \(type(of: T.self)) type")
) {
self.value = AtomicValue(value)
self.expectation = expectation
}
public struct ValueIsNilError<Value>: Error, CustomStringConvertible {
public var description: String {
"Value of type \(type(of: Value.self)) was not yet set"
}
}
public func get() throws -> T {
if let value = value.currentValue() {
return value
}
throw ValueIsNilError<T>()
}
public func set(_ newValue: T) {
self.value.set(newValue)
expectation.fulfill()
}
public func getWhenAvailable(
testCase: XCTestCase,
timeout: TimeInterval = 10
) throws -> T {
testCase.wait(for: expectation, timeout: timeout)
return try get()
}
public func callAsFunction() throws -> T {
return try get()
}
}