Skip to content

Commit aa26c40

Browse files
author
Hadevs
committed
added unit tests for Containerable
1 parent d75be69 commit aa26c40

File tree

10 files changed

+236
-36
lines changed

10 files changed

+236
-36
lines changed

SwiftInjector/SwiftInjector.xcodeproj/project.pbxproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
E676F73522C9688F00DC1282 /* TestClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = E676F73422C9688F00DC1282 /* TestClass.swift */; };
2323
E6BF5C6322CD2A32009A9331 /* SILogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6BF5C6222CD2A32009A9331 /* SILogger.swift */; };
2424
E6BF5C6522CD45C0009A9331 /* RecursiveResolvingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6BF5C6422CD45C0009A9331 /* RecursiveResolvingTests.swift */; };
25+
E6C8BEFF22CD592300D99B89 /* AutoRecursionContainer_2Parent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6C8BEFE22CD592300D99B89 /* AutoRecursionContainer_2Parent.swift */; };
26+
E6F3F57B22D11056003D86FA /* ContainerablePropertyNameTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6F3F57A22D11056003D86FA /* ContainerablePropertyNameTests.swift */; };
27+
E6F3F57D22D111CB003D86FA /* ContainerableRegisterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6F3F57C22D111CB003D86FA /* ContainerableRegisterTests.swift */; };
28+
E6F3F57F22D11646003D86FA /* ContainerablePropertyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6F3F57E22D11646003D86FA /* ContainerablePropertyTests.swift */; };
29+
E6F3F58122D117EE003D86FA /* ContainerableFormattedStringTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6F3F58022D117EE003D86FA /* ContainerableFormattedStringTests.swift */; };
2530
/* End PBXBuildFile section */
2631

2732
/* Begin PBXContainerItemProxy section */
@@ -54,6 +59,11 @@
5459
E676F73422C9688F00DC1282 /* TestClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestClass.swift; sourceTree = "<group>"; };
5560
E6BF5C6222CD2A32009A9331 /* SILogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SILogger.swift; sourceTree = "<group>"; };
5661
E6BF5C6422CD45C0009A9331 /* RecursiveResolvingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecursiveResolvingTests.swift; sourceTree = "<group>"; };
62+
E6C8BEFE22CD592300D99B89 /* AutoRecursionContainer_2Parent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutoRecursionContainer_2Parent.swift; sourceTree = "<group>"; };
63+
E6F3F57A22D11056003D86FA /* ContainerablePropertyNameTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContainerablePropertyNameTests.swift; sourceTree = "<group>"; };
64+
E6F3F57C22D111CB003D86FA /* ContainerableRegisterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContainerableRegisterTests.swift; sourceTree = "<group>"; };
65+
E6F3F57E22D11646003D86FA /* ContainerablePropertyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContainerablePropertyTests.swift; sourceTree = "<group>"; };
66+
E6F3F58022D117EE003D86FA /* ContainerableFormattedStringTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContainerableFormattedStringTests.swift; sourceTree = "<group>"; };
5767
/* End PBXFileReference section */
5868

5969
/* Begin PBXFrameworksBuildPhase section */
@@ -112,6 +122,10 @@
112122
children = (
113123
E676F71A22C94F2E00DC1282 /* SwiftInjectorTests.swift */,
114124
E676F72A22C9511200DC1282 /* ContainerableMainTests.swift */,
125+
E6F3F57C22D111CB003D86FA /* ContainerableRegisterTests.swift */,
126+
E6F3F58022D117EE003D86FA /* ContainerableFormattedStringTests.swift */,
127+
E6F3F57E22D11646003D86FA /* ContainerablePropertyTests.swift */,
128+
E6F3F57A22D11056003D86FA /* ContainerablePropertyNameTests.swift */,
115129
E6BF5C6422CD45C0009A9331 /* RecursiveResolvingTests.swift */,
116130
E676F71C22C94F2E00DC1282 /* Info.plist */,
117131
);
@@ -124,6 +138,7 @@
124138
E676F72622C94F4700DC1282 /* RootContainer.swift */,
125139
E6BF5C6222CD2A32009A9331 /* SILogger.swift */,
126140
E676F73022C954BB00DC1282 /* TestContainer.swift */,
141+
E6C8BEFE22CD592300D99B89 /* AutoRecursionContainer_2Parent.swift */,
127142
E676F73222C955B200DC1282 /* DIContainer.swift */,
128143
E676F72E22C9548B00DC1282 /* Container.swift */,
129144
E676F72822C94F6000DC1282 /* Containerable.swift */,
@@ -232,6 +247,7 @@
232247
isa = PBXSourcesBuildPhase;
233248
buildActionMask = 2147483647;
234249
files = (
250+
E6C8BEFF22CD592300D99B89 /* AutoRecursionContainer_2Parent.swift in Sources */,
235251
E676F73522C9688F00DC1282 /* TestClass.swift in Sources */,
236252
E676F73322C955B200DC1282 /* DIContainer.swift in Sources */,
237253
E676F70822C94F2B00DC1282 /* ViewController.swift in Sources */,
@@ -248,9 +264,13 @@
248264
isa = PBXSourcesBuildPhase;
249265
buildActionMask = 2147483647;
250266
files = (
267+
E6F3F57F22D11646003D86FA /* ContainerablePropertyTests.swift in Sources */,
251268
E6BF5C6522CD45C0009A9331 /* RecursiveResolvingTests.swift in Sources */,
252269
E676F72B22C9511200DC1282 /* ContainerableMainTests.swift in Sources */,
270+
E6F3F58122D117EE003D86FA /* ContainerableFormattedStringTests.swift in Sources */,
271+
E6F3F57D22D111CB003D86FA /* ContainerableRegisterTests.swift in Sources */,
253272
E676F71B22C94F2E00DC1282 /* SwiftInjectorTests.swift in Sources */,
273+
E6F3F57B22D11056003D86FA /* ContainerablePropertyNameTests.swift in Sources */,
254274
);
255275
runOnlyForDeploymentPostprocessing = 0;
256276
};

SwiftInjector/SwiftInjector/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1818
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1919
let newVc: ViewController? = self.container.resolve(name: "123")
2020
let testClass = newVc?.testClass
21-
#error("NOT FINISHED RECURSIVE RESOLVING. BECAUSE AFTER RESOLVING I DONT PASS NAME VARIABLE TO RESOLVE.")
21+
// #error("NOT FINISHED RECURSIVE RESOLVING. BECAUSE AFTER RESOLVING I DONT PASS NAME VARIABLE TO RESOLVE.")
2222
print(testClass)
2323
return true
2424
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// AutoRecursionContainer_2Parent.swift
3+
// SwiftInjector
4+
//
5+
// Created by Ghost on 04.07.2019.
6+
// Copyright © 2019 Ghost. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
class AuthRecursionContainer_2Parent: DIContainer {
12+
override func register() {
13+
14+
}
15+
}

SwiftInjector/SwiftInjector/Source/Containerable.swift

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,13 @@ extension Containerable {
122122
}
123123
}
124124

125-
private func formattedString<T: Object>(of object: T) -> String {
125+
// MARK: *TESTED*
126+
func formattedString<T: Object>(of object: T) -> String {
126127
return String(describing: type(of: object)).replacingOccurrences(of: "Optional<", with: "").replacingOccurrences(of: ">", with: "")
127128
}
128129

129-
private func doesObjectHas(object: Object, propertyType: String) -> Bool {
130-
let mirror = Mirror(reflecting: object)
131-
for attr in mirror.children {
132-
let objectType = type(of: attr.value)
133-
let typeString = String(describing: objectType).replacingOccurrences(of: "Optional<", with: "").replacingOccurrences(of: ">", with: "")
134-
if typeString == typeString {
135-
return true
136-
}
137-
}
138-
139-
return false
140-
}
141-
142-
private func property<T>(object: Object, propertyName: String) -> T? {
130+
// MARK: *TESTED*
131+
func property<T>(object: Object, propertyName: String) -> T? {
143132
let mirror = Mirror(reflecting: object)
144133
for attr in mirror.children {
145134
if attr.label == propertyName {
@@ -150,7 +139,9 @@ extension Containerable {
150139
return nil
151140
}
152141

153-
private func propertyName(by typeString: String, in object: Object) -> String? {
142+
// MARK: *TESTED*
143+
// Returns only first variable of type
144+
func propertyName(by typeString: String, in object: Object) -> String? {
154145
let mirror = Mirror(reflecting: object)
155146
for attr in mirror.children {
156147
let propertyType = type(of: attr.value)
@@ -163,8 +154,8 @@ extension Containerable {
163154
return nil
164155
}
165156

157+
// MARK: *TESTED*
166158
func register<T: Object>(_ registration: @escaping (() -> T), name: String? = nil) {
167-
168159
dispatchRegistrationGroup.enter()
169160
let object = registration()
170161
let key = String(describing: type(of: object))

SwiftInjector/SwiftInjector/TestClass.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
import Foundation
1010

11-
class TestClass {
11+
class TestClass: Equatable {
1212
var name: String = "123"
1313
weak var viewController: ViewController?
14+
15+
static func == (lhs: TestClass, rhs: TestClass) -> Bool {
16+
return "\(Unmanaged.passUnretained(lhs).toOpaque())" == "\(Unmanaged.passUnretained(rhs).toOpaque())"
17+
}
1418
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// ContainerableFormattedStringTests.swift
3+
// SwiftInjectorTests
4+
//
5+
// Created by Ghost on 06.07.2019.
6+
// Copyright © 2019 Ghost. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import SwiftInjector
11+
12+
class ContainerableFormattedStringTests: XCTestCase {
13+
let container = TestContainer()
14+
override func setUp() {
15+
// Put setup code here. This method is called before the invocation of each test method in the class.
16+
}
17+
18+
override func tearDown() {
19+
// Put teardown code here. This method is called after the invocation of each test method in the class.
20+
}
21+
22+
func testExampleRegister() {
23+
let containerable: Containerable = RootContainer()
24+
let object = TestClass()
25+
let objectTypeString = containerable.formattedString(of: object)
26+
XCTAssertEqual(objectTypeString, "TestClass")
27+
}
28+
29+
func testExampleRegisterInvalid() {
30+
let containerable: Containerable = RootContainer()
31+
let object = TestClass()
32+
let objectTypeString = containerable.formattedString(of: object)
33+
XCTAssertNotEqual(objectTypeString, "TestClass1")
34+
}
35+
}
36+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// ContainerablePropertyName.swift
3+
// SwiftInjectorTests
4+
//
5+
// Created by Ghost on 06.07.2019.
6+
// Copyright © 2019 Ghost. All rights reserved.
7+
//
8+
9+
10+
import XCTest
11+
@testable import SwiftInjector
12+
13+
class ContainerablePropertyNameTests: XCTestCase {
14+
let container: Containerable = RootContainer()
15+
16+
override func setUp() {
17+
// Put setup code here. This method is called before the invocation of each test method in the class.
18+
}
19+
20+
override func tearDown() {
21+
// Put teardown code here. This method is called after the invocation of each test method in the class.
22+
}
23+
24+
func testPropertyNameWithOneString() {
25+
class Test {
26+
var name: String = "123"
27+
}
28+
let typeString = "String"
29+
let name = container.propertyName(by: typeString, in: Test())
30+
XCTAssertEqual(name, "name")
31+
}
32+
33+
func testPropertyNameWithTwoStrings() {
34+
class Test {
35+
var name: String = "123"
36+
var test: String = "456"
37+
}
38+
let typeString = "String"
39+
let name = container.propertyName(by: typeString, in: Test())
40+
XCTAssertEqual(name, "name")
41+
}
42+
43+
func testPropertyNameWithTest() {
44+
class Test {
45+
var test: Test?
46+
}
47+
let typeString = "Test"
48+
let name = container.propertyName(by: typeString, in: Test())
49+
XCTAssertEqual(name, "test")
50+
}
51+
}
52+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// ContainerablePropertyTests.swift
3+
// SwiftInjectorTests
4+
//
5+
// Created by Ghost on 06.07.2019.
6+
// Copyright © 2019 Ghost. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import SwiftInjector
11+
12+
class ContainerablePropertyTests: XCTestCase {
13+
14+
override func setUp() {
15+
// Put setup code here. This method is called before the invocation of each test method in the class.
16+
}
17+
18+
override func tearDown() {
19+
// Put teardown code here. This method is called after the invocation of each test method in the class.
20+
}
21+
22+
func testPropertyNil() {
23+
let container: Containerable = RootContainer()
24+
let object = TestClass()
25+
let property: String? = container.property(object: object, propertyName: "name1")
26+
XCTAssertNil(property)
27+
}
28+
29+
func testPropertyNotNil() {
30+
let container: Containerable = RootContainer()
31+
let object = TestClass()
32+
let property: String? = container.property(object: object, propertyName: "name")
33+
XCTAssertNotNil(property)
34+
XCTAssertEqual(property, "123")
35+
}
36+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// ContainerableRegister.swift
3+
// SwiftInjectorTests
4+
//
5+
// Created by Ghost on 06.07.2019.
6+
// Copyright © 2019 Ghost. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import SwiftInjector
11+
12+
class ContainerableRegisterTests: XCTestCase {
13+
14+
override func setUp() {
15+
// Put setup code here. This method is called before the invocation of each test method in the class.
16+
}
17+
18+
override func tearDown() {
19+
// Put teardown code here. This method is called after the invocation of each test method in the class.
20+
}
21+
22+
func testExampleRegisterWithoutResolving() {
23+
let container: Containerable = RootContainer()
24+
let object = TestClass()
25+
container.register({ () -> TestClass in
26+
return object
27+
}, name: nil)
28+
29+
let key = String(describing: type(of: object))
30+
XCTAssertNotNil(container.services[key])
31+
XCTAssert(container.services[key]?.count ?? 0 == 1)
32+
33+
let containerObject = container.services[key]?.first
34+
XCTAssertNil(containerObject?.object)
35+
// it cannot be exists, because it has been not resolved yet
36+
}
37+
38+
func testExampleRegisterWithResolving() {
39+
let container: Containerable = RootContainer()
40+
let object = TestClass()
41+
container.register({ () -> TestClass in
42+
return object
43+
}, name: nil)
44+
45+
let key = String(describing: type(of: object))
46+
XCTAssertNotNil(container.services[key])
47+
XCTAssert(container.services[key]?.count ?? 0 == 1)
48+
49+
let obj: TestClass? = container.resolve()
50+
XCTAssertNotNil(obj)
51+
let containerObject = container.services[key]?.first
52+
XCTAssertNotNil(containerObject?.object)
53+
XCTAssert(containerObject?.object is TestClass)
54+
XCTAssertEqual(containerObject?.object as? TestClass, object)
55+
}
56+
}
57+

SwiftInjector/SwiftInjectorTests/RecursiveResolvingTests.swift

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,17 @@ class RecursiveResolvingTests: XCTestCase {
3939
// Put teardown code here. This method is called after the invocation of each test method in the class.
4040
}
4141

42+
let container = TestContainer()
4243

43-
class TestingContainer_1: DIContainer {
44-
let firstClass = FirstClass()
45-
let secondClass = SecondClass()
46-
override func register() {
47-
register { self.firstClass }
48-
register { self.secondClass }
49-
}
50-
}
51-
let container = TestingContainer_1()
5244
func testClassToClassRegister() {
53-
54-
var firstClass: FirstClass? = container.resolve()
55-
let secondClass: SecondClass? = container.resolve()
45+
let firstClass: ViewController? = container.resolve()
46+
let secondClass: TestClass? = container.resolve()
5647
print("---------")
57-
print(firstClass, secondClass?.firstClass)
5848
print("---------")
5949
XCTAssertNotNil(firstClass)
6050
XCTAssertNotNil(secondClass)
61-
XCTAssertNotNil(secondClass?.firstClass)
62-
XCTAssertEqual(firstClass?.secondClass, secondClass)
63-
XCTAssertEqual(secondClass?.firstClass, firstClass)
51+
XCTAssertNotNil(secondClass?.viewController)
52+
XCTAssertEqual(secondClass?.viewController, firstClass)
53+
XCTAssertEqual(firstClass?.testClass, secondClass)
6454
}
65-
6655
}

0 commit comments

Comments
 (0)