Skip to content

Instantly share code, notes, and snippets.

View Kdan's full-sized avatar

Kewin Remeczki Kdan

  • Copenhagen, Denmark
View GitHub Profile
@Kdan
Kdan / Mock.swift
Last active January 26, 2021 14:24
class Mock<T: RawRepresentable> where T.RawValue == FunctionName {
let log = FunctionLogger<T>()
/// Retrieve the potential calls for a given function name.
/// - Parameters:
/// - name: The name of the function to retrieve calls for.
/// - Returns: The optional `FunctionCall` for the name provided.
func calls(with name: T) -> FunctionCall? { log[name] }
/// Log a function call with the given name and parameters.
class FunctionCall: Hashable {
/// The name of the function
let name: FunctionName
/// The number of calls for the function.
var numberOfCalls: Int { calls.count }
/// The parameters sent to the function.
private(set) var calls = [Any?]()
init(name: FunctionName) {
self.name = name
// When `b` returns true, `hasAnswer` should be true.
func testCheckAnswerTrue() {
b.validateAnswerResponse = true
a.checkAnswer()
XCTAssertTrue(a.hasAnswer)
}
/// A Bool indicating if the class has received any function calls.
var hasReceivedCalls: Bool { get }
/// Retrieve the potential calls for a given function name.
/// - Parameters:
/// - name: The name of the function to retrieve calls for.
/// - Returns: The optional `FunctionCall` for the name provided.
func calls(with name: FunctionName) -> FunctionCall?
/// Log a function call with the given name and parameters.
XCTAssertTrue(self.router.hasReceivedCalls)
XCTAssertFalse(self.reviewMock.hasReceivedCalls)
let call = self.router.calls(with: RouterMock.AlertRouterTypeFunctions.presentAlert)
XCTAssertNotNil(call)
XCTAssertEqual(call?.calls, 1)
XCTAssertEqual(call?.parameters[0] as? String, NSLocalizedString("fw_promo_mysound_hdr", comment: ""))
XCTAssertEqual(call?.parameters[1] as? String, "Update Jabra Elite 45h to get you started with MySound.")
XCTAssertEqual((call?.parameters[2] as? AlertAction)?.title, NSLocalizedString("jabra_support_fwpop_btn_later", comment: ""))
XCTAssertEqual((call?.parameters[3] as? AlertAction)?.title, NSLocalizedString("update_now", comment: ""))
func presentAlert(title: String? = nil, message: String? = nil, cancelAction: AlertAction? = nil, okAction: AlertAction? = nil) {
router.presentAlert(title: title, message: message, cancelAction: cancelAction, okAction: okAction, from: router.topViewController)
}
class A {
/// A `Bool` indicating if we have the answer to the universe.
private(set) var hasAnswer = false
/// The instance of `BType` we are using to check if we have the answer.
private let b: BType
init(b: BType) {
self.b = b
}
protocol BType {
/// Validates the answer.
func validateAnswer() -> Bool
}
protocol CType {
/// The answer to the universe.
var number: Int { get }
}
let a = A()
a.b = B()
a.checkAnswer()
class A {
/// The instance of B we are using to check if we have the answer.
var b: B?
/// A `Bool` indicating if we have the answer to the universe.
private(set) var hasAnswer = false
/// Checks if we have the answer to the universe and updates the `hasAnswer` property.
func checkAnswer() {
hasAnswer = b?.validateAnswer() ?? false
}