-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoreDataSampleMock.swift
More file actions
580 lines (499 loc) · 28.7 KB
/
Copy pathCoreDataSampleMock.swift
File metadata and controls
580 lines (499 loc) · 28.7 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//
// CoreDataSampleMock.swift
// IntrospectiveTests
//
// Created by Bryan Nova on 8/16/19.
// Copyright © 2019 Bryan Nova. All rights reserved.
//
import Foundation
import SwiftyMocky
import CSV
@testable import Introspective
@testable import Attributes
@testable import Samples
// sourcery: mock = "CoreDataSample"
class CoreDataSampleMock: CoreDataSample, Mock, StaticMock {
public var description: String = ""
// sourcery:inline:auto:CoreDataSampleMock.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
}
}
static var matcher: Matcher = Matcher.default
static var stubbingPolicy: StubbingPolicy = .wrap
static var sequencingPolicy: SequencingPolicy = .lastWrittenResolvedFirst
static private var invocations: [StaticMethodType] = []
static private var methodReturnValues: [StaticGiven] = []
static private var methodPerformValues: [StaticPerform] = []
public typealias StaticPropertyStub = StaticGiven
public typealias StaticMethodStub = StaticGiven
/// 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 static 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 = [] }
}
public static var name: String {
get { CoreDataSampleMock.invocations.append(.p_name_get); return CoreDataSampleMock.__p_name ?? givenGetterValue(.p_name_get, "CoreDataSampleMock - stub value for name was not defined") }
}
private static var __p_name: (String)?
public static var attributes: [Attribute] {
get { CoreDataSampleMock.invocations.append(.p_attributes_get); return CoreDataSampleMock.__p_attributes ?? givenGetterValue(.p_attributes_get, "CoreDataSampleMock - stub value for attributes was not defined") }
}
private static var __p_attributes: ([Attribute])?
public static var defaultDependentAttribute: Attribute {
get { CoreDataSampleMock.invocations.append(.p_defaultDependentAttribute_get); return CoreDataSampleMock.__p_defaultDependentAttribute ?? givenGetterValue(.p_defaultDependentAttribute_get, "CoreDataSampleMock - stub value for defaultDependentAttribute was not defined") }
}
private static var __p_defaultDependentAttribute: (Attribute)?
public static var defaultIndependentAttribute: Attribute {
get { CoreDataSampleMock.invocations.append(.p_defaultIndependentAttribute_get); return CoreDataSampleMock.__p_defaultIndependentAttribute ?? givenGetterValue(.p_defaultIndependentAttribute_get, "CoreDataSampleMock - stub value for defaultIndependentAttribute was not defined") }
}
private static var __p_defaultIndependentAttribute: (Attribute)?
public static var dateAttributes: [DateType: DateAttribute] {
get { CoreDataSampleMock.invocations.append(.p_dateAttributes_get); return CoreDataSampleMock.__p_dateAttributes ?? givenGetterValue(.p_dateAttributes_get, "CoreDataSampleMock - stub value for dateAttributes was not defined") }
}
private static var __p_dateAttributes: ([DateType: DateAttribute])?
open func graphableValue(of attribute: Attribute) throws -> Any? {
addInvocation(.m_graphableValue__of_attribute(Parameter<Attribute>.value(`attribute`)))
let perform = methodPerformValue(.m_graphableValue__of_attribute(Parameter<Attribute>.value(`attribute`))) as? (Attribute) -> Void
perform?(`attribute`)
var __value: Any? = nil
do {
__value = try methodReturnValue(.m_graphableValue__of_attribute(Parameter<Attribute>.value(`attribute`))).casted()
} catch MockError.notStubed {
// do nothing
} catch {
throw error
}
return __value
}
open func dates() -> [DateType: Date] {
addInvocation(.m_dates)
let perform = methodPerformValue(.m_dates) as? () -> Void
perform?()
var __value: [DateType: Date]
do {
__value = try methodReturnValue(.m_dates).casted()
} catch {
onFatalFailure("Stub return value not specified for dates(). Use given")
Failure("Stub return value not specified for dates(). Use given")
}
return __value
}
open func equalTo(_ otherSample: Sample) -> Bool {
addInvocation(.m_equalTo__otherSample(Parameter<Sample>.value(`otherSample`)))
let perform = methodPerformValue(.m_equalTo__otherSample(Parameter<Sample>.value(`otherSample`))) as? (Sample) -> Void
perform?(`otherSample`)
var __value: Bool
do {
__value = try methodReturnValue(.m_equalTo__otherSample(Parameter<Sample>.value(`otherSample`))).casted()
} catch {
onFatalFailure("Stub return value not specified for equalTo(_ otherSample: Sample). Use given")
Failure("Stub return value not specified for equalTo(_ otherSample: Sample). Use given")
}
return __value
}
open func safeEqualityCheck<Type: Equatable>( _ attribute: Attribute, _ otherSample: Sample, as: Type.Type ) -> Bool {
addInvocation(.m_safeEqualityCheck__attribute_otherSampleas_as(Parameter<Attribute>.value(`attribute`), Parameter<Sample>.value(`otherSample`), Parameter<Type.Type>.value(`as`).wrapAsGeneric()))
let perform = methodPerformValue(.m_safeEqualityCheck__attribute_otherSampleas_as(Parameter<Attribute>.value(`attribute`), Parameter<Sample>.value(`otherSample`), Parameter<Type.Type>.value(`as`).wrapAsGeneric())) as? (Attribute, Sample, Type.Type) -> Void
perform?(`attribute`, `otherSample`, `as`)
var __value: Bool
do {
__value = try methodReturnValue(.m_safeEqualityCheck__attribute_otherSampleas_as(Parameter<Attribute>.value(`attribute`), Parameter<Sample>.value(`otherSample`), Parameter<Type.Type>.value(`as`).wrapAsGeneric())).casted()
} catch {
onFatalFailure("Stub return value not specified for safeEqualityCheck<Type: Equatable>( _ attribute: Attribute, _ otherSample: Sample, as: Type.Type ). Use given")
Failure("Stub return value not specified for safeEqualityCheck<Type: Equatable>( _ attribute: Attribute, _ otherSample: Sample, as: Type.Type ). Use given")
}
return __value
}
fileprivate enum StaticMethodType {
case p_name_get
case p_attributes_get
case p_defaultDependentAttribute_get
case p_defaultIndependentAttribute_get
case p_dateAttributes_get
static func compareParameters(lhs: StaticMethodType, rhs: StaticMethodType, matcher: Matcher) -> Matcher.ComparisonResult {
switch (lhs, rhs) { case (.p_name_get,.p_name_get): return Matcher.ComparisonResult.match
case (.p_attributes_get,.p_attributes_get): return Matcher.ComparisonResult.match
case (.p_defaultDependentAttribute_get,.p_defaultDependentAttribute_get): return Matcher.ComparisonResult.match
case (.p_defaultIndependentAttribute_get,.p_defaultIndependentAttribute_get): return Matcher.ComparisonResult.match
case (.p_dateAttributes_get,.p_dateAttributes_get): return Matcher.ComparisonResult.match
default: return .none
}
}
func intValue() -> Int {
switch self {
case .p_name_get: return 0
case .p_attributes_get: return 0
case .p_defaultDependentAttribute_get: return 0
case .p_defaultIndependentAttribute_get: return 0
case .p_dateAttributes_get: return 0
}
}
func assertionName() -> String {
switch self {
case .p_name_get: return "[get] .name"
case .p_attributes_get: return "[get] .attributes"
case .p_defaultDependentAttribute_get: return "[get] .defaultDependentAttribute"
case .p_defaultIndependentAttribute_get: return "[get] .defaultIndependentAttribute"
case .p_dateAttributes_get: return "[get] .dateAttributes"
}
}
}
open class StaticGiven: StubbedMethod {
fileprivate var method: StaticMethodType
private init(method: StaticMethodType, products: [StubProduct]) {
self.method = method
super.init(products)
}
public static func name(getter defaultValue: String...) -> StaticPropertyStub {
return StaticGiven(method: .p_name_get, products: defaultValue.map({ StubProduct.return($0 as Any) }))
}
public static func attributes(getter defaultValue: [Attribute]...) -> StaticPropertyStub {
return StaticGiven(method: .p_attributes_get, products: defaultValue.map({ StubProduct.return($0 as Any) }))
}
public static func defaultDependentAttribute(getter defaultValue: Attribute...) -> StaticPropertyStub {
return StaticGiven(method: .p_defaultDependentAttribute_get, products: defaultValue.map({ StubProduct.return($0 as Any) }))
}
public static func defaultIndependentAttribute(getter defaultValue: Attribute...) -> StaticPropertyStub {
return StaticGiven(method: .p_defaultIndependentAttribute_get, products: defaultValue.map({ StubProduct.return($0 as Any) }))
}
public static func dateAttributes(getter defaultValue: [DateType: DateAttribute]...) -> StaticPropertyStub {
return StaticGiven(method: .p_dateAttributes_get, products: defaultValue.map({ StubProduct.return($0 as Any) }))
}
}
public struct StaticVerify {
fileprivate var method: StaticMethodType
public static var name: StaticVerify { return StaticVerify(method: .p_name_get) }
public static var attributes: StaticVerify { return StaticVerify(method: .p_attributes_get) }
public static var defaultDependentAttribute: StaticVerify { return StaticVerify(method: .p_defaultDependentAttribute_get) }
public static var defaultIndependentAttribute: StaticVerify { return StaticVerify(method: .p_defaultIndependentAttribute_get) }
public static var dateAttributes: StaticVerify { return StaticVerify(method: .p_dateAttributes_get) }
}
public struct StaticPerform {
fileprivate var method: StaticMethodType
var performs: Any
}
fileprivate enum MethodType {
case m_graphableValue__of_attribute(Parameter<Attribute>)
case m_dates
case m_equalTo__otherSample(Parameter<Sample>)
case m_safeEqualityCheck__attribute_otherSampleas_as(Parameter<Attribute>, Parameter<Sample>, Parameter<GenericAttribute>)
static func compareParameters(lhs: MethodType, rhs: MethodType, matcher: Matcher) -> Matcher.ComparisonResult {
switch (lhs, rhs) {
case (.m_graphableValue__of_attribute(let lhsAttribute), .m_graphableValue__of_attribute(let rhsAttribute)):
var noncapturingComparisons: [Bool] = []
var comparison: Bool
var results: [Matcher.ParameterComparisonResult] = []
if !isCapturing(lhsAttribute) && !isCapturing(rhsAttribute) {
comparison = Parameter.compare(lhs: lhsAttribute, rhs: rhsAttribute, with: matcher)
noncapturingComparisons.append(comparison)
results.append(Matcher.ParameterComparisonResult(comparison, lhsAttribute, rhsAttribute, "of attribute"))
}
if isCapturing(lhsAttribute) || isCapturing(rhsAttribute) {
comparison = Parameter.compare(lhs: lhsAttribute, rhs: rhsAttribute, with: matcher, nonCapturingParamsMatch: noncapturingComparisons.allSatisfy({$0}))
results.append(Matcher.ParameterComparisonResult(comparison, lhsAttribute, rhsAttribute, "of attribute"))
}
return Matcher.ComparisonResult(results)
case (.m_dates, .m_dates): return .match
case (.m_equalTo__otherSample(let lhsOthersample), .m_equalTo__otherSample(let rhsOthersample)):
var noncapturingComparisons: [Bool] = []
var comparison: Bool
var results: [Matcher.ParameterComparisonResult] = []
if !isCapturing(lhsOthersample) && !isCapturing(rhsOthersample) {
comparison = Parameter.compare(lhs: lhsOthersample, rhs: rhsOthersample, with: matcher)
noncapturingComparisons.append(comparison)
results.append(Matcher.ParameterComparisonResult(comparison, lhsOthersample, rhsOthersample, "_ otherSample"))
}
if isCapturing(lhsOthersample) || isCapturing(rhsOthersample) {
comparison = Parameter.compare(lhs: lhsOthersample, rhs: rhsOthersample, with: matcher, nonCapturingParamsMatch: noncapturingComparisons.allSatisfy({$0}))
results.append(Matcher.ParameterComparisonResult(comparison, lhsOthersample, rhsOthersample, "_ otherSample"))
}
return Matcher.ComparisonResult(results)
case (.m_safeEqualityCheck__attribute_otherSampleas_as(let lhsAttribute, let lhsOthersample, let lhsAs), .m_safeEqualityCheck__attribute_otherSampleas_as(let rhsAttribute, let rhsOthersample, let rhsAs)):
var noncapturingComparisons: [Bool] = []
var comparison: Bool
var results: [Matcher.ParameterComparisonResult] = []
if !isCapturing(lhsAttribute) && !isCapturing(rhsAttribute) {
comparison = Parameter.compare(lhs: lhsAttribute, rhs: rhsAttribute, with: matcher)
noncapturingComparisons.append(comparison)
results.append(Matcher.ParameterComparisonResult(comparison, lhsAttribute, rhsAttribute, "_ attribute"))
}
if !isCapturing(lhsOthersample) && !isCapturing(rhsOthersample) {
comparison = Parameter.compare(lhs: lhsOthersample, rhs: rhsOthersample, with: matcher)
noncapturingComparisons.append(comparison)
results.append(Matcher.ParameterComparisonResult(comparison, lhsOthersample, rhsOthersample, "_ otherSample"))
}
if !isCapturing(lhsAs) && !isCapturing(rhsAs) {
comparison = Parameter.compare(lhs: lhsAs, rhs: rhsAs, with: matcher)
noncapturingComparisons.append(comparison)
results.append(Matcher.ParameterComparisonResult(comparison, lhsAs, rhsAs, "as"))
}
if isCapturing(lhsAttribute) || isCapturing(rhsAttribute) {
comparison = Parameter.compare(lhs: lhsAttribute, rhs: rhsAttribute, with: matcher, nonCapturingParamsMatch: noncapturingComparisons.allSatisfy({$0}))
results.append(Matcher.ParameterComparisonResult(comparison, lhsAttribute, rhsAttribute, "_ attribute"))
}
if isCapturing(lhsOthersample) || isCapturing(rhsOthersample) {
comparison = Parameter.compare(lhs: lhsOthersample, rhs: rhsOthersample, with: matcher, nonCapturingParamsMatch: noncapturingComparisons.allSatisfy({$0}))
results.append(Matcher.ParameterComparisonResult(comparison, lhsOthersample, rhsOthersample, "_ otherSample"))
}
if isCapturing(lhsAs) || isCapturing(rhsAs) {
comparison = Parameter.compare(lhs: lhsAs, rhs: rhsAs, with: matcher, nonCapturingParamsMatch: noncapturingComparisons.allSatisfy({$0}))
results.append(Matcher.ParameterComparisonResult(comparison, lhsAs, rhsAs, "as"))
}
return Matcher.ComparisonResult(results)
default: return .none
}
}
func intValue() -> Int {
switch self {
case let .m_graphableValue__of_attribute(p0): return p0.intValue
case .m_dates: return 0
case let .m_equalTo__otherSample(p0): return p0.intValue
case let .m_safeEqualityCheck__attribute_otherSampleas_as(p0, p1, p2): return p0.intValue + p1.intValue + p2.intValue
}
}
func assertionName() -> String {
switch self {
case .m_graphableValue__of_attribute: return ".graphableValue(of:)"
case .m_dates: return ".dates()"
case .m_equalTo__otherSample: return ".equalTo(_:)"
case .m_safeEqualityCheck__attribute_otherSampleas_as: return ".safeEqualityCheck(_:_:as:)"
}
}
}
open class Given: StubbedMethod {
fileprivate var method: MethodType
private init(method: MethodType, products: [StubProduct]) {
self.method = method
super.init(products)
}
public static func graphableValue(of attribute: Parameter<Attribute>, willReturn: Any?...) -> MethodStub {
return Given(method: .m_graphableValue__of_attribute(`attribute`), products: willReturn.map({ StubProduct.return($0 as Any) }))
}
public static func dates(willReturn: [DateType: Date]...) -> MethodStub {
return Given(method: .m_dates, products: willReturn.map({ StubProduct.return($0 as Any) }))
}
public static func equalTo(_ otherSample: Parameter<Sample>, willReturn: Bool...) -> MethodStub {
return Given(method: .m_equalTo__otherSample(`otherSample`), products: willReturn.map({ StubProduct.return($0 as Any) }))
}
public static func safeEqualityCheck<Type: Equatable>(_ attribute: Parameter<Attribute>, _ otherSample: Parameter<Sample>, as: Parameter<Type.Type>, willReturn: Bool...) -> MethodStub {
return Given(method: .m_safeEqualityCheck__attribute_otherSampleas_as(`attribute`, `otherSample`, `as`.wrapAsGeneric()), products: willReturn.map({ StubProduct.return($0 as Any) }))
}
public static func dates(willProduce: (Stubber<[DateType: Date]>) -> Void) -> MethodStub {
let willReturn: [[DateType: Date]] = []
let given: Given = { return Given(method: .m_dates, products: willReturn.map({ StubProduct.return($0 as Any) })) }()
let stubber = given.stub(for: ([DateType: Date]).self)
willProduce(stubber)
return given
}
public static func equalTo(_ otherSample: Parameter<Sample>, willProduce: (Stubber<Bool>) -> Void) -> MethodStub {
let willReturn: [Bool] = []
let given: Given = { return Given(method: .m_equalTo__otherSample(`otherSample`), products: willReturn.map({ StubProduct.return($0 as Any) })) }()
let stubber = given.stub(for: (Bool).self)
willProduce(stubber)
return given
}
public static func safeEqualityCheck<Type: Equatable>(_ attribute: Parameter<Attribute>, _ otherSample: Parameter<Sample>, as: Parameter<Type.Type>, willProduce: (Stubber<Bool>) -> Void) -> MethodStub {
let willReturn: [Bool] = []
let given: Given = { return Given(method: .m_safeEqualityCheck__attribute_otherSampleas_as(`attribute`, `otherSample`, `as`.wrapAsGeneric()), products: willReturn.map({ StubProduct.return($0 as Any) })) }()
let stubber = given.stub(for: (Bool).self)
willProduce(stubber)
return given
}
public static func graphableValue(of attribute: Parameter<Attribute>, willThrow: Error...) -> MethodStub {
return Given(method: .m_graphableValue__of_attribute(`attribute`), products: willThrow.map({ StubProduct.throw($0) }))
}
public static func graphableValue(of attribute: Parameter<Attribute>, willProduce: (StubberThrows<Any?>) -> Void) -> MethodStub {
let willThrow: [Error] = []
let given: Given = { return Given(method: .m_graphableValue__of_attribute(`attribute`), products: willThrow.map({ StubProduct.throw($0) })) }()
let stubber = given.stubThrows(for: (Any?).self)
willProduce(stubber)
return given
}
}
public struct Verify {
fileprivate var method: MethodType
public static func graphableValue(of attribute: Parameter<Attribute>) -> Verify { return Verify(method: .m_graphableValue__of_attribute(`attribute`))}
public static func dates() -> Verify { return Verify(method: .m_dates)}
public static func equalTo(_ otherSample: Parameter<Sample>) -> Verify { return Verify(method: .m_equalTo__otherSample(`otherSample`))}
public static func safeEqualityCheck<Type>(_ attribute: Parameter<Attribute>, _ otherSample: Parameter<Sample>, as: Parameter<Type.Type>) -> Verify where Type:Equatable { return Verify(method: .m_safeEqualityCheck__attribute_otherSampleas_as(`attribute`, `otherSample`, `as`.wrapAsGeneric()))}
}
public struct Perform {
fileprivate var method: MethodType
var performs: Any
public static func graphableValue(of attribute: Parameter<Attribute>, perform: @escaping (Attribute) -> Void) -> Perform {
return Perform(method: .m_graphableValue__of_attribute(`attribute`), performs: perform)
}
public static func dates(perform: @escaping () -> Void) -> Perform {
return Perform(method: .m_dates, performs: perform)
}
public static func equalTo(_ otherSample: Parameter<Sample>, perform: @escaping (Sample) -> Void) -> Perform {
return Perform(method: .m_equalTo__otherSample(`otherSample`), performs: perform)
}
public static func safeEqualityCheck<Type>(_ attribute: Parameter<Attribute>, _ otherSample: Parameter<Sample>, as: Parameter<Type.Type>, perform: @escaping (Attribute, Sample, Type.Type) -> Void) -> Perform where Type:Equatable {
return Perform(method: .m_safeEqualityCheck__attribute_otherSampleas_as(`attribute`, `otherSample`, `as`.wrapAsGeneric()), 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)
}
static public func given(_ method: StaticGiven) {
methodReturnValues.append(method)
}
static public func perform(_ method: StaticPerform) {
methodPerformValues.append(method)
methodPerformValues.sort { $0.method.intValue() < $1.method.intValue() }
}
static public func verify(_ method: StaticVerify, 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 StaticMethodType.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)
}
static private func addInvocation(_ call: StaticMethodType) {
invocations.append(call)
}
static private func methodReturnValue(_ method: StaticMethodType) throws -> StubProduct {
let candidates = sequencingPolicy.sorted(methodReturnValues, by: { $0.method.intValue() > $1.method.intValue() })
let matched = candidates.first(where: { $0.isValid && StaticMethodType.compareParameters(lhs: $0.method, rhs: method, matcher: matcher).isFullMatch })
guard let product = matched?.getProduct(policy: self.stubbingPolicy) else { throw MockError.notStubed }
return product
}
static private func methodPerformValue(_ method: StaticMethodType) -> Any? {
let matched = methodPerformValues.reversed().first { StaticMethodType.compareParameters(lhs: $0.method, rhs: method, matcher: matcher).isFullMatch }
return matched?.performs
}
static private func matchingCalls(_ method: StaticMethodType, file: StaticString?, line: UInt?) -> [StaticMethodType] {
matcher.set(file: file, line: line)
defer { matcher.clearFileAndLine() }
return invocations.filter { StaticMethodType.compareParameters(lhs: $0, rhs: method, matcher: matcher).isFullMatch }
}
static private func matchingCalls(_ method: StaticVerify, file: StaticString?, line: UInt?) -> Int {
return matchingCalls(method.method, file: file, line: line).count
}
static private func givenGetterValue<T>(_ method: StaticMethodType, _ message: String) -> T {
do {
return try methodReturnValue(method).casted()
} catch {
Failure(message)
}
}
static private func optionalGivenGetterValue<T>(_ method: StaticMethodType, _ message: String) -> T? {
do {
return try methodReturnValue(method).casted()
} catch {
return nil
}
}
// sourcery:end
}