-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXCTestCase+Helpers.swift
57 lines (50 loc) · 1.63 KB
/
XCTestCase+Helpers.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// XCTestCase+Helpers.swift
// SHCoordinator
//
// Created by 양승현 on 4/9/24.
//
#if os(iOS)
import XCTest
import Foundation
// MARK: - SHCoordinator+XCTestCase
extension XCTestCase {
public typealias TestableFlowCoordinator = (
FlowCoordinator
& FlowCoordinatorNavigatable
& UINavigationControllerDelegate)
/// 이 함수로 코디네이터의 start, dismiss 후 memory leak를 체크하려면
/// navigationController(_: didShow: animated:) 를 준수후 내부에 handlePopViewController(_:didShow:animated:)를 호출해야합니다.
public func assertCoordinatorDeallocation<
Coordinator: TestableFlowCoordinator
>(
_ file: StaticString = #file,
line: UInt = #line,
of createCoordinator: () -> Coordinator
) {
weak var weakCoordinator: Coordinator?
let autoreleaseExpectation = expectation(description: "Autoreleasepool should drain.")
autoreleasepool {
let strongCoordinator = createCoordinator()
weakCoordinator = strongCoordinator
XCTAssertNotNil(
weakCoordinator,
"Weak reference must hold an instance weakly, but it's nil.",
file: file,
line: line)
weakCoordinator?.start()
// Delayed dismissal after 1 second
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
weakCoordinator?.presenter?.popViewController(animated: false)
autoreleaseExpectation.fulfill()
}
wait(for: [autoreleaseExpectation], timeout: 3)
}
XCTAssertNil(
weakCoordinator,
"Weak reference not released, may be a retain cycle.",
file: file,
line: line)
}
}
#endif