Skip to content

Commit 0a10481

Browse files
committed
테스트 코드 추가 및 코드 개선
1 parent 7276d9e commit 0a10481

File tree

10 files changed

+197
-43
lines changed

10 files changed

+197
-43
lines changed

Sources/DIContainer/Scan/ModuleScanner.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,13 @@ public struct ModuleScanner {
132132
}
133133
}
134134
#endif
135+
136+
#if DEBUG
137+
public extension Container {
138+
@MainActor
139+
static func autoRegisterModules() {
140+
let moduleList = ModuleScanner().scanModuleList
141+
Container(modules: moduleList).build()
142+
}
143+
}
144+
#endif
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import DIContainer
2+
import Foundation
3+
4+
final class MockServiceFactoryKey: InjectionKey {
5+
typealias Value = MockServiceFactoryProtocol
6+
}
7+
8+
protocol MockServiceFactoryProtocol {
9+
func makeWeakService() -> WeakMockService
10+
func makeService() -> MockService
11+
}
12+
13+
final class MockServiceFactory: AutoModule, MockServiceFactoryProtocol {
14+
typealias ModuleKeyType = MockServiceFactoryKey
15+
16+
func makeWeakService() -> WeakMockService {
17+
WeakMockServiceImpl()
18+
}
19+
20+
func makeService() -> MockService {
21+
MockServiceImpl()
22+
}
23+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// CachedModuleTests.swift
3+
// DIContainer
4+
//
5+
// Created by minsOne on 1/23/25.
6+
//
7+
8+
import Testing
9+
10+
@testable import DIContainer
11+
12+
@MainActor
13+
@Suite(.serialized)
14+
struct CachedModuleTests {
15+
init() {
16+
Container.autoRegisterModules()
17+
}
18+
}
19+
20+
extension CachedModuleTests {
21+
@Test
22+
func cachedModule() throws {
23+
let service1 = getMockService()
24+
let service2 = getMockService()
25+
26+
service1.doSomething()
27+
do {
28+
let service = try #require(service1 as? MockServiceImpl)
29+
#expect(service.count == 1)
30+
}
31+
32+
service2.doSomething()
33+
do {
34+
let service = try #require(service2 as? MockServiceImpl)
35+
#expect(service.count == 2)
36+
}
37+
}
38+
39+
@Test
40+
func generateService() throws {
41+
let factory1 = getMockServiceFactory()
42+
let factory2 = getMockServiceFactory()
43+
44+
let service1 = factory1.makeService()
45+
service1.doSomething()
46+
47+
do {
48+
let service = try #require(service1 as? MockServiceImpl)
49+
#expect(service.count == 1)
50+
}
51+
52+
let service2 = factory2.makeService()
53+
service2.doSomething()
54+
55+
do {
56+
let service = try #require(service2 as? MockServiceImpl)
57+
#expect(service.count == 1)
58+
}
59+
}
60+
}
61+
62+
extension CachedModuleTests {
63+
func getMockService() -> MockService {
64+
@Inject(MockServiceKey.self) var service
65+
return service
66+
}
67+
68+
func getMockServiceFactory() -> MockServiceFactoryProtocol {
69+
@Inject(MockServiceFactoryKey.self) var factory
70+
return factory
71+
}
72+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// CompareModuleTests.swift
3+
// DIContainer
4+
//
5+
// Created by minsOne on 1/23/25.
6+
//
7+
8+
import Testing
9+
10+
@testable import DIContainer
11+
12+
@Suite(.serialized)
13+
struct CompareModuleTests {
14+
@Test
15+
func compare() {
16+
let lhs = Module(MockServiceKey.self) { MockServiceImpl() }
17+
let rhs = Module(MockServiceImpl.self)
18+
19+
#expect(lhs == rhs)
20+
#expect(lhs.hashValue == rhs.hashValue)
21+
}
22+
}

Tests/DIContainerTests/Tests/InjectTestsTests.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Testing
12
import XCTest
23

34
@testable import DIContainer
@@ -27,16 +28,16 @@ extension ContainerTests {
2728

2829
private func injection() {
2930
let serviceKey = MockServiceKey.self
30-
XCTAssertNotNil(serviceKey.module?.resolve())
31-
XCTAssertNotNil(serviceKey.module?.resolve() as? MockServiceKey.Value)
32-
XCTAssertNotNil(serviceKey.module?.resolve() as? MockService)
31+
#expect(serviceKey.module?.resolve() != nil)
32+
#expect(serviceKey.module?.resolve() as? MockServiceKey.Value != nil)
33+
#expect(serviceKey.module?.resolve() as? MockService != nil)
3334

3435
@Inject(MockServiceKey.self) var service; _ = service
3536
}
3637

3738
private func injectBehavior() {
3839
@Inject(MockServiceKey.self) var service: MockService
3940
service.doSomething()
40-
XCTAssertEqual((service as? MockServiceImpl)?.count, 1)
41+
#expect((service as? MockServiceImpl)?.count == 1)
4142
}
4243
}

Tests/DIContainerTests/Tests/MachOLoaderTests.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ import Testing
1212
import XCTest
1313

1414
@MainActor
15-
struct MachOLoaderTests {
15+
@Suite(.serialized)
16+
struct MachOLoaderTests {}
17+
18+
extension MachOLoaderTests {
1619
@Test
1720
func findAllTypes() {
18-
#expect(MachOLoader().keyList.count == 2)
19-
#expect(MachOLoader().scanModuleTypeList.count == 2)
20-
#expect(MachOLoader().scanModuleList.count == 2)
21+
#expect(MachOLoader().keyList.count == 3)
22+
#expect(MachOLoader().scanModuleTypeList.count == 3)
23+
#expect(MachOLoader().scanModuleList.count == 3)
2124
}
2225
}
2326

Tests/DIContainerTests/Tests/ModuleListExtensionTests.swift

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,28 @@ import XCTest
55
@testable import DIContainer
66

77
@MainActor
8-
struct ModuleListExtensionTest {
8+
@Suite(.serialized)
9+
struct ModuleListExtensionTest {}
10+
11+
extension ModuleListExtensionTest {
912
@Test
1013
func replaceOneModule() {
1114
// Given
1215
let newModule = Module(MockServiceKey.self) { Service1() }
1316
var moduleList = ModuleScanner().scanModuleList
14-
XCTAssertEqual(moduleList.count, 2)
17+
#expect(moduleList.count == 3)
1518

1619
// When
1720
moduleList = moduleList.replacing(newModule)
1821

1922
// Then
20-
XCTAssertEqual(moduleList.count, 2)
21-
XCTAssertEqual(moduleList.last?.name, newModule.name)
23+
#expect(moduleList.count == 3)
24+
#expect(moduleList.last?.name == newModule.name)
2225

2326
let service = moduleList.last?.resolve() as? Service1
2427
service?.doSomething()
25-
XCTAssertNotNil(service)
26-
XCTAssertEqual(service?.count, 1)
28+
#expect(service != nil)
29+
#expect(service?.count == 1)
2730
}
2831

2932
@Test
@@ -32,34 +35,34 @@ struct ModuleListExtensionTest {
3235
let newModule1 = Module(MockServiceKey.self) { Service1() }
3336
let newModule2 = Module(WeakMockServiceKey.self) { Service2() }
3437
var moduleList = ModuleScanner().scanModuleList
35-
XCTAssertEqual(moduleList.count, 2)
38+
#expect(moduleList.count == 3)
3639

3740
// When
3841
moduleList = moduleList.replacing(contentsOf: [newModule1, newModule2])
3942

4043
// Then
41-
XCTAssertEqual(moduleList.count, 2)
44+
#expect(moduleList.count == 3)
4245

4346
do {
4447
let module = moduleList.popLast()
4548
let service = module?.resolve() as? Service2
46-
XCTAssertNotNil(module)
47-
XCTAssertNotNil(service)
49+
#expect(module != nil)
50+
#expect(service != nil)
4851

4952
service?.doSomething()
50-
XCTAssertEqual(module?.name, newModule2.name)
51-
XCTAssertEqual(service?.count, 1)
53+
#expect(module?.name == newModule2.name)
54+
#expect(service?.count == 1)
5255
}
5356

5457
do {
5558
let module = moduleList.popLast()
5659
let service = module?.resolve() as? Service1
57-
XCTAssertNotNil(module)
58-
XCTAssertNotNil(service)
60+
#expect(module != nil)
61+
#expect(service != nil)
5962

6063
service?.doSomething()
61-
XCTAssertEqual(module?.name, newModule1.name)
62-
XCTAssertEqual(service?.count, 1)
64+
#expect(module?.name == newModule1.name)
65+
#expect(service?.count == 1)
6366
}
6467
}
6568
}

Tests/DIContainerTests/Tests/ModuleScannerTests.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ import XCTest
66
@testable import DIContainer
77

88
@MainActor
9-
struct ModuleScannerTest {
9+
@Suite(.serialized)
10+
struct ModuleScannerTest {}
11+
12+
extension ModuleScannerTest {
1013
@Test
1114
func scanInjectKeys() {
1215
Container {
1316
Module(MockServiceKey.self) { MockServiceImpl() }
1417
Module(WeakMockServiceImpl.self)
18+
Module(MockServiceFactory.self)
1519
}
1620
.build()
1721

@@ -25,14 +29,14 @@ struct ModuleScannerTest {
2529
└────────────────────────────────────────────────
2630
""")
2731

28-
XCTAssertEqual(keyList.isEmpty, false)
29-
XCTAssertEqual(keyList.count, 2)
32+
#expect(keyList.isEmpty == false)
33+
#expect(keyList.count == 3)
3034

3135
for key in keyList {
3236
let obj: any Any = Container.resolve(for: key)
3337

3438
if case Optional<Any>.none = obj {
35-
XCTFail("\(key)가 등록되어 있지 않습니다.")
39+
assertionFailure("\(key)가 등록되어 있지 않습니다.")
3640
}
3741
}
3842
}
@@ -49,8 +53,8 @@ struct ModuleScannerTest {
4953
└────────────────────────────────────────────────
5054
""")
5155

52-
XCTAssertEqual(moduleList.isEmpty, false)
53-
XCTAssertEqual(moduleList.count, 2)
56+
#expect(moduleList.isEmpty == false)
57+
#expect(moduleList.count == 3)
5458

5559
Container(modules: moduleList)
5660
.build()

Tests/DIContainerTests/Tests/WeakInjectNilTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import XCTest
44
@testable import DIContainer
55

66
@MainActor
7+
@Suite(.serialized)
78
struct WeakInjectNilTest {
89
init() {
910
Container().build()

0 commit comments

Comments
 (0)