-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubQueryMatcherMock.swift
More file actions
323 lines (277 loc) · 16.5 KB
/
Copy pathSubQueryMatcherMock.swift
File metadata and controls
323 lines (277 loc) · 16.5 KB
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
//
// SubQueryMatcherMock.swift
// IntrospectiveTests
//
// Created by Bryan Nova on 8/20/18.
// Copyright © 2018 Bryan Nova. All rights reserved.
//
import Foundation
import SwiftyMocky
@testable import Introspective
@testable import Attributes
@testable import Queries
@testable import Samples
// sourcery: mock = "SubQueryMatcher"
class SubQueryMatcherMock: SubQueryMatcher, Mock {
fileprivate var _description: String!
var description: String {
get { return _description }
set { _description = newValue }
}
// sourcery:inline:auto:SubQueryMatcherMock.autoMocked
var matcher: Matcher = Matcher.default
var stubbingPolicy: StubbingPolicy = .wrap
var sequencingPolicy: SequencingPolicy = .lastWrittenResolvedFirst
private var invocations: [MethodType] = []
private var methodReturnValues: [Given] = []
private var methodPerformValues: [Perform] = []
private var file: StaticString?
private var line: UInt?
public typealias PropertyStub = Given
public typealias MethodStub = Given
public typealias SubscriptStub = Given
/// Convenience method - call setupMock() to extend debug information when failure occurs
public func setupMock(file: StaticString = #file, line: UInt = #line) {
self.file = file
self.line = line
}
/// Clear mock internals. You can specify what to reset (invocations aka verify, givens or performs) or leave it empty to clear all mock internals
public func resetMock(_ scopes: MockScope...) {
let scopes: [MockScope] = scopes.isEmpty ? [.invocation, .given, .perform] : scopes
if scopes.contains(.invocation) { invocations = [] }
if scopes.contains(.given) { methodReturnValues = [] }
if scopes.contains(.perform) { methodPerformValues = [] }
}
private static func isCapturing<T>(_ param: Parameter<T>) -> Bool {
switch param {
// can't use `case .capturing(_, _):` here because it causes an EXC_BAD_ACCESS error
case .value(_):
return false
case .matching(_):
return false
case ._:
return false
default:
return true
}
}
public var mostRecentOnly: Bool {
get { invocations.append(.p_mostRecentOnly_get); return __p_mostRecentOnly ?? givenGetterValue(.p_mostRecentOnly_get, "SubQueryMatcherMock - stub value for mostRecentOnly was not defined") }
set { invocations.append(.p_mostRecentOnly_set(.value(newValue))); __p_mostRecentOnly = newValue }
}
private var __p_mostRecentOnly: (Bool)?
public required init() { }
open func getSamples<QuerySampleType: Sample>( from querySamples: [QuerySampleType], matching subQuerySamples: [Sample] ) throws -> [QuerySampleType] {
addInvocation(.m_getSamples__from_querySamplesmatching_subQuerySamples(Parameter<[QuerySampleType]>.value(`querySamples`).wrapAsGeneric(), Parameter<[Sample]>.value(`subQuerySamples`)))
let perform = methodPerformValue(.m_getSamples__from_querySamplesmatching_subQuerySamples(Parameter<[QuerySampleType]>.value(`querySamples`).wrapAsGeneric(), Parameter<[Sample]>.value(`subQuerySamples`))) as? ([QuerySampleType], [Sample]) -> Void
perform?(`querySamples`, `subQuerySamples`)
var __value: [QuerySampleType]
do {
__value = try methodReturnValue(.m_getSamples__from_querySamplesmatching_subQuerySamples(Parameter<[QuerySampleType]>.value(`querySamples`).wrapAsGeneric(), Parameter<[Sample]>.value(`subQuerySamples`))).casted()
} catch MockError.notStubed {
onFatalFailure("Stub return value not specified for getSamples<QuerySampleType: Sample>( from querySamples: [QuerySampleType], matching subQuerySamples: [Sample] ). Use given")
Failure("Stub return value not specified for getSamples<QuerySampleType: Sample>( from querySamples: [QuerySampleType], matching subQuerySamples: [Sample] ). Use given")
} catch {
throw error
}
return __value
}
open func equalTo(_ otherMatcher: SubQueryMatcher) -> Bool {
addInvocation(.m_equalTo__otherMatcher(Parameter<SubQueryMatcher>.value(`otherMatcher`)))
let perform = methodPerformValue(.m_equalTo__otherMatcher(Parameter<SubQueryMatcher>.value(`otherMatcher`))) as? (SubQueryMatcher) -> Void
perform?(`otherMatcher`)
var __value: Bool
do {
__value = try methodReturnValue(.m_equalTo__otherMatcher(Parameter<SubQueryMatcher>.value(`otherMatcher`))).casted()
} catch {
onFatalFailure("Stub return value not specified for equalTo(_ otherMatcher: SubQueryMatcher). Use given")
Failure("Stub return value not specified for equalTo(_ otherMatcher: SubQueryMatcher). Use given")
}
return __value
}
fileprivate enum MethodType {
case m_getSamples__from_querySamplesmatching_subQuerySamples(Parameter<GenericAttribute>, Parameter<[Sample]>)
case m_equalTo__otherMatcher(Parameter<SubQueryMatcher>)
case p_mostRecentOnly_get
case p_mostRecentOnly_set(Parameter<Bool>)
static func compareParameters(lhs: MethodType, rhs: MethodType, matcher: Matcher) -> Matcher.ComparisonResult {
switch (lhs, rhs) {
case (.m_getSamples__from_querySamplesmatching_subQuerySamples(let lhsQuerysamples, let lhsSubquerysamples), .m_getSamples__from_querySamplesmatching_subQuerySamples(let rhsQuerysamples, let rhsSubquerysamples)):
var noncapturingComparisons: [Bool] = []
var comparison: Bool
var results: [Matcher.ParameterComparisonResult] = []
if !isCapturing(lhsQuerysamples) && !isCapturing(rhsQuerysamples) {
comparison = Parameter.compare(lhs: lhsQuerysamples, rhs: rhsQuerysamples, with: matcher)
noncapturingComparisons.append(comparison)
results.append(Matcher.ParameterComparisonResult(comparison, lhsQuerysamples, rhsQuerysamples, "from querySamples"))
}
if !isCapturing(lhsSubquerysamples) && !isCapturing(rhsSubquerysamples) {
comparison = Parameter.compare(lhs: lhsSubquerysamples, rhs: rhsSubquerysamples, with: matcher)
noncapturingComparisons.append(comparison)
results.append(Matcher.ParameterComparisonResult(comparison, lhsSubquerysamples, rhsSubquerysamples, "matching subQuerySamples"))
}
if isCapturing(lhsQuerysamples) || isCapturing(rhsQuerysamples) {
comparison = Parameter.compare(lhs: lhsQuerysamples, rhs: rhsQuerysamples, with: matcher, nonCapturingParamsMatch: noncapturingComparisons.allSatisfy({$0}))
results.append(Matcher.ParameterComparisonResult(comparison, lhsQuerysamples, rhsQuerysamples, "from querySamples"))
}
if isCapturing(lhsSubquerysamples) || isCapturing(rhsSubquerysamples) {
comparison = Parameter.compare(lhs: lhsSubquerysamples, rhs: rhsSubquerysamples, with: matcher, nonCapturingParamsMatch: noncapturingComparisons.allSatisfy({$0}))
results.append(Matcher.ParameterComparisonResult(comparison, lhsSubquerysamples, rhsSubquerysamples, "matching subQuerySamples"))
}
return Matcher.ComparisonResult(results)
case (.m_equalTo__otherMatcher(let lhsOthermatcher), .m_equalTo__otherMatcher(let rhsOthermatcher)):
var noncapturingComparisons: [Bool] = []
var comparison: Bool
var results: [Matcher.ParameterComparisonResult] = []
if !isCapturing(lhsOthermatcher) && !isCapturing(rhsOthermatcher) {
comparison = Parameter.compare(lhs: lhsOthermatcher, rhs: rhsOthermatcher, with: matcher)
noncapturingComparisons.append(comparison)
results.append(Matcher.ParameterComparisonResult(comparison, lhsOthermatcher, rhsOthermatcher, "_ otherMatcher"))
}
if isCapturing(lhsOthermatcher) || isCapturing(rhsOthermatcher) {
comparison = Parameter.compare(lhs: lhsOthermatcher, rhs: rhsOthermatcher, with: matcher, nonCapturingParamsMatch: noncapturingComparisons.allSatisfy({$0}))
results.append(Matcher.ParameterComparisonResult(comparison, lhsOthermatcher, rhsOthermatcher, "_ otherMatcher"))
}
return Matcher.ComparisonResult(results)
case (.p_mostRecentOnly_get,.p_mostRecentOnly_get): return Matcher.ComparisonResult.match
case (.p_mostRecentOnly_set(let left),.p_mostRecentOnly_set(let right)): return Matcher.ComparisonResult([Matcher.ParameterComparisonResult(Parameter<Bool>.compare(lhs: left, rhs: right, with: matcher), left, right, "newValue")])
default: return .none
}
}
func intValue() -> Int {
switch self {
case let .m_getSamples__from_querySamplesmatching_subQuerySamples(p0, p1): return p0.intValue + p1.intValue
case let .m_equalTo__otherMatcher(p0): return p0.intValue
case .p_mostRecentOnly_get: return 0
case .p_mostRecentOnly_set(let newValue): return newValue.intValue
}
}
func assertionName() -> String {
switch self {
case .m_getSamples__from_querySamplesmatching_subQuerySamples: return ".getSamples(from:matching:)"
case .m_equalTo__otherMatcher: return ".equalTo(_:)"
case .p_mostRecentOnly_get: return "[get] .mostRecentOnly"
case .p_mostRecentOnly_set: return "[set] .mostRecentOnly"
}
}
}
open class Given: StubbedMethod {
fileprivate var method: MethodType
private init(method: MethodType, products: [StubProduct]) {
self.method = method
super.init(products)
}
public static func mostRecentOnly(getter defaultValue: Bool...) -> PropertyStub {
return Given(method: .p_mostRecentOnly_get, products: defaultValue.map({ StubProduct.return($0 as Any) }))
}
public static func getSamples<QuerySampleType: Sample>(from querySamples: Parameter<[QuerySampleType]>, matching subQuerySamples: Parameter<[Sample]>, willReturn: [QuerySampleType]...) -> MethodStub {
return Given(method: .m_getSamples__from_querySamplesmatching_subQuerySamples(`querySamples`.wrapAsGeneric(), `subQuerySamples`), products: willReturn.map({ StubProduct.return($0 as Any) }))
}
public static func equalTo(_ otherMatcher: Parameter<SubQueryMatcher>, willReturn: Bool...) -> MethodStub {
return Given(method: .m_equalTo__otherMatcher(`otherMatcher`), products: willReturn.map({ StubProduct.return($0 as Any) }))
}
public static func equalTo(_ otherMatcher: Parameter<SubQueryMatcher>, willProduce: (Stubber<Bool>) -> Void) -> MethodStub {
let willReturn: [Bool] = []
let given: Given = { return Given(method: .m_equalTo__otherMatcher(`otherMatcher`), products: willReturn.map({ StubProduct.return($0 as Any) })) }()
let stubber = given.stub(for: (Bool).self)
willProduce(stubber)
return given
}
public static func getSamples<QuerySampleType:Sample>(from querySamples: Parameter<[QuerySampleType]>, matching subQuerySamples: Parameter<[Sample]>, willThrow: Error...) -> MethodStub {
return Given(method: .m_getSamples__from_querySamplesmatching_subQuerySamples(`querySamples`.wrapAsGeneric(), `subQuerySamples`), products: willThrow.map({ StubProduct.throw($0) }))
}
public static func getSamples<QuerySampleType: Sample>(from querySamples: Parameter<[QuerySampleType]>, matching subQuerySamples: Parameter<[Sample]>, willProduce: (StubberThrows<[QuerySampleType]>) -> Void) -> MethodStub {
let willThrow: [Error] = []
let given: Given = { return Given(method: .m_getSamples__from_querySamplesmatching_subQuerySamples(`querySamples`.wrapAsGeneric(), `subQuerySamples`), products: willThrow.map({ StubProduct.throw($0) })) }()
let stubber = given.stubThrows(for: ([QuerySampleType]).self)
willProduce(stubber)
return given
}
}
public struct Verify {
fileprivate var method: MethodType
public static func getSamples<QuerySampleType>(from querySamples: Parameter<[QuerySampleType]>, matching subQuerySamples: Parameter<[Sample]>) -> Verify where QuerySampleType:Sample { return Verify(method: .m_getSamples__from_querySamplesmatching_subQuerySamples(`querySamples`.wrapAsGeneric(), `subQuerySamples`))}
public static func equalTo(_ otherMatcher: Parameter<SubQueryMatcher>) -> Verify { return Verify(method: .m_equalTo__otherMatcher(`otherMatcher`))}
public static var mostRecentOnly: Verify { return Verify(method: .p_mostRecentOnly_get) }
public static func mostRecentOnly(set newValue: Parameter<Bool>) -> Verify { return Verify(method: .p_mostRecentOnly_set(newValue)) }
}
public struct Perform {
fileprivate var method: MethodType
var performs: Any
public static func getSamples<QuerySampleType>(from querySamples: Parameter<[QuerySampleType]>, matching subQuerySamples: Parameter<[Sample]>, perform: @escaping ([QuerySampleType], [Sample]) -> Void) -> Perform where QuerySampleType:Sample {
return Perform(method: .m_getSamples__from_querySamplesmatching_subQuerySamples(`querySamples`.wrapAsGeneric(), `subQuerySamples`), performs: perform)
}
public static func equalTo(_ otherMatcher: Parameter<SubQueryMatcher>, perform: @escaping (SubQueryMatcher) -> Void) -> Perform {
return Perform(method: .m_equalTo__otherMatcher(`otherMatcher`), performs: perform)
}
}
public func given(_ method: Given) {
methodReturnValues.append(method)
}
public func perform(_ method: Perform) {
methodPerformValues.append(method)
methodPerformValues.sort { $0.method.intValue() < $1.method.intValue() }
}
public func verify(_ method: Verify, count: Count = Count.moreOrEqual(to: 1), file: StaticString = #file, line: UInt = #line) {
let fullMatches = matchingCalls(method, file: file, line: line)
let success = count.matches(fullMatches)
let assertionName = method.method.assertionName()
let feedback: String = {
guard !success else { return "" }
return Utils.closestCallsMessage(
for: self.invocations.map { invocation in
matcher.set(file: file, line: line)
defer { matcher.clearFileAndLine() }
return MethodType.compareParameters(lhs: invocation, rhs: method.method, matcher: matcher)
},
name: assertionName
)
}()
MockyAssert(success, "Expected: \(count) invocations of `\(assertionName)`, but was: \(fullMatches).\(feedback)", file: file, line: line)
}
private func addInvocation(_ call: MethodType) {
invocations.append(call)
}
private func methodReturnValue(_ method: MethodType) throws -> StubProduct {
matcher.set(file: self.file, line: self.line)
defer { matcher.clearFileAndLine() }
let candidates = sequencingPolicy.sorted(methodReturnValues, by: { $0.method.intValue() > $1.method.intValue() })
let matched = candidates.first(where: { $0.isValid && MethodType.compareParameters(lhs: $0.method, rhs: method, matcher: matcher).isFullMatch })
guard let product = matched?.getProduct(policy: self.stubbingPolicy) else { throw MockError.notStubed }
return product
}
private func methodPerformValue(_ method: MethodType) -> Any? {
matcher.set(file: self.file, line: self.line)
defer { matcher.clearFileAndLine() }
let matched = methodPerformValues.reversed().first { MethodType.compareParameters(lhs: $0.method, rhs: method, matcher: matcher).isFullMatch }
return matched?.performs
}
private func matchingCalls(_ method: MethodType, file: StaticString?, line: UInt?) -> [MethodType] {
matcher.set(file: file ?? self.file, line: line ?? self.line)
defer { matcher.clearFileAndLine() }
return invocations.filter { MethodType.compareParameters(lhs: $0, rhs: method, matcher: matcher).isFullMatch }
}
private func matchingCalls(_ method: Verify, file: StaticString?, line: UInt?) -> Int {
return matchingCalls(method.method, file: file, line: line).count
}
private func givenGetterValue<T>(_ method: MethodType, _ message: String) -> T {
do {
return try methodReturnValue(method).casted()
} catch {
onFatalFailure(message)
Failure(message)
}
}
private func optionalGivenGetterValue<T>(_ method: MethodType, _ message: String) -> T? {
do {
return try methodReturnValue(method).casted()
} catch {
return nil
}
}
private func onFatalFailure(_ message: String) {
guard let file = self.file, let line = self.line else { return } // Let if fail if cannot handle gratefully
SwiftyMockyTestObserver.handleFatalError(message: message, file: file, line: line)
}
// sourcery:end
}