@@ -11985,173 +11985,6 @@ open class TestAutoImportMock: TestAutoImport, Mock {
11985
11985
}
11986
11986
}
11987
11987
11988
- // MARK: - URLSessionDelegateExt
11989
- open class URLSessionDelegateExtMock: NSObject, URLSessionDelegateExt, Mock {
11990
- init(sequencing sequencingPolicy: SequencingPolicy = .lastWrittenResolvedFirst, stubbing stubbingPolicy: StubbingPolicy = .wrap, file: StaticString = #file, line: UInt = #line) {
11991
- SwiftyMockyTestObserver.setup()
11992
- self.sequencingPolicy = sequencingPolicy
11993
- self.stubbingPolicy = stubbingPolicy
11994
- self.file = file
11995
- self.line = line
11996
- }
11997
-
11998
- var matcher: Matcher = Matcher.default
11999
- var stubbingPolicy: StubbingPolicy = .wrap
12000
- var sequencingPolicy: SequencingPolicy = .lastWrittenResolvedFirst
12001
- private var invocations: [MethodType] = []
12002
- private var methodReturnValues: [Given] = []
12003
- private var methodPerformValues: [Perform] = []
12004
- private var file: StaticString?
12005
- private var line: UInt?
12006
-
12007
- public typealias PropertyStub = Given
12008
- public typealias MethodStub = Given
12009
- public typealias SubscriptStub = Given
12010
-
12011
- /// Convenience method - call setupMock() to extend debug information when failure occurs
12012
- public func setupMock(file: StaticString = #file, line: UInt = #line) {
12013
- self.file = file
12014
- self.line = line
12015
- }
12016
-
12017
- /// Clear mock internals. You can specify what to reset (invocations aka verify, givens or performs) or leave it empty to clear all mock internals
12018
- public func resetMock(_ scopes: MockScope...) {
12019
- let scopes: [MockScope] = scopes.isEmpty ? [.invocation, .given, .perform] : scopes
12020
- if scopes.contains(.invocation) { invocations = [] }
12021
- if scopes.contains(.given) { methodReturnValues = [] }
12022
- if scopes.contains(.perform) { methodPerformValues = [] }
12023
- }
12024
-
12025
-
12026
-
12027
-
12028
-
12029
- open func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
12030
- addInvocation(.m_urlSession__sessiondidBecomeInvalidWithError_error(Parameter<URLSession>.value(`session`), Parameter<Error?>.value(`error`)))
12031
- let perform = methodPerformValue(.m_urlSession__sessiondidBecomeInvalidWithError_error(Parameter<URLSession>.value(`session`), Parameter<Error?>.value(`error`))) as? (URLSession, Error?) -> Void
12032
- perform?(`session`, `error`)
12033
- }
12034
-
12035
- open func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
12036
- addInvocation(.m_urlSession__sessiondidReceive_challengecompletionHandler_completionHandler(Parameter<URLSession>.value(`session`), Parameter<URLAuthenticationChallenge>.value(`challenge`), Parameter<(URLSession.AuthChallengeDisposition, URLCredential?) -> Void>.value(`completionHandler`)))
12037
- let perform = methodPerformValue(.m_urlSession__sessiondidReceive_challengecompletionHandler_completionHandler(Parameter<URLSession>.value(`session`), Parameter<URLAuthenticationChallenge>.value(`challenge`), Parameter<(URLSession.AuthChallengeDisposition, URLCredential?) -> Void>.value(`completionHandler`))) as? (URLSession, URLAuthenticationChallenge, @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) -> Void
12038
- perform?(`session`, `challenge`, `completionHandler`)
12039
- }
12040
-
12041
-
12042
- fileprivate enum MethodType {
12043
- case m_urlSession__sessiondidBecomeInvalidWithError_error(Parameter<URLSession>, Parameter<Error?>)
12044
- case m_urlSession__sessiondidReceive_challengecompletionHandler_completionHandler(Parameter<URLSession>, Parameter<URLAuthenticationChallenge>, Parameter<(URLSession.AuthChallengeDisposition, URLCredential?) -> Void>)
12045
-
12046
- static func compareParameters(lhs: MethodType, rhs: MethodType, matcher: Matcher) -> Bool {
12047
- switch (lhs, rhs) {
12048
- case (.m_urlSession__sessiondidBecomeInvalidWithError_error(let lhsSession, let lhsError), .m_urlSession__sessiondidBecomeInvalidWithError_error(let rhsSession, let rhsError)):
12049
- guard Parameter.compare(lhs: lhsSession, rhs: rhsSession, with: matcher) else { return false }
12050
- guard Parameter.compare(lhs: lhsError, rhs: rhsError, with: matcher) else { return false }
12051
- return true
12052
- case (.m_urlSession__sessiondidReceive_challengecompletionHandler_completionHandler(let lhsSession, let lhsChallenge, let lhsCompletionhandler), .m_urlSession__sessiondidReceive_challengecompletionHandler_completionHandler(let rhsSession, let rhsChallenge, let rhsCompletionhandler)):
12053
- guard Parameter.compare(lhs: lhsSession, rhs: rhsSession, with: matcher) else { return false }
12054
- guard Parameter.compare(lhs: lhsChallenge, rhs: rhsChallenge, with: matcher) else { return false }
12055
- guard Parameter.compare(lhs: lhsCompletionhandler, rhs: rhsCompletionhandler, with: matcher) else { return false }
12056
- return true
12057
- default: return false
12058
- }
12059
- }
12060
-
12061
- func intValue() -> Int {
12062
- switch self {
12063
- case let .m_urlSession__sessiondidBecomeInvalidWithError_error(p0, p1): return p0.intValue + p1.intValue
12064
- case let .m_urlSession__sessiondidReceive_challengecompletionHandler_completionHandler(p0, p1, p2): return p0.intValue + p1.intValue + p2.intValue
12065
- }
12066
- }
12067
- }
12068
-
12069
- open class Given: StubbedMethod {
12070
- fileprivate var method: MethodType
12071
-
12072
- private init(method: MethodType, products: [StubProduct]) {
12073
- self.method = method
12074
- super.init(products)
12075
- }
12076
-
12077
-
12078
- }
12079
-
12080
- public struct Verify {
12081
- fileprivate var method: MethodType
12082
-
12083
- public static func urlSession(_ session: Parameter<URLSession>, didBecomeInvalidWithError error: Parameter<Error?>) -> Verify { return Verify(method: .m_urlSession__sessiondidBecomeInvalidWithError_error(`session`, `error`))}
12084
- public static func urlSession(_ session: Parameter<URLSession>, didReceive challenge: Parameter<URLAuthenticationChallenge>, completionHandler: Parameter<(URLSession.AuthChallengeDisposition, URLCredential?) -> Void>) -> Verify { return Verify(method: .m_urlSession__sessiondidReceive_challengecompletionHandler_completionHandler(`session`, `challenge`, `completionHandler`))}
12085
- }
12086
-
12087
- public struct Perform {
12088
- fileprivate var method: MethodType
12089
- var performs: Any
12090
-
12091
- public static func urlSession(_ session: Parameter<URLSession>, didBecomeInvalidWithError error: Parameter<Error?>, perform: @escaping (URLSession, Error?) -> Void) -> Perform {
12092
- return Perform(method: .m_urlSession__sessiondidBecomeInvalidWithError_error(`session`, `error`), performs: perform)
12093
- }
12094
- public static func urlSession(_ session: Parameter<URLSession>, didReceive challenge: Parameter<URLAuthenticationChallenge>, completionHandler: Parameter<(URLSession.AuthChallengeDisposition, URLCredential?) -> Void>, perform: @escaping (URLSession, URLAuthenticationChallenge, @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) -> Void) -> Perform {
12095
- return Perform(method: .m_urlSession__sessiondidReceive_challengecompletionHandler_completionHandler(`session`, `challenge`, `completionHandler`), performs: perform)
12096
- }
12097
- }
12098
-
12099
- public func given(_ method: Given) {
12100
- methodReturnValues.append(method)
12101
- }
12102
-
12103
- public func perform(_ method: Perform) {
12104
- methodPerformValues.append(method)
12105
- methodPerformValues.sort { $0.method.intValue() < $1.method.intValue() }
12106
- }
12107
-
12108
- public func verify(_ method: Verify, count: Count = Count.moreOrEqual(to: 1), file: StaticString = #file, line: UInt = #line) {
12109
- let invocations = matchingCalls(method.method)
12110
- MockyAssert(count.matches(invocations.count), "Expected: \(count) invocations of `\(method.method)`, but was: \(invocations.count)", file: file, line: line)
12111
- }
12112
-
12113
- private func addInvocation(_ call: MethodType) {
12114
- invocations.append(call)
12115
- }
12116
- private func methodReturnValue(_ method: MethodType) throws -> StubProduct {
12117
- let candidates = sequencingPolicy.sorted(methodReturnValues, by: { $0.method.intValue() > $1.method.intValue() })
12118
- let matched = candidates.first(where: { $0.isValid && MethodType.compareParameters(lhs: $0.method, rhs: method, matcher: matcher) })
12119
- guard let product = matched?.getProduct(policy: self.stubbingPolicy) else { throw MockError.notStubed }
12120
- return product
12121
- }
12122
- private func methodPerformValue(_ method: MethodType) -> Any? {
12123
- let matched = methodPerformValues.reversed().first { MethodType.compareParameters(lhs: $0.method, rhs: method, matcher: matcher) }
12124
- return matched?.performs
12125
- }
12126
- private func matchingCalls(_ method: MethodType) -> [MethodType] {
12127
- return invocations.filter { MethodType.compareParameters(lhs: $0, rhs: method, matcher: matcher) }
12128
- }
12129
- private func matchingCalls(_ method: Verify) -> Int {
12130
- return matchingCalls(method.method).count
12131
- }
12132
- private func givenGetterValue<T>(_ method: MethodType, _ message: String) -> T {
12133
- do {
12134
- return try methodReturnValue(method).casted()
12135
- } catch {
12136
- onFatalFailure(message)
12137
- Failure(message)
12138
- }
12139
- }
12140
- private func optionalGivenGetterValue<T>(_ method: MethodType, _ message: String) -> T? {
12141
- do {
12142
- return try methodReturnValue(method).casted()
12143
- } catch {
12144
- return nil
12145
- }
12146
- }
12147
- private func onFatalFailure(_ message: String) {
12148
- #if Mocky
12149
- guard let file = self.file, let line = self.line else { return } // Let if fail if cannot handle gratefully
12150
- SwiftyMockyTestObserver.handleMissingStubError(message: message, file: file, line: line)
12151
- #endif
12152
- }
12153
- }
12154
-
12155
11988
// MARK: - UserNetworkType
12156
11989
open class UserNetworkTypeMock: UserNetworkType, Mock {
12157
11990
init(sequencing sequencingPolicy: SequencingPolicy = .lastWrittenResolvedFirst, stubbing stubbingPolicy: StubbingPolicy = .wrap, file: StaticString = #file, line: UInt = #line) {
0 commit comments