Skip to content

Commit bcaecb0

Browse files
author
Florian Rieger
authored
Merge pull request #7 from AppCron/lazy-interactor
LazyInteractor
2 parents c152e23 + 104c4ec commit bcaecb0

File tree

4 files changed

+149
-26
lines changed

4 files changed

+149
-26
lines changed

Sources/LazyInteractor.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Foundation
2+
3+
class LazyInteractor<InteractorType: Interactor>: Interactor {
4+
5+
private(set) var lazyInstance: InteractorType?
6+
private let factory: (Void -> InteractorType)
7+
8+
init(factory:(Void -> InteractorType)) {
9+
self.factory = factory
10+
}
11+
12+
func getInteractor() -> InteractorType {
13+
if let instance = lazyInstance {
14+
return instance
15+
}
16+
17+
let instance = factory()
18+
self.lazyInstance = instance
19+
20+
return instance
21+
}
22+
23+
func execute(request: InteractorType.Request) {
24+
self.getInteractor().execute(request)
25+
}
26+
27+
}

Tests/InteractorExecuterTests.swift

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class InteractorTests: XCTestCase {
3939

4040
// Assert
4141
XCTAssertEqual(firstInteractor.numberOfExceuteCalls, 1)
42+
XCTAssert(firstInteractor.executedRequest === firstRequest)
4243
}
4344

4445
func testExecute_withTwoInteractors_executeOnSecond_callsExecuteOnSecondInteractor() {
@@ -51,6 +52,7 @@ class InteractorTests: XCTestCase {
5152

5253
// Assert
5354
XCTAssertEqual(secondInteractor.numberOfExceuteCalls, 1)
55+
XCTAssert(secondInteractor.executedRequest === secondRequest)
5456
}
5557

5658
func testExecute_withTwoInteractors_executeOnBoth_callsExecuteOnEachInteractor() {
@@ -64,7 +66,10 @@ class InteractorTests: XCTestCase {
6466

6567
// Assert
6668
XCTAssertEqual(firstInteractor.numberOfExceuteCalls, 1)
69+
XCTAssert(firstInteractor.executedRequest === firstRequest)
70+
6771
XCTAssertEqual(secondInteractor.numberOfExceuteCalls, 1)
72+
XCTAssert(secondInteractor.executedRequest === secondRequest)
6873
}
6974

7075
func testExecute_withUnknownRequest_callsErrorOnRequest() {
@@ -88,36 +93,35 @@ class InteractorTests: XCTestCase {
8893
XCTAssertEqual(errorMessageFromFirstRequest, expected)
8994
}
9095

91-
}
92-
93-
94-
// MARK: Test Interactors
95-
96-
class TestIntactor {
97-
init() {
98-
print("initT")
99-
}
100-
var numberOfExceuteCalls = 0
10196

102-
func testExecute() {
103-
numberOfExceuteCalls += 1
104-
}
105-
}
106-
107-
class FirstInteractor: TestIntactor, Interactor {
108-
class Request: InteractorRequest<NSString> {
109-
}
97+
// MARK: Test Interactors
11098

99+
class FirstInteractor: Interactor {
100+
var numberOfExceuteCalls = 0
101+
var executedRequest: Request?
102+
103+
class Request: InteractorRequest<NSString> {
104+
}
105+
111106
func execute(request: Request) {
112-
self.testExecute()
113-
}
114-
}
115-
116-
class SecondInteractor: TestIntactor, Interactor {
117-
class Request: InteractorRequest<NSString> {
107+
self.numberOfExceuteCalls += 1
108+
self.executedRequest = request
109+
}
118110
}
119111

120-
func execute(request: Request) {
121-
self.testExecute()
112+
class SecondInteractor: Interactor {
113+
var numberOfExceuteCalls = 0
114+
var executedRequest: Request?
115+
116+
class Request: InteractorRequest<NSString> {
117+
}
118+
119+
func execute(request: Request) {
120+
self.numberOfExceuteCalls += 1
121+
self.executedRequest = request
122+
}
122123
}
124+
123125
}
126+
127+

Tests/LazyInteractorTests.swift

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import XCTest
2+
@testable import ACInteractor
3+
4+
class LazyInteractorTests: XCTestCase {
5+
6+
let testDependency = "testDependency"
7+
let testFactory = {return TestInteractor(dependency: "testDependency")}
8+
var lazyInteractor: LazyInteractor<TestInteractor>!
9+
10+
override func setUp() {
11+
super.setUp()
12+
// Put setup code here. This method is called before the invocation of each test method in the class.
13+
14+
lazyInteractor = LazyInteractor(factory: testFactory)
15+
}
16+
17+
// MARK: init()
18+
19+
func testInit_doesNotInitializeLazyInstance()
20+
{
21+
// Assert
22+
XCTAssertNil(lazyInteractor.lazyInstance)
23+
}
24+
25+
// MARK: getInteractor()
26+
27+
func testGetInteractor_returnsInstanceBuiltWithFactory()
28+
{
29+
// Act
30+
let interactor = lazyInteractor.getInteractor()
31+
32+
// Assert
33+
XCTAssertEqual(interactor.dependency, testDependency)
34+
}
35+
36+
37+
func testGetInteractor_calledTwice_doesNotCreateNewInstance()
38+
{
39+
// Act
40+
let firstInteractor = lazyInteractor.getInteractor()
41+
let secondInteractor = lazyInteractor.getInteractor()
42+
43+
// Assert
44+
XCTAssert(firstInteractor === secondInteractor)
45+
}
46+
47+
// MARK: execute()
48+
49+
func testExceute_callsExecuteOfInteractor()
50+
{
51+
// Arrange
52+
let request = TestInteractor.Request()
53+
54+
// Act
55+
lazyInteractor.execute(request)
56+
57+
// Assert
58+
let interactor = lazyInteractor.getInteractor()
59+
XCTAssertEqual(interactor.numberOfExceuteCalls, 1)
60+
XCTAssert(interactor.executedRequest === request)
61+
}
62+
63+
64+
// MARK: Test Interactors
65+
66+
class TestInteractor: Interactor {
67+
let dependency: String
68+
var numberOfExceuteCalls = 0
69+
var executedRequest: Request?
70+
71+
init(dependency: String) {
72+
self.dependency = dependency
73+
}
74+
75+
class Request: InteractorRequest<NSString> {
76+
}
77+
78+
func execute(request: Request) {
79+
self.numberOfExceuteCalls += 1
80+
self.executedRequest = request
81+
}
82+
}
83+
84+
}

Xcode/ACInteractor.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
173A77D21D3849E200FF88BA /* ACInteractor.h in Headers */ = {isa = PBXBuildFile; fileRef = 173A77431D3849E100FF88BA /* ACInteractor.h */; settings = {ATTRIBUTES = (Public, ); }; };
1616
17567E551D3A61C900C464BB /* InteractorError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17567E541D3A61C900C464BB /* InteractorError.swift */; };
1717
17567E591D3A640000C464BB /* InteractorErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17567E581D3A640000C464BB /* InteractorErrorTests.swift */; };
18+
17B0BF701D3CD057004AA98B /* LazyInteractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17B0BF6F1D3CD057004AA98B /* LazyInteractor.swift */; };
19+
17B0BF721D3CD0A9004AA98B /* LazyInteractorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17B0BF711D3CD0A9004AA98B /* LazyInteractorTests.swift */; };
1820
/* End PBXBuildFile section */
1921

2022
/* Begin PBXContainerItemProxy section */
@@ -38,6 +40,8 @@
3840
173A77D11D3849E200FF88BA /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3941
17567E541D3A61C900C464BB /* InteractorError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InteractorError.swift; sourceTree = "<group>"; };
4042
17567E581D3A640000C464BB /* InteractorErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InteractorErrorTests.swift; sourceTree = "<group>"; };
43+
17B0BF6F1D3CD057004AA98B /* LazyInteractor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LazyInteractor.swift; sourceTree = "<group>"; };
44+
17B0BF711D3CD0A9004AA98B /* LazyInteractorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LazyInteractorTests.swift; sourceTree = "<group>"; };
4145
/* End PBXFileReference section */
4246

4347
/* Begin PBXFrameworksBuildPhase section */
@@ -85,6 +89,7 @@
8589
173A77391D3844D400FF88BA /* InteractorRequest.swift */,
8690
17567E541D3A61C900C464BB /* InteractorError.swift */,
8791
173A77381D3844D400FF88BA /* InteractorExecuter.swift */,
92+
17B0BF6F1D3CD057004AA98B /* LazyInteractor.swift */,
8893
);
8994
name = Sources;
9095
path = ../Sources;
@@ -95,6 +100,7 @@
95100
children = (
96101
173A77401D38461D00FF88BA /* InteractorExecuterTests.swift */,
97102
17567E581D3A640000C464BB /* InteractorErrorTests.swift */,
103+
17B0BF711D3CD0A9004AA98B /* LazyInteractorTests.swift */,
98104
);
99105
name = Tests;
100106
path = ../Tests;
@@ -221,13 +227,15 @@
221227
17567E551D3A61C900C464BB /* InteractorError.swift in Sources */,
222228
173A773C1D3844D400FF88BA /* InteractorExecuter.swift in Sources */,
223229
173A773D1D3844D400FF88BA /* InteractorRequest.swift in Sources */,
230+
17B0BF701D3CD057004AA98B /* LazyInteractor.swift in Sources */,
224231
);
225232
runOnlyForDeploymentPostprocessing = 0;
226233
};
227234
173A77221D3842A200FF88BA /* Sources */ = {
228235
isa = PBXSourcesBuildPhase;
229236
buildActionMask = 2147483647;
230237
files = (
238+
17B0BF721D3CD0A9004AA98B /* LazyInteractorTests.swift in Sources */,
231239
173A77411D38461D00FF88BA /* InteractorExecuterTests.swift in Sources */,
232240
17567E591D3A640000C464BB /* InteractorErrorTests.swift in Sources */,
233241
);

0 commit comments

Comments
 (0)