Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tech] Publisher: Add global mock and XCTTest #413

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions core/Unit-tests/Classes/Publisher/PublisherMock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// PublisherMock.swift
// Spark
//
// Created by robin.lemaire on 31/08/2023.
// Copyright © 2023 Adevinta. All rights reserved.
//

import Combine
import Foundation

final class PublisherMock<T: Publisher> {

// MARK: - Properties

private let publisher: T
let name: String
var sinkValue: T.Output?
var sinkValues = [T.Output]()
var sinkCount = 0
var sinkCalled: Bool {
return self.sinkCount > 0
}

// MARK: - Initialization

init(
publisher: T,
name: String = String(describing: T.Output.self)
) {
self.publisher = publisher
self.name = name
}

// MARK: - Methods

func reset() {
self.sinkValue = nil
self.sinkValues.removeAll()
self.sinkCount = 0
}
}

// MARK: - Tests Management

extension PublisherMock where T.Failure == Never {

func loadTesting(on subscriptions: inout Set<AnyCancellable>) {
self.publisher.sink { value in
self.sinkValue = value
self.sinkValues.append(value)
self.sinkCount += 1
}.store(in: &subscriptions)
}
}
94 changes: 94 additions & 0 deletions core/Unit-tests/Classes/Publisher/XCTest+PublisherMock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// XCTest+PublisherMock.swift
// SparkCoreTests
//
// Created by robin.lemaire on 04/09/2023.
// Copyright © 2023 Adevinta. All rights reserved.
//

import XCTest
import Combine

extension XCTest {

// MARK: - Tests

func XCTAssertPublisherSinkCountEqual<T: Publisher>(
on mock: PublisherMock<T>,
_ expression: Int
) {
XCTAssertEqual(
mock.sinkCount,
expression,
Self.errorMessage(on: mock, for: .count)
)
}

func XCTAssertPublisherSinkIsCalled<T: Publisher>(
on mock: PublisherMock<T>,
_ expression: Bool
) {
XCTAssertEqual(
mock.sinkCalled,
expression,
Self.errorMessage(on: mock, for: .isCalled)
)
}

func XCTAssertPublisherSinkValueEqual<T: Publisher>(
on mock: PublisherMock<T>,
_ expression: T.Output
) where T.Output: Equatable {
XCTAssertEqual(
mock.sinkValue,
expression,
Self.errorMessage(on: mock, for: .value)
)
}

func XCTAssertPublisherSinkValueIdentical<T: Publisher, Z: AnyObject>(
on mock: PublisherMock<T>,
_ expression: Z?,
expressionShouldBeSet: Bool = true
) {
guard (expressionShouldBeSet && expression != nil) || !expressionShouldBeSet else {
XCTFail("\(Z.self) expression should be set")
return
}

XCTAssertIdentical(
mock.sinkValue as? Z,
expression,
Self.errorMessage(on: mock, for: .value)
)
}

func XCTAssertPublisherSinkValuesEqual<T: Publisher>(
on mock: PublisherMock<T>,
_ expression: [T.Output]
) where T.Output: Equatable {
XCTAssertEqual(
mock.sinkValues,
expression,
Self.errorMessage(on: mock, for: .values)
)
}

// MARK: - Test Type

private enum TestingSinkType: String {
case count
case isCalled
case value
case values
}

// MARK: - Message

private static func errorMessage<T: Publisher>(
on mock: PublisherMock<T>,
for type: TestingSinkType
) -> String {
return "Wrong \(mock.name) sink \(type.rawValue) value"
}
}
Loading