-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThrowingMock.swift
63 lines (53 loc) · 2.21 KB
/
ThrowingMock.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// ThrowingMock.swift
//
//
// Created by Joel Saltzman on 5/23/21.
//
import Foundation
/// Mock a throwing function and store it's usage history.
@propertyWrapper public final class ThrowingMock<Context, Value>: Mockable {
/// Get direct access the underlying ThrowingMock object.
public var projectedValue: ThrowingMock<Context, Value> { return self }
/// Stores the input and output of the when the Mock gets a value.
public var usage: MockUsage<Context, Value>
/// The initial close that the Mock was setup with.
public var defaultValueLoader: (Context) throws -> Value
/// Use to the current value load to get an expected value.
public var wrappedValue: (Context) throws -> Value {
get {
return getValue
}
set {
currentValueLoader = newValue
}
}
/// This will either be the `defaultValueLoader` from when you initialized the ThrowingMock.
/// Or this will be a custom one for mocking expected values.
var currentValueLoader: (Context) throws -> Value
/// Initial the Mock with a default value loader.
/// - Parameter valueLoader: A closure to return a value. This is typically the live version of the function. You can override this later and reset back to this original value loader.
public init(wrappedValue: @escaping (Context) throws -> Value) {
self.usage = MockUsage()
self.defaultValueLoader = wrappedValue
self.currentValueLoader = wrappedValue
}
/// Get a value from the current value loader
/// - Parameter context: The context required by the closure to get the value.
/// - Returns: The value from the value loader.
public func getValue(_ context: Context) throws -> Value {
let result: Value
do {
result = try currentValueLoader(context)
usage.addResult(context: context, value: result)
} catch {
usage.addError(context: context, error: error)
throw error
}
return result
}
/// Reset the value loader to the default one used when the Mock was created.
public func resetLoader() {
self.wrappedValue = defaultValueLoader
}
}