Skip to content

Commit

Permalink
Fix examples tests failing upon retry (#2177)
Browse files Browse the repository at this point in the history
  • Loading branch information
evil159 committed Jun 11, 2024
1 parent e8e6d34 commit ef62a57
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions Tests/ExamplesTests/TestableExampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,24 @@ import ObjectiveC.runtime
import MapboxMaps

final class TestableExampleTests: XCTestCase {
private var example: Example?
private weak var weakExampleViewController: UIViewController?
private weak var weakMapView: MapView?
private var exampleControllerRemovedExpectation: XCTestExpectation?

override static var defaultTestSuite: XCTestSuite {
let newTestSuite = XCTestSuite(forTestCaseClass: TestableExampleTests.self)

guard let method = class_getInstanceMethod(Self.self, #selector(runExample)) else {
fatalError("Cannot find method 'runExample'")
}

let existingImpl = method_getImplementation(method)

for example in Examples.all.flatMap(\.examples) {
// Add a method for this test, but using the same implementation
let selectorName = "test\(example.type)"
let testSelector = Selector((selectorName))
class_addMethod(Self.self, testSelector, existingImpl, "v@:f")
let testSelector = Selector("test\(example.type)")
let myBlock: @convention(block) (TestableExampleTests) -> Void = { testCase in
testCase.runExample(example)
}

let closureImpl = imp_implementationWithBlock(myBlock)
class_addMethod(Self.self, testSelector, closureImpl, "v@:")

let test = TestableExampleTests(selector: testSelector)
test.example = example
print("Adding test for \(example)")
newTestSuite.addTest(test)
}
Expand All @@ -42,17 +38,12 @@ final class TestableExampleTests: XCTestCase {
XCTAssertNil(weakMapView, "Example mapView is part of a memory leak")
}

@objc private func runExample() {
private func runExample(_ example: Example) {
guard let navigationController = UIApplication.shared.windows.first?.rootViewController as? UINavigationController else {
XCTFail("Root controller is not a UINavigationController")
return
}

// example can be nil, when the test is repeated, because test runner uses default `init(selector:)` and cannot set example property to the test
guard let example else {
return XCTFail("Currently selected example is nil")
}

navigationController.delegate = self

let exampleViewController = example.makeViewController()
Expand Down

0 comments on commit ef62a57

Please sign in to comment.