forked from pointfreeco/swift-composable-architecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMock.swift
334 lines (329 loc) · 15.6 KB
/
Mock.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#if DEBUG
import CoreLocation
import ComposableArchitecture
extension LocationManager {
/// The mock implementation of the `LocationManager` interface. By default this implementation
/// stubs all of its endpoints as functions that immediately `fatalError`. So, to construct a
/// mock you will invoke the `.mock` static method, and provide implementations for all of the
/// endpoints that you expect your test to need access to.
///
/// This allows you to test an even deeper property of your features: that they use only the
/// location manager endpoints that you specify and nothing else. This can be useful as a
/// measurement of just how complex a particular test is. Tests that need to stub many endpoints
/// are in some sense more complicated than tests that only need to stub a few endpoints. It's
/// not necessarily a bad thing to stub many endpoints. Sometimes it's needed.
///
/// As an example, to create a mock manager that simulates a location manager that has already
/// authorized access to location, and when a location is requested it immediately responds
/// with a mock location we can do something like this:
///
/// // Send actions to this subject to simulate the location manager's delegate methods
/// // being called.
/// let locationManagerSubject = PassthroughSubject<LocationManager.Action, Never>()
///
/// // The mock location we want the manager to say we are located at
/// let mockLocation = Location(
/// coordinate: CLLocationCoordinate2D(latitude: 40.6501, longitude: -73.94958),
/// // A whole bunch of other properties have been omitted.
/// )
///
/// let manager = LocationManager.mock(
/// // Override any CLLocationManager endpoints your test invokes:
///
/// authorizationStatus: { .authorizedAlways },
/// create: { _ in locationManagerSubject.eraseToEffect() },
/// locationServicesEnabled: { true },
/// requestLocation: { _ in
/// .fireAndForget { locationManagerSubject.send(.didUpdateLocations([mockLocation])) }
/// }
/// )
///
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
public static func mock(
authorizationStatus: @escaping () -> CLAuthorizationStatus = {
_unimplemented("authorizationStatus")
},
create: @escaping (_ id: AnyHashable) -> Effect<Action, Never> = { _ in
_unimplemented("create")
},
destroy: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in _unimplemented("destroy") },
dismissHeadingCalibrationDisplay: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("dismissHeadingCalibrationDisplay")
},
heading: @escaping (AnyHashable) -> Heading? = { _ in _unimplemented("heading") },
headingAvailable: @escaping () -> Bool = { _unimplemented("headingAvailable") },
isRangingAvailable: @escaping () -> Bool = { _unimplemented("isRangingAvailable") },
location: @escaping (AnyHashable) -> Location = { _ in _unimplemented("location") },
locationServicesEnabled: @escaping () -> Bool = { _unimplemented("locationServicesEnabled") },
maximumRegionMonitoringDistance: @escaping (AnyHashable) -> CLLocationDistance = { _ in
_unimplemented("maximumRegionMonitoringDistance")
},
monitoredRegions: @escaping (AnyHashable) -> Set<Region> = { _ in
_unimplemented("monitoredRegions")
},
requestAlwaysAuthorization: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("requestAlwaysAuthorization")
},
requestLocation: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("requestLocation")
},
requestWhenInUseAuthorization: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("requestWhenInUseAuthorization")
},
set: @escaping (_ id: AnyHashable, _ properties: Properties) -> Effect<Never, Never> = {
_, _ in _unimplemented("set")
},
significantLocationChangeMonitoringAvailable: @escaping () -> Bool = {
_unimplemented("significantLocationChangeMonitoringAvailable")
},
startMonitoringSignificantLocationChanges: @escaping (AnyHashable) -> Effect<Never, Never> = {
_ in _unimplemented("startMonitoringSignificantLocationChanges")
},
startMonitoringForRegion: @escaping (AnyHashable, Region) -> Effect<Never, Never> = { _, _ in
_unimplemented("startMonitoringForRegion")
},
startMonitoringVisits: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("startMonitoringVisits")
},
startUpdatingLocation: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("startUpdatingLocation")
},
stopMonitoringSignificantLocationChanges: @escaping (AnyHashable) -> Effect<Never, Never> = {
_ in _unimplemented("stopMonitoringSignificantLocationChanges")
},
stopMonitoringForRegion: @escaping (AnyHashable, Region) -> Effect<Never, Never> = { _, _ in
_unimplemented("stopMonitoringForRegion")
},
stopMonitoringVisits: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("stopMonitoringVisits")
},
startUpdatingHeading: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("startUpdatingHeading")
},
stopUpdatingHeading: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("stopUpdatingHeading")
},
stopUpdatingLocation: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("stopUpdatingLocation")
}
) -> Self {
Self(
authorizationStatus: authorizationStatus,
create: create,
destroy: destroy,
dismissHeadingCalibrationDisplay: dismissHeadingCalibrationDisplay,
heading: heading,
headingAvailable: headingAvailable,
isRangingAvailable: isRangingAvailable,
location: location,
locationServicesEnabled: locationServicesEnabled,
maximumRegionMonitoringDistance: maximumRegionMonitoringDistance,
monitoredRegions: monitoredRegions,
requestAlwaysAuthorization: requestAlwaysAuthorization,
requestLocation: requestLocation,
requestWhenInUseAuthorization: requestWhenInUseAuthorization,
set: set,
significantLocationChangeMonitoringAvailable: significantLocationChangeMonitoringAvailable,
startMonitoringForRegion: startMonitoringForRegion,
startMonitoringSignificantLocationChanges: startMonitoringSignificantLocationChanges,
startMonitoringVisits: startMonitoringVisits,
startUpdatingHeading: startUpdatingHeading,
startUpdatingLocation: startUpdatingLocation,
stopMonitoringForRegion: stopMonitoringForRegion,
stopMonitoringSignificantLocationChanges: stopMonitoringSignificantLocationChanges,
stopMonitoringVisits: stopMonitoringVisits,
stopUpdatingHeading: stopUpdatingHeading,
stopUpdatingLocation: stopUpdatingLocation
)
}
@available(iOS, unavailable)
@available(macCatalyst, unavailable)
@available(macOS, unavailable)
@available(tvOS, unavailable)
public static func mock(
authorizationStatus: @escaping () -> CLAuthorizationStatus = {
_unimplemented("authorizationStatus")
},
create: @escaping (_ id: AnyHashable) -> Effect<Action, Never> = { _ in
_unimplemented("create")
},
destroy: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in _unimplemented("destroy") },
dismissHeadingCalibrationDisplay: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("dismissHeadingCalibrationDisplay")
},
heading: @escaping (AnyHashable) -> Heading? = { _ in _unimplemented("heading") },
headingAvailable: @escaping () -> Bool = { _unimplemented("headingAvailable") },
location: @escaping (AnyHashable) -> Location = { _ in _unimplemented("location") },
locationServicesEnabled: @escaping () -> Bool = { _unimplemented("locationServicesEnabled") },
requestAlwaysAuthorization: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("requestAlwaysAuthorization")
},
requestLocation: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("requestLocation")
},
requestWhenInUseAuthorization: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("requestWhenInUseAuthorization")
},
set: @escaping (_ id: AnyHashable, _ properties: Properties) -> Effect<Never, Never> = {
_, _ in _unimplemented("set")
},
startUpdatingHeading: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("startUpdatingHeading")
},
startUpdatingLocation: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("startUpdatingLocation")
},
stopUpdatingHeading: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("stopUpdatingHeading")
},
stopUpdatingLocation: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("stopUpdatingLocation")
}
) -> Self {
Self(
authorizationStatus: authorizationStatus,
create: create,
destroy: destroy,
dismissHeadingCalibrationDisplay: dismissHeadingCalibrationDisplay,
heading: heading,
headingAvailable: headingAvailable,
location: location,
locationServicesEnabled: locationServicesEnabled,
requestAlwaysAuthorization: requestAlwaysAuthorization,
requestLocation: requestLocation,
requestWhenInUseAuthorization: requestWhenInUseAuthorization,
set: set,
startUpdatingHeading: startUpdatingHeading,
startUpdatingLocation: startUpdatingLocation,
stopUpdatingHeading: stopUpdatingHeading,
stopUpdatingLocation: stopUpdatingLocation
)
}
@available(iOS, unavailable)
@available(macCatalyst, unavailable)
@available(macOS, unavailable)
@available(watchOS, unavailable)
public static func mock(
authorizationStatus: @escaping () -> CLAuthorizationStatus = {
_unimplemented("authorizationStatus")
},
create: @escaping (_ id: AnyHashable) -> Effect<Action, Never> = { _ in
_unimplemented("create")
},
destroy: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in _unimplemented("destroy") },
location: @escaping (AnyHashable) -> Location = { _ in _unimplemented("location") },
locationServicesEnabled: @escaping () -> Bool = { _unimplemented("locationServicesEnabled") },
requestLocation: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("requestLocation")
},
requestWhenInUseAuthorization: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("requestWhenInUseAuthorization")
},
set: @escaping (_ id: AnyHashable, _ properties: Properties) -> Effect<Never, Never> = {
_, _ in _unimplemented("set")
},
stopUpdatingLocation: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("stopUpdatingLocation")
}
) -> Self {
Self(
authorizationStatus: authorizationStatus,
create: create,
destroy: destroy,
location: location,
locationServicesEnabled: locationServicesEnabled,
requestLocation: requestLocation,
requestWhenInUseAuthorization: requestWhenInUseAuthorization,
set: set,
stopUpdatingLocation: stopUpdatingLocation
)
}
@available(iOS, unavailable)
@available(macCatalyst, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
public static func mock(
authorizationStatus: @escaping () -> CLAuthorizationStatus = {
_unimplemented("authorizationStatus")
},
create: @escaping (_ id: AnyHashable) -> Effect<Action, Never> = { _ in
_unimplemented("create")
},
destroy: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in _unimplemented("destroy") },
headingAvailable: @escaping () -> Bool = { _unimplemented("headingAvailable") },
location: @escaping (AnyHashable) -> Location = { _ in _unimplemented("location") },
locationServicesEnabled: @escaping () -> Bool = { _unimplemented("locationServicesEnabled") },
maximumRegionMonitoringDistance: @escaping (AnyHashable) -> CLLocationDistance = { _ in
_unimplemented("maximumRegionMonitoringDistance")
},
monitoredRegions: @escaping (AnyHashable) -> Set<Region> = { _ in
_unimplemented("monitoredRegions")
},
requestAlwaysAuthorization: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("requestAlwaysAuthorization")
},
requestLocation: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("requestLocation")
},
set: @escaping (_ id: AnyHashable, _ properties: Properties) -> Effect<Never, Never> = {
_, _ in _unimplemented("set")
},
significantLocationChangeMonitoringAvailable: @escaping () -> Bool = {
_unimplemented("significantLocationChangeMonitoringAvailable")
},
startMonitoringForRegion: @escaping (AnyHashable, Region) -> Effect<Never, Never> = { _, _ in
_unimplemented("startMonitoringForRegion")
},
startMonitoringSignificantLocationChanges: @escaping (AnyHashable) -> Effect<Never, Never> = {
_ in _unimplemented("startMonitoringSignificantLocationChanges")
},
startUpdatingLocation: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("startUpdatingLocation")
},
stopMonitoringForRegion: @escaping (AnyHashable, Region) -> Effect<Never, Never> = { _, _ in
_unimplemented("stopMonitoringForRegion")
},
stopMonitoringSignificantLocationChanges: @escaping (AnyHashable) -> Effect<Never, Never> = {
_ in _unimplemented("stopMonitoringSignificantLocationChanges")
},
stopUpdatingLocation: @escaping (AnyHashable) -> Effect<Never, Never> = { _ in
_unimplemented("stopUpdatingLocation")
}
) -> Self {
Self(
authorizationStatus: authorizationStatus,
create: create,
destroy: destroy,
headingAvailable: headingAvailable,
location: location,
locationServicesEnabled: locationServicesEnabled,
maximumRegionMonitoringDistance: maximumRegionMonitoringDistance,
monitoredRegions: monitoredRegions,
requestAlwaysAuthorization: requestAlwaysAuthorization,
requestLocation: requestLocation,
set: set,
significantLocationChangeMonitoringAvailable: significantLocationChangeMonitoringAvailable,
startMonitoringForRegion: startMonitoringForRegion,
startMonitoringSignificantLocationChanges: startMonitoringSignificantLocationChanges,
startUpdatingLocation: startUpdatingLocation,
stopMonitoringForRegion: stopMonitoringForRegion,
stopMonitoringSignificantLocationChanges: stopMonitoringSignificantLocationChanges,
stopUpdatingLocation: stopUpdatingLocation
)
}
}
#endif
public func _unimplemented(
_ function: StaticString, file: StaticString = #file, line: UInt = #line
) -> Never {
fatalError(
"""
`\(function)` was called but is not implemented. Be sure to provide an implementation for
this endpoint when creating the mock.
""",
file: file,
line: line
)
}