Skip to content

Commit

Permalink
Merge pull request #25 from AppCron/register-interactor-with-request-…
Browse files Browse the repository at this point in the history
…type

Register interactor with request type
  • Loading branch information
Florian Rieger authored May 15, 2019
2 parents f0f814d + b7f5415 commit 0febcd0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 14 deletions.
15 changes: 11 additions & 4 deletions Sources/ACInteractor/InteractorExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@ open class InteractorExecutor {

// MARK: - Register

open func registerInteractor<InteractorProtocol: Interactor, RequestProtocol: InteractorRequestProtocol>
(_ 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<InteractorProtocol: Interactor, Response>
(_ interactor: InteractorProtocol, request: InteractorRequest<Response>)
{
let key = String(describing: request)
interactors[key] = InteractorWrapper(interactor: interactor)
registerInteractor(interactor, request: type(of: request))
}

// MARK: - Execute

open func execute<Request: InteractorRequestProtocol>(_ request: Request) {
let key = String(describing: request)
let key = String(reflecting: request)
let optionalValue = interactors[key]

guard let value = optionalValue else {
Expand All @@ -38,7 +45,7 @@ open class InteractorExecutor {
// MARK: - GetInteractor

open func getInteractor<Request: InteractorRequestProtocol>(request: Request) -> AnyObject? {
let key = String(describing: request)
let key = String(reflecting: request)
let optional = interactors[key]

guard let wrapper = optional as? InteractorWrapper<Request> else {
Expand Down
76 changes: 66 additions & 10 deletions Tests/ACInteractorTests/InteractorExecutorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)
}

}

0 comments on commit 0febcd0

Please sign in to comment.