diff --git a/Sources/ACInteractor/InteractorExecutor.swift b/Sources/ACInteractor/InteractorExecutor.swift index ab7ae77..c355b89 100644 --- a/Sources/ACInteractor/InteractorExecutor.swift +++ b/Sources/ACInteractor/InteractorExecutor.swift @@ -9,17 +9,24 @@ open class InteractorExecutor { // MARK: - Register + open func registerInteractor + (_ interactor: InteractorProtocol, request: RequestProtocol.Type) + { + let key = String(reflecting: request) + interactors[key] = InteractorWrapper(interactor: interactor) + } + + @available(*, deprecated, message: "Use MyInteractor.Request.self as request parameter.") open func registerInteractor (_ interactor: InteractorProtocol, request: InteractorRequest) { - let key = String(describing: request) - interactors[key] = InteractorWrapper(interactor: interactor) + registerInteractor(interactor, request: type(of: request)) } // MARK: - Execute open func execute(_ request: Request) { - let key = String(describing: request) + let key = String(reflecting: request) let optionalValue = interactors[key] guard let value = optionalValue else { @@ -38,7 +45,7 @@ open class InteractorExecutor { // MARK: - GetInteractor open func getInteractor(request: Request) -> AnyObject? { - let key = String(describing: request) + let key = String(reflecting: request) let optional = interactors[key] guard let wrapper = optional as? InteractorWrapper else { diff --git a/Tests/ACInteractorTests/InteractorExecutorTests.swift b/Tests/ACInteractorTests/InteractorExecutorTests.swift index cab352e..e5cea42 100644 --- a/Tests/ACInteractorTests/InteractorExecutorTests.swift +++ b/Tests/ACInteractorTests/InteractorExecutorTests.swift @@ -20,16 +20,40 @@ class InteractorExecutorTests: XCTestCase { } } - // MARK: - registerInteractor + // MARK: - execute - func testRegisterInteractor_succeeds(){ + func testExecute_callsExecuteOnInteractor_thatIsRegisteredForRequest() { + // Arrange + executor.registerInteractor(firstInteractor, request: FirstInteractor.Request.self) + executor.registerInteractor(secondInteractor, request: SecondInteractor.Request.self) + // Act - executor.registerInteractor(firstInteractor, request: firstRequest) + executor.execute(firstRequest) + executor.execute(secondRequest) + + // Assert + XCTAssertEqual(firstInteractor.executedRequests.count, 1) + XCTAssert(firstInteractor.executedRequests.first === firstRequest) + + XCTAssertEqual(secondInteractor.executedRequests.count, 1) + XCTAssert(secondInteractor.executedRequests.first === secondRequest) } - func testRegisterInteractor_callsErrorClosureOnRequest_whenInteractorDoesNotMatchRequest() { + func testExecute_callsErrorOnRequest_whenNoInteractorIsRegisteredForRequestType() { // Arrange - executor.registerInteractor(secondInteractor, request: firstRequest) + executor.registerInteractor(secondInteractor, request: SecondInteractor.Request.self) + + // Act + executor.execute(firstRequest) + + // Assert + let expected = "ACInteractor.ACInteractorExecutor: No Interactor is registered for this request!" + XCTAssertEqual(errorMessageFromFirstRequest, expected) + } + + func testExecute_callsErrorOnRequest_whenRegisteredInteractorDoesNotMatchRequest() { + // Arrange + executor.registerInteractor(secondInteractor, request: FirstInteractor.Request.self) // Act executor.execute(firstRequest) @@ -39,9 +63,10 @@ class InteractorExecutorTests: XCTestCase { XCTAssertEqual(errorMessageFromFirstRequest, expected) } - // MARK: - execute + // MARK: - execute (deprecated) - func testExecute_callsExecuteOnInteractor_thatIsRegisteredForRequest() { + @available(*, deprecated) + func testExecute_callsExecuteOnInteractor_thatIsRegisteredForRequestInstance() { // Arrange executor.registerInteractor(firstInteractor, request: firstRequest) executor.registerInteractor(secondInteractor, request: secondRequest) @@ -58,7 +83,8 @@ class InteractorExecutorTests: XCTestCase { XCTAssert(secondInteractor.executedRequests.first === secondRequest) } - func testExecute_callsErrorOnRequest_whenNoInteractorIsRegisteredForRequest() { + @available(*, deprecated) + func testExecute_callsErrorOnRequest_whenNoInteractorIsRegisteredForRequestInstance() { // Arrange executor.registerInteractor(secondInteractor, request: secondRequest) @@ -70,12 +96,25 @@ class InteractorExecutorTests: XCTestCase { XCTAssertEqual(errorMessageFromFirstRequest, expected) } + @available(*, deprecated) + func testExecute_callsErrorOnRequest_whenRegisteredInteractorDoesNotMatchRequestInstance() { + // Arrange + executor.registerInteractor(secondInteractor, request: firstRequest) + + // Act + executor.execute(firstRequest) + + // Assert + let expected = "ACInteractor.ACInteractorExecutor: Request does not match execute function of registered Interactor!" + XCTAssertEqual(errorMessageFromFirstRequest, expected) + } + // MARK: - getInteractor func testGetInteractor_returnsInteractor_registeredForRequest() { // Arrange - executor.registerInteractor(firstInteractor, request: firstRequest) - executor.registerInteractor(secondInteractor, request: secondRequest) + executor.registerInteractor(firstInteractor, request: FirstInteractor.Request.self) + executor.registerInteractor(secondInteractor, request: SecondInteractor.Request.self) // Act let firstResult = executor.getInteractor(request: firstRequest) @@ -94,4 +133,21 @@ class InteractorExecutorTests: XCTestCase { XCTAssertNil(result) } + // MARK: - getInteractor (deprecated) + + @available(*, deprecated) + func testGetInteractor_returnsInteractor_registeredForRequestInstance() { + // Arrange + executor.registerInteractor(firstInteractor, request: firstRequest) + executor.registerInteractor(secondInteractor, request: secondRequest) + + // Act + let firstResult = executor.getInteractor(request: firstRequest) + let secondResult = executor.getInteractor(request: secondRequest) + + // Assert + XCTAssert(firstResult === firstInteractor) + XCTAssert(secondResult === secondInteractor) + } + }