Skip to content

Commit

Permalink
refactor: make mocks easier to use (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
levibostian authored Oct 15, 2021
1 parent c0eb273 commit 845f3af
Show file tree
Hide file tree
Showing 13 changed files with 374 additions and 49 deletions.
41 changes: 40 additions & 1 deletion Sources/MessagingPush/autogenerated/AutoMockable.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,56 @@ import CioTracking

*/

public class MessagingPushMocks {
public static var shared = MessagingPushMocks()

public var mocks: [MessagingPushMock] = []
private init() {}

func add(mock: MessagingPushMock) {
mocks.append(mock)
}

func resetAll() {
mocks.forEach {
$0.reset()
}
}
}

public protocol MessagingPushMock {
func reset()
}

/**
Class to easily create a mocked version of the `MessagingPushInstance` class.
This class is equipped with functions and properties ready for you to mock!

Note: This file is automatically generated. This means the mocks should always be up-to-date and has a consistent API.
See the SDK documentation to learn the basics behind using the mock classes in the SDK.
*/
public class MessagingPushInstanceMock: MessagingPushInstance {
public class MessagingPushInstanceMock: MessagingPushInstance, MessagingPushMock {
/// If *any* interactions done on mock. `true` if any method or property getter/setter called.
public var mockCalled: Bool = false //

init() {
MessagingPushMocks.shared.add(mock: self)
}

public func reset() {
mockCalled = false

registerDeviceTokenCallsCount = 0
registerDeviceTokenReceivedArguments = nil
registerDeviceTokenReceivedInvocations = []
deleteDeviceTokenCallsCount = 0
deleteDeviceTokenReceivedArguments = nil
deleteDeviceTokenReceivedInvocations = []
trackMetricCallsCount = 0
trackMetricReceivedArguments = nil
trackMetricReceivedInvocations = []
}

// MARK: - registerDeviceToken

/// Number of times the function was called.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,56 @@ import CioTracking

*/

public class MessagingPushAPNMocks {
public static var shared = MessagingPushAPNMocks()

public var mocks: [MessagingPushAPNMock] = []
private init() {}

func add(mock: MessagingPushAPNMock) {
mocks.append(mock)
}

func resetAll() {
mocks.forEach {
$0.reset()
}
}
}

public protocol MessagingPushAPNMock {
func reset()
}

/**
Class to easily create a mocked version of the `MessagingPushAPNInstance` class.
This class is equipped with functions and properties ready for you to mock!

Note: This file is automatically generated. This means the mocks should always be up-to-date and has a consistent API.
See the SDK documentation to learn the basics behind using the mock classes in the SDK.
*/
public class MessagingPushAPNInstanceMock: MessagingPushAPNInstance {
public class MessagingPushAPNInstanceMock: MessagingPushAPNInstance, MessagingPushAPNMock {
/// If *any* interactions done on mock. `true` if any method or property getter/setter called.
public var mockCalled: Bool = false //

init() {
MessagingPushAPNMocks.shared.add(mock: self)
}

public func reset() {
mockCalled = false

registerAPNDeviceTokenCallsCount = 0
registerAPNDeviceTokenReceivedArguments = nil
registerAPNDeviceTokenReceivedInvocations = []
didRegisterForRemoteNotificationsCallsCount = 0
didRegisterForRemoteNotificationsReceivedArguments = nil
didRegisterForRemoteNotificationsReceivedInvocations = []
didFailToRegisterForRemoteNotificationsCallsCount = 0
didFailToRegisterForRemoteNotificationsReceivedArguments = nil
didFailToRegisterForRemoteNotificationsReceivedInvocations = []
}

// MARK: - registerDeviceToken

/// Number of times the function was called.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,53 @@ import CioTracking

*/

public class MessagingPushFCMMocks {
public static var shared = MessagingPushFCMMocks()

public var mocks: [MessagingPushFCMMock] = []
private init() {}

func add(mock: MessagingPushFCMMock) {
mocks.append(mock)
}

func resetAll() {
mocks.forEach {
$0.reset()
}
}
}

public protocol MessagingPushFCMMock {
func reset()
}

/**
Class to easily create a mocked version of the `MessagingPushFCMInstance` class.
This class is equipped with functions and properties ready for you to mock!

Note: This file is automatically generated. This means the mocks should always be up-to-date and has a consistent API.
See the SDK documentation to learn the basics behind using the mock classes in the SDK.
*/
public class MessagingPushFCMInstanceMock: MessagingPushFCMInstance {
public class MessagingPushFCMInstanceMock: MessagingPushFCMInstance, MessagingPushFCMMock {
/// If *any* interactions done on mock. `true` if any method or property getter/setter called.
public var mockCalled: Bool = false //

init() {
MessagingPushFCMMocks.shared.add(mock: self)
}

public func reset() {
mockCalled = false

didReceiveRegistrationTokenCallsCount = 0
didReceiveRegistrationTokenReceivedArguments = nil
didReceiveRegistrationTokenReceivedInvocations = []
didFailToRegisterForRemoteNotificationsCallsCount = 0
didFailToRegisterForRemoteNotificationsReceivedArguments = nil
didFailToRegisterForRemoteNotificationsReceivedInvocations = []
}

// MARK: - messaging

/// Number of times the function was called.
Expand Down
49 changes: 48 additions & 1 deletion Sources/Templates/AutoMockable.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ class ExampleTest: XCTestCase {

*/

public class {{ argument.moduleName }}Mocks {
public static var shared: {{ argument.moduleName }}Mocks = {{ argument.moduleName }}Mocks()

public var mocks: [{{ argument.moduleName }}Mock] = []

private init() {}

func add(mock: {{ argument.moduleName }}Mock) {
self.mocks.append(mock)
}

func resetAll() {
self.mocks.forEach {
$0.reset()
}
}
}

public protocol {{ argument.moduleName }}Mock {
func reset()
}

{% macro swiftifyMethodName method %}{% if method|annotated:"Name" %}{{ method.annotations["Name"] }}{% else %}{{ method.callName | replace:"(","_" | replace:")","" | replace:":","_" | replace:"`","" | snakeToCamelCase | lowerFirstWord }}{% endif %}{% endmacro %}

{# Input: method parameter. Output: `ParameterType`, `@escaping ParamType`. Automatically get the info needed or override with `Type=ParamType` annotation (used when using generics) #}
Expand Down Expand Up @@ -235,14 +257,39 @@ class ExampleTest: XCTestCase {
Note: This file is automatically generated. This means the mocks should always be up-to-date and has a consistent API.
See the SDK documentation to learn the basics behind using the mock classes in the SDK.
*/
{{ type.accessLevel }} class {{ type.name }}Mock: {{ type.name }} {
{{ type.accessLevel }} class {{ type.name }}Mock: {{ type.name }}, {{ argument.moduleName }}Mock {
/// If *any* interactions done on mock. `true` if any method or property getter/setter called.
{{ type.accessLevel }} var mockCalled: Bool = false //

init() {
{{ argument.moduleName }}Mocks.shared.add(mock: self)
}

{% for variable in type.allVariables|!definedInExtension %}
{% if variable.isOptional %}{% call mockOptionalVariable variable %}{% elif variable.isArray or variable.isDictionary %}{% call mockNonOptionalArrayOrDictionaryVariable variable %}{% else %}{% call mockNonOptionalVariable variable %}{% endif %}
{% endfor %}

public func reset() {
self.mockCalled = false

{% for variable in type.allVariables|!definedInExtension %}
{% if variable.isOptional %}
{% call mockedVariableName variable %} = nil
{% else %}
{% call mockedVariableName variable %}Called = false
{% call mockedVariableName variable %}GetCalled = false
{% call mockedVariableName variable %}SetCalled = false
{% endif %}
{% endfor %}
{% for method in type.allMethods|!definedInExtension %}
{% call swiftifyMethodName method %}CallsCount = 0
{% if method.parameters.count > 0 %}
{% call swiftifyMethodName method %}ReceivedArguments = nil
{% call swiftifyMethodName method %}ReceivedInvocations = []
{% endif %}
{% endfor %}
}

{% for method in type.allMethods|!definedInExtension %}
{% call mockMethod method %}
{% endfor %}
Expand Down
Loading

0 comments on commit 845f3af

Please sign in to comment.