Auto-Mocking with Sourcery (#597)
* work in progress, was able to generate a mock for the RoomProxyProtocol, I'll try if I can swap the mock we have with this one * removing swiftformat from generated * added the disable of swiftlint directly into the .stencil * testing if danger still complains * improved the stencil * session verification controller proxy using auto mockable * BugReport mocks and tests added * changelog * fixing a typo Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com> * fix typo in the test Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com> * removing the Protocol word from the type if present * using extension in place of a subclass * removed unused imports * improved the yielding code * moved Sourcery files * stencil master --------- Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com>pull/633/head
parent
7d35876753
commit
c67c4e362e
@ -0,0 +1,143 @@
|
||||
// Generated using Sourcery 2.0.1 — https://github.com/krzysztofzablocki/Sourcery
|
||||
// DO NOT EDIT
|
||||
|
||||
// swiftlint:disable all
|
||||
|
||||
import Combine
|
||||
import MatrixRustSDK
|
||||
class BugReportServiceMock: BugReportServiceProtocol {
|
||||
var crashedLastRun: Bool {
|
||||
get { return underlyingCrashedLastRun }
|
||||
set(value) { underlyingCrashedLastRun = value }
|
||||
}
|
||||
var underlyingCrashedLastRun: Bool!
|
||||
// MARK: - crash
|
||||
|
||||
var crashCallsCount = 0
|
||||
var crashCalled: Bool {
|
||||
return crashCallsCount > 0
|
||||
}
|
||||
var crashClosure: (() -> Void)?
|
||||
func crash() {
|
||||
crashCallsCount += 1
|
||||
crashClosure?()
|
||||
}
|
||||
// MARK: - submitBugReport
|
||||
|
||||
var submitBugReportProgressListenerThrowableError: Error?
|
||||
var submitBugReportProgressListenerCallsCount = 0
|
||||
var submitBugReportProgressListenerCalled: Bool {
|
||||
return submitBugReportProgressListenerCallsCount > 0
|
||||
}
|
||||
var submitBugReportProgressListenerReceivedArguments: (bugReport: BugReport, progressListener: ProgressListener?)?
|
||||
var submitBugReportProgressListenerReceivedInvocations: [(bugReport: BugReport, progressListener: ProgressListener?)] = []
|
||||
var submitBugReportProgressListenerReturnValue: SubmitBugReportResponse!
|
||||
var submitBugReportProgressListenerClosure: ((BugReport, ProgressListener?) async throws -> SubmitBugReportResponse)?
|
||||
func submitBugReport(_ bugReport: BugReport, progressListener: ProgressListener?) async throws -> SubmitBugReportResponse {
|
||||
if let error = submitBugReportProgressListenerThrowableError {
|
||||
throw error
|
||||
}
|
||||
submitBugReportProgressListenerCallsCount += 1
|
||||
submitBugReportProgressListenerReceivedArguments = (bugReport: bugReport, progressListener: progressListener)
|
||||
submitBugReportProgressListenerReceivedInvocations.append((bugReport: bugReport, progressListener: progressListener))
|
||||
if let submitBugReportProgressListenerClosure = submitBugReportProgressListenerClosure {
|
||||
return try await submitBugReportProgressListenerClosure(bugReport, progressListener)
|
||||
} else {
|
||||
return submitBugReportProgressListenerReturnValue
|
||||
}
|
||||
}
|
||||
}
|
||||
class SessionVerificationControllerProxyMock: SessionVerificationControllerProxyProtocol {
|
||||
var callbacks: PassthroughSubject<SessionVerificationControllerProxyCallback, Never> {
|
||||
get { return underlyingCallbacks }
|
||||
set(value) { underlyingCallbacks = value }
|
||||
}
|
||||
var underlyingCallbacks: PassthroughSubject<SessionVerificationControllerProxyCallback, Never>!
|
||||
var isVerified: Bool {
|
||||
get { return underlyingIsVerified }
|
||||
set(value) { underlyingIsVerified = value }
|
||||
}
|
||||
var underlyingIsVerified: Bool!
|
||||
// MARK: - requestVerification
|
||||
|
||||
var requestVerificationCallsCount = 0
|
||||
var requestVerificationCalled: Bool {
|
||||
return requestVerificationCallsCount > 0
|
||||
}
|
||||
var requestVerificationReturnValue: Result<Void, SessionVerificationControllerProxyError>!
|
||||
var requestVerificationClosure: (() async -> Result<Void, SessionVerificationControllerProxyError>)?
|
||||
func requestVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
|
||||
requestVerificationCallsCount += 1
|
||||
if let requestVerificationClosure = requestVerificationClosure {
|
||||
return await requestVerificationClosure()
|
||||
} else {
|
||||
return requestVerificationReturnValue
|
||||
}
|
||||
}
|
||||
// MARK: - startSasVerification
|
||||
|
||||
var startSasVerificationCallsCount = 0
|
||||
var startSasVerificationCalled: Bool {
|
||||
return startSasVerificationCallsCount > 0
|
||||
}
|
||||
var startSasVerificationReturnValue: Result<Void, SessionVerificationControllerProxyError>!
|
||||
var startSasVerificationClosure: (() async -> Result<Void, SessionVerificationControllerProxyError>)?
|
||||
func startSasVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
|
||||
startSasVerificationCallsCount += 1
|
||||
if let startSasVerificationClosure = startSasVerificationClosure {
|
||||
return await startSasVerificationClosure()
|
||||
} else {
|
||||
return startSasVerificationReturnValue
|
||||
}
|
||||
}
|
||||
// MARK: - approveVerification
|
||||
|
||||
var approveVerificationCallsCount = 0
|
||||
var approveVerificationCalled: Bool {
|
||||
return approveVerificationCallsCount > 0
|
||||
}
|
||||
var approveVerificationReturnValue: Result<Void, SessionVerificationControllerProxyError>!
|
||||
var approveVerificationClosure: (() async -> Result<Void, SessionVerificationControllerProxyError>)?
|
||||
func approveVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
|
||||
approveVerificationCallsCount += 1
|
||||
if let approveVerificationClosure = approveVerificationClosure {
|
||||
return await approveVerificationClosure()
|
||||
} else {
|
||||
return approveVerificationReturnValue
|
||||
}
|
||||
}
|
||||
// MARK: - declineVerification
|
||||
|
||||
var declineVerificationCallsCount = 0
|
||||
var declineVerificationCalled: Bool {
|
||||
return declineVerificationCallsCount > 0
|
||||
}
|
||||
var declineVerificationReturnValue: Result<Void, SessionVerificationControllerProxyError>!
|
||||
var declineVerificationClosure: (() async -> Result<Void, SessionVerificationControllerProxyError>)?
|
||||
func declineVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
|
||||
declineVerificationCallsCount += 1
|
||||
if let declineVerificationClosure = declineVerificationClosure {
|
||||
return await declineVerificationClosure()
|
||||
} else {
|
||||
return declineVerificationReturnValue
|
||||
}
|
||||
}
|
||||
// MARK: - cancelVerification
|
||||
|
||||
var cancelVerificationCallsCount = 0
|
||||
var cancelVerificationCalled: Bool {
|
||||
return cancelVerificationCallsCount > 0
|
||||
}
|
||||
var cancelVerificationReturnValue: Result<Void, SessionVerificationControllerProxyError>!
|
||||
var cancelVerificationClosure: (() async -> Result<Void, SessionVerificationControllerProxyError>)?
|
||||
func cancelVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
|
||||
cancelVerificationCallsCount += 1
|
||||
if let cancelVerificationClosure = cancelVerificationClosure {
|
||||
return await cancelVerificationClosure()
|
||||
} else {
|
||||
return cancelVerificationReturnValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// swiftlint:enable all
|
@ -0,0 +1,87 @@
|
||||
//
|
||||
// Copyright 2023 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Combine
|
||||
|
||||
extension SessionVerificationControllerProxyMock {
|
||||
static let emojis = [SessionVerificationEmoji(symbol: "🦋", description: "Butterfly"),
|
||||
SessionVerificationEmoji(symbol: "🐘", description: "Elephant"),
|
||||
SessionVerificationEmoji(symbol: "🦋", description: "Butterfly"),
|
||||
SessionVerificationEmoji(symbol: "🎂", description: "Cake"),
|
||||
SessionVerificationEmoji(symbol: "🎂", description: "Cake"),
|
||||
SessionVerificationEmoji(symbol: "🏁", description: "Flag"),
|
||||
SessionVerificationEmoji(symbol: "🌏", description: "Globe")]
|
||||
|
||||
static func configureMock(callbacks: PassthroughSubject<SessionVerificationControllerProxyCallback, Never> = .init(),
|
||||
isVerified: Bool = false,
|
||||
requestDelay: Duration = .seconds(1)) -> SessionVerificationControllerProxyMock {
|
||||
let mock = SessionVerificationControllerProxyMock()
|
||||
mock.underlyingCallbacks = callbacks
|
||||
mock.underlyingIsVerified = isVerified
|
||||
|
||||
mock.requestVerificationClosure = { [unowned mock] in
|
||||
Task.detached {
|
||||
try await Task.sleep(for: requestDelay)
|
||||
mock.callbacks.send(.acceptedVerificationRequest)
|
||||
}
|
||||
|
||||
return .success(())
|
||||
}
|
||||
|
||||
mock.startSasVerificationClosure = { [unowned mock] in
|
||||
Task.detached {
|
||||
try await Task.sleep(for: requestDelay)
|
||||
mock.callbacks.send(.startedSasVerification)
|
||||
|
||||
Task.detached {
|
||||
try await Task.sleep(for: requestDelay)
|
||||
mock.callbacks.send(.receivedVerificationData(Self.emojis))
|
||||
}
|
||||
}
|
||||
|
||||
return .success(())
|
||||
}
|
||||
|
||||
mock.approveVerificationClosure = { [unowned mock] in
|
||||
Task.detached {
|
||||
try await Task.sleep(for: requestDelay)
|
||||
mock.callbacks.send(.finished)
|
||||
}
|
||||
|
||||
return .success(())
|
||||
}
|
||||
|
||||
mock.declineVerificationClosure = { [unowned mock] in
|
||||
Task.detached {
|
||||
try await Task.sleep(for: requestDelay)
|
||||
mock.callbacks.send(.cancelled)
|
||||
}
|
||||
|
||||
return .success(())
|
||||
}
|
||||
|
||||
mock.cancelVerificationClosure = { [unowned mock] in
|
||||
Task.detached {
|
||||
try await Task.sleep(for: requestDelay)
|
||||
mock.callbacks.send(.cancelled)
|
||||
}
|
||||
|
||||
return .success(())
|
||||
}
|
||||
|
||||
return mock
|
||||
}
|
||||
}
|
@ -1,90 +0,0 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import Foundation
|
||||
import MatrixRustSDK
|
||||
|
||||
struct MockSessionVerificationControllerProxy: SessionVerificationControllerProxyProtocol {
|
||||
var callbacks = PassthroughSubject<SessionVerificationControllerProxyCallback, Never>()
|
||||
|
||||
var isVerified = false
|
||||
var requestDelay: Duration = .seconds(1)
|
||||
|
||||
func requestVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
|
||||
Task.detached {
|
||||
try await Task.sleep(for: requestDelay)
|
||||
callbacks.send(.acceptedVerificationRequest)
|
||||
}
|
||||
|
||||
return .success(())
|
||||
}
|
||||
|
||||
func startSasVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
|
||||
Task.detached {
|
||||
try await Task.sleep(for: requestDelay)
|
||||
callbacks.send(.startedSasVerification)
|
||||
|
||||
Task.detached {
|
||||
try await Task.sleep(for: requestDelay)
|
||||
callbacks.send(.receivedVerificationData(Self.emojis))
|
||||
}
|
||||
}
|
||||
|
||||
return .success(())
|
||||
}
|
||||
|
||||
func approveVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
|
||||
Task.detached {
|
||||
try await Task.sleep(for: requestDelay)
|
||||
callbacks.send(.finished)
|
||||
}
|
||||
|
||||
return .success(())
|
||||
}
|
||||
|
||||
func declineVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
|
||||
Task.detached {
|
||||
try await Task.sleep(for: requestDelay)
|
||||
callbacks.send(.cancelled)
|
||||
}
|
||||
|
||||
return .success(())
|
||||
}
|
||||
|
||||
func cancelVerification() async -> Result<Void, SessionVerificationControllerProxyError> {
|
||||
Task.detached {
|
||||
try await Task.sleep(for: requestDelay)
|
||||
callbacks.send(.cancelled)
|
||||
}
|
||||
|
||||
return .success(())
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
static var emojis: [SessionVerificationEmoji] {
|
||||
[
|
||||
SessionVerificationEmoji(symbol: "🦋", description: "Butterfly"),
|
||||
SessionVerificationEmoji(symbol: "🐘", description: "Elephant"),
|
||||
SessionVerificationEmoji(symbol: "🦋", description: "Butterfly"),
|
||||
SessionVerificationEmoji(symbol: "🎂", description: "Cake"),
|
||||
SessionVerificationEmoji(symbol: "🎂", description: "Cake"),
|
||||
SessionVerificationEmoji(symbol: "🏁", description: "Flag"),
|
||||
SessionVerificationEmoji(symbol: "🌏", description: "Globe")
|
||||
]
|
||||
}
|
||||
}
|
@ -0,0 +1,190 @@
|
||||
// swiftlint:disable all
|
||||
|
||||
{% for import in argument.autoMockableImports %}
|
||||
import {{ import }}
|
||||
{% endfor %}
|
||||
{% for import in argument.autoMockableTestableImports %}
|
||||
@testable import {{ import }}
|
||||
{% endfor %}
|
||||
{% macro swiftifyMethodName name %}{{ name | replace:"(","_" | replace:")","" | replace:":","_" | replace:"`","" | snakeToCamelCase | lowerFirstWord }}{% endmacro %}
|
||||
{% macro accessLevel level %}{% if level != 'internal' %}{{ level }} {% endif %}{% endmacro %}
|
||||
{% macro staticSpecifier method %}{% if method.isStatic and not method.isInitializer %}static {% endif %}{% endmacro %}
|
||||
{% macro methodThrowableErrorDeclaration method %}
|
||||
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ThrowableError: Error?
|
||||
{% endmacro %}
|
||||
{% macro methodThrowableErrorUsage method %}
|
||||
if let error = {% call swiftifyMethodName method.selectorName %}ThrowableError {
|
||||
throw error
|
||||
}
|
||||
{% endmacro %}
|
||||
{% macro methodReceivedParameters method %}
|
||||
{% set hasNonEscapingClosures %}
|
||||
{%- for param in method.parameters where param.isClosure and not param.typeAttributes.escaping %}
|
||||
{{ true }}
|
||||
{% endfor -%}
|
||||
{% endset %}
|
||||
{% if method.parameters.count == 1 and not hasNonEscapingClosures %}
|
||||
{% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name|upperFirstLetter }} = {{ param.name }}{% endfor %}
|
||||
{% call swiftifyMethodName method.selectorName %}ReceivedInvocations.append({% for param in method.parameters %}{{ param.name }}){% endfor %}
|
||||
{% else %}
|
||||
{% if not method.parameters.count == 0 and not hasNonEscapingClosures %}
|
||||
{% call swiftifyMethodName method.selectorName %}ReceivedArguments = ({% for param in method.parameters %}{{ param.name }}: {{ param.name }}{% if not forloop.last%}, {% endif %}{% endfor %})
|
||||
{% call swiftifyMethodName method.selectorName %}ReceivedInvocations.append(({% for param in method.parameters %}{{ param.name }}: {{ param.name }}{% if not forloop.last%}, {% endif %}{% endfor %}))
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
{% macro methodClosureName method %}{% call swiftifyMethodName method.selectorName %}Closure{% endmacro %}
|
||||
{% macro closureReturnTypeName method %}{% if method.isOptionalReturnType %}{{ method.unwrappedReturnTypeName }}?{% else %}{{ method.returnTypeName }}{% endif %}{% endmacro %}
|
||||
{% macro methodClosureDeclaration method %}
|
||||
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call methodClosureName method %}: (({% for param in method.parameters %}{{ param.typeName }}{% if not forloop.last %}, {% endif %}{% endfor %}) {% if method.isAsync %}async {% endif %}{% if method.throws %}throws {% endif %}-> {% if method.isInitializer %}Void{% else %}{% call closureReturnTypeName method %}{% endif %})?
|
||||
{% endmacro %}
|
||||
{% macro methodClosureCallParameters method %}{% for param in method.parameters %}{{ param.name }}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %}
|
||||
{% macro mockMethod method %}
|
||||
// MARK: - {{ method.shortName }}
|
||||
|
||||
{% if method.throws %}
|
||||
{% call methodThrowableErrorDeclaration method %}
|
||||
{% endif %}
|
||||
{% if not method.isInitializer %}
|
||||
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}CallsCount = 0
|
||||
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}Called: Bool {
|
||||
return {% call swiftifyMethodName method.selectorName %}CallsCount > 0
|
||||
}
|
||||
{% endif %}
|
||||
{% set hasNonEscapingClosures %}
|
||||
{%- for param in method.parameters where param.isClosure and not param.typeAttributes.escaping %}
|
||||
{{ true }}
|
||||
{% endfor -%}
|
||||
{% endset %}
|
||||
{% if method.parameters.count == 1 and not hasNonEscapingClosures %}
|
||||
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name|upperFirstLetter }}: {{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}?{% endfor %}
|
||||
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations{% for param in method.parameters %}: [{{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}{%if param.typeName.isOptional%}?{%endif%}]{% endfor %} = []
|
||||
{% elif not method.parameters.count == 0 and not hasNonEscapingClosures %}
|
||||
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedArguments: ({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})?
|
||||
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations: [({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})] = []
|
||||
{% endif %}
|
||||
{% if not method.returnTypeName.isVoid and not method.isInitializer %}
|
||||
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReturnValue: {{ '(' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ method.returnTypeName }}{{ ')' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ '!' if not method.isOptionalReturnType }}
|
||||
{% endif %}
|
||||
{% call methodClosureDeclaration method %}
|
||||
{% if method.isInitializer %}
|
||||
{% call accessLevel method.accessLevel %}required {{ method.name }} {
|
||||
{% call methodReceivedParameters method %}
|
||||
{% call methodClosureName method %}?({% call methodClosureCallParameters method %})
|
||||
}
|
||||
{% else %}
|
||||
{% for name, attribute in method.attributes %}
|
||||
{% for value in attribute %}
|
||||
{{ value }}
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}func {{ method.name }}{{ ' async' if method.isAsync }}{{ ' throws' if method.throws }}{% if not method.returnTypeName.isVoid %} -> {{ method.returnTypeName }}{% endif %} {
|
||||
{% if method.throws %}
|
||||
{% call methodThrowableErrorUsage method %}
|
||||
{% endif %}
|
||||
{% call swiftifyMethodName method.selectorName %}CallsCount += 1
|
||||
{% call methodReceivedParameters method %}
|
||||
{% if method.returnTypeName.isVoid %}
|
||||
{% if method.throws %}try {% endif %}{% if method.isAsync %}await {% endif %}{% call methodClosureName method %}?({% call methodClosureCallParameters method %})
|
||||
{% else %}
|
||||
if let {% call methodClosureName method %} = {% call methodClosureName method %} {
|
||||
return {{ 'try ' if method.throws }}{{ 'await ' if method.isAsync }}{% call methodClosureName method %}({% call methodClosureCallParameters method %})
|
||||
} else {
|
||||
return {% call swiftifyMethodName method.selectorName %}ReturnValue
|
||||
}
|
||||
{% endif %}
|
||||
}
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
{% macro resetMethod method %}
|
||||
{# for type method which are mocked, a way to reset the invocation, argument, etc #}
|
||||
{% if method.isStatic and not method.isInitializer %} // MARK: - {{ method.shortName }}
|
||||
{% if not method.isInitializer %}
|
||||
{% call swiftifyMethodName method.selectorName %}CallsCount = 0
|
||||
{% endif %}
|
||||
{% if method.parameters.count == 1 %}
|
||||
{% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name|upperFirstLetter }}{% endfor %} = nil
|
||||
{% call swiftifyMethodName method.selectorName %}ReceivedInvocations = []
|
||||
{% elif not method.parameters.count == 0 %}
|
||||
{% call swiftifyMethodName method.selectorName %}ReceivedArguments = nil
|
||||
{% call swiftifyMethodName method.selectorName %}ReceivedInvocations = []
|
||||
{% endif %}
|
||||
{% call methodClosureName method %} = nil
|
||||
{% if method.throws %}
|
||||
{% call swiftifyMethodName method.selectorName %}ThrowableError = nil
|
||||
{% endif %}
|
||||
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
{% macro mockOptionalVariable variable %}
|
||||
{% call accessLevel variable.readAccess %}var {% call mockedVariableName variable %}: {{ variable.typeName }}
|
||||
{% endmacro %}
|
||||
{% macro mockNonOptionalArrayOrDictionaryVariable variable %}
|
||||
{% call accessLevel variable.readAccess %}var {% call mockedVariableName variable %}: {{ variable.typeName }} = {% if variable.isArray %}[]{% elif variable.isDictionary %}[:]{% endif %}
|
||||
{% endmacro %}
|
||||
{% macro mockNonOptionalVariable variable %}
|
||||
{% call accessLevel variable.readAccess %}var {% call mockedVariableName variable %}: {{ variable.typeName }} {
|
||||
get { return {% call underlyingMockedVariableName variable %} }
|
||||
set(value) { {% call underlyingMockedVariableName variable %} = value }
|
||||
}
|
||||
{% call accessLevel variable.readAccess %}var {% call underlyingMockedVariableName variable %}: {{ variable.typeName }}!
|
||||
{% endmacro %}
|
||||
{% macro variableThrowableErrorDeclaration variable %}
|
||||
{% call accessLevel variable.readAccess %}var {% call mockedVariableName variable %}ThrowableError: Error?
|
||||
{% endmacro %}
|
||||
{% macro variableThrowableErrorUsage variable %}
|
||||
if let error = {% call mockedVariableName variable %}ThrowableError {
|
||||
throw error
|
||||
}
|
||||
{% endmacro %}
|
||||
{% macro variableClosureDeclaration variable %}
|
||||
{% call accessLevel variable.readAccess %}var {% call variableClosureName variable %}: (() {% if variable.isAsync %}async {% endif %}{% if variable.throws %}throws {% endif %}-> {{ variable.typeName }})?
|
||||
{% endmacro %}
|
||||
{% macro variableClosureName variable %}{% call mockedVariableName variable %}Closure{% endmacro %}
|
||||
{% macro mockAsyncOrThrowingVariable variable %}
|
||||
{% call accessLevel variable.readAccess %}var {% call mockedVariableName variable %}CallsCount = 0
|
||||
{% call accessLevel variable.readAccess %}var {% call mockedVariableName variable %}Called: Bool {
|
||||
return {% call mockedVariableName variable %}CallsCount > 0
|
||||
}
|
||||
{% call accessLevel variable.readAccess %}var {% call mockedVariableName variable %}: {{ variable.typeName }} {
|
||||
get {% if variable.isAsync %}async {% endif %}{% if variable.throws %}throws {% endif %}{
|
||||
{% if variable.throws %}
|
||||
{% call variableThrowableErrorUsage variable %}
|
||||
{% endif %}
|
||||
{% call mockedVariableName variable %}CallsCount += 1
|
||||
if let {% call variableClosureName variable %} = {% call variableClosureName variable %} {
|
||||
return {{ 'try ' if variable.throws }}{{ 'await ' if variable.isAsync }}{% call variableClosureName variable %}()
|
||||
} else {
|
||||
return {% call underlyingMockedVariableName variable %}
|
||||
}
|
||||
}
|
||||
}
|
||||
{% call accessLevel variable.readAccess %}var {% call underlyingMockedVariableName variable %}: {{ variable.typeName }}{{ '!' if not variable.isOptional }}
|
||||
{% if variable.throws %}
|
||||
{% call variableThrowableErrorDeclaration variable %}
|
||||
{% endif %}
|
||||
{% call variableClosureDeclaration method %}
|
||||
{% endmacro %}
|
||||
{% macro underlyingMockedVariableName variable %}underlying{{ variable.name|upperFirstLetter }}{% endmacro %}
|
||||
{% macro mockedVariableName variable %}{{ variable.name }}{% endmacro %}
|
||||
{% for type in types.protocols where type.based.AutoMockable or type|annotated:"AutoMockable" %}{% if type.name != "AutoMockable" %}
|
||||
{% call accessLevel type.accessLevel %}class {{ type.name | replace:"Protocol","" }}Mock: {{ type.name }} {
|
||||
{% if type.accessLevel == "public" %}public init() {}{% endif %}
|
||||
{% for variable in type.allVariables|!definedInExtension %}
|
||||
{% if variable.isAsync or variable.throws %}{% call mockAsyncOrThrowingVariable variable %}{% elif variable.isOptional %}{% call mockOptionalVariable variable %}{% elif variable.isArray or variable.isDictionary %}{% call mockNonOptionalArrayOrDictionaryVariable variable %}{% else %}{% call mockNonOptionalVariable variable %}{% endif %}
|
||||
{% endfor %}
|
||||
{% if type.allMethods|static|count != 0 and type.allMethods|initializer|count != type.allMethods|static|count %}
|
||||
static func reset()
|
||||
{
|
||||
{% for method in type.allMethods|static %}
|
||||
{% call resetMethod method %}
|
||||
{% endfor %}
|
||||
}
|
||||
{% endif %}
|
||||
{% for method in type.allMethods|!definedInExtension %}
|
||||
{% call mockMethod method %}
|
||||
{% endfor %}
|
||||
}
|
||||
{% endif %}{% endfor %}
|
||||
|
||||
// swiftlint:enable all
|
@ -0,0 +1,9 @@
|
||||
sources:
|
||||
- ../../ElementX
|
||||
templates:
|
||||
- AutoMockable.stencil
|
||||
output:
|
||||
../../ElementX/Sources/Mocks/Generated/GeneratedMocks.swift
|
||||
args:
|
||||
automMockableTestableImports: []
|
||||
autoMockableImports: [Combine, MatrixRustSDK]
|
@ -0,0 +1 @@
|
||||
Auto Mocks generator added to the project.
|
Loading…
Reference in New Issue